[core] change RapidJSON usage to use CrtAllocator and bump GeoJSON VT

This commit is contained in:
Konstantin Käfer
2015-12-14 16:07:15 -08:00
parent 9b8f5d3be1
commit 95cef66e22
36 changed files with 165 additions and 168 deletions

View File

@@ -7,7 +7,7 @@ std::unique_ptr<StyleLayer> BackgroundLayer::clone() const {
return std::make_unique<BackgroundLayer>(*this);
}
void BackgroundLayer::parsePaints(const JSVal& layer) {
void BackgroundLayer::parsePaints(const JSValue& layer) {
paint.opacity.parse("background-opacity", layer);
paint.color.parse("background-color", layer);
paint.pattern.parse("background-pattern", layer);

View File

@@ -17,8 +17,8 @@ class BackgroundLayer : public StyleLayer {
public:
std::unique_ptr<StyleLayer> clone() const override;
void parseLayout(const JSVal&) override {};
void parsePaints(const JSVal&) override;
void parseLayout(const JSValue&) override {};
void parsePaints(const JSValue&) override;
void cascade(const StyleCascadeParameters&) override;
bool recalculate(const StyleCalculationParameters&) override;

View File

@@ -8,7 +8,7 @@ std::unique_ptr<StyleLayer> CircleLayer::clone() const {
return std::make_unique<CircleLayer>(*this);
}
void CircleLayer::parsePaints(const JSVal& layer) {
void CircleLayer::parsePaints(const JSValue& layer) {
paint.radius.parse("circle-radius", layer);
paint.color.parse("circle-color", layer);
paint.opacity.parse("circle-opacity", layer);

View File

@@ -24,8 +24,8 @@ class CircleLayer : public StyleLayer {
public:
std::unique_ptr<StyleLayer> clone() const override;
void parseLayout(const JSVal&) override {};
void parsePaints(const JSVal&) override;
void parseLayout(const JSValue&) override {};
void parsePaints(const JSValue&) override;
void cascade(const StyleCascadeParameters&) override;
bool recalculate(const StyleCalculationParameters&) override;

View File

@@ -24,8 +24,8 @@ public:
private:
std::unique_ptr<StyleLayer> clone() const final;
void parseLayout(const JSVal&) final {}
void parsePaints(const JSVal&) final {}
void parseLayout(const JSValue&) final {}
void parsePaints(const JSValue&) final {}
void cascade(const StyleCascadeParameters&) final {}
bool recalculate(const StyleCalculationParameters&) final;

View File

@@ -8,7 +8,7 @@ std::unique_ptr<StyleLayer> FillLayer::clone() const {
return std::make_unique<FillLayer>(*this);
}
void FillLayer::parsePaints(const JSVal& layer) {
void FillLayer::parsePaints(const JSValue& layer) {
paint.antialias.parse("fill-antialias", layer);
paint.opacity.parse("fill-opacity", layer);
paint.color.parse("fill-color", layer);

View File

@@ -21,8 +21,8 @@ class FillLayer : public StyleLayer {
public:
std::unique_ptr<StyleLayer> clone() const override;
void parseLayout(const JSVal&) override {};
void parsePaints(const JSVal&) override;
void parseLayout(const JSValue&) override {};
void parsePaints(const JSValue&) override;
void cascade(const StyleCascadeParameters&) override;
bool recalculate(const StyleCalculationParameters&) override;

View File

@@ -9,14 +9,14 @@ std::unique_ptr<StyleLayer> LineLayer::clone() const {
return std::make_unique<LineLayer>(*this);
}
void LineLayer::parseLayout(const JSVal& value) {
void LineLayer::parseLayout(const JSValue& value) {
layout.cap.parse("line-cap", value);
layout.join.parse("line-join", value);
layout.miterLimit.parse("line-miter-limit", value);
layout.roundLimit.parse("line-round-limit", value);
}
void LineLayer::parsePaints(const JSVal& layer) {
void LineLayer::parsePaints(const JSValue& layer) {
paint.opacity.parse("line-opacity", layer);
paint.color.parse("line-color", layer);
paint.translate.parse("line-translate", layer);

View File

@@ -40,8 +40,8 @@ class LineLayer : public StyleLayer {
public:
std::unique_ptr<StyleLayer> clone() const override;
void parseLayout(const JSVal&) override;
void parsePaints(const JSVal&) override;
void parseLayout(const JSValue&) override;
void parsePaints(const JSValue&) override;
void cascade(const StyleCascadeParameters&) override;
bool recalculate(const StyleCalculationParameters&) override;

View File

@@ -7,7 +7,7 @@ std::unique_ptr<StyleLayer> RasterLayer::clone() const {
return std::make_unique<RasterLayer>(*this);
}
void RasterLayer::parsePaints(const JSVal& layer) {
void RasterLayer::parsePaints(const JSValue& layer) {
paint.opacity.parse("raster-opacity", layer);
paint.hueRotate.parse("raster-hue-rotate", layer);
paint.brightnessMin.parse("raster-brightness-min", layer);

View File

@@ -21,8 +21,8 @@ class RasterLayer : public StyleLayer {
public:
std::unique_ptr<StyleLayer> clone() const override;
void parseLayout(const JSVal&) override {};
void parsePaints(const JSVal&) override;
void parseLayout(const JSValue&) override {};
void parsePaints(const JSValue&) override;
void cascade(const StyleCascadeParameters&) override;
bool recalculate(const StyleCalculationParameters&) override;

View File

@@ -9,7 +9,7 @@ std::unique_ptr<StyleLayer> SymbolLayer::clone() const {
return std::make_unique<SymbolLayer>(*this);
}
void SymbolLayer::parseLayout(const JSVal& value) {
void SymbolLayer::parseLayout(const JSValue& value) {
layout.placement.parse("symbol-placement", value);
layout.spacing.parse("symbol-spacing", value);
layout.avoidEdges.parse("symbol-avoid-edges", value);
@@ -45,7 +45,7 @@ void SymbolLayer::parseLayout(const JSVal& value) {
layout.text.optional.parse("text-optional", value);
}
void SymbolLayer::parsePaints(const JSVal& layer) {
void SymbolLayer::parsePaints(const JSValue& layer) {
paint.icon.opacity.parse("icon-opacity", layer);
paint.icon.color.parse("icon-color", layer);
paint.icon.haloColor.parse("icon-halo-color", layer);

View File

@@ -86,8 +86,8 @@ class SymbolLayer : public StyleLayer {
public:
std::unique_ptr<StyleLayer> clone() const override;
void parseLayout(const JSVal&) override;
void parsePaints(const JSVal&) override;
void parseLayout(const JSValue&) override;
void parsePaints(const JSValue&) override;
void cascade(const StyleCascadeParameters&) override;
bool recalculate(const StyleCalculationParameters&) override;

View File

@@ -77,7 +77,7 @@ void Source::load() {
return;
}
rapidjson::Document d;
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> d;
d.Parse<0>(res.data->c_str());
if (d.HasParseError()) {

View File

@@ -11,12 +11,12 @@ namespace mbgl {
namespace {
void parse(const rapidjson::Value& value, std::vector<std::string>& target, const char* name) {
void parse(const JSValue& value, std::vector<std::string>& target, const char* name) {
if (!value.HasMember(name)) {
return;
}
const rapidjson::Value& property = value[name];
const JSValue& property = value[name];
if (!property.IsArray()) {
return;
}
@@ -32,12 +32,12 @@ void parse(const rapidjson::Value& value, std::vector<std::string>& target, cons
}
}
void parse(const rapidjson::Value& value, std::string& target, const char* name) {
void parse(const JSValue& value, std::string& target, const char* name) {
if (!value.HasMember(name)) {
return;
}
const rapidjson::Value& property = value[name];
const JSValue& property = value[name];
if (!property.IsString()) {
return;
}
@@ -45,12 +45,12 @@ void parse(const rapidjson::Value& value, std::string& target, const char* name)
target = { property.GetString(), property.GetStringLength() };
}
void parse(const rapidjson::Value& value, uint16_t& target, const char* name) {
void parse(const JSValue& value, uint16_t& target, const char* name) {
if (!value.HasMember(name)) {
return;
}
const rapidjson::Value& property = value[name];
const JSValue& property = value[name];
if (!property.IsUint()) {
return;
}
@@ -64,12 +64,12 @@ void parse(const rapidjson::Value& value, uint16_t& target, const char* name) {
}
template <size_t N>
void parse(const rapidjson::Value& value, std::array<float, N>& target, const char* name) {
void parse(const JSValue& value, std::array<float, N>& target, const char* name) {
if (!value.HasMember(name)) {
return;
}
const rapidjson::Value& property = value[name];
const JSValue& property = value[name];
if (!property.IsArray() || property.Size() != N) {
return;
}
@@ -90,7 +90,7 @@ void parse(const rapidjson::Value& value, std::array<float, N>& target, const ch
// Destructor in implementation file because header only contains forward declarations.
SourceInfo::~SourceInfo() = default;
void SourceInfo::parseTileJSONProperties(const rapidjson::Value& value) {
void SourceInfo::parseTileJSONProperties(const JSValue& value) {
parse(value, tiles, "tiles");
parse(value, min_zoom, "minzoom");
parse(value, max_zoom, "maxzoom");
@@ -99,7 +99,7 @@ void SourceInfo::parseTileJSONProperties(const rapidjson::Value& value) {
parse(value, bounds, "bounds");
}
void SourceInfo::parseGeoJSON(const rapidjson::Value& value) {
void SourceInfo::parseGeoJSON(const JSValue& value) {
using namespace mapbox::geojsonvt;
try {

View File

@@ -6,9 +6,9 @@
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/rapidjson.hpp>
#include <mapbox/variant.hpp>
#include <rapidjson/document.h>
#include <array>
#include <vector>
@@ -38,8 +38,8 @@ public:
std::string source_id = "";
std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> geojsonvt;
void parseTileJSONProperties(const rapidjson::Value&);
void parseGeoJSON(const rapidjson::Value&);
void parseTileJSONProperties(const JSValue&);
void parseGeoJSON(const JSValue&);
std::string tileURL(const TileID& id, float pixelRatio) const;
};

View File

@@ -4,9 +4,7 @@
#include <mbgl/platform/log.hpp>
#include <mbgl/util/image.hpp>
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <mbgl/util/rapidjson.hpp>
#include <cmath>
#include <limits>
@@ -59,7 +57,7 @@ SpriteImagePtr createSpriteImage(const PremultipliedImage& image,
namespace {
inline uint16_t getUInt16(const rapidjson::Value& value, const char* name, const uint16_t def = 0) {
inline uint16_t getUInt16(const JSValue& value, const char* name, const uint16_t def = 0) {
if (value.HasMember(name)) {
auto& v = value[name];
if (v.IsUint() && v.GetUint() <= std::numeric_limits<uint16_t>::max()) {
@@ -73,7 +71,7 @@ inline uint16_t getUInt16(const rapidjson::Value& value, const char* name, const
return def;
}
inline double getDouble(const rapidjson::Value& value, const char* name, const double def = 0) {
inline double getDouble(const JSValue& value, const char* name, const double def = 0) {
if (value.HasMember(name)) {
auto& v = value[name];
if (v.IsNumber()) {
@@ -86,7 +84,7 @@ inline double getDouble(const rapidjson::Value& value, const char* name, const d
return def;
}
inline bool getBoolean(const rapidjson::Value& value, const char* name, const bool def = false) {
inline bool getBoolean(const JSValue& value, const char* name, const bool def = false) {
if (value.HasMember(name)) {
auto& v = value[name];
if (v.IsBool()) {
@@ -102,8 +100,6 @@ inline bool getBoolean(const rapidjson::Value& value, const char* name, const bo
} // namespace
SpriteParseResult parseSprite(const std::string& image, const std::string& json) {
using namespace rapidjson;
Sprites sprites;
PremultipliedImage raster;
@@ -113,7 +109,7 @@ SpriteParseResult parseSprite(const std::string& image, const std::string& json)
return std::string("Could not parse sprite image");
}
Document doc;
JSDocument doc;
doc.Parse<0>(json.c_str());
if (doc.HasParseError()) {
@@ -123,9 +119,9 @@ SpriteParseResult parseSprite(const std::string& image, const std::string& json)
} else if (!doc.IsObject()) {
return std::string("Sprite JSON root must be an object");
} else {
for (Value::ConstMemberIterator itr = doc.MemberBegin(); itr != doc.MemberEnd(); ++itr) {
for (JSValue::ConstMemberIterator itr = doc.MemberBegin(); itr != doc.MemberEnd(); ++itr) {
const std::string name = { itr->name.GetString(), itr->name.GetStringLength() };
const Value& value = itr->value;
const JSValue& value = itr->value;
if (value.IsObject()) {
const uint16_t x = getUInt16(value, "x", 0);

View File

@@ -18,7 +18,7 @@ Value parseFeatureType(const Value& value) {
}
template <class Expression>
FilterExpression parseBinaryFilter(const rapidjson::Value& value) {
FilterExpression parseBinaryFilter(const JSValue& value) {
FilterExpression empty;
if (value.Size() < 3) {
@@ -43,7 +43,7 @@ FilterExpression parseBinaryFilter(const rapidjson::Value& value) {
}
template <class Expression>
FilterExpression parseSetFilter(const rapidjson::Value& value) {
FilterExpression parseSetFilter(const JSValue& value) {
FilterExpression empty;
if (value.Size() < 2) {
@@ -69,7 +69,7 @@ FilterExpression parseSetFilter(const rapidjson::Value& value) {
}
template <class Expression>
FilterExpression parseCompoundFilter(const rapidjson::Value& value) {
FilterExpression parseCompoundFilter(const JSValue& value) {
Expression expression;
for (rapidjson::SizeType i = 1; i < value.Size(); ++i) {
expression.expressions.push_back(parseFilterExpression(value[i]));
@@ -77,7 +77,7 @@ FilterExpression parseCompoundFilter(const rapidjson::Value& value) {
return expression;
}
FilterExpression parseFilterExpression(const rapidjson::Value& value) {
FilterExpression parseFilterExpression(const JSValue& value) {
FilterExpression empty;
if (!value.IsArray()) {

View File

@@ -2,8 +2,7 @@
#define MBGL_STYLE_FILTER_EXPRESSION
#include <mbgl/style/value.hpp>
#include <rapidjson/document.h>
#include <mbgl/util/rapidjson.hpp>
#include <string>
#include <vector>
@@ -25,7 +24,7 @@ typedef mapbox::util::variant<
struct NoneExpression
> FilterExpression;
FilterExpression parseFilterExpression(const rapidjson::Value&);
FilterExpression parseFilterExpression(const JSValue&);
template <class Extractor>
bool evaluate(const FilterExpression&, const Extractor&);

View File

@@ -3,20 +3,18 @@
#include <mbgl/style/property_parsing.hpp>
#include <mbgl/style/function.hpp>
#include <mbgl/util/rapidjson.hpp>
#include <utility>
#include <rapidjson/document.h>
namespace mbgl {
using JSVal = rapidjson::Value;
template <typename T>
class LayoutProperty {
public:
explicit LayoutProperty(T v) : value(std::move(v)) {}
void parse(const char * name, const JSVal& layout) {
void parse(const char * name, const JSValue& layout) {
if (layout.HasMember(name)) {
parsedValue = parseProperty<Function<T>>(name, layout[name]);
}

View File

@@ -9,16 +9,13 @@
#include <mbgl/style/style_calculation_parameters.hpp>
#include <mbgl/util/interpolate.hpp>
#include <mbgl/util/std.hpp>
#include <rapidjson/document.h>
#include <mbgl/util/rapidjson.hpp>
#include <map>
#include <utility>
namespace mbgl {
using JSVal = rapidjson::Value;
template <typename T, typename Result = T>
class PaintProperty {
public:
@@ -34,7 +31,7 @@ public:
transitions(other.transitions) {
}
void parse(const char* name, const JSVal& layer) {
void parse(const char* name, const JSValue& layer) {
mbgl::util::erase_if(values, [] (const auto& p) { return p.first != ClassID::Fallback; });
std::string transitionName = { name };

View File

@@ -11,7 +11,7 @@
namespace mbgl {
template <>
optional<bool> parseProperty(const char* name, const JSVal& value) {
optional<bool> parseProperty(const char* name, const JSValue& value) {
if (!value.IsBool()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a boolean", name);
return {};
@@ -21,7 +21,7 @@ optional<bool> parseProperty(const char* name, const JSVal& value) {
}
template <>
optional<float> parseProperty(const char* name, const JSVal& value) {
optional<float> parseProperty(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 +31,7 @@ optional<float> parseProperty(const char* name, const JSVal& value) {
}
template <>
optional<std::string> parseProperty(const char* name, const JSVal& value) {
optional<std::string> parseProperty(const char* name, const JSValue& value) {
if (std::string { "text-font" } == name) {
if (!value.IsArray()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be an array of strings", name);
@@ -40,7 +40,7 @@ optional<std::string> parseProperty(const char* name, const JSVal& value) {
std::string result = "";
for (rapidjson::SizeType i = 0; i < value.Size(); ++i) {
const JSVal& stop = value[i];
const JSValue& stop = value[i];
if (stop.IsString()) {
result += stop.GetString();
if (i < value.Size()-1) {
@@ -63,7 +63,7 @@ optional<std::string> parseProperty(const char* name, const JSVal& value) {
}
template <>
optional<Color> parseProperty(const char* name, const JSVal& value) {
optional<Color> parseProperty(const char* name, const JSValue& value) {
if (!value.IsString()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name);
return {};
@@ -81,7 +81,7 @@ optional<Color> parseProperty(const char* name, const JSVal& value) {
}
template <>
optional<TranslateAnchorType> parseProperty(const char* name, const JSVal& value) {
optional<TranslateAnchorType> parseProperty(const char* name, const JSValue& value) {
if (!value.IsString()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name);
return {};
@@ -91,7 +91,7 @@ optional<TranslateAnchorType> parseProperty(const char* name, const JSVal& value
}
template <>
optional<RotateAnchorType> parseProperty<RotateAnchorType>(const char* name, const JSVal& value) {
optional<RotateAnchorType> parseProperty<RotateAnchorType>(const char* name, const JSValue& value) {
if (!value.IsString()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name);
return {};
@@ -101,7 +101,7 @@ optional<RotateAnchorType> parseProperty<RotateAnchorType>(const char* name, con
}
template <>
optional<CapType> parseProperty<CapType>(const char* name, const JSVal& value) {
optional<CapType> parseProperty<CapType>(const char* name, const JSValue& value) {
if (!value.IsString()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name);
return {};
@@ -111,7 +111,7 @@ optional<CapType> parseProperty<CapType>(const char* name, const JSVal& value) {
}
template <>
optional<JoinType> parseProperty<JoinType>(const char* name, const JSVal& value) {
optional<JoinType> parseProperty<JoinType>(const char* name, const JSValue& value) {
if (!value.IsString()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name);
return {};
@@ -121,7 +121,7 @@ optional<JoinType> parseProperty<JoinType>(const char* name, const JSVal& value)
}
template <>
optional<PlacementType> parseProperty<PlacementType>(const char* name, const JSVal& value) {
optional<PlacementType> parseProperty<PlacementType>(const char* name, const JSValue& value) {
if (!value.IsString()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name);
return {};
@@ -131,7 +131,7 @@ optional<PlacementType> parseProperty<PlacementType>(const char* name, const JSV
}
template <>
optional<TextAnchorType> parseProperty<TextAnchorType>(const char* name, const JSVal& value) {
optional<TextAnchorType> parseProperty<TextAnchorType>(const char* name, const JSValue& value) {
if (!value.IsString()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name);
return {};
@@ -141,7 +141,7 @@ optional<TextAnchorType> parseProperty<TextAnchorType>(const char* name, const J
}
template <>
optional<TextJustifyType> parseProperty<TextJustifyType>(const char* name, const JSVal& value) {
optional<TextJustifyType> parseProperty<TextJustifyType>(const char* name, const JSValue& value) {
if (!value.IsString()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name);
return {};
@@ -151,7 +151,7 @@ optional<TextJustifyType> parseProperty<TextJustifyType>(const char* name, const
}
template <>
optional<TextTransformType> parseProperty<TextTransformType>(const char* name, const JSVal& value) {
optional<TextTransformType> parseProperty<TextTransformType>(const char* name, const JSValue& value) {
if (!value.IsString()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name);
return {};
@@ -161,7 +161,7 @@ optional<TextTransformType> parseProperty<TextTransformType>(const char* name, c
}
template <>
optional<RotationAlignmentType> parseProperty<RotationAlignmentType>(const char* name, const JSVal& value) {
optional<RotationAlignmentType> parseProperty<RotationAlignmentType>(const char* name, const JSValue& value) {
if (!value.IsString()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a string", name);
return {};
@@ -171,7 +171,7 @@ optional<RotationAlignmentType> parseProperty<RotationAlignmentType>(const char*
}
template <>
optional<std::array<float, 2>> parseProperty(const char* name, const JSVal& value) {
optional<std::array<float, 2>> parseProperty(const char* name, const JSValue& value) {
if (value.IsArray() && value.Size() == 2 &&
value[rapidjson::SizeType(0)].IsNumber() &&
value[rapidjson::SizeType(1)].IsNumber()) {
@@ -186,7 +186,7 @@ optional<std::array<float, 2>> parseProperty(const char* name, const JSVal& valu
}
template <>
optional<std::vector<float>> parseProperty(const char* name, const JSVal& value) {
optional<std::vector<float>> parseProperty(const char* name, const JSValue& value) {
if (!value.IsArray()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be an array of numbers", name);
return {};
@@ -195,7 +195,7 @@ optional<std::vector<float>> parseProperty(const char* name, const JSVal& value)
std::vector<float> result;
for (rapidjson::SizeType i = 0; i < value.Size(); ++i) {
const JSVal& part = value[i];
const JSValue& part = value[i];
if (!part.IsNumber()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be an array of numbers", name);
@@ -209,7 +209,7 @@ optional<std::vector<float>> parseProperty(const char* name, const JSVal& value)
}
template <>
optional<PropertyTransition> parseProperty(const char *, const JSVal& value) {
optional<PropertyTransition> parseProperty(const char *, const JSValue& value) {
PropertyTransition transition;
if (value.IsObject()) {
bool parsed = false;
@@ -231,7 +231,7 @@ optional<PropertyTransition> parseProperty(const char *, const JSVal& value) {
// --- Function ---
template <typename T>
optional<std::vector<std::pair<float, T>>> parseStops(const char* name, const JSVal& value) {
optional<std::vector<std::pair<float, T>>> parseStops(const char* name, const JSValue& value) {
if (!value.IsArray()) {
Log::Warning(Event::ParseStyle, "stops function must specify a stops array");
return {};
@@ -240,7 +240,7 @@ optional<std::vector<std::pair<float, T>>> parseStops(const char* name, const JS
std::vector<std::pair<float, T>> stops;
for (rapidjson::SizeType i = 0; i < value.Size(); ++i) {
const JSVal& stop = value[i];
const JSValue& stop = value[i];
if (!stop.IsArray()) {
Log::Warning(Event::ParseStyle, "function argument must be a numeric value");
@@ -252,7 +252,7 @@ optional<std::vector<std::pair<float, T>>> parseStops(const char* name, const JS
return {};
}
const JSVal& z = stop[rapidjson::SizeType(0)];
const JSValue& z = stop[rapidjson::SizeType(0)];
if (!z.IsNumber()) {
Log::Warning(Event::ParseStyle, "zoom level in stop must be a number");
return {};
@@ -270,7 +270,7 @@ optional<std::vector<std::pair<float, T>>> parseStops(const char* name, const JS
}
template <typename T>
optional<Function<T>> parseFunction(const char* name, const JSVal& value) {
optional<Function<T>> parseFunction(const char* name, const JSValue& value) {
if (!value.IsObject()) {
auto constant = parseProperty<T>(name, value);
if (!constant) {
@@ -287,7 +287,7 @@ optional<Function<T>> parseFunction(const char* name, const JSVal& value) {
float base = 1.0f;
if (value.HasMember("base")) {
const JSVal& value_base = value["base"];
const JSValue& value_base = value["base"];
if (!value_base.IsNumber()) {
Log::Warning(Event::ParseStyle, "base must be numeric");
@@ -306,64 +306,64 @@ optional<Function<T>> parseFunction(const char* name, const JSVal& value) {
return { Function<T>(*stops, base) };
}
template <> optional<Function<std::array<float, 2>>> parseProperty(const char* name, const JSVal& value) {
template <> optional<Function<std::array<float, 2>>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<std::array<float, 2>>(name, value);
}
template <> optional<Function<std::string>> parseProperty(const char* name, const JSVal& value) {
template <> optional<Function<std::string>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<std::string>(name, value);
}
template <> optional<Function<TranslateAnchorType>> parseProperty(const char* name, const JSVal& value) {
template <> optional<Function<TranslateAnchorType>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<TranslateAnchorType>(name, value);
}
template <> optional<Function<RotateAnchorType>> parseProperty(const char* name, const JSVal& value) {
template <> optional<Function<RotateAnchorType>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<RotateAnchorType>(name, value);
}
template <> optional<Function<CapType>> parseProperty(const char* name, const JSVal& value) {
template <> optional<Function<CapType>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<CapType>(name, value);
}
template <> optional<Function<JoinType>> parseProperty(const char* name, const JSVal& value) {
template <> optional<Function<JoinType>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<JoinType>(name, value);
}
template <> optional<Function<PlacementType>> parseProperty(const char* name, const JSVal& value) {
template <> optional<Function<PlacementType>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<PlacementType>(name, value);
}
template <> optional<Function<TextAnchorType>> parseProperty(const char* name, const JSVal& value) {
template <> optional<Function<TextAnchorType>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<TextAnchorType>(name, value);
}
template <> optional<Function<TextJustifyType>> parseProperty(const char* name, const JSVal& value) {
template <> optional<Function<TextJustifyType>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<TextJustifyType>(name, value);
}
template <> optional<Function<TextTransformType>> parseProperty(const char* name, const JSVal& value) {
template <> optional<Function<TextTransformType>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<TextTransformType>(name, value);
}
template <> optional<Function<RotationAlignmentType>> parseProperty(const char* name, const JSVal& value) {
template <> optional<Function<RotationAlignmentType>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<RotationAlignmentType>(name, value);
}
template <> optional<Function<bool>> parseProperty(const char* name, const JSVal& value) {
template <> optional<Function<bool>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<bool>(name, value);
}
template<> optional<Function<float>> parseProperty(const char* name, const JSVal& value) {
template<> optional<Function<float>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<float>(name, value);
}
template<> optional<Function<Color>> parseProperty(const char* name, const JSVal& value) {
template<> optional<Function<Color>> parseProperty(const char* name, const JSValue& value) {
return parseFunction<Color>(name, value);
}
template <typename T>
optional<Function<Faded<T>>> parseFadedFunction(const JSVal& value) {
optional<Function<Faded<T>>> parseFadedFunction(const JSValue& value) {
if (!value.HasMember("stops")) {
Log::Warning(Event::ParseStyle, "function must specify a function type");
return {};
@@ -379,7 +379,7 @@ optional<Function<Faded<T>>> parseFadedFunction(const JSVal& value) {
}
template <>
optional<Function<Faded<std::vector<float>>>> parseProperty(const char* name, const JSVal& value) {
optional<Function<Faded<std::vector<float>>>> parseProperty(const char* name, const JSValue& value) {
if (value.IsObject()) {
return parseFadedFunction<std::vector<float>>(value);
}
@@ -392,7 +392,7 @@ optional<Function<Faded<std::vector<float>>>> parseProperty(const char* name, co
}
template <>
optional<Function<Faded<std::string>>> parseProperty(const char* name, const JSVal& value) {
optional<Function<Faded<std::string>>> parseProperty(const char* name, const JSValue& value) {
if (value.IsObject()) {
return parseFadedFunction<std::string>(value);
}

View File

@@ -3,20 +3,19 @@
#include <mbgl/style/types.hpp>
#include <mbgl/util/rapidjson.hpp>
#include <mapbox/optional.hpp>
#include <rapidjson/document.h>
#include <functional>
namespace mbgl {
using JSVal = rapidjson::Value;
template <typename T>
using optional = mapbox::util::optional<T>;
template <typename T>
optional<T> parseProperty(const char* name, const JSVal&);
optional<T> parseProperty(const char* name, const JSValue&);
} // namespace mbgl

View File

@@ -42,7 +42,7 @@ Style::Style(MapData& data_)
}
void Style::setJSON(const std::string& json, const std::string&) {
rapidjson::Document doc;
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> doc;
doc.Parse<0>((const char *const)json.c_str());
if (doc.HasParseError()) {
Log::Error(Event::ParseStyle, "Error parsing style JSON at %i: %s", doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError()));

View File

@@ -5,8 +5,7 @@
#include <mbgl/style/filter_expression.hpp>
#include <mbgl/renderer/render_pass.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <rapidjson/document.h>
#include <mbgl/util/rapidjson.hpp>
#include <memory>
#include <string>
@@ -19,8 +18,6 @@ class StyleCalculationParameters;
class StyleBucketParameters;
class Bucket;
using JSVal = rapidjson::Value;
class StyleLayer {
public:
virtual ~StyleLayer() = default;
@@ -35,8 +32,8 @@ public:
// Create a copy of this layer.
virtual std::unique_ptr<StyleLayer> clone() const = 0;
virtual void parseLayout(const JSVal& value) = 0;
virtual void parsePaints(const JSVal& value) = 0;
virtual void parseLayout(const JSValue& value) = 0;
virtual void parsePaints(const JSValue& value) = 0;
// If the layer has a ref, the ref. Otherwise, the id.
const std::string& bucketName() const;

View File

@@ -14,7 +14,7 @@ namespace mbgl {
StyleParser::~StyleParser() = default;
void StyleParser::parse(const JSVal& document) {
void StyleParser::parse(const JSValue& document) {
if (document.HasMember("version")) {
int version = document["version"].GetInt();
if (version != 8) {
@@ -31,30 +31,30 @@ void StyleParser::parse(const JSVal& document) {
}
if (document.HasMember("sprite")) {
const JSVal& sprite = document["sprite"];
const JSValue& sprite = document["sprite"];
if (sprite.IsString()) {
spriteURL = { sprite.GetString(), sprite.GetStringLength() };
}
}
if (document.HasMember("glyphs")) {
const JSVal& glyphs = document["glyphs"];
const JSValue& glyphs = document["glyphs"];
if (glyphs.IsString()) {
glyphURL = { glyphs.GetString(), glyphs.GetStringLength() };
}
}
}
void StyleParser::parseSources(const JSVal& value) {
void StyleParser::parseSources(const JSValue& value) {
if (!value.IsObject()) {
Log::Warning(Event::ParseStyle, "sources must be an object");
return;
}
rapidjson::Value::ConstMemberIterator itr = value.MemberBegin();
JSValue::ConstMemberIterator itr = value.MemberBegin();
for (; itr != value.MemberEnd(); ++itr) {
const JSVal& nameVal = itr->name;
const JSVal& sourceVal = itr->value;
const JSValue& nameVal = itr->name;
const JSValue& sourceVal = itr->value;
std::unique_ptr<Source> source = std::make_unique<Source>();
@@ -65,7 +65,7 @@ void StyleParser::parseSources(const JSVal& value) {
continue;
}
const JSVal& typeVal = sourceVal["type"];
const JSValue& typeVal = sourceVal["type"];
if (!typeVal.IsString()) {
Log::Warning(Event::ParseStyle, "source type must have one of the enum values");
continue;
@@ -98,10 +98,10 @@ void StyleParser::parseSources(const JSVal& value) {
}
}
bool StyleParser::parseVectorSource(Source& source, const JSVal& sourceVal) {
bool StyleParser::parseVectorSource(Source& source, const JSValue& sourceVal) {
// A vector tile source either specifies the URL of a TileJSON file...
if (sourceVal.HasMember("url")) {
const JSVal& urlVal = sourceVal["url"];
const JSValue& urlVal = sourceVal["url"];
if (!urlVal.IsString()) {
Log::Warning(Event::ParseStyle, "source url must be a string");
@@ -118,9 +118,9 @@ bool StyleParser::parseVectorSource(Source& source, const JSVal& sourceVal) {
return true;
}
bool StyleParser::parseRasterSource(Source& source, const JSVal& sourceVal) {
bool StyleParser::parseRasterSource(Source& source, const JSValue& sourceVal) {
if (sourceVal.HasMember("tileSize")) {
const JSVal& tileSizeVal = sourceVal["tileSize"];
const JSValue& tileSizeVal = sourceVal["tileSize"];
if (!tileSizeVal.IsUint()) {
Log::Warning(Event::ParseStyle, "source tileSize must be an unsigned integer");
@@ -138,7 +138,7 @@ bool StyleParser::parseRasterSource(Source& source, const JSVal& sourceVal) {
// A raster tile source either specifies the URL of a TileJSON file...
if (sourceVal.HasMember("url")) {
const JSVal& urlVal = sourceVal["url"];
const JSValue& urlVal = sourceVal["url"];
if (!urlVal.IsString()) {
Log::Warning(Event::ParseStyle, "source url must be a string");
@@ -155,13 +155,13 @@ bool StyleParser::parseRasterSource(Source& source, const JSVal& sourceVal) {
return true;
}
bool StyleParser::parseGeoJSONSource(Source& source, const JSVal& sourceVal) {
bool StyleParser::parseGeoJSONSource(Source& source, const JSValue& sourceVal) {
if (!sourceVal.HasMember("data")) {
Log::Warning(Event::ParseStyle, "GeoJSON source must have a data value");
return false;
}
const JSVal& dataVal = sourceVal["data"];
const JSValue& dataVal = sourceVal["data"];
if (dataVal.IsString()) {
// We need to load an external GeoJSON file
source.info.url = { dataVal.GetString(), dataVal.GetStringLength() };
@@ -176,7 +176,7 @@ bool StyleParser::parseGeoJSONSource(Source& source, const JSVal& sourceVal) {
return true;
}
void StyleParser::parseLayers(const JSVal& value) {
void StyleParser::parseLayers(const JSValue& value) {
std::vector<std::string> ids;
if (!value.IsArray()) {
@@ -185,7 +185,7 @@ void StyleParser::parseLayers(const JSVal& value) {
}
for (rapidjson::SizeType i = 0; i < value.Size(); ++i) {
const JSVal& layerValue = value[i];
const JSValue& layerValue = value[i];
if (!layerValue.IsObject()) {
Log::Warning(Event::ParseStyle, "layer must be an object");
@@ -197,7 +197,7 @@ void StyleParser::parseLayers(const JSVal& value) {
continue;
}
const JSVal& id = layerValue["id"];
const JSValue& id = layerValue["id"];
if (!id.IsString()) {
Log::Warning(Event::ParseStyle, "layer id must be a string");
continue;
@@ -209,7 +209,7 @@ void StyleParser::parseLayers(const JSVal& value) {
continue;
}
layersMap.emplace(layerID, std::pair<const JSVal&, std::unique_ptr<StyleLayer>> { layerValue, nullptr });
layersMap.emplace(layerID, std::pair<const JSValue&, std::unique_ptr<StyleLayer>> { layerValue, nullptr });
ids.push_back(layerID);
}
@@ -230,7 +230,7 @@ void StyleParser::parseLayers(const JSVal& value) {
}
}
void StyleParser::parseLayer(const std::string& id, const JSVal& value, std::unique_ptr<StyleLayer>& layer) {
void StyleParser::parseLayer(const std::string& id, const JSValue& value, std::unique_ptr<StyleLayer>& layer) {
if (layer) {
// Skip parsing this again. We already have a valid layer definition.
return;
@@ -244,7 +244,7 @@ void StyleParser::parseLayer(const std::string& id, const JSVal& value, std::uni
if (value.HasMember("ref")) {
// This layer is referencing another layer. Recursively parse that layer.
const JSVal& refVal = value["ref"];
const JSValue& refVal = value["ref"];
if (!refVal.IsString()) {
Log::Warning(Event::ParseStyle, "layer ref of '%s' must be a string", id.c_str());
return;
@@ -280,7 +280,7 @@ void StyleParser::parseLayer(const std::string& id, const JSVal& value, std::uni
return;
}
const JSVal& typeVal = value["type"];
const JSValue& typeVal = value["type"];
if (!typeVal.IsString()) {
Log::Warning(Event::ParseStyle, "layer '%s' has an invalid type", id.c_str());
return;
@@ -308,7 +308,7 @@ void StyleParser::parseLayer(const std::string& id, const JSVal& value, std::uni
layer->id = id;
if (value.HasMember("source")) {
const JSVal& value_source = value["source"];
const JSValue& value_source = value["source"];
if (value_source.IsString()) {
layer->source = { value_source.GetString(), value_source.GetStringLength() };
auto source_it = sourcesMap.find(layer->source);
@@ -321,7 +321,7 @@ void StyleParser::parseLayer(const std::string& id, const JSVal& value, std::uni
}
if (value.HasMember("source-layer")) {
const JSVal& value_source_layer = value["source-layer"];
const JSValue& value_source_layer = value["source-layer"];
if (value_source_layer.IsString()) {
layer->sourceLayer = { value_source_layer.GetString(), value_source_layer.GetStringLength() };
} else {
@@ -334,7 +334,7 @@ void StyleParser::parseLayer(const std::string& id, const JSVal& value, std::uni
}
if (value.HasMember("minzoom")) {
const JSVal& min_zoom = value["minzoom"];
const JSValue& min_zoom = value["minzoom"];
if (min_zoom.IsNumber()) {
layer->minZoom = min_zoom.GetDouble();
} else {
@@ -343,7 +343,7 @@ void StyleParser::parseLayer(const std::string& id, const JSVal& value, std::uni
}
if (value.HasMember("maxzoom")) {
const JSVal& max_zoom = value["maxzoom"];
const JSValue& max_zoom = value["maxzoom"];
if (max_zoom.IsNumber()) {
layer->maxZoom = max_zoom.GetDouble();
} else {
@@ -360,7 +360,7 @@ void StyleParser::parseLayer(const std::string& id, const JSVal& value, std::uni
layer->parsePaints(value);
}
void StyleParser::parseVisibility(StyleLayer& layer, const JSVal& value) {
void StyleParser::parseVisibility(StyleLayer& layer, const JSValue& value) {
if (!value.HasMember("visibility")) {
return;
} else if (!value["visibility"].IsString()) {

View File

@@ -3,8 +3,7 @@
#include <mbgl/style/style_layer.hpp>
#include <mbgl/map/source.hpp>
#include <rapidjson/document.h>
#include <mbgl/util/rapidjson.hpp>
#include <vector>
#include <memory>
@@ -17,13 +16,11 @@ namespace mbgl {
class StyleLayer;
class Source;
using JSVal = rapidjson::Value;
class StyleParser {
public:
~StyleParser();
void parse(const JSVal&);
void parse(const JSValue&);
std::string spriteURL;
std::string glyphURL;
@@ -32,16 +29,16 @@ public:
std::vector<std::unique_ptr<StyleLayer>> layers;
private:
void parseSources(const JSVal&);
bool parseVectorSource(Source&, const JSVal&);
bool parseRasterSource(Source&, const JSVal&);
bool parseGeoJSONSource(Source&, const JSVal&);
void parseLayers(const JSVal&);
void parseLayer(const std::string& id, const JSVal&, std::unique_ptr<StyleLayer>&);
void parseVisibility(StyleLayer&, const JSVal& value);
void parseSources(const JSValue&);
bool parseVectorSource(Source&, const JSValue&);
bool parseRasterSource(Source&, const JSValue&);
bool parseGeoJSONSource(Source&, const JSValue&);
void parseLayers(const JSValue&);
void parseLayer(const std::string& id, const JSValue&, std::unique_ptr<StyleLayer>&);
void parseVisibility(StyleLayer&, const JSValue& value);
std::unordered_map<std::string, const Source*> sourcesMap;
std::unordered_map<std::string, std::pair<const JSVal&, std::unique_ptr<StyleLayer>>> layersMap;
std::unordered_map<std::string, std::pair<const JSValue&, std::unique_ptr<StyleLayer>>> layersMap;
// Store a stack of layer IDs we're parsing right now. This is to prevent reference cycles.
std::forward_list<std::string> stack;

View File

@@ -10,7 +10,7 @@ std::string mbgl::toString(const mbgl::Value& value) {
else return "null";
}
mbgl::Value mbgl::parseValue(const rapidjson::Value& value) {
mbgl::Value mbgl::parseValue(const JSValue& value) {
switch (value.GetType()) {
case rapidjson::kNullType:
case rapidjson::kFalseType:

View File

@@ -2,7 +2,7 @@
#define MBGL_STYLE_VALUE
#include <mapbox/variant.hpp>
#include <rapidjson/document.h>
#include <mbgl/util/rapidjson.hpp>
#include <cstdlib>
#include <cerrno>
@@ -13,7 +13,7 @@ typedef mapbox::util::variant<bool, int64_t, uint64_t, double, std::string> Valu
std::string toString(const Value &value);
Value parseValue(const rapidjson::Value&);
Value parseValue(const JSValue&);
namespace util {
inline bool parseNumericString(const std::string &str, double &result) {

View File

@@ -0,0 +1,14 @@
#ifndef MBGL_UTIL_RAPIDJSON
#define MBGL_UTIL_RAPIDJSON
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
namespace mbgl {
using JSDocument = rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator>;
using JSValue = rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>;
} // namespace mbgl
#endif