[android] Move conversion code to .cpp files
This commit is contained in:
38
platform/android/src/conversion/collection.cpp
Normal file
38
platform/android/src/conversion/collection.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "collection.hpp"
|
||||
#include "constant.hpp"
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
std::vector<std::string> toVector(JNIEnv& env, jni::jarray<jni::jobject>& array) {
|
||||
std::vector<std::string> vector;
|
||||
std::size_t len = jni::GetArrayLength(env, array);
|
||||
vector.reserve(len);
|
||||
|
||||
for (std::size_t i = 0; i < len; i++) {
|
||||
jni::jstring* jstr = reinterpret_cast<jni::jstring*>(jni::GetObjectArrayElement(env, array, i));
|
||||
vector.push_back(*convert<std::string, jni::String>(env, jni::String(jstr)));
|
||||
jni::DeleteLocalRef(env, jstr);
|
||||
}
|
||||
|
||||
return vector;
|
||||
}
|
||||
|
||||
std::vector<std::string> toVector(JNIEnv& env, 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++) {
|
||||
jni::String jstr = array.Get(env, i);
|
||||
vector.push_back(*convert<std::string, jni::String>(env, jstr));
|
||||
jni::DeleteLocalRef(env, jstr);
|
||||
}
|
||||
|
||||
return vector;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "conversion.hpp"
|
||||
#include "constant.hpp"
|
||||
|
||||
#include <mbgl/util/optional.hpp>
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
#include <vector>
|
||||
@@ -16,7 +14,7 @@ namespace conversion {
|
||||
* Convert jarray -> ArrayList
|
||||
*/
|
||||
template <class T>
|
||||
inline jni::jobject* toArrayList(JNIEnv& env, jni::jarray<T>& array) {
|
||||
jni::jobject* toArrayList(JNIEnv& env, jni::jarray<T>& array) {
|
||||
static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/util/Arrays")).release();
|
||||
static jni::jmethodID* asList = &jni::GetStaticMethodID(env, *javaClass, "asList", "([Ljava/lang/Object;)Ljava/util/List;");
|
||||
return reinterpret_cast<jni::jobject*>(jni::CallStaticMethod<jni::jobject*>(env, *javaClass, *asList, array));
|
||||
@@ -24,34 +22,8 @@ inline jni::jobject* toArrayList(JNIEnv& env, jni::jarray<T>& array) {
|
||||
|
||||
// Java -> C++
|
||||
|
||||
|
||||
inline std::vector<std::string> toVector(JNIEnv& env, jni::jarray<jni::jobject>& array) {
|
||||
std::vector<std::string> vector;
|
||||
std::size_t len = jni::GetArrayLength(env, array);
|
||||
vector.reserve(len);
|
||||
|
||||
for (std::size_t i = 0; i < len; i++) {
|
||||
jni::jstring* jstr = reinterpret_cast<jni::jstring*>(jni::GetObjectArrayElement(env, array, i));
|
||||
vector.push_back(*convert<std::string, jni::String>(env, jni::String(jstr)));
|
||||
jni::DeleteLocalRef(env, jstr);
|
||||
}
|
||||
|
||||
return vector;
|
||||
}
|
||||
|
||||
inline std::vector<std::string> toVector(JNIEnv& env, 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++) {
|
||||
jni::String jstr = array.Get(env, i);
|
||||
vector.push_back(*convert<std::string, jni::String>(env, jstr));
|
||||
jni::DeleteLocalRef(env, jstr);
|
||||
}
|
||||
|
||||
return vector;
|
||||
}
|
||||
std::vector<std::string> toVector(JNIEnv& env, jni::jarray<jni::jobject>& array);
|
||||
std::vector<std::string> toVector(JNIEnv& env, jni::Array<jni::String> array);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
17
platform/android/src/conversion/color.cpp
Normal file
17
platform/android/src/conversion/color.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "color.hpp"
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
Result<mbgl::Color> Converter<mbgl::Color, int>::operator()(jni::JNIEnv&, const int& color) const {
|
||||
float r = (color >> 16) & 0xFF;
|
||||
float g = (color >> 8) & 0xFF;
|
||||
float b = (color) & 0xFF;
|
||||
float a = (color >> 24) & 0xFF;
|
||||
return { mbgl::Color( r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f ) };
|
||||
}
|
||||
|
||||
} // namespace conversion
|
||||
} // namespace style
|
||||
} // namespace mbgl
|
||||
@@ -10,13 +10,7 @@ namespace conversion {
|
||||
|
||||
template <>
|
||||
struct Converter<mbgl::Color, int> {
|
||||
Result<mbgl::Color> operator()(jni::JNIEnv&, const int& color) const {
|
||||
float r = (color >> 16) & 0xFF;
|
||||
float g = (color >> 8) & 0xFF;
|
||||
float b = (color) & 0xFF;
|
||||
float a = (color >> 24) & 0xFF;
|
||||
return { mbgl::Color( r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f ) };
|
||||
}
|
||||
Result<mbgl::Color> operator()(jni::JNIEnv&, const int& color) const;
|
||||
};
|
||||
|
||||
} // namespace conversion
|
||||
|
||||
86
platform/android/src/conversion/constant.cpp
Normal file
86
platform/android/src/conversion/constant.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include "constant.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
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::jboolean> Converter<jni::jboolean, bool>::operator()(jni::JNIEnv&, const bool& value) const {
|
||||
return {(jni::jboolean) value};
|
||||
}
|
||||
|
||||
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::jfloat> Converter<jni::jfloat, float>::operator()(jni::JNIEnv&, const float& value) const {
|
||||
return {(jni::jfloat) 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::jdouble> Converter<jni::jdouble, float>::operator()(jni::JNIEnv&, const double& value) const {
|
||||
return {(jni::jdouble) 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::jstring*> Converter<jni::jstring*, std::string>::operator()(jni::JNIEnv& env, const std::string& value) const {
|
||||
return {jni::Make<jni::String>(env, value).Get()};
|
||||
}
|
||||
|
||||
Result<jni::jobject*> Converter<jni::jobject*, 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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return &jarray;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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) };
|
||||
}
|
||||
|
||||
} // namespace conversion
|
||||
} // namespace style
|
||||
} // namespace mbgl
|
||||
@@ -2,14 +2,12 @@
|
||||
|
||||
#include "conversion.hpp"
|
||||
|
||||
#include <mbgl/util/optional.hpp>
|
||||
#include <mbgl/util/color.hpp>
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
@@ -17,51 +15,33 @@ namespace conversion {
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jobject*, bool> {
|
||||
Result<jni::jobject*> 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::jobject*> operator()(jni::JNIEnv& env, const bool& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jboolean, bool> {
|
||||
Result<jni::jboolean> operator()(jni::JNIEnv&, const bool& value) const {
|
||||
return {(jni::jboolean) value};
|
||||
}
|
||||
Result<jni::jboolean> operator()(jni::JNIEnv&, const bool& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jobject*, float> {
|
||||
Result<jni::jobject*> 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::jobject*> operator()(jni::JNIEnv& env, const float& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jfloat, float> {
|
||||
Result<jni::jfloat> operator()(jni::JNIEnv&, const float& value) const {
|
||||
return {(jni::jfloat) value};
|
||||
}
|
||||
Result<jni::jfloat> operator()(jni::JNIEnv&, const float& value) const;
|
||||
};
|
||||
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jobject*, double> {
|
||||
Result<jni::jobject*> 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::jobject*> operator()(jni::JNIEnv& env, const double& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jdouble, float> {
|
||||
Result<jni::jdouble> operator()(jni::JNIEnv&, const double& value) const {
|
||||
return {(jni::jdouble) value};
|
||||
}
|
||||
Result<jni::jdouble> operator()(jni::JNIEnv&, const double& value) const;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -81,26 +61,17 @@ struct Converter<jni::jobject*, T, typename std::enable_if<std::is_integral<T>::
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jobject*, std::string> {
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::string& value) const {
|
||||
return {jni::Make<jni::String>(env, value).Get()};
|
||||
}
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::string& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jstring*, std::string> {
|
||||
Result<jni::jstring*> operator()(jni::JNIEnv& env, const std::string& value) const {
|
||||
return {jni::Make<jni::String>(env, value).Get()};
|
||||
}
|
||||
Result<jni::jstring*> 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 {
|
||||
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);
|
||||
}
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const Color& value) const;
|
||||
};
|
||||
|
||||
template <std::size_t N>
|
||||
@@ -116,41 +87,19 @@ struct Converter<jni::jobject*, std::array<float, N>> {
|
||||
|
||||
template <>
|
||||
struct Converter<jni::jobject*, std::vector<std::string>> {
|
||||
Result<jni::jobject*> 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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return &jarray;
|
||||
}
|
||||
Result<jni::jobject*> 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 {
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return &jarray;
|
||||
}
|
||||
Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::vector<float>& value) const;
|
||||
};
|
||||
|
||||
// Java -> C++
|
||||
|
||||
template <>
|
||||
struct Converter<std::string, jni::String> {
|
||||
Result<std::string> operator()(jni::JNIEnv& env, const jni::String& value) const {
|
||||
return { jni::Make<std::string>(env, value) };
|
||||
}
|
||||
Result<std::string> operator()(jni::JNIEnv& env, const jni::String& value) const;
|
||||
};
|
||||
|
||||
} // namespace conversion
|
||||
|
||||
Reference in New Issue
Block a user