From 45ff9084736be39eba994d55a052c53af482193f Mon Sep 17 00:00:00 2001 From: Anand Thakker Date: Thu, 10 Aug 2017 17:19:58 -0400 Subject: Make types other than Value non-nullable --- include/mbgl/style/expression/type.hpp | 10 +++++++- include/mbgl/style/function/convert.hpp | 29 ++++++++++++++++++++--- src/mbgl/style/expression/check_subtype.cpp | 2 +- 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>; + mapbox::util::recursive_wrapper, + 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>(), ctx))); } + + static std::unique_ptr makeError(std::string message) { + return std::make_unique(message); + } + template static ParseResult makeCoalesceToDefault(std::unique_ptr main, optional defaultValue) { @@ -150,7 +173,7 @@ struct Convert { return ParseResult(std::make_unique>(valueTypeToExpressionType(), std::move(input), std::move(cases), - makeLiteral(Null))); + makeError("No matching label"))); } template @@ -159,10 +182,10 @@ struct Convert { // case expression std::vector 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(valueTypeToExpressionType(), 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 checkSubtype(const Type& expected, const Type& t, optional context) { - if (t.is()) return {}; + if (t.is()) return {}; optional result = expected.match( [&] (const Array& expectedArray) -> optional { 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 CompoundExpressions::definitions = i define("e", []() -> Result { return 2.718281828459045f; }), define("pi", []() -> Result { return 3.141592653589793f; }), define("ln2", []() -> Result { return 0.6931471805599453; }), - + define("typeof", [](const Value& v) -> Result { return toString(typeOf(v)); }), define("number", assertion), define("string", assertion), -- cgit v1.2.1