Move JNI bindings code from mapbox-gl-native to this repo. (#301)
This commit is contained in:
32
MapboxGLAndroidSDK/src/cpp/conversion/collection.cpp
Normal file
32
MapboxGLAndroidSDK/src/cpp/conversion/collection.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "collection.hpp"
|
||||
#include "constant.hpp"
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
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(jni::Make<std::string>(env, array.Get(env, i)));
|
||||
}
|
||||
|
||||
return 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::Make<jni::String>(env, vector.at(i)));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
18
MapboxGLAndroidSDK/src/cpp/conversion/collection.hpp
Normal file
18
MapboxGLAndroidSDK/src/cpp/conversion/collection.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "conversion.hpp"
|
||||
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
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>&);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
17
MapboxGLAndroidSDK/src/cpp/conversion/color.cpp
Normal file
17
MapboxGLAndroidSDK/src/cpp/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
|
||||
18
MapboxGLAndroidSDK/src/cpp/conversion/color.hpp
Normal file
18
MapboxGLAndroidSDK/src/cpp/conversion/color.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "conversion.hpp"
|
||||
|
||||
#include <mbgl/util/color.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
template <>
|
||||
struct Converter<mbgl::Color, int> {
|
||||
Result<mbgl::Color> operator()(jni::JNIEnv&, const int& color) const;
|
||||
};
|
||||
|
||||
} // namespace conversion
|
||||
} // namespace style
|
||||
} // namespace mbgl
|
||||
62
MapboxGLAndroidSDK/src/cpp/conversion/constant.cpp
Normal file
62
MapboxGLAndroidSDK/src/cpp/conversion/constant.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include "constant.hpp"
|
||||
#include "collection.hpp"
|
||||
#include "../style/formatted.hpp"
|
||||
|
||||
#include <mbgl/util/string.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
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::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, float>::operator()(jni::JNIEnv& env, const float& value) const {
|
||||
return jni::Box(env, 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::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::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, Color>::operator()(jni::JNIEnv& env, const Color& value) const {
|
||||
return jni::Make<jni::String>(env, value.stringify());
|
||||
}
|
||||
|
||||
Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, style::expression::Formatted>::operator()(jni::JNIEnv& env, const style::expression::Formatted& value) const {
|
||||
return Formatted::New(env, value);
|
||||
}
|
||||
|
||||
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 (std::size_t i = 0; i < value.size(); i++) {
|
||||
result.Set(env, i, jni::Make<jni::String>(env, value.at(i)));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
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 (std::size_t i = 0; i < value.size(); i++) {
|
||||
result.Set(env, i, jni::Box(env, value.at(i)));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, style::expression::Image>::operator()(
|
||||
jni::JNIEnv& env, const style::expression::Image& value) const {
|
||||
return jni::Make<jni::String>(env, value.id());
|
||||
}
|
||||
|
||||
} // namespace conversion
|
||||
} // namespace style
|
||||
} // namespace mbgl
|
||||
110
MapboxGLAndroidSDK/src/cpp/conversion/constant.hpp
Normal file
110
MapboxGLAndroidSDK/src/cpp/conversion/constant.hpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#pragma once
|
||||
|
||||
#include "conversion.hpp"
|
||||
|
||||
#include <mbgl/util/color.hpp>
|
||||
#include <mbgl/util/enum.hpp>
|
||||
|
||||
#include <mbgl/style/expression/formatted.hpp>
|
||||
#include <mbgl/style/expression/image.hpp>
|
||||
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <array>
|
||||
#include <vector>
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
template <>
|
||||
struct Converter<jni::Local<jni::Object<>>, bool> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const bool& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::Local<jni::Object<>>, float> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const float& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::Local<jni::Object<>>, double> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const double& value) const;
|
||||
};
|
||||
|
||||
/**
|
||||
* All integrals. java is limited to 64 bit signed, so...
|
||||
* TODO: use BigDecimal for > 64 / unsigned?
|
||||
*/
|
||||
template<typename T>
|
||||
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::Local<jni::Object<>>, std::string> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const std::string& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::Local<jni::Object<>>, Color> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const Color& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::Local<jni::Object<>>, style::expression::Formatted> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const style::expression::Formatted& value) const;
|
||||
};
|
||||
|
||||
template <std::size_t N>
|
||||
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::Local<jni::Object<>>, std::vector<float>>(env, v);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
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::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::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));
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct Converter<jni::Local<jni::Object<>>, std::vector<T>, typename std::enable_if_t<std::is_enum<T>::value>> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const std::vector<T>& value) const {
|
||||
auto result = jni::Array<jni::String>::New(env, value.size());
|
||||
for (std::size_t i = 0; i < value.size(); ++i) {
|
||||
result.Set(env, i, jni::Make<jni::String>(env, Enum<T>::toString(value.at(i))));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::Local<jni::Object<>>, style::expression::Image> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const style::expression::Image& value) const;
|
||||
};
|
||||
|
||||
} // namespace conversion
|
||||
} // namespace android
|
||||
} // namespace mbgl
|
||||
50
MapboxGLAndroidSDK/src/cpp/conversion/conversion.hpp
Normal file
50
MapboxGLAndroidSDK/src/cpp/conversion/conversion.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/util/variant.hpp>
|
||||
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
namespace conversion {
|
||||
|
||||
struct Error { std::string message; };
|
||||
|
||||
template <class T>
|
||||
class Result : private variant<T, Error> {
|
||||
public:
|
||||
using variant<T, Error>::variant;
|
||||
|
||||
explicit operator bool() const {
|
||||
return this->template is<T>();
|
||||
}
|
||||
|
||||
T& operator*() {
|
||||
assert(this->template is<T>());
|
||||
return this->template get<T>();
|
||||
}
|
||||
|
||||
const T& operator*() const {
|
||||
assert(this->template is<T>());
|
||||
return this->template get<T>();
|
||||
}
|
||||
|
||||
const Error& error() const {
|
||||
assert(this->template is<Error>());
|
||||
return this->template get<Error>();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T, class V, class Enable = void>
|
||||
struct Converter;
|
||||
|
||||
template <class T, typename V, class...Args>
|
||||
Result<T> convert(jni::JNIEnv& env, const V& value, Args&&...args) {
|
||||
return Converter<T, V>()(env, value, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
} // namespace conversion
|
||||
} // namespace android
|
||||
} // namespace mbgl
|
||||
Reference in New Issue
Block a user