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

@@ -9,111 +9,136 @@
#include <mbgl/util/optional.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/geojson.hpp>
namespace mbgl {
namespace style {
namespace conversion {
inline bool isUndefined(v8::Local<v8::Value> value) {
Nan::HandleScope scope;
return value->IsUndefined() || value->IsNull();
}
template <>
class ConversionTraits<v8::Local<v8::Value>> {
public:
static bool isUndefined(const v8::Local<v8::Value>& value) {
Nan::HandleScope scope;
return value->IsUndefined() || value->IsNull();
}
inline bool isArray(v8::Local<v8::Value> value) {
Nan::HandleScope scope;
return value->IsArray();
}
static bool isArray(const v8::Local<v8::Value>& value) {
Nan::HandleScope scope;
return value->IsArray();
}
inline std::size_t arrayLength(v8::Local<v8::Value> value) {
Nan::HandleScope scope;
return value.As<v8::Array>()->Length();
}
static std::size_t arrayLength(const v8::Local<v8::Value>& value) {
Nan::HandleScope scope;
// const_cast because v8::Local<T>::As is not marked const until node v8.0
v8::Local<v8::Array> array = const_cast<v8::Local<v8::Value>&>(value).As<v8::Array>();
return array->Length();
}
inline v8::Local<v8::Value> arrayMember(v8::Local<v8::Value> value, std::size_t i) {
Nan::EscapableHandleScope scope;
return scope.Escape(Nan::Get(value.As<v8::Array>(), i).ToLocalChecked());
}
static v8::Local<v8::Value> arrayMember(const v8::Local<v8::Value>& value, std::size_t i) {
Nan::EscapableHandleScope scope;
// const_cast because v8::Local<T>::As is not marked const until node v8.0
v8::Local<v8::Array> array = const_cast<v8::Local<v8::Value>&>(value).As<v8::Array>();
return scope.Escape(Nan::Get(array, i).ToLocalChecked());
}
inline bool isObject(v8::Local<v8::Value> value) {
Nan::HandleScope scope;
return value->IsObject() && !value->IsArray();
}
static bool isObject(const v8::Local<v8::Value>& value) {
Nan::HandleScope scope;
return value->IsObject() && !value->IsArray();
}
inline optional<v8::Local<v8::Value>> objectMember(v8::Local<v8::Value> value, const char * name) {
Nan::EscapableHandleScope scope;
if (!Nan::Has(Nan::To<v8::Object>(value).ToLocalChecked(), Nan::New(name).ToLocalChecked()).FromJust()) {
static optional<v8::Local<v8::Value>> objectMember(const v8::Local<v8::Value>& value, const char * name) {
Nan::EscapableHandleScope scope;
if (!Nan::Has(Nan::To<v8::Object>(value).ToLocalChecked(), Nan::New(name).ToLocalChecked()).FromJust()) {
return {};
}
Nan::MaybeLocal<v8::Value> result = Nan::Get(Nan::To<v8::Object>(value).ToLocalChecked(), Nan::New(name).ToLocalChecked());
if (result.IsEmpty()) {
return {};
}
return {scope.Escape(result.ToLocalChecked())};
}
template <class Fn>
static optional<Error> eachMember(const v8::Local<v8::Value>& value, Fn&& fn) {
Nan::HandleScope scope;
v8::Local<v8::Array> names = Nan::GetOwnPropertyNames(Nan::To<v8::Object>(value).ToLocalChecked()).ToLocalChecked();
for (uint32_t i = 0; i < names->Length(); ++i) {
v8::Local<v8::Value> k = Nan::Get(names, i).ToLocalChecked();
v8::Local<v8::Value> v = Nan::Get(Nan::To<v8::Object>(value).ToLocalChecked(), k).ToLocalChecked();
optional<Error> result = fn(*Nan::Utf8String(k), std::move(v));
if (result) {
return result;
}
}
return {};
}
Nan::MaybeLocal<v8::Value> result = Nan::Get(Nan::To<v8::Object>(value).ToLocalChecked(), Nan::New(name).ToLocalChecked());
if (result.IsEmpty()) {
return {};
}
return scope.Escape(result.ToLocalChecked());
}
template <class Fn>
optional<Error> eachMember(v8::Local<v8::Value> value, Fn&& fn) {
Nan::HandleScope scope;
v8::Local<v8::Array> names = Nan::GetOwnPropertyNames(Nan::To<v8::Object>(value).ToLocalChecked()).ToLocalChecked();
for (uint32_t i = 0; i < names->Length(); ++i) {
v8::Local<v8::Value> k = Nan::Get(names, i).ToLocalChecked();
v8::Local<v8::Value> v = Nan::Get(Nan::To<v8::Object>(value).ToLocalChecked(), k).ToLocalChecked();
optional<Error> result = fn(*Nan::Utf8String(k), v);
if (result) {
return result;
static optional<bool> toBool(const v8::Local<v8::Value>& value) {
Nan::HandleScope scope;
if (!value->IsBoolean()) {
return {};
}
return value->BooleanValue();
}
static optional<float> toNumber(const v8::Local<v8::Value>& value) {
Nan::HandleScope scope;
if (!value->IsNumber()) {
return {};
}
return value->NumberValue();
}
static optional<double> toDouble(const v8::Local<v8::Value>& value) {
Nan::HandleScope scope;
if (!value->IsNumber()) {
return {};
}
return value->NumberValue();
}
static optional<std::string> toString(const v8::Local<v8::Value>& value) {
Nan::HandleScope scope;
if (!value->IsString()) {
return {};
}
return std::string(*Nan::Utf8String(value));
}
static optional<Value> toValue(const v8::Local<v8::Value>& value) {
if (value->IsFalse()) {
return { false };
} else if (value->IsTrue()) {
return { true };
} else if (value->IsString()) {
return { std::string(*Nan::Utf8String(value)) };
} else if (value->IsUint32()) {
return { std::uint64_t(value->Uint32Value()) };
} else if (value->IsInt32()) {
return { std::int64_t(value->Int32Value()) };
} else if (value->IsNumber()) {
return { value->NumberValue() };
} else {
return {};
}
}
return {};
}
inline optional<bool> toBool(v8::Local<v8::Value> value) {
Nan::HandleScope scope;
if (!value->IsBoolean()) {
return {};
static optional<GeoJSON> toGeoJSON(const v8::Local<v8::Value>& value, Error& error) {
try {
Nan::JSON JSON;
std::string string = *Nan::Utf8String(JSON.Stringify(value->ToObject()).ToLocalChecked());
return parseGeoJSON(string, error);
} catch (const std::exception& ex) {
error = { ex.what() };
return {};
}
}
return value->BooleanValue();
}
};
inline optional<float> toNumber(v8::Local<v8::Value> value) {
Nan::HandleScope scope;
if (!value->IsNumber()) {
return {};
}
return value->NumberValue();
}
inline optional<double> toDouble(v8::Local<v8::Value> value) {
Nan::HandleScope scope;
if (!value->IsNumber()) {
return {};
}
return value->NumberValue();
}
inline optional<std::string> toString(v8::Local<v8::Value> value) {
Nan::HandleScope scope;
if (!value->IsString()) {
return {};
}
return std::string(*Nan::Utf8String(value));
}
inline optional<Value> toValue(v8::Local<v8::Value> value) {
if (value->IsFalse()) {
return { false };
} else if (value->IsTrue()) {
return { true };
} else if (value->IsString()) {
return { std::string(*Nan::Utf8String(value)) };
} else if (value->IsUint32()) {
return { std::uint64_t(value->Uint32Value()) };
} else if (value->IsInt32()) {
return { std::int64_t(value->Int32Value()) };
} else if (value->IsNumber()) {
return { value->NumberValue() };
} else {
return {};
}
template <class T, class...Args>
optional<T> convert(const v8::Local<v8::Value>& value, Error& error, Args&&...args) {
return convert<T>(Convertible(value), error, std::forward<Args>(args)...);
}
} // namespace conversion

View File

@@ -1,17 +0,0 @@
#include <mbgl/style/conversion/geojson.hpp>
#include <string>
namespace mbgl {
namespace style {
namespace conversion {
template <>
optional<GeoJSON> Converter<GeoJSON>::operator()(const v8::Local<v8::Value>& value, Error& error) const {
Nan::JSON JSON;
std::string string = *Nan::Utf8String(JSON.Stringify(value->ToObject()).ToLocalChecked());
return convert<GeoJSON>(string, error);
}
} // namespace conversion
} // namespace style
} // namespace mbgl

View File

@@ -2,7 +2,6 @@
#include "node_request.hpp"
#include "node_feature.hpp"
#include "node_conversion.hpp"
#include "node_geojson.hpp"
#include <mbgl/util/exception.hpp>
#include <mbgl/renderer/renderer.hpp>
@@ -10,6 +9,15 @@
#include <mbgl/style/conversion/source.hpp>
#include <mbgl/style/conversion/layer.hpp>
#include <mbgl/style/conversion/filter.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/style/style.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/map/map_observer.hpp>
@@ -741,7 +749,7 @@ void NodeMap::SetLayoutProperty(const Nan::FunctionCallbackInfo<v8::Value>& info
return Nan::ThrowTypeError("Second argument must be a string");
}
mbgl::optional<Error> error = setLayoutProperty(*layer, *Nan::Utf8String(info[1]), info[2]);
mbgl::optional<Error> error = setLayoutProperty(*layer, *Nan::Utf8String(info[1]), Convertible(info[2]));
if (error) {
return Nan::ThrowTypeError(error->message.c_str());
}
@@ -773,7 +781,7 @@ void NodeMap::SetPaintProperty(const Nan::FunctionCallbackInfo<v8::Value>& info)
return Nan::ThrowTypeError("Second argument must be a string");
}
mbgl::optional<Error> error = setPaintProperty(*layer, *Nan::Utf8String(info[1]), info[2]);
mbgl::optional<Error> error = setPaintProperty(*layer, *Nan::Utf8String(info[1]), Convertible(info[2]));
if (error) {
return Nan::ThrowTypeError(error->message.c_str());
}