Skip to content

Commit c71dbce

Browse files
committed
Better wrapping structure.
Avoid the Handle and directly use a shared_ptr. This simplify a lot the wrapping code and potentially remove some bug.
1 parent 803abbd commit c71dbce

File tree

9 files changed

+115
-139
lines changed

9 files changed

+115
-139
lines changed

lib/src/main/cpp/libzim/archive.cpp

+33-38
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,19 @@
3131
#include <zim/item.h>
3232

3333
/* Kiwix Reader JNI functions */
34-
JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchive(
35-
JNIEnv* env, jobject obj, jstring filename)
34+
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchive(
35+
JNIEnv* env, jobject thisObj, jstring filename)
3636
{
3737
std::string cPath = TO_C(filename);
3838

3939
LOG("Attempting to create reader with: %s", cPath.c_str());
4040
Lock l;
4141
try {
42-
zim::Archive* reader = new zim::Archive(cPath);
43-
return reinterpret_cast<jlong>(new Handle<zim::Archive>(reader));
42+
auto archive = std::make_shared<zim::Archive>(cPath);
43+
SET_PTR(archive);
4444
} catch (std::exception& e) {
4545
LOG("Error opening ZIM file");
4646
LOG("%s", e.what());
47-
return 0;
4847
}
4948
}
5049

@@ -68,49 +67,45 @@ int jni2fd(const jobject& fdObj, JNIEnv* env)
6867

6968
} // unnamed namespace
7069

71-
JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchiveByFD(
72-
JNIEnv* env, jobject obj, jobject fdObj)
70+
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveByFD(
71+
JNIEnv* env, jobject thisObj, jobject fdObj)
7372
{
7473
#ifndef _WIN32
7574
int fd = jni2fd(fdObj, env);
7675

7776
LOG("Attempting to create reader with fd: %d", fd);
7877
Lock l;
7978
try {
80-
zim::Archive* reader = new zim::Archive(fd);
81-
return reinterpret_cast<jlong>(new Handle<zim::Archive>(reader));
79+
auto archive = std::make_shared<zim::Archive>(fd);
80+
SET_PTR(archive);
8281
} catch (std::exception& e) {
8382
LOG("Error opening ZIM file");
8483
LOG("%s", e.what());
85-
return 0;
8684
}
8785
#else
8886
jclass exception = env->FindClass("java/lang/UnsupportedOperationException");
89-
env->ThrowNew(exception, "org.kiwix.libzim.Archive.getNativeArchiveByFD() is not supported under Windows");
90-
return 0;
87+
env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveByFD() is not supported under Windows");
9188
#endif
9289
}
9390

94-
JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchiveEmbedded(
95-
JNIEnv* env, jobject obj, jobject fdObj, jlong offset, jlong size)
91+
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbedded(
92+
JNIEnv* env, jobject thisObj, jobject fdObj, jlong offset, jlong size)
9693
{
9794
#ifndef _WIN32
9895
int fd = jni2fd(fdObj, env);
9996

10097
LOG("Attempting to create reader with fd: %d", fd);
10198
Lock l;
10299
try {
103-
zim::Archive* reader = new zim::Archive(fd, offset, size);
104-
return reinterpret_cast<jlong>(new Handle<zim::Archive>(reader));
100+
auto archive = std::make_shared<zim::Archive>(fd, offset, size);
101+
SET_PTR(archive);
105102
} catch (std::exception& e) {
106103
LOG("Error opening ZIM file");
107104
LOG("%s", e.what());
108-
return 0;
109105
}
110106
#else
111107
jclass exception = env->FindClass("java/lang/UnsupportedOperationException");
112-
env->ThrowNew(exception, "org.kiwix.libzim.Archive.getNativeArchiveEmbedded() is not supported under Windows");
113-
return 0;
108+
env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveEmbedded() is not supported under Windows");
114109
#endif
115110
}
116111

@@ -120,7 +115,7 @@ Java_org_kiwix_libzim_Archive_dispose(JNIEnv* env, jobject thisObj)
120115
dispose<zim::Archive>(env, thisObj);
121116
}
122117

