diff options
author | Anand Thakker <github@anandthakker.net> | 2017-08-10 17:19:58 -0400 |
---|---|---|
committer | Anand Thakker <github@anandthakker.net> | 2017-08-11 21:55:04 -0400 |
commit | 71a23a70a1220a62d38fb6dde47516ce50a783e5 (patch) | |
tree | e74db80655f4804600143702d127449215d2c45d | |
parent | dff55fc47be5fb4dc3a42cba55ac308f32ecea03 (diff) | |
download | qtlocation-mapboxgl-71a23a70a1220a62d38fb6dde47516ce50a783e5.tar.gz |
Make types other than Value non-nullable
-rw-r--r-- | include/mbgl/style/expression/type.hpp | 10 | ||||
-rw-r--r-- | include/mbgl/style/function/convert.hpp | 29 | ||||
-rw-r--r-- | src/mbgl/style/expression/check_subtype.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/style/expression/compound_expression.cpp | 2 |
4 files changed, 37 insertions, 6 deletions
diff --git a/include/mbgl/style/expression/type.hpp b/include/mbgl/style/expression/type.hpp index 3ad2faa3c8..21b01234c6 100644 --- a/include/mbgl/style/expression/type.hpp +++ b/include/mbgl/style/expression/type.hpp @@ -50,6 +50,12 @@ struct ObjectType { bool operator==(const ObjectType&) const { return true; } }; +struct ErrorType { + constexpr ErrorType() {} + std::string getName() const { return "Error"; } + bool operator==(const ErrorType&) const { return true; } +}; + struct ValueType { constexpr ValueType() {} std::string getName() const { return "Value"; } @@ -63,6 +69,7 @@ constexpr BooleanType Boolean; constexpr ColorType Color; constexpr ValueType Value; constexpr ObjectType Object; +constexpr ErrorType Error; struct Array; @@ -74,7 +81,8 @@ using Type = variant< ColorType, ObjectType, ValueType, - mapbox::util::recursive_wrapper<Array>>; + mapbox::util::recursive_wrapper<Array>, + ErrorType>; struct Array { Array(Type itemType_) : itemType(itemType_) {} diff --git a/include/mbgl/style/function/convert.hpp b/include/mbgl/style/function/convert.hpp index ad42f3f9d2..edbd0c6fa0 100644 --- a/include/mbgl/style/function/convert.hpp +++ b/include/mbgl/style/function/convert.hpp @@ -23,6 +23,24 @@ namespace mbgl { namespace style { namespace expression { +namespace detail { + +class ErrorExpression : public Expression { +public: + ErrorExpression(std::string message_) : Expression(type::Error), message(std::move(message_)) {} + bool isFeatureConstant() const override { return true; } + bool isZoomConstant() const override { return true; } + + EvaluationResult evaluate(const EvaluationParameters&) const override { + return EvaluationError{message}; + } + +private: + std::string message; +}; + +} + // Create expressions representing 'classic' (i.e. stop-based) style functions struct Convert { @@ -53,6 +71,11 @@ struct Convert { std::vector<std::unique_ptr<Expression>>(), ctx))); } + + static std::unique_ptr<Expression> makeError(std::string message) { + return std::make_unique<detail::ErrorExpression>(message); + } + template <typename T> static ParseResult makeCoalesceToDefault(std::unique_ptr<Expression> main, optional<T> defaultValue) { @@ -150,7 +173,7 @@ struct Convert { return ParseResult(std::make_unique<Match<Key>>(valueTypeToExpressionType<T>(), std::move(input), std::move(cases), - makeLiteral(Null))); + makeError("No matching label"))); } template <typename T> @@ -159,10 +182,10 @@ struct Convert { // case expression std::vector<typename Case::Branch> cases; auto true_case = stops.stops.find(true) == stops.stops.end() ? - makeLiteral(Null) : + makeError("No matching label") : makeLiteral(stops.stops.at(true)); auto false_case = stops.stops.find(false) == stops.stops.end() ? - makeLiteral(Null) : + makeError("No matching label") : makeLiteral(stops.stops.at(false)); cases.push_back(std::make_pair(std::move(input), std::move(true_case))); return ParseResult(std::make_unique<Case>(valueTypeToExpressionType<T>(), std::move(cases), std::move(false_case))); diff --git a/src/mbgl/style/expression/check_subtype.cpp b/src/mbgl/style/expression/check_subtype.cpp index 9a70bdde9e..bcdc5128b9 100644 --- a/src/mbgl/style/expression/check_subtype.cpp +++ b/src/mbgl/style/expression/check_subtype.cpp @@ -10,7 +10,7 @@ std::string errorMessage(const Type& expected, const Type& t) { } optional<std::string> checkSubtype(const Type& expected, const Type& t, optional<ParsingContext> context) { - if (t.is<NullType>()) return {}; + if (t.is<ErrorType>()) return {}; optional<std::string> result = expected.match( [&] (const Array& expectedArray) -> optional<std::string> { diff --git a/src/mbgl/style/expression/compound_expression.cpp b/src/mbgl/style/expression/compound_expression.cpp index e235c3007d..1821200fe6 100644 --- a/src/mbgl/style/expression/compound_expression.cpp +++ b/src/mbgl/style/expression/compound_expression.cpp @@ -190,7 +190,7 @@ std::unordered_map<std::string, Definition> CompoundExpressions::definitions = i define("e", []() -> Result<float> { return 2.718281828459045f; }), define("pi", []() -> Result<float> { return 3.141592653589793f; }), define("ln2", []() -> Result<float> { return 0.6931471805599453; }), - + define("typeof", [](const Value& v) -> Result<std::string> { return toString(typeOf(v)); }), define("number", assertion<float>), define("string", assertion<std::string>), |