diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-04-26 10:36:25 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-04-26 11:44:48 -0700 |
commit | 878310b8912c83445ce7b12ee2b30a4489bf34f7 (patch) | |
tree | 826e2c7087f11fbfcb5556e98ee4e501548e19bb /src/mbgl/style/property_parsing.hpp | |
parent | 64fe75b01109d38977b20c83773e6236e55905a8 (diff) | |
download | qtlocation-mapboxgl-878310b8912c83445ce7b12ee2b30a4489bf34f7.tar.gz |
[core] Simplify property parsing
This is a followup to #4811. Now that use of Function is consistent, we can have a single separate parseProperty template function that delegates to a parseConstant template function.
Diffstat (limited to 'src/mbgl/style/property_parsing.hpp')
-rw-r--r-- | src/mbgl/style/property_parsing.hpp | 102 |
1 files changed, 95 insertions, 7 deletions
diff --git a/src/mbgl/style/property_parsing.hpp b/src/mbgl/style/property_parsing.hpp index 73bc5a72c7..8deb58c00b 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 <mbgl/style/types.hpp> +#include <mbgl/style/function.hpp> +#include <mbgl/style/property_transition.hpp> #include <mbgl/util/rapidjson.hpp> #include <mbgl/util/optional.hpp> -#include <functional> +#include <mbgl/platform/log.hpp> + +#include <string> +#include <array> +#include <vector> namespace mbgl { template <typename T> -using optional = optional<T>; +optional<T> parseConstant(const char* name, const JSValue&); + +template <> optional<bool> parseConstant(const char*, const JSValue&); +template <> optional<float> parseConstant(const char*, const JSValue&); +template <> optional<std::string> parseConstant(const char*, const JSValue&); +template <> optional<Color> parseConstant(const char*, const JSValue&); +template <> optional<TranslateAnchorType> parseConstant(const char*, const JSValue&); +template <> optional<RotateAnchorType> parseConstant(const char*, const JSValue&); +template <> optional<LineCapType> parseConstant(const char*, const JSValue&); +template <> optional<LineJoinType> parseConstant(const char*, const JSValue&); +template <> optional<SymbolPlacementType> parseConstant(const char*, const JSValue&); +template <> optional<TextAnchorType> parseConstant(const char*, const JSValue&); +template <> optional<TextJustifyType> parseConstant(const char*, const JSValue&); +template <> optional<TextTransformType> parseConstant(const char*, const JSValue&); +template <> optional<RotationAlignmentType> parseConstant(const char*, const JSValue&); +template <> optional<std::array<float, 2>> parseConstant(const char*, const JSValue&); +template <> optional<std::vector<float>> parseConstant(const char*, const JSValue&); +template <> optional<std::vector<std::string>> parseConstant(const char*, const JSValue&); template <typename T> -optional<T> parseProperty(const char* name, const JSValue&); +optional<Function<T>> parseProperty(const char* name, const JSValue& value) { + if (!value.IsObject()) { + auto constant = parseConstant<T>(name, value); -} // namespace mbgl + if (!constant) { + return {}; + } + + return { Function<T>(*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 {}; + } -#endif + std::vector<std::pair<float, T>> 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<T> v = parseConstant<T>(name, stop[rapidjson::SizeType(1)]); + if (!v) { + return {}; + } + + stops.emplace_back(z.GetDouble(), *v); + } + + return { Function<T>(stops, base) }; +} + +optional<PropertyTransition> parsePropertyTransition(const char * name, const JSValue&); + +} // namespace mbgl |