mirror of
https://github.com/kiwix/java-libkiwix.git
synced 2025-09-11 08:08:46 -04:00
Correctly construct Book.
Introduce a new set of wrapper (with a nice `2` postfix) with use a constructor taking a handle as parameter. It allow Book to have two constructor: - One creating a default empty (cpp) book. - One setting the wrapper around a exsiting cpp book.
This commit is contained in:
parent
a0fd51228c
commit
81993af3c1
@ -50,7 +50,7 @@ METHOD(jboolean, addBook, jobject book)
|
||||
}
|
||||
|
||||
METHOD(jobject, getBookById, jstring id) {
|
||||
return BUILD_WRAPPER("org/kiwix/libkiwix/Book", THIS->getBookById(TO_C(id)));
|
||||
return BUILD_WRAPPER2("org/kiwix/libkiwix/Book", THIS->getBookById(TO_C(id)));
|
||||
}
|
||||
|
||||
METHOD(jobject, getArchiveById, jstring id) {
|
||||
|
@ -60,6 +60,18 @@ inline jobject newObject(const char* className, JNIEnv* env) {
|
||||
}
|
||||
#define NEW_OBJECT(CLASSNAME) newObject(CLASSNAME, env)
|
||||
|
||||
// Create a java object using a constructor setting handle.
|
||||
template<typename T>
|
||||
inline jobject newObject2(const char* className, JNIEnv* env, shared_ptr<T>&& ptr) {
|
||||
jclass objClass = env->FindClass(className);
|
||||
jmethodID initMethod = env->GetMethodID(objClass, "<init>", "(J)V");
|
||||
// allocate a shared_ptr on the head
|
||||
shared_ptr<T>* handle = new shared_ptr<T>(ptr);
|
||||
jobject wrapper = env->NewObject(objClass, initMethod, reinterpret_cast<jlong>(handle));
|
||||
return wrapper;
|
||||
}
|
||||
#define NEW_OBJECT2(CLASSNAME, ptr) newObject2(CLASSNAME, env, ptr)
|
||||
|
||||
|
||||
// Set the pointer to the wrapped object.
|
||||
template<typename T>
|
||||
@ -131,6 +143,15 @@ inline jobject buildWrapper(JNIEnv* env, const char* class_name, T&& obj, const
|
||||
}
|
||||
#define BUILD_WRAPPER(CLASSNAME, OBJ) buildWrapper(env, CLASSNAME, std::move(OBJ))
|
||||
|
||||
template<typename T>
|
||||
inline jobject buildWrapper2(JNIEnv* env, const char* class_name, T&& obj, const char* handleName = "nativeHandle") {
|
||||
auto ptr = std::make_shared<T>(std::move(obj));
|
||||
auto wrapper = newObject2(class_name, env, std::move(ptr));
|
||||
return wrapper;
|
||||
}
|
||||
#define BUILD_WRAPPER2(CLASSNAME, OBJ) buildWrapper2(env, CLASSNAME, std::move(OBJ))
|
||||
|
||||
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
@ -7,6 +7,9 @@ import org.kiwix.libkiwix.Illustration;
|
||||
public class Book
|
||||
{
|
||||
public Book() { allocate(); }
|
||||
public Book(long handle) {
|
||||
nativeHandle = handle;
|
||||
}
|
||||
|
||||
|
||||
public native void update(Book book);
|
||||
|
Loading…
x
Reference in New Issue
Block a user