Skip to content

Commit 2951827

Browse files
committed
Introduce macros.h to define common macro
1 parent edea648 commit 2951827

20 files changed

+207
-211
lines changed

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,29 @@
2626
#include <zim/archive.h>
2727

2828
#define NATIVE_TYPE kiwix::Book
29-
#define THIS GET_PTR(NATIVE_TYPE)
29+
#define TYPENAME libkiwix_Book
30+
#include <macros.h>
3031

31-
JNIEXPORT void JNICALL
32-
Java_org_kiwix_libkiwix_Book_allocate(
33-
JNIEnv* env, jobject thisObj)
32+
METHOD0(void, allocate)
3433
{
3534
SET_PTR(std::make_shared<NATIVE_TYPE>());
3635
}
3736

38-
JNIEXPORT void JNICALL
39-
Java_org_kiwix_libkiwix_Book_dispose(JNIEnv* env, jobject thisObj)
37+
METHOD0(void, dispose)
4038
{
4139
dispose<NATIVE_TYPE>(env, thisObj);
4240
}
4341

44-
METHOD(void, Book, update__Lorg_kiwix_libkiwix_Book_2, jobject otherBook)
42+
METHOD(void, update__Lorg_kiwix_libkiwix_Book_2, jobject otherBook)
4543
{
4644
THIS->update(*getPtr<kiwix::Book>(env, otherBook));
4745
}
4846

49-
METHOD(void, Book, update__Lorg_kiwix_libkiwix_JNIKiwixReader_2, jobject archive)
47+
METHOD(void, update__Lorg_kiwix_libkiwix_JNIKiwixReader_2, jobject archive)
5048
{
5149
THIS->update(*getPtr<zim::Archive>(env, archive));
5250
}
5351

54-
#define GETTER(retType, name) GETTER_METHOD(retType, libkiwix_Book, THIS, name)
55-
5652
GETTER(jstring, getId)
5753
GETTER(jstring, getPath)
5854
GETTER(jboolean, isPathValid)
@@ -74,10 +70,8 @@ GETTER(jstring, getFavicon)
7470
GETTER(jstring, getFaviconUrl)
7571
GETTER(jstring, getFaviconMimeType)
7672

