Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Refactor Android conversion code #12678

Merged
merged 4 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions platform/android/core-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 0 additions & 17 deletions platform/android/scripts/generate-style-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})
);
24 changes: 24 additions & 0 deletions platform/android/src/conversion/collection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "collection.hpp"
#include "constant.hpp"

namespace mbgl {
namespace android {
namespace conversion {

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;
}

}
}
}
43 changes: 1 addition & 42 deletions platform/android/src/conversion/collection.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#pragma once

#include "conversion.hpp"
#include "constant.hpp"

#include <mbgl/util/optional.hpp>
#include <jni/jni.hpp>

#include <vector>
Expand All @@ -12,46 +10,7 @@ namespace mbgl {
namespace android {
namespace conversion {

/**
* Convert jarray -> ArrayList
*/
template <class T>
inline 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));
}

// 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::Array<jni::String> array);

}
}
Expand Down
17 changes: 17 additions & 0 deletions platform/android/src/conversion/color.cpp
Original file line number Diff line number Diff line change
@@ -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
8 changes: 1 addition & 7 deletions platform/android/src/conversion/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions platform/android/src/conversion/constant.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#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::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::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::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::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
93 changes: 15 additions & 78 deletions platform/android/src/conversion/constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,32 @@

#include "conversion.hpp"

#include <mbgl/util/optional.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>

#include <jni/jni.hpp>

#include <string>
#include <array>
#include <vector>
#include <sstream>

namespace mbgl {
namespace android {
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)};
}
};

template <>
struct Converter<jni::jboolean, bool> {
Result<jni::jboolean> operator()(jni::JNIEnv&, const bool& value) const {
return {(jni::jboolean) value};
}
Result<jni::jobject*> 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 {
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)};
}
};

template <>
struct Converter<jni::jfloat, float> {
Result<jni::jfloat> operator()(jni::JNIEnv&, const float& value) const {
return {(jni::jfloat) value};
}
Result<jni::jobject*> 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 {
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)};
}
};

template <>
struct Converter<jni::jdouble, float> {
Result<jni::jdouble> operator()(jni::JNIEnv&, const double& value) const {
return {(jni::jdouble) value};
}
Result<jni::jobject*> operator()(jni::JNIEnv& env, const double& value) const;
};

/**
Expand All @@ -81,26 +47,12 @@ 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()};
}
};

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::jobject*> 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>
Expand All @@ -116,41 +68,26 @@ 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);
}
Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::vector<float>& value) const;
};

return &jarray;
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));
}
};

// 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
Expand Down
Loading