123-
#define THIS (Handle<zim::Archive>::getHandle(env, thisObj))
118+
#define THIS GET_PTR(zim::Archive)
124119
#define GETTER(retType, name) JNIEXPORT retType JNICALL \
125120
Java_org_kiwix_libzim_Archive_##name (JNIEnv* env, jobject thisObj) \
126121
{ \
@@ -144,16 +139,16 @@ METHOD(jstring, Archive, getMetadata, jstring name) {
144139
}
145140

146141
METHOD(jobject, Archive, getMetadataItem, jstring name) {
147-
auto item = THIS->getMetadataItem(TO_C(name));
148-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Item", item);
142+
auto obj = NEW_OBJECT("org/kiwix/libzim/Item");
143+
SET_HANDLE(zim::Item, obj, THIS->getMetadataItem(TO_C(name)));
149144
return obj;
150145
}
151146

152147
GETTER(jobjectArray, getMetadataKeys)
153148

154149
METHOD(jobject, Archive, getIllustrationItem, jint size) {
155-
auto item = THIS->getIllustrationItem(TO_C(size));
156-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Item", item);
150+
auto obj = NEW_OBJECT("org/kiwix/libzim/Item");
151+
SET_HANDLE(zim::Item, obj, THIS->getIllustrationItem(TO_C(size)));
157152
return obj;
158153
}
159154

@@ -164,44 +159,44 @@ METHOD(jboolean, Archive, hasIllustration, jint size) {
164159
GETTER(jlongArray, getIllustrationSizes)
165160

166161
METHOD(jobject, Archive, getEntryByPath, jlong index) {
167-
auto entry = THIS->getEntryByPath(TO_C(index));
168-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry);
162+
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
163+
SET_HANDLE(zim::Entry, obj, THIS->getEntryByPath(TO_C(index)));
169164
return obj;
170165
}
171166

172167
METHOD(jobject, Archive, getEntryByPath, jstring path) {
173-
auto entry = THIS->getEntryByPath(TO_C(path));
174-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry);
168+
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
169+
SET_HANDLE(zim::Entry, obj, THIS->getEntryByPath(TO_C(path)));
175170
return obj;
176171
}
177172

178173
METHOD(jobject, Archive, getEntryByTitle, jlong index) {
179-
auto entry = THIS->getEntryByTitle(TO_C(index));
180-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry);
174+
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
175+
SET_HANDLE(zim::Entry, obj, THIS->getEntryByTitle(TO_C(index)));
181176
return obj;
182177
}
183178

184179
METHOD(jobject, Archive, getEntryByTitle, jstring title) {
185-
auto entry = THIS->getEntryByTitle(TO_C(title));
186-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry);
180+
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
181+
SET_HANDLE(zim::Entry, obj, THIS->getEntryByTitle(TO_C(title)));
187182
return obj;
188183
}
189184

190185
METHOD(jobject, Archive, getEntryByClusterOrder, jlong index) {
191-
auto entry = THIS->getEntryByClusterOrder(TO_C(index));
192-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry);
186+
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
187+
SET_HANDLE(zim::Entry, obj, THIS->getEntryByClusterOrder(TO_C(index)));
193188
return obj;
194189
}
195190

196191
METHOD0(jobject, Archive, getMainEntry) {
197-
auto entry = THIS->getMainEntry();
198-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry);
192+
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
193+
SET_HANDLE(zim::Entry, obj, THIS->getMainEntry());
199194
return obj;
200195
}
201196

202197
METHOD0(jobject, Archive, getRandomEntry) {
203-
auto entry = THIS->getRandomEntry();
204-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry);
198+
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
199+
SET_HANDLE(zim::Entry, obj, THIS->getRandomEntry());
205200
return obj;
206201
}
207202

lib/src/main/cpp/libzim/blob.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@
2828

2929
#include <zim/blob.h>
3030