77-
METHOD(jstring, Book, getTagStr, jstring tagName) try {
73+
METHOD(jstring, getTagStr, jstring tagName) try {
7874
return TO_JNI(THIS->getTagStr(TO_C(tagName)));
7975
} catch(...) {
8076
return c2jni<std::string>("", env);
8177
}
82-
83-
#undef GETTER

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,29 @@
2525
#include "utils.h"
2626

2727
#define NATIVE_TYPE kiwix::Filter
28-
#define THIS GET_PTR(NATIVE_TYPE)
28+
#define TYPENAME libkiwix_Filter
29+
#include "macros.h"
30+
2931

3032

3133
/* Kiwix Reader JNI functions */
32-
METHOD0(void, Filter, allocate) {
34+
METHOD0(void, allocate) {
3335
SET_PTR(std::make_shared<NATIVE_TYPE>());
3436
}
3537

36-
METHOD0(void, Filter, dispose) {
38+
METHOD0(void, dispose) {
3739
dispose<kiwix::Library>(env, thisObj);
3840
}
3941

4042

4143
#define FORWARD(name, args_type) \
42-
METHOD(jobject, Filter, name, args_type value) { \
44+
METHOD(jobject, name, args_type value) { \
4345
THIS->name(jni2c(value, env)); \
4446
return thisObj; \
4547
}
4648

4749
#define FORWARDA(name, args_type) \
48-
METHOD(jobject, Filter, name, jobjectArray value) { \
50+
METHOD(jobject, name, jobjectArray value) { \
4951
THIS->name(jni2c<args_type>(value, env)); \
5052
return thisObj; \
5153
}

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

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
#include "utils.h"
2727

2828
#define NATIVE_TYPE kiwix::Server
29-
#define THIS GET_PTR(NATIVE_TYPE)
29+
#define TYPENAME libkiwix_Server
30+
#include <macros.h>
31+
32+
3033

3134
/* Kiwix Reader JNI functions */
32-
JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixServer_setNativeServer(
33-
JNIEnv* env, jobject thisObj, jobject jLibrary)
35+
METHOD(void, setNativeServer, jobject jLibrary)
3436
{
3537
LOG("Attempting to create server");
3638
Lock l;
@@ -43,57 +45,48 @@ JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixServer_setNativeServer(
4345
}
4446
}
4547

46-
JNIEXPORT void JNICALL
47-
Java_org_kiwix_kiwixlib_JNIKiwixServer_dispose(JNIEnv* env, jobject obj)
48+
METHOD0(void, dispose)
4849
{
49-
dispose<NATIVE_TYPE>(env, obj);
50+
dispose<NATIVE_TYPE>(env, thisObj);
5051
}
5152

5253
/* Kiwix library functions */
53-
JNIEXPORT void JNICALL
54-
Java_org_kiwix_kiwixlib_JNIKiwixServer_setRoot(JNIEnv* env, jobject thisObj, jstring root)
54+
METHOD(void, setRoot, jstring root)
5555
{
5656
THIS->setRoot(TO_C(root));
5757
}
5858

59-
JNIEXPORT void JNICALL
60-
Java_org_kiwix_kiwixlib_JNIKiwixServer_setAddress(JNIEnv* env, jobject thisObj, jstring address)
59+
METHOD(void, setAddress, jstring address)
6160
{
6261
THIS->setAddress(TO_C(address));
6362
}
6463

65-
JNIEXPORT void JNICALL
66-
Java_org_kiwix_kiwixlib_JNIKiwixServer_setPort(JNIEnv* env, jobject thisObj, int port)
64+
METHOD(void, setPort, int port)
6765
{
6866
THIS->setPort(TO_C(port));
6967
}
7068

71-
JNIEXPORT void JNICALL
72-
Java_org_kiwix_kiwixlib_JNIKiwixServer_setNbThreads(JNIEnv* env, jobject thisObj, int threads)
69+
METHOD(void, setNbThreads, int threads)
7370
{
7471
THIS->setNbThreads(TO_C(threads));
7572
}
7673

77-
JNIEXPORT void JNICALL
78-
Java_org_kiwix_kiwixlib_JNIKiwixServer_setTaskbar(JNIEnv* env, jobject thisObj, jboolean withTaskbar, jboolean withLibraryButton)
74+
METHOD(void, setTaskbar, jboolean withTaskbar, jboolean withLibraryButton)
7975
{
8076
THIS->setTaskbar(TO_C(withTaskbar), TO_C(withLibraryButton));
8177
}
8278

83-
JNIEXPORT void JNICALL
84-
Java_org_kiwix_kiwixlib_JNIKiwixServer_setBlockExternalLinks(JNIEnv* env, jobject thisObj, jboolean blockExternalLinks)
79+
METHOD(void, setBlockExternalLinks, jboolean blockExternalLinks)
8580
{
8681
THIS->setBlockExternalLinks(TO_C(blockExternalLinks));
8782
}
8883

89-
JNIEXPORT jboolean JNICALL
90-
Java_org_kiwix_kiwixlib_JNIKiwixServer_start(JNIEnv* env, jobject thisObj)
84+
METHOD0(jboolean, start)
9185
{
9286
return THIS->start();
9387
}
9488

95-
JNIEXPORT void JNICALL
96-
Java_org_kiwix_kiwixlib_JNIKiwixServer_stop(JNIEnv* env, jobject thisObj)
89+
METHOD0(void, stop)
9790
{
9891
THIS->stop();
9992
}

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@
2525
#include "utils.h"
2626

2727
#define NATIVE_TYPE kiwix::Library
28-
#define THIS GET_PTR(NATIVE_TYPE)
28+
#define TYPENAME libkiwix_Library
29+
#include "macros.h"
2930

3031
/* Kiwix Reader JNI functions */
31-
JNIEXPORT void JNICALL
32-
Java_org_kiwix_kiwixlib_Library_allocate(
33-
JNIEnv* env, jobject thisObj)
32+
METHOD0(void, allocate)
3433
{
3534
SET_PTR(std::make_shared<NATIVE_TYPE>());
3635
}
3736

38-
JNIEXPORT void JNICALL
39-
Java_org_kiwix_kiwixlib_Library_dispose(JNIEnv* env, jobject thisObj)
37+
METHOD0(void, dispose)
4038
{
4139
dispose<NATIVE_TYPE>(env, thisObj);
4240
}
@@ -59,24 +57,22 @@ Java_org_kiwix_kiwixlib_Library_addBook(
5957
return false;
6058
}*/
6159

62-
METHOD(jobject, Library, getBookById, jstring id) {
60+
METHOD(jobject, getBookById, jstring id) {
6361
auto obj = NEW_OBJECT("org/kiwix/libkiwix/Book");
6462
SET_HANDLE(kiwix::Book, obj, THIS->getBookById(TO_C(id)));
6563
return obj;
6664
}
6765

68-
METHOD(jint, Library, getBookCount, jboolean localBooks, jboolean remoteBooks) {
66+
METHOD(jint, getBookCount, jboolean localBooks, jboolean remoteBooks) {
6967
return THIS->getBookCount(localBooks, remoteBooks);
7068
}
7169

72-
#define GETTER(retType, name) GETTER_METHOD(retType, libkiwix_Library, THIS, name)
73-
7470
GETTER(jobjectArray, getBooksIds)
7571
GETTER(jobjectArray, getBooksLanguages)
7672
GETTER(jobjectArray, getBooksCreators)
7773
GETTER(jobjectArray, getBooksPublishers)
7874

79-
METHOD(jobjectArray, Library, filter, jobject filterObj) {
75+
METHOD(jobjectArray, filter, jobject filterObj) {
8076
auto filter = getPtr<kiwix::Filter>(env, filterObj);
8177
return c2jni(THIS->filter(*filter), env);
8278
}

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

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,40 @@
2525
#include "utils.h"
2626

2727
#define NATIVE_TYPE kiwix::Manager
28-
#define THIS GET_PTR(NATIVE_TYPE)
28+
#define TYPENAME libkiwix_Manager
29+
#include <macros.h>
2930

30-
JNIEXPORT void JNICALL
31-
Java_org_kiwix_libkiwix_Manager_allocate(
32-
JNIEnv* env, jobject thisObj, jobject libraryObj)
31+
METHOD(void, allocate, jobject libraryObj)
3332
{
3433
auto lib = getPtr<kiwix::Library>(env, libraryObj);
3534
SET_PTR(std::make_shared<NATIVE_TYPE>(lib.get()));
3635
}
3736

38-
JNIEXPORT void JNICALL
39-
Java_org_kiwix_libkiwix_Manager_dispose(JNIEnv* env, jobject thisObj)
37+
METHOD0(void, dispose)
4038
{
41-
dispose<kiwix::Manager>(env, thisObj);
39+
dispose<NATIVE_TYPE>(env, thisObj);
4240
}
43-
44-
#define MANAGER (getPtr<kiwix::Manager>(env, thisObj))
45-
4641
/* Kiwix manager functions */
47-
JNIEXPORT jboolean JNICALL
48-
Java_org_kiwix_libkiwix_Manager_readFile(
49-
JNIEnv* env, jobject thisObj, jstring path)
42+
METHOD(jboolean, readFile, jstring path)
5043
{
51-
auto cPath = jni2c(path, env);
44+
auto cPath = TO_C(path);
5245

5346
try {
54-
return MANAGER->readFile(cPath);
47+
return THIS->readFile(cPath);
5548
} catch (std::exception& e) {
5649
LOG("Unable to get readFile");
5750
LOG("%s", e.what());
5851
}
5952
return false;
6053
}
6154

62-
JNIEXPORT jboolean JNICALL
63-
Java_org_kiwix_libkiwix_Manager_readXml(
64-
JNIEnv* env, jobject thisObj, jstring content, jstring libraryPath)
55+
METHOD(jboolean, readXml, jstring content, jstring libraryPath)
6556
{
66-
auto cContent = jni2c(content, env);
67-
auto cPath = jni2c(libraryPath, env);
57+
auto cContent = TO_C(content);
58+
auto cPath = TO_C(libraryPath);
6859

6960
try {
70-
return MANAGER->readXml(cContent, false, cPath);
61+
return THIS->readXml(cContent, false, cPath);
7162
} catch (std::exception& e) {
7263
LOG("Unable to get ZIM id");
7364
LOG("%s", e.what());
@@ -76,15 +67,13 @@ Java_org_kiwix_libkiwix_Manager_readXml(
7667
return false;
7768
}
7869

79-
JNIEXPORT jboolean JNICALL
80-
Java_org_kiwix_libkiwix_Manager_readOpds(
81-
JNIEnv* env, jobject thisObj, jstring content, jstring urlHost)
70+
METHOD(jboolean, readOpds, jstring content, jstring urlHost)
8271
{
83-
auto cContent = jni2c(content, env);
84-
auto cUrl = jni2c(urlHost, env);
72+
auto cContent = TO_C(content);
73+
auto cUrl = TO_C(urlHost);
8574

8675
try {
87-
return MANAGER->readOpds(cContent, cUrl);
76+
return THIS->readOpds(cContent, cUrl);
8877
} catch (std::exception& e) {
8978
LOG("Unable to get ZIM id");
9079
LOG("%s", e.what());
@@ -93,14 +82,12 @@ Java_org_kiwix_libkiwix_Manager_readOpds(
9382
return false;
9483
}
9584

96-
JNIEXPORT jboolean JNICALL
97-
Java_org_kiwix_libkiwix_Manager_readBookmarkFile(
98-
JNIEnv* env, jobject thisObj, jstring path)
85+
METHOD(jboolean, readBookmarkFile, jstring path)
9986
{
100-
auto cPath = jni2c(path, env);
87+
auto cPath = TO_C(path);
10188

10289
try {
103-
return MANAGER->readBookmarkFile(cPath);
90+
return THIS->readBookmarkFile(cPath);
10491
} catch (std::exception& e) {
10592
LOG("Unable to get ZIM id");
10693
LOG("%s", e.what());
@@ -109,18 +96,15 @@ Java_org_kiwix_libkiwix_Manager_readBookmarkFile(
10996
return false;
11097
}
11198

112-
JNIEXPORT jstring JNICALL
113-
Java_org_kiwix_libkiwix_Manager_addBookFromPath(
114-
JNIEnv* env, jobject thisObj,
115-
jstring pathToOpen, jstring pathToSave, jstring url, jboolean checkMetaData)
99+
METHOD(jstring, addBookFromPath, jstring pathToOpen, jstring pathToSave, jstring url, jboolean checkMetaData)
116100
{
117-
auto cPathToOpen = jni2c(pathToOpen, env);
118-
auto cPathToSave = jni2c(pathToSave, env);
119-
auto cUrl = jni2c(url, env);
101+
auto cPathToOpen = TO_C(pathToOpen);
102+
auto cPathToSave = TO_C(pathToSave);
103+
auto cUrl = TO_C(url);
120104
jstring id = NULL;
121105

122106
try {
123-
auto cId = MANAGER->addBookFromPathAndGetId(cPathToOpen, cPathToSave, cUrl, checkMetaData);
107+
auto cId = THIS->addBookFromPathAndGetId(cPathToOpen, cPathToSave, cUrl, checkMetaData);
124108
if ( !cId.empty() ) {
125109
id = c2jni(cId, env);
126110
}

0 commit comments

Comments
 (0)