diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-06-10 21:17:23 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-06-13 17:22:05 -0700 |
commit | 282e626ac112b1ca7a9bf9f9d4c0239db8fbd97a (patch) | |
tree | f83ef7974687a84341dd616b364b2aa5a9c012a8 /src/mbgl/style/parser.cpp | |
parent | 36bef2157595e1455aa55c64c6c526f096dd1c8e (diff) | |
download | qtlocation-mapboxgl-282e626ac112b1ca7a9bf9f9d4c0239db8fbd97a.tar.gz |
[core] Make enum ↔ string conversion more generic-friendly
Rewrite enum.hpp in such a way that parseConstant can be defined generically for all enumerated types.
While there, properly validated enumerated property values.
Diffstat (limited to 'src/mbgl/style/parser.cpp')
-rw-r--r-- | src/mbgl/style/parser.cpp | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/src/mbgl/style/parser.cpp b/src/mbgl/style/parser.cpp index 8e632ac6a0..870fd6d71e 100644 --- a/src/mbgl/style/parser.cpp +++ b/src/mbgl/style/parser.cpp @@ -14,6 +14,7 @@ #include <mbgl/tile/geometry_tile_data.hpp> #include <mbgl/util/mapbox.hpp> +#include <mbgl/util/enum.hpp> #include <rapidjson/document.h> #include <rapidjson/error/en.h> @@ -161,11 +162,15 @@ void Parser::parseSources(const JSValue& value) { const JSValue& typeVal = sourceVal["type"]; if (!typeVal.IsString()) { - Log::Warning(Event::ParseStyle, "source type must have one of the enum values"); + Log::Warning(Event::ParseStyle, "source type must be a string"); continue; } - const auto type = SourceTypeClass({ typeVal.GetString(), typeVal.GetStringLength() }); + const auto type = Enum<SourceType>::toEnum({ typeVal.GetString(), typeVal.GetStringLength() }); + if (!type) { + Log::Warning(Event::ParseStyle, "source type must have one of the enum values"); + continue; + } // Sources can have URLs, either because they reference an external TileJSON file, or // because reference a GeoJSON file. They don't have to have one though when all source @@ -177,7 +182,7 @@ void Parser::parseSources(const JSValue& value) { std::unique_ptr<Tileset> tileset; std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> geojsonvt; - switch (type) { + switch (*type) { case SourceType::Raster: if (sourceVal.HasMember("tileSize")) { const JSValue& tileSizeVal = sourceVal["tileSize"]; @@ -236,7 +241,7 @@ void Parser::parseSources(const JSValue& value) { } const std::string id { nameVal.GetString(), nameVal.GetStringLength() }; - std::unique_ptr<Source> source = std::make_unique<Source>(type, id, url, tileSize, std::move(tileset), std::move(geojsonvt)); + std::unique_ptr<Source> source = std::make_unique<Source>(*type, id, url, tileSize, std::move(tileset), std::move(geojsonvt)); sourcesMap.emplace(id, source.get()); sources.emplace_back(std::move(source)); @@ -481,21 +486,25 @@ void Parser::parseLayer(const std::string& id, const JSValue& value, std::unique } } -MBGL_DEFINE_ENUM_CLASS(VisibilityTypeClass, VisibilityType, { - { VisibilityType::Visible, "visible" }, - { VisibilityType::None, "none" }, -}) - void Parser::parseVisibility(Layer& layer, const JSValue& value) { Layer::Impl& impl = *layer.baseImpl; + if (!value.HasMember("visibility")) { return; - } else if (!value["visibility"].IsString()) { + } + + if (!value["visibility"].IsString()) { Log::Warning(Event::ParseStyle, "value of 'visibility' must be a string"); - impl.visibility = VisibilityType::Visible; return; } - impl.visibility = VisibilityTypeClass({ value["visibility"].GetString(), value["visibility"].GetStringLength() }); + + const auto enumValue = Enum<VisibilityType>::toEnum({ value["visibility"].GetString(), value["visibility"].GetStringLength() }); + if (!enumValue) { + Log::Warning(Event::ParseStyle, "value of 'visibility' must be a valid enumeration value"); + return; + } + + impl.visibility = *enumValue; } Value parseFeatureType(const Value& value) { |