summaryrefslogtreecommitdiff
path: root/include/mbgl/style/expression
diff options
context:
space:
mode:
Diffstat (limited to 'include/mbgl/style/expression')
-rw-r--r--include/mbgl/style/expression/coercion.hpp7
-rw-r--r--include/mbgl/style/expression/expression.hpp1
-rw-r--r--include/mbgl/style/expression/format_expression.hpp52
-rw-r--r--include/mbgl/style/expression/formatted.hpp62
-rw-r--r--include/mbgl/style/expression/type.hpp9
-rw-r--r--include/mbgl/style/expression/value.hpp2
6 files changed, 128 insertions, 5 deletions
diff --git a/include/mbgl/style/expression/coercion.hpp b/include/mbgl/style/expression/coercion.hpp
index d83bd6dfa7..28c2c0c679 100644
--- a/include/mbgl/style/expression/coercion.hpp
+++ b/include/mbgl/style/expression/coercion.hpp
@@ -10,11 +10,6 @@ namespace mbgl {
namespace style {
namespace expression {
-/**
- * Special form for error-coalescing coercion expressions "to-number",
- * "to-color". Since these coercions can fail at runtime, they accept multiple
- * arguments, only evaluating one at a time until one succeeds.
- */
class Coercion : public Expression {
public:
Coercion(type::Type type_, std::vector<std::unique_ptr<Expression>> inputs_);
@@ -23,6 +18,8 @@ public:
EvaluationResult evaluate(const EvaluationContext& params) const override;
void eachChild(const std::function<void(const Expression&)>& visit) const override;
+
+ mbgl::Value serialize() const override;
bool operator==(const Expression& e) const override;
diff --git a/include/mbgl/style/expression/expression.hpp b/include/mbgl/style/expression/expression.hpp
index ce02c4114b..97b143b3d9 100644
--- a/include/mbgl/style/expression/expression.hpp
+++ b/include/mbgl/style/expression/expression.hpp
@@ -133,6 +133,7 @@ enum class Kind : int32_t {
Any,
All,
Comparison,
+ FormatExpression,
};
class Expression {
diff --git a/include/mbgl/style/expression/format_expression.hpp b/include/mbgl/style/expression/format_expression.hpp
new file mode 100644
index 0000000000..b00674a88e
--- /dev/null
+++ b/include/mbgl/style/expression/format_expression.hpp
@@ -0,0 +1,52 @@
+#pragma once
+
+#include <mbgl/style/expression/expression.hpp>
+#include <mbgl/style/expression/formatted.hpp>
+#include <mbgl/style/expression/parsing_context.hpp>
+#include <mbgl/style/conversion.hpp>
+
+#include <memory>
+
+namespace mbgl {
+namespace style {
+namespace expression {
+
+struct FormatExpressionSection {
+ FormatExpressionSection(std::unique_ptr<Expression> text_,
+ optional<std::unique_ptr<Expression>> fontScale_,
+ optional<std::unique_ptr<Expression>> textFont_);
+
+ std::shared_ptr<Expression> text;
+ optional<std::shared_ptr<Expression>> fontScale;
+ optional<std::shared_ptr<Expression>> textFont;
+};
+
+class FormatExpression : public Expression {
+public:
+ FormatExpression(std::vector<FormatExpressionSection> sections);
+
+ EvaluationResult evaluate(const EvaluationContext&) const override;
+ static ParseResult parse(const mbgl::style::conversion::Convertible&, ParsingContext&);
+
+ void eachChild(const std::function<void(const Expression&)>&) const override;
+
+ bool operator==(const Expression& e) const override;
+
+ std::vector<optional<Value>> possibleOutputs() const override {
+ // Technically the combinatoric set of all children
+ // Usually, this.text will be undefined anyway
+ return { nullopt };
+ }
+
+ mbgl::Value serialize() const override;
+ std::string getOperator() const override { return "format"; }
+private:
+ std::vector<FormatExpressionSection> sections;
+ std::unique_ptr<Expression> text;
+ optional<std::unique_ptr<Expression>> fontScale;
+ optional<std::unique_ptr<Expression>> textFont;
+};
+
+} // namespace expression
+} // namespace style
+} // namespace mbgl
diff --git a/include/mbgl/style/expression/formatted.hpp b/include/mbgl/style/expression/formatted.hpp
new file mode 100644
index 0000000000..9e7e7308cb
--- /dev/null
+++ b/include/mbgl/style/expression/formatted.hpp
@@ -0,0 +1,62 @@
+#pragma once
+
+#include <mbgl/style/conversion.hpp>
+#include <mbgl/util/font_stack.hpp>
+#include <mbgl/util/optional.hpp>
+#include <mbgl/util/variant.hpp>
+
+#include <vector>
+#include <string>
+
+namespace mbgl {
+namespace style {
+namespace expression {
+
+struct FormattedSection {
+ FormattedSection(std::string text_, optional<double> fontScale_, optional<FontStack> fontStack_)
+ : text(std::move(text_))
+ , fontScale(std::move(fontScale_))
+ , fontStack(std::move(fontStack_))
+ {}
+ std::string text;
+ optional<double> fontScale;
+ optional<FontStack> fontStack;
+};
+
+class Formatted {
+public:
+ Formatted() = default;
+
+ Formatted(const char* plainU8String) {
+ sections.emplace_back(std::string(plainU8String), nullopt, nullopt);
+ }
+
+ Formatted(std::vector<FormattedSection> sections_)
+ : sections(std::move(sections_))
+ {}
+
+ bool operator==(const Formatted& ) const;
+
+ std::string toString() const;
+
+ bool empty() const {
+ return sections.empty() || sections.at(0).text.empty();
+ }
+
+ std::vector<FormattedSection> sections;
+};
+
+} // namespace expression
+
+namespace conversion {
+
+template <>
+struct Converter<mbgl::style::expression::Formatted> {
+public:
+ optional<mbgl::style::expression::Formatted> operator()(const Convertible& value, Error& error) const;
+};
+
+} // namespace conversion
+
+} // namespace style
+} // namespace mbgl
diff --git a/include/mbgl/style/expression/type.hpp b/include/mbgl/style/expression/type.hpp
index 316496839b..a5a1e76164 100644
--- a/include/mbgl/style/expression/type.hpp
+++ b/include/mbgl/style/expression/type.hpp
@@ -66,6 +66,13 @@ struct CollatorType {
std::string getName() const { return "collator"; }
bool operator==(const CollatorType&) const { return true; }
};
+
+struct FormattedType {
+ constexpr FormattedType() {}; // NOLINT
+ std::string getName() const { return "formatted"; }
+ bool operator==(const FormattedType&) const { return true; }
+};
+
constexpr NullType Null;
constexpr NumberType Number;
@@ -75,6 +82,7 @@ constexpr ColorType Color;
constexpr ValueType Value;
constexpr ObjectType Object;
constexpr CollatorType Collator;
+constexpr FormattedType Formatted;
constexpr ErrorType Error;
struct Array;
@@ -89,6 +97,7 @@ using Type = variant<
ValueType,
mapbox::util::recursive_wrapper<Array>,
CollatorType,
+ FormattedType,
ErrorType>;
struct Array {
diff --git a/include/mbgl/style/expression/value.hpp b/include/mbgl/style/expression/value.hpp
index 2036ab8abe..91239d083f 100644
--- a/include/mbgl/style/expression/value.hpp
+++ b/include/mbgl/style/expression/value.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <mbgl/style/expression/collator.hpp>
+#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/style/expression/type.hpp>
#include <mbgl/style/position.hpp>
#include <mbgl/style/types.hpp>
@@ -25,6 +26,7 @@ using ValueBase = variant<
std::string,
Color,
Collator,
+ Formatted,
mapbox::util::recursive_wrapper<std::vector<Value>>,
mapbox::util::recursive_wrapper<std::unordered_map<std::string, Value>>>;
struct Value : ValueBase {