forked from remram44/java-cpp-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjni_wrapper.cpp
More file actions
55 lines (47 loc) · 1.43 KB
/
jni_wrapper.cpp
File metadata and controls
55 lines (47 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "cpplib/MyClass.h"
#include "MyClass_jni.h"
static jfieldID _get_self_id(JNIEnv *env, jobject thisObj)
{
static int init = 0;
static jfieldID fidSelfPtr;
if(!init)
{
jclass thisClass = env->GetObjectClass(thisObj);
fidSelfPtr = env->GetFieldID(thisClass, "self_ptr", "J");
}
return fidSelfPtr;
}
static MyClass *_get_self(JNIEnv *env, jobject thisObj)
{
jlong selfPtr = env->GetLongField(thisObj, _get_self_id(env, thisObj));
return *(MyClass**)&selfPtr;
}
static void _set_self(JNIEnv *env, jobject thisObj, MyClass *self)
{
jlong selfPtr = *(jlong*)&self;
env->SetLongField(thisObj, _get_self_id(env, thisObj), selfPtr);
}
JNIEXPORT void JNICALL Java_edu_nyu_cpptest_cpplib_MyClass_init(JNIEnv *env, jobject thisObj, jint nb)
{
MyClass *self = new MyClass(nb);
_set_self(env, thisObj, self);
}
JNIEXPORT jint JNICALL Java_edu_nyu_cpptest_cpplib_MyClass_getValue(JNIEnv *env, jobject thisObj)
{
MyClass *self = _get_self(env, thisObj);
return self->getValue();
}
JNIEXPORT void JNICALL Java_edu_nyu_cpptest_cpplib_MyClass_increment(JNIEnv *env, jobject thisObj)
{
MyClass *self = _get_self(env, thisObj);
self->increment();
}
JNIEXPORT void JNICALL Java_edu_nyu_cpptest_cpplib_MyClass_finalize(JNIEnv *env, jobject thisObj)
{
MyClass *self = _get_self(env, thisObj);
if(self != NULL)
{
delete self;
_set_self(env, thisObj, NULL);
}
}