forked from mapbox/jni.hpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
native_peer.cpp
31 lines (23 loc) · 1.05 KB
/
native_peer.cpp
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
#include <jni/jni.hpp>
#include <iostream>
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*)
{
struct Calculator
{
static constexpr auto Name() { return "Calculator"; }
Calculator(JNIEnv&) { std::cout << "Native peer initialized" << std::endl; }
Calculator(const Calculator&) = delete; // noncopyable
~Calculator() { std::cout << "Native peer finalized" << std::endl; }
jni::jlong Add(jni::JNIEnv&, jni::jlong a, jni::jlong b) { return a + b; }
jni::jlong Subtract(jni::JNIEnv&, jni::jlong a, jni::jlong b) { return a - b; }
};
jni::JNIEnv& env { jni::GetEnv(*vm) };
#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)
jni::RegisterNativePeer<Calculator>(env, jni::Class<Calculator>::Find(env), "peer",
jni::MakePeer<Calculator>,
"initialize",
"finalize",
METHOD(&Calculator::Add, "add"),
METHOD(&Calculator::Subtract, "subtract"));
return jni::Unwrap(jni::jni_version_1_2);
}