Replace compile-time polymorphism with runtime polymorphism in the conversion system

This commit is contained in:
John Firebaugh
2017-09-26 14:14:44 -07:00
parent bb58ddaea8
commit 4b2e1cddb4
60 changed files with 2435 additions and 2175 deletions

View File

@@ -4,8 +4,9 @@
#include <mbgl/util/feature.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/geojson.hpp>
#include <jni/jni.hpp>
@@ -13,89 +14,105 @@ namespace mbgl {
namespace style {
namespace conversion {
inline bool isUndefined(const mbgl::android::Value& value) {
return value.isNull();
}
template <>
class ConversionTraits<mbgl::android::Value> {
public:
static bool isUndefined(const mbgl::android::Value& value) {
return value.isNull();
}
inline bool isArray(const mbgl::android::Value& value) {
return value.isArray();
}
static bool isArray(const mbgl::android::Value& value) {
return value.isArray();
}
inline bool isObject(const mbgl::android::Value& value) {
return value.isObject();
}
static bool isObject(const mbgl::android::Value& value) {
return value.isObject();
}
inline std::size_t arrayLength(const mbgl::android::Value& value) {
return value.getLength();;
}
static std::size_t arrayLength(const mbgl::android::Value& value) {
return value.getLength();;
}
inline mbgl::android::Value arrayMember(const mbgl::android::Value& value, std::size_t i) {
return value.get(i);
}
static mbgl::android::Value arrayMember(const mbgl::android::Value& value, std::size_t i) {
return value.get(i);
}
inline optional<mbgl::android::Value> objectMember(const mbgl::android::Value& value, const char* key) {
mbgl::android::Value member = value.get(key);
static optional<mbgl::android::Value> objectMember(const mbgl::android::Value& value, const char* key) {
mbgl::android::Value member = value.get(key);
if (!member.isNull()) {
return member;
} else {
return {};
}
}
if (!member.isNull()) {
return member;
} else {
template <class Fn>
static optional<Error> eachMember(const mbgl::android::Value&, Fn&&) {
// TODO
mbgl::Log::Warning(mbgl::Event::Android, "eachMember not implemented");
return {};
}
}
template <class Fn>
optional<Error> eachMember(const mbgl::android::Value&, Fn&&) {
// TODO
mbgl::Log::Warning(mbgl::Event::Android, "eachMember not implemented");
return {};
}
inline optional<bool> toBool(const mbgl::android::Value& value) {
if (value.isBool()) {
return value.toBool();
} else {
return {};
static optional<bool> toBool(const mbgl::android::Value& value) {
if (value.isBool()) {
return value.toBool();
} else {
return {};
}
}
}
inline optional<float> toNumber(const mbgl::android::Value& value) {
if (value.isNumber()) {
auto num = value.toFloat();
return num;
} else {
return {};
static optional<float> toNumber(const mbgl::android::Value& value) {
if (value.isNumber()) {
auto num = value.toFloat();
return num;
} else {
return {};
}
}
}
inline optional<double> toDouble(const mbgl::android::Value& value) {
if (value.isNumber()) {
return value.toDouble();
} else {
return {};
static optional<double> toDouble(const mbgl::android::Value& value) {
if (value.isNumber()) {
return value.toDouble();
} else {
return {};
}
}
}
inline optional<std::string> toString(const mbgl::android::Value& value) {
if (value.isString()) {
return value.toString();
} else {
return {};
static optional<std::string> toString(const mbgl::android::Value& value) {
if (value.isString()) {
return value.toString();
} else {
return {};
}
}
}
inline optional<Value> toValue(const mbgl::android::Value& value) {
if (value.isNull()) {
return {};
} else if (value.isBool()) {
return { value.toBool() };
} else if (value.isString()) {
return { value.toString() };
} else if (value.isNumber()) {
auto doubleVal = value.toDouble();
return { doubleVal - (int) doubleVal > 0.0 ? doubleVal : value.toLong() };
} else {
return {};
static optional<Value> toValue(const mbgl::android::Value& value) {
if (value.isNull()) {
return {};
} else if (value.isBool()) {
return { value.toBool() };
} else if (value.isString()) {
return { value.toString() };
} else if (value.isNumber()) {
auto doubleVal = value.toDouble();
return { doubleVal - (int) doubleVal > 0.0 ? doubleVal : value.toLong() };
} else {
return {};
}
}
static optional<GeoJSON> toGeoJSON(const mbgl::android::Value &value, Error &error) {
if (value.isNull() || !value.isString()) {
error = { "no json data found" };
return {};
}
return parseGeoJSON(value.toString(), error);
}
};
template <class T, class...Args>
optional<T> convert(mbgl::android::Value&& value, Error& error, Args&&...args) {
return convert<T>(Convertible(std::move(value)), error, std::forward<Args>(args)...);
}
} // namespace conversion

View File

@@ -16,9 +16,8 @@ namespace conversion {
inline optional<mbgl::style::Filter> toFilter(jni::JNIEnv& env, jni::Array<jni::Object<>> jfilter) {
mbgl::optional<mbgl::style::Filter> filter;
if (jfilter) {
Value filterValue(env, jfilter);
mbgl::style::conversion::Error error;
auto converted = mbgl::style::conversion::convert<mbgl::style::Filter>(filterValue, error);
auto converted = mbgl::style::conversion::convert<mbgl::style::Filter>(Value(env, jfilter), error);
if (!converted) {
mbgl::Log::Error(mbgl::Event::JNI, "Error converting filter: " + error.message);
}

View File

@@ -1,24 +0,0 @@
#pragma once
#include <mapbox/geojson.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/geojson.hpp>
#include <jni/jni.hpp>
namespace mbgl {
namespace style {
namespace conversion {
template <>
optional<GeoJSON> Converter<GeoJSON>::operator()(const mbgl::android::Value& value, Error& error) const {
if(value.isNull() || !value.isString()) {
error = { "no json data found" };
return {};
}
return convert<GeoJSON>(value.toString(), error);
}
} // namespace conversion
} // namespace style
} // namespace mbgl

View File

@@ -17,18 +17,19 @@ 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<std::string, Tileset> convertURLOrTileset(const Value& value) {
inline variant<std::string, Tileset> convertURLOrTileset(mbgl::android::Value&& value) {
using namespace mbgl::style::conversion;
if (isObject(value)) {
const Convertible convertible(std::move(value));
if (isObject(convertible)) {
Error error;
optional<Tileset> tileset = convert<Tileset>(value, error);
optional<Tileset> tileset = convert<Tileset>(convertible, error);
if (!tileset) {
throw std::logic_error(error.message);
}
return { *tileset };
} else {
return { *toString(value) };
return { *toString(convertible) };
}
}

View File

@@ -4,11 +4,20 @@
#include <jni/jni.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/style/layers/background_layer.hpp>
#include <mbgl/style/layers/circle_layer.hpp>
#include <mbgl/style/layers/fill_layer.hpp>
#include <mbgl/style/layers/fill_extrusion_layer.hpp>
#include <mbgl/style/layers/line_layer.hpp>
#include <mbgl/style/layers/raster_layer.hpp>
#include <mbgl/style/layers/symbol_layer.hpp>
#include <mbgl/util/logging.hpp>
// Java -> C++ conversion
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/filter.hpp>
#include <mbgl/style/conversion/layer.hpp>
#include <mbgl/style/conversion/source.hpp>
@@ -78,10 +87,8 @@ namespace android {
}
void Layer::setLayoutProperty(jni::JNIEnv& env, jni::String jname, jni::Object<> jvalue) {
Value value(env, jvalue);
// Convert and set property
optional<mbgl::style::conversion::Error> error = mbgl::style::conversion::setLayoutProperty(layer, jni::Make<std::string>(env, jname), value);
optional<mbgl::style::conversion::Error> error = mbgl::style::conversion::setLayoutProperty(layer, jni::Make<std::string>(env, jname), Value(env, jvalue));
if (error) {
mbgl::Log::Error(mbgl::Event::JNI, "Error setting property: " + jni::Make<std::string>(env, jname) + " " + error->message);
return;
@@ -89,10 +96,8 @@ namespace android {
}
void Layer::setPaintProperty(jni::JNIEnv& env, jni::String jname, jni::Object<> jvalue) {
Value value(env, jvalue);
// Convert and set property
optional<mbgl::style::conversion::Error> error = mbgl::style::conversion::setPaintProperty(layer, jni::Make<std::string>(env, jname), value);
optional<mbgl::style::conversion::Error> error = mbgl::style::conversion::setPaintProperty(layer, jni::Make<std::string>(env, jname), Value(env, jvalue));
if (error) {
mbgl::Log::Error(mbgl::Event::JNI, "Error setting property: " + jni::Make<std::string>(env, jname) + " " + error->message);
return;
@@ -116,10 +121,8 @@ namespace android {
using namespace mbgl::style;
using namespace mbgl::style::conversion;
Value wrapped(env, jfilter);
Error error;
optional<Filter> converted = convert<Filter>(wrapped, error);
optional<Filter> converted = convert<Filter>(Value(env, jfilter), error);
if (!converted) {
mbgl::Log::Error(mbgl::Event::JNI, "Error setting filter: " + error.message);
return;

View File

@@ -5,15 +5,15 @@
// Java -> C++ conversion
#include "../android_conversion.hpp"
#include "../conversion/filter.hpp"
#include "../conversion/geojson.hpp"
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/geojson.hpp>
#include <mbgl/style/conversion/geojson_options.hpp>
// C++ -> Java conversion
#include "../../conversion/conversion.hpp"
#include "../../conversion/collection.hpp"
#include "../../geojson/conversion/feature.hpp"
#include "../conversion/url_or_tileset.hpp"
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/geojson_options.hpp>
#include <string>
@@ -29,7 +29,7 @@ namespace android {
return style::GeoJSONOptions();
}
Error error;
optional<style::GeoJSONOptions> result = convert<style::GeoJSONOptions>(Value(env, options), error);
optional<style::GeoJSONOptions> result = convert<style::GeoJSONOptions>(mbgl::android::Value(env, options), error);
if (!result) {
throw std::logic_error(error.message);
}
@@ -54,7 +54,7 @@ namespace android {
// Convert the jni object
Error error;
optional<GeoJSON> converted = convert<GeoJSON>(Value(env, json), error);
optional<GeoJSON> converted = convert<GeoJSON>(mbgl::android::Value(env, json), error);
if(!converted) {
mbgl::Log::Error(mbgl::Event::JNI, "Error setting geo json: " + error.message);
return;

View File

@@ -24,8 +24,6 @@ namespace android {
Value::Value(jni::JNIEnv& _env, jni::jobject* _value) : env(_env), value(_value, ObjectDeleter(env)) {}
Value::~Value() = default;
bool Value::isNull() const {
return value == nullptr;
}

View File

@@ -9,9 +9,13 @@ namespace android {
class Value {
public:
Value(jni::JNIEnv&, jni::jobject*);
virtual ~Value();
Value(Value&&) = default;
Value& operator=(Value&&) = default;
Value(const Value&) = delete;
Value& operator=(const Value&) = delete;
bool isNull() const;
bool isArray() const;