Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I use the library to expose a native method taking std::string? #45

Open
haelix888 opened this issue Nov 22, 2018 · 1 comment

Comments

@haelix888
Copy link

Or otherwise said, do I expect of the library to "wrap"/generate marshalling code (and if so, to which extent?), or should I declare my native methods to take something as close as possible to the Java types? In this case, jstring. But that fails like:

/mnt/data/dev/mapbox-jnihpp/include/jni/tagging.hpp:130:40:   required by substitution of ‘template<class T> using UntaggedType = decltype (jni::Untag(declval<T>())) [with T = jni::jstring]’
/mnt/data/dev/mapbox-jnihpp/include/jni/tagging.hpp:124:64: error: ‘const struct jni::jstring’ has no member named ‘get’
@this-kirke
Copy link

For marshaling strings, the methods you want to know are:

const char* c_str = "Hello World";
std::string std_str( c_str );
jni::Local<jni::String> c_string_to_java_string = jni::Make<jni::String>( env, c_str );
jni::Local<jni::String> std_string_to_java_string = jni::Make<jni::String>( env, std_str );
std::string std_str_from_java_string = jni::Make<std::string>( env, c_string_to_java_string );

Use jni::Local<jni::String> in place of jstring for both java->native and native->Java methods.

The standard types - jni::jstring, jni::jobject, etc are part of the low-level API. You can use them, but for methods taking Unique parameters you'll need to create an ownership type wrapper first, see jni::NewLocal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants