MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
value.hpp
Go to the documentation of this file.
1 #pragma once
2 
9 #include <mbgl/style/types.hpp>
10 #include <mbgl/util/color.hpp>
11 #include <mbgl/util/enum.hpp>
12 #include <mbgl/util/feature.hpp>
13 #include <mbgl/util/variant.hpp>
14 
15 #include <array>
16 #include <vector>
17 #include <optional>
18 
19 namespace mbgl {
20 namespace style {
21 namespace expression {
22 
23 struct Value;
24 
26  bool,
27  double,
29  Color,
30  Collator,
31  Formatted,
32  Image,
33  mapbox::util::recursive_wrapper<std::vector<Value>>,
34  mapbox::util::recursive_wrapper<std::unordered_map<std::string, Value>>>;
35 struct Value : ValueBase {
37 
38  VARIANT_INLINE Value() : ValueBase() {}
39 
40  template <typename T>
41  VARIANT_INLINE Value(T&& val) : ValueBase(val) {}
42 
43  // Javascript's Number.MAX_SAFE_INTEGER
44  static uint64_t maxSafeInteger() { return 9007199254740991ULL; }
45 
46  static bool isSafeInteger(uint64_t x) { return x <= maxSafeInteger(); };
47  static bool isSafeInteger(int64_t x) {
48  return static_cast<uint64_t>(x > 0 ? x : -x) <= maxSafeInteger();
49  }
50  static bool isSafeInteger(double x) {
51  return static_cast<uint64_t>(x > 0 ? x : -x) <= maxSafeInteger();
52  }
53 
54 };
55 
56 constexpr NullValue Null = NullValue();
57 
58 type::Type typeOf(const Value& value);
59 
60 std::string toString(const Value& value);
61 std::string stringify(const Value& value);
62 
63 /*
64  Returns a Type object representing the expression type that corresponds to
65  the value type T. (Specialized for primitives and specific array types in
66  the .cpp.)
67 */
68 template <typename T>
70 
71 /*
72  Conversions between style value types and expression::Value
73 */
74 
75 template <class T, class Enable = void>
77  static Value toExpressionValue(const T& value) {
78  return Value(value);
79  }
80 
81  static std::optional<T> fromExpressionValue(const Value& value) {
82  return value.template is<T>() ? value.template get<T>() : std::optional<T>();
83  }
84 };
85 
86 template <>
88  static type::Type expressionType() { return type::Value; }
89  static Value toExpressionValue(const Value& value) { return value; }
90  static std::optional<Value> fromExpressionValue(const Value& value) { return value; }
91 };
92 
93 template <>
95  static Value toExpressionValue(const mbgl::Value& value);
96  static mbgl::Value fromExpressionValue(const Value& value);
97 };
98 
99 template <>
100 struct ValueConverter<float> {
102  static Value toExpressionValue(float value);
103  static std::optional<float> fromExpressionValue(const Value& value);
104 };
105 
106 template <typename T, std::size_t N>
107 struct ValueConverter<std::array<T, N>> {
108  static type::Type expressionType() {
109  return type::Array(valueTypeToExpressionType<T>(), N);
110  }
111  static Value toExpressionValue(const std::array<T, N>& value);
112  static std::optional<std::array<T, N>> fromExpressionValue(const Value& value);
113 };
114 
115 template <typename T>
116 struct ValueConverter<std::vector<T>> {
117  static type::Type expressionType() {
118  return type::Array(valueTypeToExpressionType<T>());
119  }
120  static Value toExpressionValue(const std::vector<T>& value);
121  static std::optional<std::vector<T>> fromExpressionValue(const Value& value);
122 };
123 
124 template <>
128  static std::optional<Position> fromExpressionValue(const Value& v);
129 };
130 
131 template <typename T>
132 struct ValueConverter<T, std::enable_if_t< std::is_enum_v<T> >> {
133  static type::Type expressionType() { return type::String; }
134  static Value toExpressionValue(const T& value);
135  static std::optional<T> fromExpressionValue(const Value& value);
136 };
137 
138 template <typename T>
139 Value toExpressionValue(const T& value) {
141 }
142 
143 template <typename T>
144 std::optional<T> fromExpressionValue(const Value& value) {
146 }
147 
148 template <typename T>
149 std::vector<std::optional<T>> fromExpressionValues(const std::vector<std::optional<Value>>& values) {
150  std::vector<std::optional<T>> result;
151  result.reserve(values.size());
152  for (const auto& value : values) {
153  result.push_back(value ? fromExpressionValue<T>(*value) : std::nullopt);
154  }
155  return result;
156 }
157 
158 template <>
162  static std::optional<Rotation> fromExpressionValue(const Value& v);
163 };
164 
165 } // namespace expression
166 } // namespace style
167 } // namespace mbgl
std::unique_ptr< Expression > string(std::unique_ptr< Expression >, std::unique_ptr< Expression > def=nullptr)
constexpr ValueType Value
Definition: type.hpp:88
constexpr NumberType Number
Definition: type.hpp:84
constexpr ColorType Color
Definition: type.hpp:87
constexpr CollatorType Collator
Definition: type.hpp:90
constexpr StringType String
Definition: type.hpp:85
constexpr FormattedType Formatted
Definition: type.hpp:91
variant< NullType, NumberType, BooleanType, StringType, ColorType, ObjectType, ValueType, mapbox::util::recursive_wrapper< Array >, CollatorType, FormattedType, ErrorType, ImageType > Type
Definition: type.hpp:108
constexpr ImageType Image
Definition: type.hpp:93
std::vector< std::optional< T > > fromExpressionValues(const std::vector< std::optional< Value >> &values)
Definition: value.hpp:149
std::string stringify(const Value &value)
Value toExpressionValue(const T &value)
Definition: value.hpp:139
constexpr NullValue Null
Definition: value.hpp:56
std::string toString(const Value &value)
variant< NullValue, bool, double, std::string, Color, Collator, Formatted, Image, mapbox::util::recursive_wrapper< std::vector< Value > >, mapbox::util::recursive_wrapper< std::unordered_map< std::string, Value > >> ValueBase
Definition: value.hpp:34
type::Type typeOf(const Value &value)
std::optional< T > fromExpressionValue(const Value &value)
Definition: value.hpp:144
type::Type valueTypeToExpressionType()
Definition: actor.hpp:15
mapbox::base::NullValue NullValue
Definition: feature.hpp:12
mapbox::base::Value Value
Definition: feature.hpp:11
mapbox::util::variant< T... > variant
Definition: variant.hpp:17
Definition: tile_id.hpp:256
static Value toExpressionValue(const mbgl::style::Position &value)
static std::optional< Position > fromExpressionValue(const Value &v)
static std::optional< Rotation > fromExpressionValue(const Value &v)
static Value toExpressionValue(const mbgl::style::Rotation &value)
static std::optional< Value > fromExpressionValue(const Value &value)
Definition: value.hpp:90
static Value toExpressionValue(const Value &value)
Definition: value.hpp:89
static std::optional< float > fromExpressionValue(const Value &value)
static mbgl::Value fromExpressionValue(const Value &value)
static Value toExpressionValue(const mbgl::Value &value)
static Value toExpressionValue(const T &value)
Definition: value.hpp:77
static std::optional< T > fromExpressionValue(const Value &value)
Definition: value.hpp:81
static uint64_t maxSafeInteger()
Definition: value.hpp:44
static bool isSafeInteger(double x)
Definition: value.hpp:50
static bool isSafeInteger(int64_t x)
Definition: value.hpp:47
VARIANT_INLINE Value(T &&val)
Definition: value.hpp:41
VARIANT_INLINE Value()
Definition: value.hpp:38
static bool isSafeInteger(uint64_t x)
Definition: value.hpp:46