0
0
mirror of https://github.com/florisboard/florisboard.git synced 2024-09-19 19:42:20 +02:00

Add native instance wrapper interface / Clean up code

This commit is contained in:
Patrick Goldinger 2021-05-12 02:25:41 +02:00
parent be1fc710ed
commit b5b89fde4f
8 changed files with 72 additions and 29 deletions

4
.gitignore vendored
View File

@ -41,6 +41,8 @@ captures/
*.jks
crowdin.properties
# C++
.cxx/
# AndroidX Room schema JSONs
/app/schemas/
/app/.cxx/

View File

@ -17,7 +17,6 @@ add_library(
SHARED
# Sources
native-lib.cpp
dev_patrickgold_florisboard_ime_nlp_SuggestionList.cpp
)

View File

@ -25,8 +25,8 @@ Java_dev_patrickgold_florisboard_ime_nlp_SuggestionList_00024Companion_nativeIni
JNIEnv *env,
jobject thiz,
jint max_size) {
auto *stagedSuggestionList = new SuggestionList(max_size);
return reinterpret_cast<jlong>(stagedSuggestionList);
auto *suggestionList = new SuggestionList(max_size);
return reinterpret_cast<jlong>(suggestionList);
}
extern "C"
@ -99,6 +99,6 @@ Java_dev_patrickgold_florisboard_ime_nlp_SuggestionList_00024Companion_nativeSiz
JNIEnv *env,
jobject thiz,
jlong native_ptr) {
auto *stagedSuggestionList = reinterpret_cast<SuggestionList *>(native_ptr);
return stagedSuggestionList->size();
auto *suggestionList = reinterpret_cast<SuggestionList *>(native_ptr);
return suggestionList->size();
}

View File

@ -1,10 +0,0 @@
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_dev_patrickgold_florisboard_ime_core_FlorisBoard_jniHelloWorld(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}

View File

@ -146,8 +146,6 @@ class FlorisBoard : InputMethodService(), LifecycleOwner, FlorisClipboardManager
lateinit var asyncExecutor: ExecutorService
external fun jniHelloWorld(): String
companion object {
@Synchronized
fun getInstance(): FlorisBoard {
@ -194,7 +192,7 @@ class FlorisBoard : InputMethodService(), LifecycleOwner, FlorisClipboardManager
.build()
)
}*/
flogInfo(LogTopic.IMS_EVENTS) { jniHelloWorld() }
flogInfo(LogTopic.IMS_EVENTS)
serviceLifecycleDispatcher.onServicePreSuperOnCreate()
imeManager = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager

View File

@ -0,0 +1,48 @@
/*
* Copyright (C) 2021 Patrick Goldinger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.patrickgold.florisboard.ime.core
/**
* Type alias for a native pointer.
*/
typealias NativePtr = Long
/**
* Constant value for a native null pointer.
*/
const val NATIVE_NULLPTR: NativePtr = 0
/**
* Generic interface for a native instance object. Defines the basic
* methods which each native instance wrapper should define and be able
* to handle to.
*/
interface NativeInstanceWrapper {
/**
* Returns the native pointer of this instance. The returned pointer
* is only valid if [dispose] has not been previously called.
*
* @return The native null pointer for this instance.
*/
fun nativePtr(): NativePtr
/**
* Deletes the native object and frees allocated resources. After
* invoking this method one MUST NOT touch this instance ever again.
*/
fun dispose()
}

View File

@ -16,10 +16,13 @@
package dev.patrickgold.florisboard.ime.nlp
import dev.patrickgold.florisboard.ime.core.NativeInstanceWrapper
import dev.patrickgold.florisboard.ime.core.NativePtr
@JvmInline
value class SuggestionList private constructor(
private val nativePtr: NativePtr
) : Collection<String> {
private val _nativePtr: NativePtr
) : Collection<String>, NativeInstanceWrapper {
companion object {
fun new(maxSize: Int): SuggestionList {
val nativePtr = nativeInitialize(maxSize)
@ -37,18 +40,18 @@ value class SuggestionList private constructor(
}
override val size: Int
get() = nativeSize(nativePtr)
get() = nativeSize(_nativePtr)
fun add(word: Word, freq: Freq): Boolean {
return nativeAdd(nativePtr, word, freq)
return nativeAdd(_nativePtr, word, freq)
}
fun clear() {
nativeClear(nativePtr)
nativeClear(_nativePtr)
}
override fun contains(element: Word): Boolean {
return nativeContains(nativePtr, element)
return nativeContains(_nativePtr, element)
}
override fun containsAll(elements: Collection<Word>): Boolean {
@ -67,7 +70,7 @@ value class SuggestionList private constructor(
}
fun getOrNull(index: Int): Word? {
return nativeGetOrNull(nativePtr, index)
return nativeGetOrNull(_nativePtr, index)
}
override fun isEmpty(): Boolean = size <= 0
@ -76,8 +79,12 @@ value class SuggestionList private constructor(
return SuggestionListIterator(this)
}
fun dispose() {
nativeDispose(nativePtr)
override fun nativePtr(): NativePtr {
return _nativePtr
}
override fun dispose() {
nativeDispose(_nativePtr)
}
class SuggestionListIterator internal constructor (

View File

@ -16,7 +16,6 @@
package dev.patrickgold.florisboard.ime.nlp
typealias NativePtr = Long
typealias Word = String
typealias Freq = Int