31+
#define NATIVE_TYPE zim::Blob
32+
3133
JNIEXPORT void JNICALL
3234
Java_org_kiwix_kiwixlib_libzim_Blob_dispose(JNIEnv* env, jobject thisObj)
3335
{
34-
dispose<zim::Blob>(env, thisObj);
36+
dispose<NATIVE_TYPE>(env, thisObj);
3537
}
36-
37-
#define THIS (Handle<zim::Blob>::getHandle(env, thisObj))
38+
#define THIS GET_PTR(NATIVE_TYPE)
3839
#define GETTER(retType, name) JNIEXPORT retType JNICALL \
3940
Java_org_kiwix_libzim_Blob__##name (JNIEnv* env, jobject thisObj) \
4041
{ \
4142
return TO_JNI(THIS->name()); \
4243
}
4344

4445
METHOD0(jstring, Blob, getData) {
45-
return TO_JNI(std::string(**THIS));
46+
return TO_JNI(std::string(*THIS));
4647
}
4748
GETTER(jlong, size)

lib/src/main/cpp/libzim/entry.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
JNIEXPORT void JNICALL
3535
Java_org_kiwix_kiwixlib_libzim_Entry_dispose(JNIEnv* env, jobject thisObj)
3636
{
37-
dispose<zim::Entry>(env, thisObj);
37+
dispose<NATIVE_TYPE>(env, thisObj);
3838
}
3939

40-
#define THIS (Handle<zim::Entry>::getHandle(env, thisObj))
40+
#define THIS GET_PTR(NATIVE_TYPE)
4141
#define GETTER(retType, name) JNIEXPORT retType JNICALL \
4242
Java_org_kiwix_libzim_Entry__##name (JNIEnv* env, jobject thisObj) \
4343
{ \
@@ -49,19 +49,19 @@ GETTER(jboolean, isRedirect)
4949
GETTER(jstring, getTitle)
5050
GETTER(jstring, getPath)
5151
METHOD(jobject, Entry, getItem, jboolean follow) {
52-
auto item = THIS->getItem(TO_C(follow));
53-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Item", item);
52+
auto obj = NEW_OBJECT("org/kiwix/libzim/Item");
53+
SET_HANDLE(zim::Item, obj, THIS->getItem(TO_C(follow)));
5454
return obj;
5555
}
5656

5757
METHOD0(jobject, Entry, getRedirect) {
58-
auto item = THIS->getRedirect();
59-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Item", item);
58+
auto obj = NEW_OBJECT("org/kiwix/libzim/Item");
59+
SET_HANDLE(zim::Item, obj, THIS->getRedirect());
6060
return obj;
6161
}
6262

6363
METHOD0(jobject, Entry, getRedirectEntry) {
64-
auto entry = THIS->getRedirectEntry();
65-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry);
64+
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
65+
SET_HANDLE(zim::Entry, obj, THIS->getRedirectEntry());
6666
return obj;
6767
}

lib/src/main/cpp/libzim/item.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828

2929
#include <zim/item.h>
3030

31+
#define NATIVE_TYPE zim::Item
32+
3133
JNIEXPORT void JNICALL
3234
Java_org_kiwix_kiwixlib_libzim_Item_dispose(JNIEnv* env, jobject thisObj)
3335
{
34-
dispose<zim::Item>(env, thisObj);
36+
dispose<NATIVE_TYPE>(env, thisObj);
3537
}
3638

37-
#define THIS (Handle<zim::Item>::getHandle(env, thisObj))
39+
#define THIS GET_PTR(NATIVE_TYPE)
3840
#define GETTER(retType, name) JNIEXPORT retType JNICALL \
3941
Java_org_kiwix_libzim_Item__##name (JNIEnv* env, jobject thisObj) \
4042
{ \
@@ -46,8 +48,8 @@ GETTER(jstring, getPath)
4648
GETTER(jstring, getMimetype)
4749

4850
METHOD0(jobject, Item, getData) {
49-
auto blob = THIS->getData();
50-
auto obj = CREATE_WRAPPER("org/kiwix/libzim/Blob", blob);
51+
auto obj = NEW_OBJECT("org/kiwix/libzim/Blob");
52+
SET_HANDLE(zim::Blob, obj, THIS->getData());
5153
return obj;
5254
}
5355

0 commit comments

Comments
 (0)