[android] jni.hpp 4.0.0
This commit is contained in:
@@ -5,23 +5,23 @@ namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
std::vector<std::string> toVector(JNIEnv& env, jni::Array<jni::String> array) {
|
||||
std::vector<std::string> toVector(JNIEnv& env, const jni::Array<jni::String>& array) {
|
||||
std::size_t len = array.Length(env);
|
||||
std::vector<std::string> vector;
|
||||
vector.reserve(len);
|
||||
|
||||
for (std::size_t i = 0; i < len; i++) {
|
||||
vector.push_back(*convert<std::string>(env, *jni::SeizeLocal(env, array.Get(env, i))));
|
||||
vector.push_back(jni::Make<std::string>(env, array.Get(env, i)));
|
||||
}
|
||||
|
||||
return vector;
|
||||
}
|
||||
|
||||
jni::Array<jni::String> toArray(JNIEnv& env, const std::vector<std::string>& vector) {
|
||||
jni::Local<jni::Array<jni::String>> toArray(JNIEnv& env, const std::vector<std::string>& vector) {
|
||||
auto result = jni::Array<jni::String>::New(env, vector.size());
|
||||
|
||||
for (std::size_t i = 0; i < vector.size(); i++) {
|
||||
result.Set(env, i, *jni::SeizeLocal(env, jni::Make<jni::String>(env, vector.at(i))));
|
||||
result.Set(env, i, jni::Make<jni::String>(env, vector.at(i)));
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -10,8 +10,8 @@ namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
std::vector<std::string> toVector(JNIEnv& env, jni::Array<jni::String> array);
|
||||
jni::Array<jni::String> toArray(JNIEnv& env, const std::vector<std::string>&);
|
||||
std::vector<std::string> toVector(JNIEnv& env, const jni::Array<jni::String>& array);
|
||||
jni::Local<jni::Array<jni::String>> toArray(JNIEnv& env, const std::vector<std::string>&);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "constant.hpp"
|
||||
#include "collection.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
@@ -6,63 +7,46 @@ namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
Result<jni::jobject*> Converter<jni::jobject*, bool>::operator()(jni::JNIEnv& env, const bool& value) const {
|
||||
static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Boolean")).release();
|
||||
static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "(Z)V");
|
||||
return {&jni::NewObject(env, *javaClass, *constructor, (jboolean) value)};
|
||||
Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, bool>::operator()(jni::JNIEnv& env, const bool& value) const {
|
||||
return jni::Box(env, value ? jni::jni_true : jni::jni_false);
|
||||
}
|
||||
|
||||
Result<jni::jobject*> Converter<jni::jobject*, float>::operator()(jni::JNIEnv& env, const float& value) const {
|
||||
static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Float")).release();
|
||||
static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "(F)V");
|
||||
return {&jni::NewObject(env, *javaClass, *constructor, (jfloat) value)};
|
||||
Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, float>::operator()(jni::JNIEnv& env, const float& value) const {
|
||||
return jni::Box(env, value);
|
||||
}
|
||||
|
||||
Result<jni::jobject*> Converter<jni::jobject*, double>::operator()(jni::JNIEnv& env, const double& value) const {
|
||||
static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Double")).release();
|
||||
static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "(D)V");
|
||||
return {&jni::NewObject(env, *javaClass, *constructor, (jfloat) value)};
|
||||
Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, double>::operator()(jni::JNIEnv& env, const double& value) const {
|
||||
return jni::Box(env, value);
|
||||
}
|
||||
|
||||
Result<jni::jobject*> Converter<jni::jobject*, std::string>::operator()(jni::JNIEnv& env, const std::string& value) const {
|
||||
return {jni::Make<jni::String>(env, value).Get()};
|
||||
Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, std::string>::operator()(jni::JNIEnv& env, const std::string& value) const {
|
||||
return jni::Make<jni::String>(env, value);
|
||||
}
|
||||
|
||||
Result<jni::jobject*> Converter<jni::jobject*, Color>::operator()(jni::JNIEnv& env, const Color& value) const {
|
||||
Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, Color>::operator()(jni::JNIEnv& env, const Color& value) const {
|
||||
std::stringstream sstream;
|
||||
sstream << "rgba(" << value.r << ", " << value.g << ", " << value.b << ", " << value.a << ")";
|
||||
std::string result = sstream.str();
|
||||
return convert<jni::jobject*, std::string>(env, result);
|
||||
return jni::Make<jni::String>(env, sstream.str());
|
||||
}
|
||||
|
||||
Result<jni::jobject*> Converter<jni::jobject*, std::vector<std::string>>::operator()(jni::JNIEnv& env, const std::vector<std::string>& value) const {
|
||||
static jni::jclass* stringCass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/String")).release();
|
||||
jni::jarray<jni::jobject>& jarray = jni::NewObjectArray(env, value.size(), *stringCass);
|
||||
Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, std::vector<std::string>>::operator()(jni::JNIEnv& env, const std::vector<std::string>& value) const {
|
||||
auto result = jni::Array<jni::String>::New(env, value.size());
|
||||
|
||||
for(size_t i = 0; i < value.size(); i = i + 1) {
|
||||
Result<jni::jobject*> converted = convert<jni::jobject*, std::string>(env, value.at(i));
|
||||
jni::SetObjectArrayElement(env, jarray, i, *converted);
|
||||
for (std::size_t i = 0; i < value.size(); i++) {
|
||||
result.Set(env, i, jni::Make<jni::String>(env, value.at(i)));
|
||||
}
|
||||
|
||||
return &jarray;
|
||||
return result;
|
||||
}
|
||||
|
||||
Result<jni::jobject*> Converter<jni::jobject*, std::vector<float>>::operator()(jni::JNIEnv& env, const std::vector<float>& value) const {
|
||||
static jni::jclass* floatClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Float")).release();
|
||||
jni::jarray<jni::jobject>& jarray = jni::NewObjectArray(env, value.size(), *floatClass);
|
||||
Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, std::vector<float>>::operator()(jni::JNIEnv& env, const std::vector<float>& value) const {
|
||||
auto result = jni::Array<jni::Float>::New(env, value.size());
|
||||
|
||||
for(size_t i = 0; i < value.size(); i = i + 1) {
|
||||
Result<jni::jobject*> converted = convert<jni::jobject*, float>(env, value.at(i));
|
||||
jni::SetObjectArrayElement(env, jarray, i, *converted);
|
||||
for (std::size_t i = 0; i < value.size(); i++) {
|
||||
result.Set(env, i, jni::Box(env, value.at(i)));
|
||||
}
|
||||
|
||||
return &jarray;
|
||||
}
|
||||
|
||||
// Java -> C++
|
||||
|
||||
Result<std::string> Converter<std::string, jni::String>::operator()(jni::JNIEnv& env, const jni::String& value) const {
|
||||
return { jni::Make<std::string>(env, value) };
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace conversion
|
||||
|
||||
@@ -16,18 +16,18 @@ namespace android {
|
||||
namespace conversion {
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jobject*, bool> {
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const bool& value) const;
|
||||
struct Converter<jni::Local<jni::Object<>>, bool> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const bool& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jobject*, float> {
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const float& value) const;
|
||||
struct Converter<jni::Local<jni::Object<>>, float> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const float& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jobject*, double> {
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const double& value) const;
|
||||
struct Converter<jni::Local<jni::Object<>>, double> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const double& value) const;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -35,61 +35,52 @@ struct Converter<jni::jobject*, double> {
|
||||
* TODO: use BigDecimal for > 64 / unsigned?
|
||||
*/
|
||||
template<typename T>
|
||||
struct Converter<jni::jobject*, T, typename std::enable_if<std::is_integral<T>::value>::type> {
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const T& value) const {
|
||||
static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Long")).release();
|
||||
static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "(J)V");
|
||||
return {&jni::NewObject(env, *javaClass, *constructor, (jlong) value)};
|
||||
struct Converter<jni::Local<jni::Object<>>, T, typename std::enable_if<std::is_integral<T>::value>::type> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const T& value) const {
|
||||
return jni::Box(env, jni::jlong(value));
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: convert integral types to primitive jni types
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jobject*, std::string> {
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::string& value) const;
|
||||
struct Converter<jni::Local<jni::Object<>>, std::string> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const std::string& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jobject*, Color> {
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const Color& value) const;
|
||||
struct Converter<jni::Local<jni::Object<>>, Color> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const Color& value) const;
|
||||
};
|
||||
|
||||
template <std::size_t N>
|
||||
struct Converter<jni::jobject*, std::array<float, N>> {
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::array<float, N>& value) const {
|
||||
struct Converter<jni::Local<jni::Object<>>, std::array<float, N>> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const std::array<float, N>& value) const {
|
||||
std::vector<float> v;
|
||||
for (const float& id : value) {
|
||||
v.push_back(id);
|
||||
}
|
||||
return convert<jni::jobject*, std::vector<float>>(env, v);
|
||||
return convert<jni::Local<jni::Object<>>, std::vector<float>>(env, v);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jobject*, std::vector<std::string>> {
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::vector<std::string>& value) const;
|
||||
struct Converter<jni::Local<jni::Object<>>, std::vector<std::string>> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const std::vector<std::string>& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jobject*, std::vector<float>> {
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::vector<float>& value) const;
|
||||
struct Converter<jni::Local<jni::Object<>>, std::vector<float>> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const std::vector<float>& value) const;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct Converter<jni::jobject*, T, typename std::enable_if_t<std::is_enum<T>::value>> {
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const T& value) const {
|
||||
return convert<jni::jobject*, std::string>(env, Enum<T>::toString(value));
|
||||
struct Converter<jni::Local<jni::Object<>>, T, typename std::enable_if_t<std::is_enum<T>::value>> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const T& value) const {
|
||||
return convert<jni::Local<jni::Object<>>, std::string>(env, Enum<T>::toString(value));
|
||||
}
|
||||
};
|
||||
|
||||
// Java -> C++
|
||||
|
||||
template <>
|
||||
struct Converter<std::string, jni::String> {
|
||||
Result<std::string> operator()(jni::JNIEnv& env, const jni::String& value) const;
|
||||
};
|
||||
|
||||
} // namespace conversion
|
||||
} // namespace style
|
||||
} // namespace android
|
||||
} // namespace mbgl
|
||||
|
||||
Reference in New Issue
Block a user