Skip to content

Commit f4b5d7a

Browse files
committed
Use a common class Wrapper and IterWrapper to wrapp native resource.
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
1 parent 4b5cc44 commit f4b5d7a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+694
-505
lines changed

lib/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ ext {
2626

2727
apply from: 'publish.gradle'
2828
android {
29-
compileSdk 32
29+
compileSdk 33
3030

3131
defaultConfig {
3232

33-
minSdk 21
33+
minSdk 33
3434
targetSdk 32
3535
versionCode 1
3636
versionName "1.0"
@@ -285,7 +285,7 @@ task checkCurrentJavaVersion() {
285285

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

291291
String getLibkiwixFiles() {

lib/src/main/cpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_library(
1010
zim_wrapper
1111

1212
SHARED
13+
wrapper.cpp
1314
libzim/archive.cpp
1415
libzim/entry.cpp
1516
libzim/entry_iterator.cpp
@@ -45,6 +46,7 @@ add_library(
4546
kiwix_wrapper
4647

4748
SHARED
49+
wrapper.cpp
4850
libkiwix/book.cpp
4951
libkiwix/filter.cpp
5052
libkiwix/kiwixicu.cpp

lib/src/main/cpp/libkiwix/book.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@
2929
#define TYPENAME libkiwix_Book
3030
#include <macros.h>
3131

32-
METHOD0(void, allocate)
32+
METHOD0(jobject, getNativeBook)
3333
{
34-
SET_PTR(std::make_shared<NATIVE_TYPE>());
34+
return NEW_RESOURCE(std::make_shared<NATIVE_TYPE>());
3535
}
3636

37-
DISPOSE
38-
3937
METHOD(void, update__Lorg_kiwix_libkiwix_Book_2, jobject otherBook)
4038
{
4139
THIS->update(*getPtr<kiwix::Book>(env, otherBook));
@@ -93,12 +91,12 @@ METHOD0(jobjectArray, getIllustrations) {
9391
jobjectArray retArray = createArray(env, illustrations.size(), "org/kiwix/libkiwix/Illustration");
9492
size_t index = 0;
9593
for (auto illu: illustrations) {
96-
auto wrapper = BUILD_WRAPPER("org/kiwix/libkiwx/Illustration", illu);
94+
auto wrapper = BUILD_WRAPPER(illu);
9795
env->SetObjectArrayElement(retArray, index++, wrapper);
9896
}
9997
return retArray;
10098
}
10199

102100
METHOD(jobject, getIllustration, jint size) {
103-
return BUILD_WRAPPER("org/kiwix/libkiwix/Illustration", THIS->getIllustration(TO_C(size)));
101+
return BUILD_WRAPPER(THIS->getIllustration(TO_C(size)));
104102
}

lib/src/main/cpp/libkiwix/bookmark.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@
2626

2727
#define NATIVE_TYPE kiwix::Bookmark
2828
#define TYPENAME libkiwix_Bookmark
29-
#include <macros.h>
29+
#include "macros.h"
3030

31-
METHOD0(void, setNativeBookmark)
31+
METHOD0(jobject, buildNativeBookmark)
3232
{
33-
SET_PTR(std::make_shared<NATIVE_TYPE>());
33+
return NEW_RESOURCE(std::make_shared<NATIVE_TYPE>());
3434
}
3535

36-
DISPOSE
37-
3836
GETTER(jstring, getBookId)
3937

4038
GETTER(jstring, getBookTitle)

lib/src/main/cpp/libkiwix/filter.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,11 @@
2828
#define TYPENAME libkiwix_Filter
2929
#include "macros.h"
3030

31-
32-
3331
/* Kiwix Reader JNI functions */
34-
METHOD0(void, allocate) {
35-
SET_PTR(std::make_shared<NATIVE_TYPE>());
32+
METHOD0(jobject, getNativeFilter) {
33+
return NEW_RESOURCE(std::make_shared<NATIVE_TYPE>());
3634
}
3735

38-
DISPOSE
39-
4036
#define FORWARD(name, args_type) \
4137
METHOD(jobject, name, args_type value) { \
4238
THIS->name(jni2c(value, env)); \

lib/src/main/cpp/libkiwix/kiwixserver.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,14 @@
3232

3333

3434
/* Kiwix Reader JNI functions */
35-
METHOD(void, setNativeServer, jobject jLibrary)
35+
METHOD(jobject, buildNativeServer, jobject jLibrary)
3636
{
3737
LOG("Attempting to create server");
38-
try {
39-
auto library = getPtr<kiwix::Library>(env, jLibrary);
40-
SET_PTR(std::make_shared<NATIVE_TYPE>(library.get()));
41-
} catch (std::exception& e) {
42-
LOG("Error creating the server");
43-
LOG("%s", e.what());
44-
}
38+
auto library = getPtr<kiwix::Library>(env, jLibrary);
39+
return NEW_RESOURCE(std::make_shared<NATIVE_TYPE>(library.get()));
4540
}
4641

4742

48-
DISPOSE
49-
5043
/* Kiwix library functions */
5144
METHOD(void, setRoot, jstring root)
5245
{

lib/src/main/cpp/libkiwix/library.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@
2929
#include "macros.h"
3030

3131
/* Kiwix Reader JNI functions */
32-
METHOD0(void, setNativeHandler)
32+
METHOD0(jobject, buildNativeLibrary)
3333
{
34-
SET_PTR(std::make_shared<NATIVE_TYPE>());
34+
return NEW_RESOURCE(std::make_shared<NATIVE_TYPE>());
3535
}
3636

37-
DISPOSE
3837

3938
/* Kiwix library functions */
4039
METHOD(jboolean, addBook, jobject book)
@@ -50,11 +49,11 @@ METHOD(jboolean, addBook, jobject book)
5049
}
5150

5251
METHOD(jobject, getBookById, jstring id) {
53-
return BUILD_WRAPPER("org/kiwix/libkiwix/Book", THIS->getBookById(TO_C(id)));
52+
return BUILD_WRAPPER(THIS->getBookById(TO_C(id)));
5453
}
5554

5655
METHOD(jobject, getArchiveById, jstring id) {
57-
return BUILD_WRAPPER("org/kiwix/libzim/Archive", THIS->getArchiveById(TO_C(id)));
56+
return BUILD_WRAPPER(THIS->getArchiveById(TO_C(id)));
5857
}
5958

6059
METHOD(jboolean, removeBookById, jstring id) {
@@ -98,7 +97,7 @@ METHOD(jobjectArray, getBookmarks, jboolean onlyValidBookmarks) {
9897
jobjectArray retArray = createArray(env, bookmarks.size(), "org/kiwix/libkiwix/Bookmark");
9998
size_t index = 0;
10099
for (auto bookmark: bookmarks) {
101-
auto wrapper = BUILD_WRAPPER("org/kiwix/libkiwx/Bookmark", bookmark);
100+
auto wrapper = BUILD_WRAPPER(bookmark);
102101
env->SetObjectArrayElement(retArray, index++, wrapper);
103102
}
104103
return retArray;

lib/src/main/cpp/libkiwix/manager.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@
2828
#define TYPENAME libkiwix_Manager
2929
#include <macros.h>
3030

31-
METHOD(void, allocate, jobject libraryObj)
31+
METHOD(jobject, buildNativeManager, jobject libraryObj)
3232
{
3333
auto lib = getPtr<kiwix::Library>(env, libraryObj);
34-
SET_PTR(std::make_shared<NATIVE_TYPE>(lib.get()));
34+
return NEW_RESOURCE(std::make_shared<NATIVE_TYPE>(lib.get()));
3535
}
3636

37-
DISPOSE
38-
3937
/* Kiwix manager functions */
4038
METHOD(jboolean, readFile, jstring path)
4139
{

0 commit comments

Comments
 (0)