diff --git a/src/mbgl/style/layout_property.hpp b/src/mbgl/style/layout_property.hpp index 894fc4446..dd3c03215 100644 --- a/src/mbgl/style/layout_property.hpp +++ b/src/mbgl/style/layout_property.hpp @@ -17,7 +17,7 @@ public: void parse(const char * name, const JSValue& layout) { if (layout.HasMember(name)) { - parsedValue = parseProperty>(name, layout[name]); + parsedValue = parseProperty(name, layout[name]); } } diff --git a/src/mbgl/style/paint_property.hpp b/src/mbgl/style/paint_property.hpp index 124b0a7c0..3c2b4ba7d 100644 --- a/src/mbgl/style/paint_property.hpp +++ b/src/mbgl/style/paint_property.hpp @@ -51,14 +51,14 @@ public: ClassID classID = isClass ? ClassDictionary::Get().lookup(paintName.substr(6)) : ClassID::Default; if (it->value.HasMember(name)) { - auto v = parseProperty(name, it->value[name]); + auto v = parseProperty(name, it->value[name]); if (v) { values.emplace(classID, *v); } } if (it->value.HasMember(transitionName.c_str())) { - auto v = parseProperty(name, it->value[transitionName.c_str()]); + auto v = parsePropertyTransition(name, it->value[transitionName.c_str()]); if (v) { transitions.emplace(classID, *v); } diff --git a/src/mbgl/style/property_parsing.cpp b/src/mbgl/style/property_parsing.cpp index 84589ea41..7994f245e 100644 --- a/src/mbgl/style/property_parsing.cpp +++ b/src/mbgl/style/property_parsing.cpp @@ -1,17 +1,11 @@ #include -#include -#include - -#include #include -#include - namespace mbgl { template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsBool()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a boolean", name); return {}; @@ -21,7 +15,7 @@ optional parseProperty(const char* name, const JSValue& value) { } template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsNumber()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a number, or a number function", name); return {}; @@ -31,7 +25,7 @@ optional parseProperty(const char* name, const JSValue& value) { } template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name); return {}; @@ -41,7 +35,7 @@ optional parseProperty(const char* name, const JSValue& value) { } template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name); return {}; @@ -64,7 +58,7 @@ MBGL_DEFINE_ENUM_CLASS(TranslateAnchorTypeClass, TranslateAnchorType, { }); template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name); return {}; @@ -79,7 +73,7 @@ MBGL_DEFINE_ENUM_CLASS(RotateAnchorTypeClass, RotateAnchorType, { }); template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name); return {}; @@ -95,7 +89,7 @@ MBGL_DEFINE_ENUM_CLASS(LineCapTypeClass, LineCapType, { }); template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name); return {}; @@ -113,7 +107,7 @@ MBGL_DEFINE_ENUM_CLASS(LineJoinTypeClass, LineJoinType, { }); template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name); return {}; @@ -128,7 +122,7 @@ MBGL_DEFINE_ENUM_CLASS(SymbolPlacementTypeClass, SymbolPlacementType, { }); template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name); return {}; @@ -150,7 +144,7 @@ MBGL_DEFINE_ENUM_CLASS(TextAnchorTypeClass, TextAnchorType, { }); template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name); return {}; @@ -166,7 +160,7 @@ MBGL_DEFINE_ENUM_CLASS(TextJustifyTypeClass, TextJustifyType, { }); template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name); return {}; @@ -182,7 +176,7 @@ MBGL_DEFINE_ENUM_CLASS(TextTransformTypeClass, TextTransformType, { }); template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name); return {}; @@ -197,7 +191,7 @@ MBGL_DEFINE_ENUM_CLASS(RotationAlignmentTypeClass, RotationAlignmentType, { }); template <> -optional parseProperty(const char* name, const JSValue& value) { +optional parseConstant(const char* name, const JSValue& value) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name); return {}; @@ -207,7 +201,7 @@ optional parseProperty(const char* } template <> -optional> parseProperty(const char* name, const JSValue& value) { +optional> parseConstant(const char* name, const JSValue& value) { if (value.IsArray() && value.Size() == 2 && value[rapidjson::SizeType(0)].IsNumber() && value[rapidjson::SizeType(1)].IsNumber()) { @@ -222,7 +216,7 @@ optional> parseProperty(const char* name, const JSValue& va } template <> -optional> parseProperty(const char* name, const JSValue& value) { +optional> parseConstant(const char* name, const JSValue& value) { if (!value.IsArray()) { Log::Warning(Event::ParseStyle, "value of '%s' must be an array of numbers", name); return {}; @@ -245,7 +239,7 @@ optional> parseProperty(const char* name, const JSValue& valu } template <> -optional> parseProperty(const char* name, const JSValue& value) { +optional> parseConstant(const char* name, const JSValue& value) { if (!value.IsArray()) { Log::Warning(Event::ParseStyle, "value of '%s' must be an array of strings", name); return {}; @@ -267,8 +261,7 @@ optional> parseProperty(const char* name, const JSValue return result; } -template <> -optional parseProperty(const char *, const JSValue& value) { +optional parsePropertyTransition(const char *, const JSValue& value) { PropertyTransition transition; if (value.IsObject()) { bool parsed = false; @@ -287,146 +280,4 @@ optional parseProperty(const char *, const JSValue& value) { return transition; } -// --- Function --- - -template -optional>> parseStops(const char* name, const JSValue& value) { - if (!value.IsArray()) { - Log::Warning(Event::ParseStyle, "stops function must specify a stops array"); - return {}; - } - - std::vector> stops; - - for (rapidjson::SizeType i = 0; i < value.Size(); ++i) { - const JSValue& stop = value[i]; - - if (!stop.IsArray()) { - Log::Warning(Event::ParseStyle, "function argument must be a numeric value"); - return {}; - } - - if (stop.Size() != 2) { - Log::Warning(Event::ParseStyle, "stop must have zoom level and value specification"); - return {}; - } - - const JSValue& z = stop[rapidjson::SizeType(0)]; - if (!z.IsNumber()) { - Log::Warning(Event::ParseStyle, "zoom level in stop must be a number"); - return {}; - } - - optional v = parseProperty(name, stop[rapidjson::SizeType(1)]); - if (!v) { - return {}; - } - - stops.emplace_back(z.GetDouble(), *v); - } - - return stops; -} - -template -optional> parseFunction(const char* name, const JSValue& value) { - if (!value.IsObject()) { - auto constant = parseProperty(name, value); - if (!constant) { - return {}; - } - return { Function(*constant) }; - } - - if (!value.HasMember("stops")) { - Log::Warning(Event::ParseStyle, "function must specify a function type"); - return {}; - } - - float base = 1.0f; - - if (value.HasMember("base")) { - const JSValue& value_base = value["base"]; - - if (!value_base.IsNumber()) { - Log::Warning(Event::ParseStyle, "base must be numeric"); - return {}; - } - - base = value_base.GetDouble(); - } - - auto stops = parseStops(name, value["stops"]); - - if (!stops) { - return {}; - } - - return { Function(*stops, base) }; -} - -template <> optional>> parseProperty(const char* name, const JSValue& value) { - return parseFunction>(name, value); -} - -template <> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template <> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template <> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template <> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template <> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template <> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template <> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template <> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template <> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template <> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template <> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template<> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template<> optional> parseProperty(const char* name, const JSValue& value) { - return parseFunction(name, value); -} - -template<> optional>> parseProperty(const char* name, const JSValue& value) { - return parseFunction>(name, value); -} - -template<> optional>> parseProperty(const char* name, const JSValue& value) { - return parseFunction>(name, value); -} - } // namespace mbgl diff --git a/src/mbgl/style/property_parsing.hpp b/src/mbgl/style/property_parsing.hpp index 73bc5a72c..8deb58c00 100644 --- a/src/mbgl/style/property_parsing.hpp +++ b/src/mbgl/style/property_parsing.hpp @@ -1,21 +1,109 @@ -#ifndef MBGL_PROPERTY_PARSING -#define MBGL_PROPERTY_PARSING +#pragma once #include +#include +#include #include #include -#include +#include + +#include +#include +#include namespace mbgl { template -using optional = optional; +optional parseConstant(const char* name, const JSValue&); + +template <> optional parseConstant(const char*, const JSValue&); +template <> optional parseConstant(const char*, const JSValue&); +template <> optional parseConstant(const char*, const JSValue&); +template <> optional parseConstant(const char*, const JSValue&); +template <> optional parseConstant(const char*, const JSValue&); +template <> optional parseConstant(const char*, const JSValue&); +template <> optional parseConstant(const char*, const JSValue&); +template <> optional parseConstant(const char*, const JSValue&); +template <> optional parseConstant(const char*, const JSValue&); +template <> optional parseConstant(const char*, const JSValue&); +template <> optional parseConstant(const char*, const JSValue&); +template <> optional parseConstant(const char*, const JSValue&); +template <> optional parseConstant(const char*, const JSValue&); +template <> optional> parseConstant(const char*, const JSValue&); +template <> optional> parseConstant(const char*, const JSValue&); +template <> optional> parseConstant(const char*, const JSValue&); template -optional parseProperty(const char* name, const JSValue&); +optional> parseProperty(const char* name, const JSValue& value) { + if (!value.IsObject()) { + auto constant = parseConstant(name, value); + + if (!constant) { + return {}; + } + + return { Function(*constant) }; + } + + if (!value.HasMember("stops")) { + Log::Warning(Event::ParseStyle, "function must specify a function type"); + return {}; + } + + float base = 1.0f; + + if (value.HasMember("base")) { + const JSValue& value_base = value["base"]; + + if (!value_base.IsNumber()) { + Log::Warning(Event::ParseStyle, "base must be numeric"); + return {}; + } + + base = value_base.GetDouble(); + } + + const JSValue& stopsValue = value["stops"]; + + if (!stopsValue.IsArray()) { + Log::Warning(Event::ParseStyle, "stops function must specify a stops array"); + return {}; + } + + std::vector> stops; + + for (rapidjson::SizeType i = 0; i < stopsValue.Size(); ++i) { + const JSValue& stop = stopsValue[i]; + + if (!stop.IsArray()) { + Log::Warning(Event::ParseStyle, "function argument must be a numeric value"); + return {}; + } + + if (stop.Size() != 2) { + Log::Warning(Event::ParseStyle, "stop must have zoom level and value specification"); + return {}; + } + + const JSValue& z = stop[rapidjson::SizeType(0)]; + if (!z.IsNumber()) { + Log::Warning(Event::ParseStyle, "zoom level in stop must be a number"); + return {}; + } + + optional v = parseConstant(name, stop[rapidjson::SizeType(1)]); + if (!v) { + return {}; + } + + stops.emplace_back(z.GetDouble(), *v); + } + + return { Function(stops, base) }; +} + +optional parsePropertyTransition(const char * name, const JSValue&); } // namespace mbgl - -#endif