diff --git a/platform/android/core-files.txt b/platform/android/core-files.txt index 7d86cb5615a..cdcffb857fe 100644 --- a/platform/android/core-files.txt +++ b/platform/android/core-files.txt @@ -48,12 +48,25 @@ platform/default/mbgl/map/map_snapshotter.hpp platform/linux/src/headless_backend_egl.cpp # Conversion C++ -> Java +platform/android/src/conversion/collection.cpp +platform/android/src/conversion/collection.hpp +platform/android/src/conversion/color.cpp +platform/android/src/conversion/color.hpp +platform/android/src/conversion/constant.cpp platform/android/src/conversion/constant.hpp platform/android/src/conversion/conversion.hpp +platform/android/src/geojson/conversion/feature.cpp +platform/android/src/geojson/conversion/feature.hpp +platform/android/src/style/conversion/filter.cpp +platform/android/src/style/conversion/filter.hpp +platform/android/src/style/conversion/position.cpp +platform/android/src/style/conversion/position.hpp platform/android/src/style/conversion/property_expression.hpp platform/android/src/style/conversion/property_value.hpp -platform/android/src/style/conversion/types.hpp -platform/android/src/style/conversion/types_string_values.hpp +platform/android/src/style/conversion/transition_options.cpp +platform/android/src/style/conversion/transition_options.hpp +platform/android/src/style/conversion/url_or_tileset.cpp +platform/android/src/style/conversion/url_or_tileset.hpp platform/android/src/map/camera_position.cpp platform/android/src/map/camera_position.hpp platform/android/src/map/image.cpp diff --git a/platform/android/scripts/generate-style-code.js b/platform/android/scripts/generate-style-code.js index 888b9fca30c..98c5a446b9d 100755 --- a/platform/android/scripts/generate-style-code.js +++ b/platform/android/scripts/generate-style-code.js @@ -363,20 +363,3 @@ writeIfModified( `platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Property.java`, enumPropertyJavaTemplate({properties: enumProperties}) ); - -// De-duplicate enum properties before processing jni property templates -const enumPropertiesDeDup = _(enumProperties).uniqBy(global.propertyNativeType).value(); - -// JNI Enum property conversion templates -const enumPropertyHppTypeStringValueTemplate = ejs.compile(fs.readFileSync('platform/android/src/style/conversion/types_string_values.hpp.ejs', 'utf8'), {strict: true}); -writeIfModified( - `platform/android/src/style/conversion/types_string_values.hpp`, - enumPropertyHppTypeStringValueTemplate({properties: enumPropertiesDeDup}) -); - -// JNI property value types conversion templates -const enumPropertyHppTypeTemplate = ejs.compile(fs.readFileSync('platform/android/src/style/conversion/types.hpp.ejs', 'utf8'), {strict: true}); -writeIfModified( - `platform/android/src/style/conversion/types.hpp`, - enumPropertyHppTypeTemplate({properties: enumPropertiesDeDup}) -); diff --git a/platform/android/src/conversion/collection.cpp b/platform/android/src/conversion/collection.cpp new file mode 100644 index 00000000000..27b614e8cdd --- /dev/null +++ b/platform/android/src/conversion/collection.cpp @@ -0,0 +1,24 @@ +#include "collection.hpp" +#include "constant.hpp" + +namespace mbgl { +namespace android { +namespace conversion { + +std::vector toVector(JNIEnv& env, jni::Array array) { + std::size_t len = array.Length(env); + std::vector vector; + vector.reserve(len); + + for (std::size_t i = 0; i < len; i++) { + jni::String jstr = array.Get(env, i); + vector.push_back(*convert(env, jstr)); + jni::DeleteLocalRef(env, jstr); + } + + return vector; +} + +} +} +} diff --git a/platform/android/src/conversion/collection.hpp b/platform/android/src/conversion/collection.hpp index 2b953e73f44..bb8941c984f 100644 --- a/platform/android/src/conversion/collection.hpp +++ b/platform/android/src/conversion/collection.hpp @@ -1,9 +1,7 @@ #pragma once #include "conversion.hpp" -#include "constant.hpp" -#include #include #include @@ -12,46 +10,7 @@ namespace mbgl { namespace android { namespace conversion { -/** - * Convert jarray -> ArrayList - */ -template -inline jni::jobject* toArrayList(JNIEnv& env, jni::jarray& 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::CallStaticMethod(env, *javaClass, *asList, array)); -} - -// Java -> C++ - - -inline std::vector toVector(JNIEnv& env, jni::jarray& array) { - std::vector 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::GetObjectArrayElement(env, array, i)); - vector.push_back(*convert(env, jni::String(jstr))); - jni::DeleteLocalRef(env, jstr); - } - - return vector; -} - -inline std::vector toVector(JNIEnv& env, jni::Array array) { - std::size_t len = array.Length(env); - std::vector vector; - vector.reserve(len); - - for (std::size_t i = 0; i < len; i++) { - jni::String jstr = array.Get(env, i); - vector.push_back(*convert(env, jstr)); - jni::DeleteLocalRef(env, jstr); - } - - return vector; -} +std::vector toVector(JNIEnv& env, jni::Array array); } } diff --git a/platform/android/src/conversion/color.cpp b/platform/android/src/conversion/color.cpp new file mode 100644 index 00000000000..ce85943e61e --- /dev/null +++ b/platform/android/src/conversion/color.cpp @@ -0,0 +1,17 @@ +#include "color.hpp" + +namespace mbgl { +namespace android { +namespace conversion { + +Result Converter::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 diff --git a/platform/android/src/conversion/color.hpp b/platform/android/src/conversion/color.hpp index 40aa68d4a92..2b4144b933e 100644 --- a/platform/android/src/conversion/color.hpp +++ b/platform/android/src/conversion/color.hpp @@ -10,13 +10,7 @@ namespace conversion { template <> struct Converter { - Result 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 operator()(jni::JNIEnv&, const int& color) const; }; } // namespace conversion diff --git a/platform/android/src/conversion/constant.cpp b/platform/android/src/conversion/constant.cpp new file mode 100644 index 00000000000..16e8b329438 --- /dev/null +++ b/platform/android/src/conversion/constant.cpp @@ -0,0 +1,70 @@ +#include "constant.hpp" + +#include + +namespace mbgl { +namespace android { +namespace conversion { + +Result Converter::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, "", "(Z)V"); + return {&jni::NewObject(env, *javaClass, *constructor, (jboolean) value)}; +} + +Result Converter::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, "", "(F)V"); + return {&jni::NewObject(env, *javaClass, *constructor, (jfloat) value)}; +} + +Result Converter::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, "", "(D)V"); + return {&jni::NewObject(env, *javaClass, *constructor, (jfloat) value)}; +} + +Result Converter::operator()(jni::JNIEnv& env, const std::string& value) const { + return {jni::Make(env, value).Get()}; +} + +Result Converter::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(env, result); +} + +Result Converter>::operator()(jni::JNIEnv& env, const std::vector& value) const { + static jni::jclass* stringCass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/String")).release(); + jni::jarray& jarray = jni::NewObjectArray(env, value.size(), *stringCass); + + for(size_t i = 0; i < value.size(); i = i + 1) { + Result converted = convert(env, value.at(i)); + jni::SetObjectArrayElement(env, jarray, i, *converted); + } + + return &jarray; +} + +Result Converter>::operator()(jni::JNIEnv& env, const std::vector& value) const { + static jni::jclass* floatClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Float")).release(); + jni::jarray& jarray = jni::NewObjectArray(env, value.size(), *floatClass); + + for(size_t i = 0; i < value.size(); i = i + 1) { + Result converted = convert(env, value.at(i)); + jni::SetObjectArrayElement(env, jarray, i, *converted); + } + + return &jarray; +} + +// Java -> C++ + +Result Converter::operator()(jni::JNIEnv& env, const jni::String& value) const { + return { jni::Make(env, value) }; +} + +} // namespace conversion +} // namespace style +} // namespace mbgl diff --git a/platform/android/src/conversion/constant.hpp b/platform/android/src/conversion/constant.hpp index f1c72eb5dd0..0e665cf56a1 100644 --- a/platform/android/src/conversion/constant.hpp +++ b/platform/android/src/conversion/constant.hpp @@ -2,14 +2,14 @@ #include "conversion.hpp" -#include #include +#include + #include #include #include #include -#include namespace mbgl { namespace android { @@ -17,51 +17,17 @@ namespace conversion { template <> struct Converter { - Result 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, "", "(Z)V"); - return {&jni::NewObject(env, *javaClass, *constructor, (jboolean) value)}; - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv&, const bool& value) const { - return {(jni::jboolean) value}; - } + Result operator()(jni::JNIEnv& env, const bool& value) const; }; template <> struct Converter { - Result 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, "", "(F)V"); - return {&jni::NewObject(env, *javaClass, *constructor, (jfloat) value)}; - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv&, const float& value) const { - return {(jni::jfloat) value}; - } + Result operator()(jni::JNIEnv& env, const float& value) const; }; - template <> struct Converter { - Result 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, "", "(D)V"); - return {&jni::NewObject(env, *javaClass, *constructor, (jfloat) value)}; - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv&, const double& value) const { - return {(jni::jdouble) value}; - } + Result operator()(jni::JNIEnv& env, const double& value) const; }; /** @@ -81,26 +47,12 @@ struct Converter:: template <> struct Converter { - Result operator()(jni::JNIEnv& env, const std::string& value) const { - return {jni::Make(env, value).Get()}; - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const std::string& value) const { - return {jni::Make(env, value).Get()}; - } + Result operator()(jni::JNIEnv& env, const std::string& value) const; }; template <> struct Converter { - Result 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(env, result); - } + Result operator()(jni::JNIEnv& env, const Color& value) const; }; template @@ -116,31 +68,18 @@ struct Converter> { template <> struct Converter> { - Result operator()(jni::JNIEnv& env, const std::vector& value) const { - static jni::jclass* stringCass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/String")).release(); - jni::jarray& jarray = jni::NewObjectArray(env, value.size(), *stringCass); - - for(size_t i = 0; i < value.size(); i = i + 1) { - Result converted = convert(env, value.at(i)); - jni::SetObjectArrayElement(env, jarray, i, *converted); - } - - return &jarray; - } + Result operator()(jni::JNIEnv& env, const std::vector& value) const; }; template <> struct Converter> { - Result operator()(jni::JNIEnv& env, const std::vector& value) const { - static jni::jclass* floatClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Float")).release(); - jni::jarray& jarray = jni::NewObjectArray(env, value.size(), *floatClass); - - for(size_t i = 0; i < value.size(); i = i + 1) { - Result converted = convert(env, value.at(i)); - jni::SetObjectArrayElement(env, jarray, i, *converted); - } + Result operator()(jni::JNIEnv& env, const std::vector& value) const; +}; - return &jarray; +template +struct Converter::value>> { + Result operator()(jni::JNIEnv& env, const T& value) const { + return convert(env, Enum::toString(value)); } }; @@ -148,9 +87,7 @@ struct Converter> { template <> struct Converter { - Result operator()(jni::JNIEnv& env, const jni::String& value) const { - return { jni::Make(env, value) }; - } + Result operator()(jni::JNIEnv& env, const jni::String& value) const; }; } // namespace conversion diff --git a/platform/android/src/geojson/conversion/feature.cpp b/platform/android/src/geojson/conversion/feature.cpp new file mode 100644 index 00000000000..3cb6d37b17b --- /dev/null +++ b/platform/android/src/geojson/conversion/feature.cpp @@ -0,0 +1,191 @@ +#include "feature.hpp" + +#include "../../conversion/constant.hpp" +#include "../../conversion/conversion.hpp" +#include "../../jni/local_object.hpp" + +namespace mbgl { +namespace android { +namespace conversion { + +/** + * Turn feature identifier into std::string + */ +class FeatureIdVisitor { +public: + + template + std::string operator()(const T& i) const { + return std::to_string(i); + } + + std::string operator()(const std::string& i) const { + return i; + } + + std::string operator()(const std::nullptr_t&) const { + return ""; + } + +}; + +/** + * Turn properties into Java GSON JsonObject's + */ +class PropertyValueEvaluator { +public: + jni::JNIEnv& env; + + /** + * null + */ + jni::jobject* operator()(const mapbox::geometry::null_value_t &) const { + return (jni::jobject*) nullptr; + } + + /** + * Boolean primitive + */ + jni::jobject* operator()(const bool& value) const { + static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonPrimitive")).release(); + static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "(Ljava/lang/Boolean;)V"); + + // Create JsonPrimitive + jni::LocalObject converted = jni::NewLocalObject(env, *convert(env, value)); + jni::jobject* object = &jni::NewObject(env, *javaClass, *constructor, *converted); + + return object; + } + + /** + * String primitive + */ + jni::jobject* operator()(const std::string& value) const { + static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonPrimitive")).release(); + static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "(Ljava/lang/String;)V"); + + // Create JsonPrimitive + jni::LocalObject converted = jni::NewLocalObject(env, *convert(env, value)); + jni::jobject* object = &jni::NewObject(env, *javaClass, *constructor, converted.get()); + + return object; + } + + /** + * Number primitives + */ + template + jni::jobject* operator()(const Number& value) const { + static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonPrimitive")).release(); + static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "(Ljava/lang/Number;)V"); + + // Create JsonPrimitive + jni::LocalObject converted = jni::NewLocalObject(env, *convert(env, value)); + jni::jobject* object = &jni::NewObject(env, *javaClass, *constructor, converted.get()); + + return object; + } + + + /** + * Json Array + */ + jni::jobject* operator()(const std::vector &values) const { + static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonArray")).release(); + static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "()V");; + static jni::jmethodID* add = &jni::GetMethodID(env, *javaClass, "add", "(Lcom/google/gson/JsonElement;)V"); + + // Create json array + jni::jobject* jarray = &jni::NewObject(env, *javaClass, *constructor); + + // Add values + for (const auto &v : values) { + jni::LocalObject converted = jni::NewLocalObject(env, mbgl::Value::visit(v, *this)); + jni::CallMethod(env, jarray, *add, converted.get()); + } + + return jarray; + } + + /** + * Json Object + */ + jni::jobject* operator()(const std::unordered_map &value) const { + // TODO: clean up duplication here + static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonObject")).release(); + static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "()V");; + static jni::jmethodID* add = &jni::GetMethodID(env, *javaClass, "add", "(Ljava/lang/String;Lcom/google/gson/JsonElement;)V"); + + // Create json object + jni::jobject* jsonObject = &jni::NewObject(env, *javaClass, *constructor); + + // Add items + for (auto &item : value) { + jni::LocalObject converted = jni::NewLocalObject(env, mbgl::Value::visit(item.second, *this)); + jni::LocalObject key = jni::NewLocalObject(env, *convert(env, item.first)); + jni::CallMethod(env, jsonObject, *add, key.get(), converted.get()); + } + + return jsonObject; + } +}; + +Result Converter>::operator()(jni::JNIEnv& env, const std::unordered_map& value) const { + static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonObject")).release(); + static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "()V");; + static jni::jmethodID* add = &jni::GetMethodID(env, *javaClass, "add", "(Ljava/lang/String;Lcom/google/gson/JsonElement;)V"); + + // Create json object + jni::jobject* jsonObject = &jni::NewObject(env, *javaClass, *constructor); + + // Add items + PropertyValueEvaluator evaluator {env}; + for (auto &item : value) { + jni::LocalObject converted = jni::NewLocalObject(env, mbgl::Value::visit(item.second, evaluator)); + jni::LocalObject key = jni::NewLocalObject(env, *convert(env, item.first)); + jni::CallMethod(env, jsonObject, *add, key.get(), converted.get()); + } + + return {jsonObject}; +} + +Result> Converter, mbgl::Feature>::operator()(jni::JNIEnv& env, const mbgl::Feature& value) const { + + // Convert Id + FeatureIdVisitor idEvaluator; + std::string id = (value.id) ? mapbox::geometry::identifier::visit(value.id.value(), idEvaluator) : ""; + auto jid = jni::Make(env, id); + + // Convert properties + auto properties = jni::Object(*convert(env, value.properties)); + + // Convert geometry + auto geometry = android::geojson::Geometry::New(env, value.geometry); + + // Create feature + auto feature = android::geojson::Feature::fromGeometry(env, geometry, properties, jid); + + //Cleanup + jni::DeleteLocalRef(env, jid); + jni::DeleteLocalRef(env, geometry); + jni::DeleteLocalRef(env, properties); + + return feature; +} + +Result>> Converter>, std::vector>::operator()(jni::JNIEnv& env, const std::vector& value) const { + using namespace mbgl::android::geojson; + auto features = jni::Array>::New(env, value.size(), Feature::javaClass); + + for(size_t i = 0; i < value.size(); i = i + 1) { + auto converted = *convert, mbgl::Feature>(env, value.at(i)); + features.Set(env, i, converted); + jni::DeleteLocalRef(env, converted); + } + + return {features}; +} + +} // namespace conversion +} // namespace android +} // namespace mbgl diff --git a/platform/android/src/geojson/conversion/feature.hpp b/platform/android/src/geojson/conversion/feature.hpp index 8fc62a27893..031449cd234 100644 --- a/platform/android/src/geojson/conversion/feature.hpp +++ b/platform/android/src/geojson/conversion/feature.hpp @@ -1,215 +1,31 @@ #pragma once -#include "../../conversion/constant.hpp" #include "../../conversion/conversion.hpp" -#include "geometry.hpp" -#include "../../gson/json_object.hpp" +#include "../feature.hpp" #include -#include -#include - #include -#include "../../jni/local_object.hpp" -#include "../feature.hpp" -#include -#include #include -#include - -#include +#include namespace mbgl { namespace android { namespace conversion { -/** - * Turn feature identifier into std::string - */ -class FeatureIdVisitor { -public: - - template - std::string operator()(const T& i) const { - return std::to_string(i); - } - - std::string operator()(const std::string& i) const { - return i; - } - - std::string operator()(const std::nullptr_t&) const { - return ""; - } - -}; - -/** - * Turn properties into Java GSON JsonObject's - */ -class PropertyValueEvaluator { -public: - jni::JNIEnv& env; - - /** - * null - */ - jni::jobject* operator()(const mapbox::geometry::null_value_t &) const { - return (jni::jobject*) nullptr; - } - - /** - * Boolean primitive - */ - jni::jobject* operator()(const bool& value) const { - static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonPrimitive")).release(); - static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "(Ljava/lang/Boolean;)V"); - - // Create JsonPrimitive - jni::LocalObject converted = jni::NewLocalObject(env, *convert(env, value)); - jni::jobject* object = &jni::NewObject(env, *javaClass, *constructor, *converted); - - return object; - } - - /** - * String primitive - */ - jni::jobject* operator()(const std::string& value) const { - static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonPrimitive")).release(); - static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "(Ljava/lang/String;)V"); - - // Create JsonPrimitive - jni::LocalObject converted = jni::NewLocalObject(env, *convert(env, value)); - jni::jobject* object = &jni::NewObject(env, *javaClass, *constructor, converted.get()); - - return object; - } - - /** - * Number primitives - */ - template - jni::jobject* operator()(const Number& value) const { - static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonPrimitive")).release(); - static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "(Ljava/lang/Number;)V"); - - // Create JsonPrimitive - jni::LocalObject converted = jni::NewLocalObject(env, *convert(env, value)); - jni::jobject* object = &jni::NewObject(env, *javaClass, *constructor, converted.get()); - - return object; - } - - - /** - * Json Array - */ - jni::jobject* operator()(const std::vector &values) const { - static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonArray")).release(); - static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "()V");; - static jni::jmethodID* add = &jni::GetMethodID(env, *javaClass, "add", "(Lcom/google/gson/JsonElement;)V"); - - // Create json array - jni::jobject* jarray = &jni::NewObject(env, *javaClass, *constructor); - - // Add values - for (const auto &v : values) { - jni::LocalObject converted = jni::NewLocalObject(env, mbgl::Value::visit(v, *this)); - jni::CallMethod(env, jarray, *add, converted.get()); - } - - return jarray; - } - - /** - * Json Object - */ - jni::jobject* operator()(const std::unordered_map &value) const { - // TODO: clean up duplication here - static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonObject")).release(); - static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "()V");; - static jni::jmethodID* add = &jni::GetMethodID(env, *javaClass, "add", "(Ljava/lang/String;Lcom/google/gson/JsonElement;)V"); - - // Create json object - jni::jobject* jsonObject = &jni::NewObject(env, *javaClass, *constructor); - - // Add items - for (auto &item : value) { - jni::LocalObject converted = jni::NewLocalObject(env, mbgl::Value::visit(item.second, *this)); - jni::LocalObject key = jni::NewLocalObject(env, *convert(env, item.first)); - jni::CallMethod(env, jsonObject, *add, key.get(), converted.get()); - } - - return jsonObject; - } -}; - template <> struct Converter> { - Result operator()(jni::JNIEnv& env, const std::unordered_map& value) const { - static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "com/google/gson/JsonObject")).release(); - static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "", "()V");; - static jni::jmethodID* add = &jni::GetMethodID(env, *javaClass, "add", "(Ljava/lang/String;Lcom/google/gson/JsonElement;)V"); - - // Create json object - jni::jobject* jsonObject = &jni::NewObject(env, *javaClass, *constructor); - - // Add items - PropertyValueEvaluator evaluator {env}; - for (auto &item : value) { - jni::LocalObject converted = jni::NewLocalObject(env, mbgl::Value::visit(item.second, evaluator)); - jni::LocalObject key = jni::NewLocalObject(env, *convert(env, item.first)); - jni::CallMethod(env, jsonObject, *add, key.get(), converted.get()); - } - - return {jsonObject}; - } + Result operator()(jni::JNIEnv& env, const std::unordered_map& value) const; }; - template <> struct Converter, mbgl::Feature> { - Result> operator()(jni::JNIEnv& env, const mbgl::Feature& value) const { - - // Convert Id - FeatureIdVisitor idEvaluator; - std::string id = (value.id) ? mapbox::geometry::identifier::visit(value.id.value(), idEvaluator) : ""; - auto jid = jni::Make(env, id); - - // Convert properties - auto properties = jni::Object(*convert(env, value.properties)); - - // Convert geometry - auto geometry = *convert>(env, value.geometry); - - // Create feature - auto feature = android::geojson::Feature::fromGeometry(env, geometry, properties, jid); - - //Cleanup - jni::DeleteLocalRef(env, jid); - jni::DeleteLocalRef(env, geometry); - jni::DeleteLocalRef(env, properties); - - return feature; - } + Result> operator()(jni::JNIEnv& env, const mbgl::Feature& value) const; }; template <> struct Converter>, std::vector> { - Result>> operator()(jni::JNIEnv& env, const std::vector& value) const { - using namespace mbgl::android::geojson; - auto features = jni::Array>::New(env, value.size(), Feature::javaClass); - - for(size_t i = 0; i < value.size(); i = i + 1) { - auto converted = *convert, mbgl::Feature>(env, value.at(i)); - features.Set(env, i, converted); - jni::DeleteLocalRef(env, converted); - } - - return {features}; - } + Result>> operator()(jni::JNIEnv& env, const std::vector& value) const; }; } // namespace conversion diff --git a/platform/android/src/geojson/conversion/geometry.hpp b/platform/android/src/geojson/conversion/geometry.hpp deleted file mode 100644 index 242a68df02e..00000000000 --- a/platform/android/src/geojson/conversion/geometry.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include "../geometry.hpp" -#include - -namespace mbgl { -namespace android { -namespace conversion { - -/** - * mapbox::geometry::geometry -> Java GeoJson Geometry - */ -template -struct Converter, mapbox::geometry::geometry> { - Result> operator()(jni::JNIEnv& env, const mapbox::geometry::geometry& value) const { - return { android::geojson::Geometry::New(env, value) }; - } -}; - - -} // conversion -} // android -} // mbgl diff --git a/platform/android/src/style/conversion/filter.cpp b/platform/android/src/style/conversion/filter.cpp new file mode 100644 index 00000000000..4eac0cf82b2 --- /dev/null +++ b/platform/android/src/style/conversion/filter.cpp @@ -0,0 +1,26 @@ +#include "filter.hpp" +#include "../android_conversion.hpp" + +#include +#include + +namespace mbgl { +namespace android { +namespace conversion { + +optional toFilter(jni::JNIEnv& env, jni::Array> jfilter) { + mbgl::optional filter; + if (jfilter) { + mbgl::style::conversion::Error error; + auto converted = mbgl::style::conversion::convert(Value(env, jfilter), error); + if (!converted) { + mbgl::Log::Error(mbgl::Event::JNI, "Error converting filter: " + error.message); + } + filter = std::move(*converted); + } + return filter; +} + +} // namespace conversion +} // namespace android +} // namespace mbgl \ No newline at end of file diff --git a/platform/android/src/style/conversion/filter.hpp b/platform/android/src/style/conversion/filter.hpp index 241c98713ac..df482de8f3c 100644 --- a/platform/android/src/style/conversion/filter.hpp +++ b/platform/android/src/style/conversion/filter.hpp @@ -1,30 +1,15 @@ #pragma once -#include "../android_conversion.hpp" -#include -#include +#include +#include #include -#include -#include - namespace mbgl { namespace android { namespace conversion { -inline optional toFilter(jni::JNIEnv& env, jni::Array> jfilter) { - mbgl::optional filter; - if (jfilter) { - mbgl::style::conversion::Error error; - auto converted = mbgl::style::conversion::convert(Value(env, jfilter), error); - if (!converted) { - mbgl::Log::Error(mbgl::Event::JNI, "Error converting filter: " + error.message); - } - filter = std::move(*converted); - } - return filter; -} +optional toFilter(jni::JNIEnv&, jni::Array>); } // namespace conversion } // namespace android diff --git a/platform/android/src/style/conversion/latlngquad.hpp b/platform/android/src/style/conversion/latlngquad.hpp deleted file mode 100644 index 95883368554..00000000000 --- a/platform/android/src/style/conversion/latlngquad.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace mbgl { -namespace style { -namespace conversion { - -template <> -optional> Converter>::operator()(const mbgl::android::Value& value, Error& error) const { - if (value.isNull() || !value.isArray()) { - error = { "value cannot be converted to LatLng array" }; - return {}; - } - - return convert(value.toString(), error); -} - -} // namespace conversion -} // namespace style -} // namespace mbgl diff --git a/platform/android/src/style/conversion/position.cpp b/platform/android/src/style/conversion/position.cpp new file mode 100644 index 00000000000..9b3925914e0 --- /dev/null +++ b/platform/android/src/style/conversion/position.cpp @@ -0,0 +1,24 @@ +#include "position.hpp" + +namespace mbgl { +namespace android { +namespace conversion { + +Result> Converter, mbgl::style::Position>::operator()(jni::JNIEnv &env, const mbgl::style::Position &value) const { + std::array cartPosition = value.getSpherical(); + return Position::fromPosition(env, cartPosition[0], cartPosition[1], cartPosition[2]); +} + +Result Converter>::operator()(jni::JNIEnv &env, const jni::Object &value) const { + float radialCoordinate = Position::getRadialCoordinate(env, value); + float azimuthalAngle = Position::getAzimuthalAngle(env, value); + float polarAngle = Position::getPolarAngle(env, value); + std::array cartPosition {{radialCoordinate, azimuthalAngle, polarAngle}}; + mbgl::style::Position position{}; + position.set(cartPosition); + return position; +} + +} +} +} diff --git a/platform/android/src/style/conversion/position.hpp b/platform/android/src/style/conversion/position.hpp index f32a892c0ca..2ef4bf43955 100644 --- a/platform/android/src/style/conversion/position.hpp +++ b/platform/android/src/style/conversion/position.hpp @@ -1,37 +1,25 @@ #pragma once #include "../../conversion/conversion.hpp" +#include "../position.hpp" -#include #include -#include "../../jni/local_object.hpp" -#include "../position.hpp" +#include namespace mbgl { namespace android { namespace conversion { -template<> +template <> struct Converter, mbgl::style::Position> { - Result> operator()(jni::JNIEnv &env, const mbgl::style::Position &value) const { - std::array cartPosition = value.getSpherical(); - return Position::fromPosition(env, cartPosition[0], cartPosition[1], cartPosition[2]); - } + Result> operator()(jni::JNIEnv &env, const mbgl::style::Position &value) const; }; -template<> +template <> struct Converter> { - Result operator()(jni::JNIEnv &env, const jni::Object &value) const { - float radialCoordinate = Position::getRadialCoordinate(env, value); - float azimuthalAngle = Position::getAzimuthalAngle(env, value); - float polarAngle = Position::getPolarAngle(env, value); - std::array cartPosition {{radialCoordinate, azimuthalAngle, polarAngle}}; - mbgl::style::Position position{}; - position.set(cartPosition); - return position; - } + Result operator()(jni::JNIEnv &env, const jni::Object &value) const; }; } } -} \ No newline at end of file +} diff --git a/platform/android/src/style/conversion/property_expression.hpp b/platform/android/src/style/conversion/property_expression.hpp index ae9d4ea41c4..08429960cb6 100644 --- a/platform/android/src/style/conversion/property_expression.hpp +++ b/platform/android/src/style/conversion/property_expression.hpp @@ -1,16 +1,11 @@ #pragma once -#include #include "../../conversion/conversion.hpp" -#include "../../conversion/constant.hpp" -#include "types.hpp" -#include "../../java/lang.hpp" - -#include #include "../../gson/json_element.hpp" -#include -#include +#include + +#include namespace mbgl { namespace android { @@ -18,11 +13,8 @@ namespace conversion { template struct Converter, mbgl::style::PropertyExpression> { - Result> operator()(jni::JNIEnv& env, const mbgl::style::PropertyExpression& value) const { - // Convert expressions - mbgl::Value expressionValue = value.getExpression().serialize(); - return gson::JsonElement::New(env, expressionValue); + return gson::JsonElement::New(env, value.getExpression().serialize()); } }; diff --git a/platform/android/src/style/conversion/property_value.hpp b/platform/android/src/style/conversion/property_value.hpp index 256647cddf0..8150285c85d 100644 --- a/platform/android/src/style/conversion/property_value.hpp +++ b/platform/android/src/style/conversion/property_value.hpp @@ -2,10 +2,10 @@ #include #include + #include "../../conversion/conversion.hpp" #include "../../conversion/constant.hpp" #include "property_expression.hpp" -#include "types.hpp" namespace mbgl { namespace android { @@ -17,25 +17,22 @@ namespace conversion { template class PropertyValueEvaluator { public: - PropertyValueEvaluator(jni::JNIEnv& _env) : env(_env) {} jni::jobject* operator()(const mbgl::style::Undefined) const { return nullptr; } - jni::jobject* operator()(const T &value) const { - Result result = convert(env, value); - return *result; + jni::jobject* operator()(const T& value) const { + return *convert(env, value); } - jni::jobject* operator()(const mbgl::style::PropertyExpression &value) const { - return *convert, mbgl::style::PropertyExpression>(env, value); + jni::jobject* operator()(const mbgl::style::PropertyExpression& value) const { + return *convert>(env, value); } private: jni::JNIEnv& env; - }; /** @@ -43,7 +40,6 @@ class PropertyValueEvaluator { */ template struct Converter> { - Result operator()(jni::JNIEnv& env, const mbgl::style::PropertyValue& value) const { PropertyValueEvaluator evaluator(env); return value.evaluate(evaluator); @@ -55,8 +51,7 @@ struct Converter> { */ template <> struct Converter { - - Result operator()(jni::JNIEnv& env, const mbgl::style::ColorRampPropertyValue value) const { + Result operator()(jni::JNIEnv& env, const mbgl::style::ColorRampPropertyValue& value) const { PropertyValueEvaluator evaluator(env); return *convert(env, value.evaluate(evaluator)); } diff --git a/platform/android/src/style/conversion/transition_options.cpp b/platform/android/src/style/conversion/transition_options.cpp new file mode 100644 index 00000000000..313333ad17e --- /dev/null +++ b/platform/android/src/style/conversion/transition_options.cpp @@ -0,0 +1,16 @@ +#include "transition_options.hpp" + +namespace mbgl { +namespace android { +namespace conversion { + +Result> Converter, mbgl::style::TransitionOptions>::operator()(jni::JNIEnv& env, const mbgl::style::TransitionOptions& value) const { + return TransitionOptions::fromTransitionOptions(env, + std::chrono::duration_cast(value.duration.value_or(mbgl::Duration::zero())).count(), + std::chrono::duration_cast(value.delay.value_or(mbgl::Duration::zero())).count() + ); +} + +} +} +} diff --git a/platform/android/src/style/conversion/transition_options.hpp b/platform/android/src/style/conversion/transition_options.hpp index ae65a32194a..6630456d37a 100644 --- a/platform/android/src/style/conversion/transition_options.hpp +++ b/platform/android/src/style/conversion/transition_options.hpp @@ -1,11 +1,11 @@ #pragma once #include "../../conversion/conversion.hpp" +#include "../transition_options.hpp" -#include #include -#include "../../jni/local_object.hpp" -#include "../transition_options.hpp" + +#include namespace mbgl { namespace android { @@ -13,18 +13,9 @@ namespace conversion { template<> struct Converter, mbgl::style::TransitionOptions> { - Result> operator()(jni::JNIEnv &env, const mbgl::style::TransitionOptions &value) const { - - // Convert duration - jlong duration = std::chrono::duration_cast(value.duration.value_or(mbgl::Duration::zero())).count(); - // Convert delay - jlong delay = std::chrono::duration_cast(value.delay.value_or(mbgl::Duration::zero())).count(); - - // Create transition options - return TransitionOptions::fromTransitionOptions(env, duration, delay); - } + Result> operator()(jni::JNIEnv&, const mbgl::style::TransitionOptions&) const; }; } } -} \ No newline at end of file +} diff --git a/platform/android/src/style/conversion/types.hpp b/platform/android/src/style/conversion/types.hpp deleted file mode 100644 index e87782fad0a..00000000000 --- a/platform/android/src/style/conversion/types.hpp +++ /dev/null @@ -1,119 +0,0 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. -#pragma once - -#include "types_string_values.hpp" -#include "../../conversion/conversion.hpp" -#include "../../conversion/constant.hpp" - -#include -#include -#include - -#include - -namespace mbgl { -namespace android { -namespace conversion { - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::VisibilityType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::LineCapType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::LineJoinType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::SymbolPlacementType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::AlignmentType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::IconTextFitType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::SymbolAnchorType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::TextJustifyType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::TextTransformType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::TranslateAnchorType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::CirclePitchScaleType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::RasterResamplingType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::HillshadeIlluminationAnchorType& value) const { - return convert(env, toString(value)); - } -}; - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::LightAnchorType& value) const { - return convert(env, toString(value)); - } -}; - - -} // namespace conversion -} // namespace android -} // namespace mbgl diff --git a/platform/android/src/style/conversion/types.hpp.ejs b/platform/android/src/style/conversion/types.hpp.ejs deleted file mode 100644 index 3cd47640158..00000000000 --- a/platform/android/src/style/conversion/types.hpp.ejs +++ /dev/null @@ -1,40 +0,0 @@ -<% - const properties = locals.properties; --%> -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. -#pragma once - -#include "types_string_values.hpp" -#include "../../conversion/conversion.hpp" -#include "../../conversion/constant.hpp" - -#include -#include -#include - -#include - -namespace mbgl { -namespace android { -namespace conversion { - -template <> -struct Converter { - Result operator()(jni::JNIEnv& env, const mbgl::style::VisibilityType& value) const { - return convert(env, toString(value)); - } -}; - -<% for (const property of properties) { -%> -template <> -struct Converter> { - Result operator()(jni::JNIEnv& env, const mbgl::style::<%- propertyNativeType(property) %>& value) const { - return convert(env, toString(value)); - } -}; - -<% } -%> - -} // namespace conversion -} // namespace android -} // namespace mbgl diff --git a/platform/android/src/style/conversion/types_string_values.hpp b/platform/android/src/style/conversion/types_string_values.hpp deleted file mode 100644 index 9f6696d1810..00000000000 --- a/platform/android/src/style/conversion/types_string_values.hpp +++ /dev/null @@ -1,257 +0,0 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. -#pragma once - -#include - -#include -#include - -namespace mbgl { -namespace android { -namespace conversion { - - // visibility - inline std::string toString(mbgl::style::VisibilityType value) { - switch (value) { - case mbgl::style::VisibilityType::Visible: - return "visible"; - break; - case mbgl::style::VisibilityType::None: - return "none"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // line-cap - inline std::string toString(mbgl::style::LineCapType value) { - switch (value) { - case mbgl::style::LineCapType::Butt: - return "butt"; - break; - case mbgl::style::LineCapType::Round: - return "round"; - break; - case mbgl::style::LineCapType::Square: - return "square"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // line-join - inline std::string toString(mbgl::style::LineJoinType value) { - switch (value) { - case mbgl::style::LineJoinType::Bevel: - return "bevel"; - break; - case mbgl::style::LineJoinType::Round: - return "round"; - break; - case mbgl::style::LineJoinType::Miter: - return "miter"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // symbol-placement - inline std::string toString(mbgl::style::SymbolPlacementType value) { - switch (value) { - case mbgl::style::SymbolPlacementType::Point: - return "point"; - break; - case mbgl::style::SymbolPlacementType::Line: - return "line"; - break; - case mbgl::style::SymbolPlacementType::LineCenter: - return "line-center"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // icon-rotation-alignment - inline std::string toString(mbgl::style::AlignmentType value) { - switch (value) { - case mbgl::style::AlignmentType::Map: - return "map"; - break; - case mbgl::style::AlignmentType::Viewport: - return "viewport"; - break; - case mbgl::style::AlignmentType::Auto: - return "auto"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // icon-text-fit - inline std::string toString(mbgl::style::IconTextFitType value) { - switch (value) { - case mbgl::style::IconTextFitType::None: - return "none"; - break; - case mbgl::style::IconTextFitType::Width: - return "width"; - break; - case mbgl::style::IconTextFitType::Height: - return "height"; - break; - case mbgl::style::IconTextFitType::Both: - return "both"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // icon-anchor - inline std::string toString(mbgl::style::SymbolAnchorType value) { - switch (value) { - case mbgl::style::SymbolAnchorType::Center: - return "center"; - break; - case mbgl::style::SymbolAnchorType::Left: - return "left"; - break; - case mbgl::style::SymbolAnchorType::Right: - return "right"; - break; - case mbgl::style::SymbolAnchorType::Top: - return "top"; - break; - case mbgl::style::SymbolAnchorType::Bottom: - return "bottom"; - break; - case mbgl::style::SymbolAnchorType::TopLeft: - return "top-left"; - break; - case mbgl::style::SymbolAnchorType::TopRight: - return "top-right"; - break; - case mbgl::style::SymbolAnchorType::BottomLeft: - return "bottom-left"; - break; - case mbgl::style::SymbolAnchorType::BottomRight: - return "bottom-right"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // text-justify - inline std::string toString(mbgl::style::TextJustifyType value) { - switch (value) { - case mbgl::style::TextJustifyType::Left: - return "left"; - break; - case mbgl::style::TextJustifyType::Center: - return "center"; - break; - case mbgl::style::TextJustifyType::Right: - return "right"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // text-transform - inline std::string toString(mbgl::style::TextTransformType value) { - switch (value) { - case mbgl::style::TextTransformType::None: - return "none"; - break; - case mbgl::style::TextTransformType::Uppercase: - return "uppercase"; - break; - case mbgl::style::TextTransformType::Lowercase: - return "lowercase"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // fill-translate-anchor - inline std::string toString(mbgl::style::TranslateAnchorType value) { - switch (value) { - case mbgl::style::TranslateAnchorType::Map: - return "map"; - break; - case mbgl::style::TranslateAnchorType::Viewport: - return "viewport"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // circle-pitch-scale - inline std::string toString(mbgl::style::CirclePitchScaleType value) { - switch (value) { - case mbgl::style::CirclePitchScaleType::Map: - return "map"; - break; - case mbgl::style::CirclePitchScaleType::Viewport: - return "viewport"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // raster-resampling - inline std::string toString(mbgl::style::RasterResamplingType value) { - switch (value) { - case mbgl::style::RasterResamplingType::Linear: - return "linear"; - break; - case mbgl::style::RasterResamplingType::Nearest: - return "nearest"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // hillshade-illumination-anchor - inline std::string toString(mbgl::style::HillshadeIlluminationAnchorType value) { - switch (value) { - case mbgl::style::HillshadeIlluminationAnchorType::Map: - return "map"; - break; - case mbgl::style::HillshadeIlluminationAnchorType::Viewport: - return "viewport"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - // anchor - inline std::string toString(mbgl::style::LightAnchorType value) { - switch (value) { - case mbgl::style::LightAnchorType::Map: - return "map"; - break; - case mbgl::style::LightAnchorType::Viewport: - return "viewport"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - - -} // namespace conversion -} // namespace android -} // namespace mbgl diff --git a/platform/android/src/style/conversion/types_string_values.hpp.ejs b/platform/android/src/style/conversion/types_string_values.hpp.ejs deleted file mode 100644 index bf529197412..00000000000 --- a/platform/android/src/style/conversion/types_string_values.hpp.ejs +++ /dev/null @@ -1,48 +0,0 @@ -<% - const properties = locals.properties; --%> -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. -#pragma once - -#include - -#include -#include - -namespace mbgl { -namespace android { -namespace conversion { - - // visibility - inline std::string toString(mbgl::style::VisibilityType value) { - switch (value) { - case mbgl::style::VisibilityType::Visible: - return "visible"; - break; - case mbgl::style::VisibilityType::None: - return "none"; - break; - default: - throw std::runtime_error("Not implemented"); - } - } - -<% for (const property of properties) { -%> - // <%- property.name %> - inline std::string toString(mbgl::style::<%- propertyNativeType(property) %> value) { - switch (value) { -<% for (const value in property.values) { -%> - case mbgl::style::<%- propertyNativeType(property) %>::<%- camelize(value) %>: - return "<%- value %>"; - break; -<% } -%> - default: - throw std::runtime_error("Not implemented"); - } - } - -<% } -%> - -} // namespace conversion -} // namespace android -} // namespace mbgl diff --git a/platform/android/src/style/conversion/url_or_tileset.cpp b/platform/android/src/style/conversion/url_or_tileset.cpp new file mode 100644 index 00000000000..2ec58567515 --- /dev/null +++ b/platform/android/src/style/conversion/url_or_tileset.cpp @@ -0,0 +1,30 @@ +#include "url_or_tileset.hpp" +#include "../android_conversion.hpp" + +#include +#include + +namespace mbgl { +namespace android { + +// This conversion is expected not to fail because it's used only in contexts where +// the value was originally a String or TileSet object on the Java side. If it fails +// to convert, it's a bug in our serialization or Java-side static typing. +variant convertURLOrTileset(mbgl::android::Value&& value) { + using namespace mbgl::style::conversion; + + const Convertible convertible(std::move(value)); + if (isObject(convertible)) { + Error error; + optional tileset = convert(convertible, error); + if (!tileset) { + throw std::logic_error(error.message); + } + return { *tileset }; + } else { + return { *toString(convertible) }; + } +} + +} +} diff --git a/platform/android/src/style/conversion/url_or_tileset.hpp b/platform/android/src/style/conversion/url_or_tileset.hpp index d6bf86639cd..f42a9b9a2a9 100644 --- a/platform/android/src/style/conversion/url_or_tileset.hpp +++ b/platform/android/src/style/conversion/url_or_tileset.hpp @@ -1,37 +1,16 @@ #pragma once -#include #include - #include -#include -#include - -#include #include +#include "../value.hpp" + namespace mbgl { namespace android { -// This conversion is expected not to fail because it's used only in contexts where -// the value was originally a String or TileSet object on the Java side. If it fails -// to convert, it's a bug in our serialization or Java-side static typing. -inline variant convertURLOrTileset(mbgl::android::Value&& value) { - using namespace mbgl::style::conversion; - - const Convertible convertible(std::move(value)); - if (isObject(convertible)) { - Error error; - optional tileset = convert(convertible, error); - if (!tileset) { - throw std::logic_error(error.message); - } - return { *tileset }; - } else { - return { *toString(convertible) }; - } -} +variant convertURLOrTileset(mbgl::android::Value&& value); } }