Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/native_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*)
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 Add(jni::JNIEnv&, jni::jlong a, jni::jlong b) const { return a + b; }
jni::jlong Subtract(jni::JNIEnv&, jni::jlong a, jni::jlong b) { return a - b; }
};

Expand Down
25 changes: 25 additions & 0 deletions include/jni/native_method.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,31 @@ namespace jni
}
};

template < class R, class P, class... Args, R (P::*method)(JNIEnv&, Args...) const>
class NativePeerMemberFunctionMethod< R (P::*)(JNIEnv&, Args...) const, method>
{
private:
const char* name;

public:
NativePeerMemberFunctionMethod(const char* n)
: name(n)
{}

template < class Peer, class TagType, class = std::enable_if_t< std::is_same<P, Peer>::value > >
auto operator()(const Field<TagType, jlong>& field)
{
auto wrapper = [field] (JNIEnv& env, Object<TagType>& obj, Args... args)
{
auto ptr = reinterpret_cast<P*>(obj.Get(env, field));
if (ptr) return (ptr->*method)(env, args...);
ThrowNew(env, jni::FindClass(env, "java/lang/IllegalStateException"),
"invalid native peer");
};
return MakeNativeMethod(name, wrapper);
}
};

template < class M, M method>
auto MakeNativePeerMethod(const char* name,
std::enable_if_t< std::is_member_function_pointer<M>::value >* = nullptr)
Expand Down