summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2018-07-11 16:44:09 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2018-07-12 09:42:55 -0700
commitf7e2fe5e3899a31dc3c36aeee59fdeaa46b570e4 (patch)
tree174a70e9c91a3b2236ed407f68a4707f4b8be262 /include
parente16c96446584f8ee36da4722eb86f40b9cbe5659 (diff)
downloadqtlocation-mapboxgl-f7e2fe5e3899a31dc3c36aeee59fdeaa46b570e4.tar.gz
[core] Simplify Value conversions
Use a single template function for each of {to,from}ExpressionValue, always dispatching to ValueConverter. Remove unused ValueConverter::ExpressionType typedefs.
Diffstat (limited to 'include')
-rw-r--r--include/mbgl/style/expression/value.hpp64
1 files changed, 23 insertions, 41 deletions
diff --git a/include/mbgl/style/expression/value.hpp b/include/mbgl/style/expression/value.hpp
index fc38c36ff0..b1126118a9 100644
--- a/include/mbgl/style/expression/value.hpp
+++ b/include/mbgl/style/expression/value.hpp
@@ -60,64 +60,39 @@ type::Type valueTypeToExpressionType();
Conversions between style value types and expression::Value
*/
-// no-op overloads
-Value toExpressionValue(const Value&);
-
-// T = Value (just wrap in optional)
-template <typename T>
-std::enable_if_t<std::is_same<T, Value>::value,
-optional<T>> fromExpressionValue(const Value& v)
-{
- return optional<T>(v);
-}
-
-// T = member type of Value
-template <typename T>
-std::enable_if_t< std::is_convertible<T, Value>::value && !std::is_same<T, Value>::value,
-optional<T>> fromExpressionValue(const Value& v)
-{
- return v.template is<T>() ? v.template get<T>() : optional<T>();
-}
-
-// real conversions
-template <typename T, typename Enable = std::enable_if_t< !std::is_convertible<T, Value>::value >>
-Value toExpressionValue(const T& value);
-
-template <typename T>
-std::enable_if_t< !std::is_convertible<T, Value>::value,
-optional<T>> fromExpressionValue(const Value& v);
-
-
-
template <class T, class Enable = void>
struct ValueConverter {
- using ExpressionType = T;
-
static Value toExpressionValue(const T& value) {
return Value(value);
}
+
static optional<T> fromExpressionValue(const Value& value) {
return value.template is<T>() ? value.template get<T>() : optional<T>();
}
};
template <>
-struct ValueConverter<float> {
- using ExpressionType = double;
- static type::Type expressionType() { return type::Number; }
- static Value toExpressionValue(const float value);
- static optional<float> fromExpressionValue(const Value& value);
+struct ValueConverter<Value> {
+ static type::Type expressionType() { return type::Value; }
+ static Value toExpressionValue(const Value& value) { return value; }
+ static optional<Value> fromExpressionValue(const Value& value) { return value; }
};
-template<>
+template <>
struct ValueConverter<mbgl::Value> {
static Value toExpressionValue(const mbgl::Value& value);
static mbgl::Value fromExpressionValue(const Value& value);
};
+template <>
+struct ValueConverter<float> {
+ static type::Type expressionType() { return type::Number; }
+ static Value toExpressionValue(const float value);
+ static optional<float> fromExpressionValue(const Value& value);
+};
+
template <typename T, std::size_t N>
struct ValueConverter<std::array<T, N>> {
- using ExpressionType = std::vector<Value>;
static type::Type expressionType() {
return type::Array(valueTypeToExpressionType<T>(), N);
}
@@ -127,7 +102,6 @@ struct ValueConverter<std::array<T, N>> {
template <typename T>
struct ValueConverter<std::vector<T>> {
- using ExpressionType = std::vector<Value>;
static type::Type expressionType() {
return type::Array(valueTypeToExpressionType<T>());
}
@@ -137,7 +111,6 @@ struct ValueConverter<std::vector<T>> {
template <>
struct ValueConverter<Position> {
- using ExpressionType = std::vector<Value>;
static type::Type expressionType() { return type::Array(type::Number, 3); }
static Value toExpressionValue(const mbgl::style::Position& value);
static optional<Position> fromExpressionValue(const Value& v);
@@ -145,13 +118,22 @@ struct ValueConverter<Position> {
template <typename T>
struct ValueConverter<T, std::enable_if_t< std::is_enum<T>::value >> {
- using ExpressionType = std::string;
static type::Type expressionType() { return type::String; }
static Value toExpressionValue(const T& value);
static optional<T> fromExpressionValue(const Value& value);
};
template <typename T>
+Value toExpressionValue(const T& value) {
+ return ValueConverter<T>::toExpressionValue(value);
+}
+
+template <typename T>
+optional<T> fromExpressionValue(const Value& value) {
+ return ValueConverter<T>::fromExpressionValue(value);
+}
+
+template <typename T>
std::vector<optional<T>> fromExpressionValues(const std::vector<optional<Value>>& values) {
std::vector<optional<T>> result;
for (const auto& value : values) {