Skip to content

Commit

Permalink
Use a common class Wrapper and IterWrapper to wrapp native resource.
Browse files Browse the repository at this point in the history
Native resource (pointer to shared_ptr) is stored as a long in the
`Wrapper.Resource` class.

As Wrapper implements AutoCloseable and we register the (java) resource
to be cleaned at object destruction, we now properly delete the native
resource.


This also adding new macro to avoid writting the class name and so
reduce potential typos.

# Conflicts:
#	lib/build.gradle
#	lib/src/main/java/org/kiwix/libkiwix/Library.java
  • Loading branch information
mgautierfr committed Mar 1, 2023
1 parent 4b5cc44 commit f4b5d7a
Show file tree
Hide file tree
Showing 46 changed files with 694 additions and 505 deletions.
6 changes: 3 additions & 3 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ ext {

apply from: 'publish.gradle'
android {
compileSdk 32
compileSdk 33

defaultConfig {

minSdk 21
minSdk 33
targetSdk 32
versionCode 1
versionName "1.0"
Expand Down Expand Up @@ -285,7 +285,7 @@ task checkCurrentJavaVersion() {

task generateHeaderFilesFromJavaWrapper(type: Exec) {
workingDir "${projectDir}/src/main/java/org/kiwix/"
commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/libzim/ ${getLibzimFiles()} ${getLibkiwixFiles()}"
commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/libzim/ *.java ${getLibzimFiles()} ${getLibkiwixFiles()}"
}

String getLibkiwixFiles() {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_library(
zim_wrapper

SHARED
wrapper.cpp
libzim/archive.cpp
libzim/entry.cpp
libzim/entry_iterator.cpp
Expand Down Expand Up @@ -45,6 +46,7 @@ add_library(
kiwix_wrapper

SHARED
wrapper.cpp
libkiwix/book.cpp
libkiwix/filter.cpp
libkiwix/kiwixicu.cpp
Expand Down
10 changes: 4 additions & 6 deletions lib/src/main/cpp/libkiwix/book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@
#define TYPENAME libkiwix_Book
#include <macros.h>

METHOD0(void, allocate)
METHOD0(jobject, getNativeBook)
{
SET_PTR(std::make_shared<NATIVE_TYPE>());
return NEW_RESOURCE(std::make_shared<NATIVE_TYPE>());
}

DISPOSE

METHOD(void, update__Lorg_kiwix_libkiwix_Book_2, jobject otherBook)
{
THIS->update(*getPtr<kiwix::Book>(env, otherBook));
Expand Down Expand Up @@ -93,12 +91,12 @@ METHOD0(jobjectArray, getIllustrations) {
jobjectArray retArray = createArray(env, illustrations.size(), "org/kiwix/libkiwix/Illustration");
size_t index = 0;
for (auto illu: illustrations) {
auto wrapper = BUILD_WRAPPER("org/kiwix/libkiwx/Illustration", illu);
auto wrapper = BUILD_WRAPPER(illu);
env->SetObjectArrayElement(retArray, index++, wrapper);
}
return retArray;
}

METHOD(jobject, getIllustration, jint size) {
return BUILD_WRAPPER("org/kiwix/libkiwix/Illustration", THIS->getIllustration(TO_C(size)));
return BUILD_WRAPPER(THIS->getIllustration(TO_C(size)));
}
8 changes: 3 additions & 5 deletions lib/src/main/cpp/libkiwix/bookmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@

#define NATIVE_TYPE kiwix::Bookmark
#define TYPENAME libkiwix_Bookmark
#include <macros.h>
#include "macros.h"

METHOD0(void, setNativeBookmark)
METHOD0(jobject, buildNativeBookmark)
{
SET_PTR(std::make_shared<NATIVE_TYPE>());
return NEW_RESOURCE(std::make_shared<NATIVE_TYPE>());
}

DISPOSE

GETTER(jstring, getBookId)

GETTER(jstring, getBookTitle)
Expand Down
8 changes: 2 additions & 6 deletions lib/src/main/cpp/libkiwix/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@
#define TYPENAME libkiwix_Filter
#include "macros.h"



/* Kiwix Reader JNI functions */
METHOD0(void, allocate) {
SET_PTR(std::make_shared<NATIVE_TYPE>());
METHOD0(jobject, getNativeFilter) {
return NEW_RESOURCE(std::make_shared<NATIVE_TYPE>());
}

DISPOSE

#define FORWARD(name, args_type) \
METHOD(jobject, name, args_type value) { \
THIS->name(jni2c(value, env)); \
Expand Down
13 changes: 3 additions & 10 deletions lib/src/main/cpp/libkiwix/kiwixserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,14 @@


/* Kiwix Reader JNI functions */
METHOD(void, setNativeServer, jobject jLibrary)
METHOD(jobject, buildNativeServer, jobject jLibrary)
{
LOG("Attempting to create server");
try {
auto library = getPtr<kiwix::Library>(env, jLibrary);
SET_PTR(std::make_shared<NATIVE_TYPE>(library.get()));
} catch (std::exception& e) {
LOG("Error creating the server");
LOG("%s", e.what());
}
auto library = getPtr<kiwix::Library>(env, jLibrary);
return NEW_RESOURCE(std::make_shared<NATIVE_TYPE>(library.get()));
}


DISPOSE

/* Kiwix library functions */
METHOD(void, setRoot, jstring root)
{
Expand Down
11 changes: 5 additions & 6 deletions lib/src/main/cpp/libkiwix/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@
#include "macros.h"

/* Kiwix Reader JNI functions */
METHOD0(void, setNativeHandler)
METHOD0(jobject, buildNativeLibrary)
{
SET_PTR(std::make_shared<NATIVE_TYPE>());
return NEW_RESOURCE(std::make_shared<NATIVE_TYPE>());
}

DISPOSE

/* Kiwix library functions */
METHOD(jboolean, addBook, jobject book)
Expand All @@ -50,11 +49,11 @@ METHOD(jboolean, addBook, jobject book)
}

METHOD(jobject, getBookById, jstring id) {
return BUILD_WRAPPER("org/kiwix/libkiwix/Book", THIS->getBookById(TO_C(id)));
return BUILD_WRAPPER(THIS->getBookById(TO_C(id)));
}

METHOD(jobject, getArchiveById, jstring id) {
return BUILD_WRAPPER("org/kiwix/libzim/Archive", THIS->getArchiveById(TO_C(id)));
return BUILD_WRAPPER(THIS->getArchiveById(TO_C(id)));
}

METHOD(jboolean, removeBookById, jstring id) {
Expand Down Expand Up @@ -98,7 +97,7 @@ METHOD(jobjectArray, getBookmarks, jboolean onlyValidBookmarks) {
jobjectArray retArray = createArray(env, bookmarks.size(), "org/kiwix/libkiwix/Bookmark");
size_t index = 0;
for (auto bookmark: bookmarks) {
auto wrapper = BUILD_WRAPPER("org/kiwix/libkiwx/Bookmark", bookmark);
auto wrapper = BUILD_WRAPPER(bookmark);
env->SetObjectArrayElement(retArray, index++, wrapper);
}
return retArray;
Expand Down
6 changes: 2 additions & 4 deletions lib/src/main/cpp/libkiwix/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@
#define TYPENAME libkiwix_Manager
#include <macros.h>

METHOD(void, allocate, jobject libraryObj)
METHOD(jobject, buildNativeManager, jobject libraryObj)
{
auto lib = getPtr<kiwix::Library>(env, libraryObj);
SET_PTR(std::make_shared<NATIVE_TYPE>(lib.get()));
return NEW_RESOURCE(std::make_shared<NATIVE_TYPE>(lib.get()));
}

DISPOSE

/* Kiwix manager functions */
METHOD(jboolean, readFile, jstring path)
{
Expand Down
Loading

0 comments on commit f4b5d7a

Please sign in to comment.