From 9ff5d34ef2ed2a236cc495f0ad84919cedce9abc Mon Sep 17 00:00:00 2001 From: Chris Loer Date: Fri, 29 Jun 2018 15:56:37 -0700 Subject: [core] Introduce "collator" expressions Cross platform parsing and evaluation code. --- include/mbgl/style/expression/collator.hpp | 29 ++++++++++++++ .../mbgl/style/expression/collator_expression.hpp | 44 ++++++++++++++++++++++ include/mbgl/style/expression/equals.hpp | 4 +- include/mbgl/style/expression/type.hpp | 8 ++++ include/mbgl/style/expression/value.hpp | 2 + 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 include/mbgl/style/expression/collator.hpp create mode 100644 include/mbgl/style/expression/collator_expression.hpp (limited to 'include') diff --git a/include/mbgl/style/expression/collator.hpp b/include/mbgl/style/expression/collator.hpp new file mode 100644 index 0000000000..2a79e55556 --- /dev/null +++ b/include/mbgl/style/expression/collator.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +#include +#include + +namespace mbgl { +namespace style { +namespace expression { + +class Collator { +public: + Collator(bool caseSensitive, bool diacriticSensitive, optional locale = {}); + + bool operator==(const Collator& other) const; + + int compare(const std::string& lhs, const std::string& rhs) const; + + std::string resolvedLocale() const; +private: + class Impl; + std::shared_ptr impl; +}; + +} // namespace expression +} // namespace style +} // namespace mbgl diff --git a/include/mbgl/style/expression/collator_expression.hpp b/include/mbgl/style/expression/collator_expression.hpp new file mode 100644 index 0000000000..2551cd19c8 --- /dev/null +++ b/include/mbgl/style/expression/collator_expression.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include + +#include + +namespace mbgl { +namespace style { +namespace expression { + +class CollatorExpression : public Expression { +public: + CollatorExpression(std::unique_ptr caseSensitive, + std::unique_ptr diacriticSensitive, + optional> locale); + + EvaluationResult evaluate(const EvaluationContext&) const override; + static ParseResult parse(const mbgl::style::conversion::Convertible&, ParsingContext&); + + void eachChild(const std::function&) const override; + + bool operator==(const Expression& e) const override; + + std::vector> possibleOutputs() const override { + // Technically the set of possible outputs is the combinatoric set of Collators produced + // by all possibleOutputs of locale/caseSensitive/diacriticSensitive + // But for the primary use of Collators in comparison operators, we ignore the Collator's + // possibleOutputs anyway, so we can get away with leaving this undefined for now. + return { nullopt }; + } + + mbgl::Value serialize() const override; + std::string getOperator() const override { return "collator"; } +private: + std::unique_ptr caseSensitive; + std::unique_ptr diacriticSensitive; + optional> locale; +}; + +} // namespace expression +} // namespace style +} // namespace mbgl diff --git a/include/mbgl/style/expression/equals.hpp b/include/mbgl/style/expression/equals.hpp index 54df890a68..1e8bf7acef 100644 --- a/include/mbgl/style/expression/equals.hpp +++ b/include/mbgl/style/expression/equals.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -12,7 +13,7 @@ namespace expression { class Equals : public Expression { public: - Equals(std::unique_ptr lhs, std::unique_ptr rhs, bool negate); + Equals(std::unique_ptr lhs, std::unique_ptr rhs, optional> collator, bool negate); static ParseResult parse(const mbgl::style::conversion::Convertible&, ParsingContext&); @@ -25,6 +26,7 @@ public: private: std::unique_ptr lhs; std::unique_ptr rhs; + optional> collator; bool negate; }; diff --git a/include/mbgl/style/expression/type.hpp b/include/mbgl/style/expression/type.hpp index 513c4bdc17..316496839b 100644 --- a/include/mbgl/style/expression/type.hpp +++ b/include/mbgl/style/expression/type.hpp @@ -60,6 +60,12 @@ struct ValueType { std::string getName() const { return "value"; } bool operator==(const ValueType&) const { return true; } }; + +struct CollatorType { + constexpr CollatorType() {}; // NOLINT + std::string getName() const { return "collator"; } + bool operator==(const CollatorType&) const { return true; } +}; constexpr NullType Null; constexpr NumberType Number; @@ -68,6 +74,7 @@ constexpr BooleanType Boolean; constexpr ColorType Color; constexpr ValueType Value; constexpr ObjectType Object; +constexpr CollatorType Collator; constexpr ErrorType Error; struct Array; @@ -81,6 +88,7 @@ using Type = variant< ObjectType, ValueType, mapbox::util::recursive_wrapper, + CollatorType, ErrorType>; struct Array { diff --git a/include/mbgl/style/expression/value.hpp b/include/mbgl/style/expression/value.hpp index 7839ff2ca7..fc38c36ff0 100644 --- a/include/mbgl/style/expression/value.hpp +++ b/include/mbgl/style/expression/value.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -23,6 +24,7 @@ using ValueBase = variant< double, std::string, Color, + Collator, mapbox::util::recursive_wrapper>, mapbox::util::recursive_wrapper>>; struct Value : ValueBase { -- cgit v1.2.1