From d27c3bd56adbb55db23a757fdc1c7270a7264c65 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 11 Jul 2018 16:44:09 -0700 Subject: [core] Simplify Value conversions Use a single template function for each of {to,from}ExpressionValue, always dispatching to ValueConverter. Remove unused ValueConverter::ExpressionType typedefs. --- include/mbgl/style/expression/value.hpp | 64 +++++++++---------------- src/mbgl/style/expression/value.cpp | 85 ++++++++------------------------- 2 files changed, 42 insertions(+), 107 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 -std::enable_if_t::value, -optional> fromExpressionValue(const Value& v) -{ - return optional(v); -} - -// T = member type of Value -template -std::enable_if_t< std::is_convertible::value && !std::is_same::value, -optional> fromExpressionValue(const Value& v) -{ - return v.template is() ? v.template get() : optional(); -} - -// real conversions -template ::value >> -Value toExpressionValue(const T& value); - -template -std::enable_if_t< !std::is_convertible::value, -optional> fromExpressionValue(const Value& v); - - - template struct ValueConverter { - using ExpressionType = T; - static Value toExpressionValue(const T& value) { return Value(value); } + static optional fromExpressionValue(const Value& value) { return value.template is() ? value.template get() : optional(); } }; template <> -struct ValueConverter { - using ExpressionType = double; - static type::Type expressionType() { return type::Number; } - static Value toExpressionValue(const float value); - static optional fromExpressionValue(const Value& value); +struct ValueConverter { + static type::Type expressionType() { return type::Value; } + static Value toExpressionValue(const Value& value) { return value; } + static optional fromExpressionValue(const Value& value) { return value; } }; -template<> +template <> struct ValueConverter { static Value toExpressionValue(const mbgl::Value& value); static mbgl::Value fromExpressionValue(const Value& value); }; +template <> +struct ValueConverter { + static type::Type expressionType() { return type::Number; } + static Value toExpressionValue(const float value); + static optional fromExpressionValue(const Value& value); +}; + template struct ValueConverter> { - using ExpressionType = std::vector; static type::Type expressionType() { return type::Array(valueTypeToExpressionType(), N); } @@ -127,7 +102,6 @@ struct ValueConverter> { template struct ValueConverter> { - using ExpressionType = std::vector; static type::Type expressionType() { return type::Array(valueTypeToExpressionType()); } @@ -137,7 +111,6 @@ struct ValueConverter> { template <> struct ValueConverter { - using ExpressionType = std::vector; static type::Type expressionType() { return type::Array(type::Number, 3); } static Value toExpressionValue(const mbgl::style::Position& value); static optional fromExpressionValue(const Value& v); @@ -145,12 +118,21 @@ struct ValueConverter { template struct ValueConverter::value >> { - using ExpressionType = std::string; static type::Type expressionType() { return type::String; } static Value toExpressionValue(const T& value); static optional fromExpressionValue(const Value& value); }; +template +Value toExpressionValue(const T& value) { + return ValueConverter::toExpressionValue(value); +} + +template +optional fromExpressionValue(const Value& value) { + return ValueConverter::fromExpressionValue(value); +} + template std::vector> fromExpressionValues(const std::vector>& values) { std::vector> result; diff --git a/src/mbgl/style/expression/value.cpp b/src/mbgl/style/expression/value.cpp index b1b05ea617..ddf1ff0ca4 100644 --- a/src/mbgl/style/expression/value.cpp +++ b/src/mbgl/style/expression/value.cpp @@ -241,27 +241,6 @@ optional ValueConverter::value >>::fromE ); } - -Value toExpressionValue(const Value& v) { - return v; -} - -template -Value toExpressionValue(const T& value) { - return ValueConverter::toExpressionValue(value); -} - -optional fromExpressionValue(const Value& v) { - return optional(v); -} - -template -std::enable_if_t< !std::is_convertible::value, -optional> fromExpressionValue(const Value& v) -{ - return ValueConverter::fromExpressionValue(v); -} - template type::Type valueTypeToExpressionType() { return ValueConverter::expressionType(); @@ -281,90 +260,64 @@ template <> type::Type valueTypeToExpressionType>() { return template <> type::Type valueTypeToExpressionType() { return type::Error; } -template Value toExpressionValue(const mbgl::Value&); -template optional fromExpressionValue(const Value&); - // for to_rgba expression template type::Type valueTypeToExpressionType>(); -template optional> fromExpressionValue>(const Value&); -template Value toExpressionValue(const std::array&); +template struct ValueConverter>; // layout/paint property types template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const float&); +template type::Type valueTypeToExpressionType(); template type::Type valueTypeToExpressionType>(); -template optional> fromExpressionValue>(const Value&); -template Value toExpressionValue(const std::array&); +template struct ValueConverter>; template type::Type valueTypeToExpressionType>(); -template optional> fromExpressionValue>(const Value&); -template Value toExpressionValue(const std::array&); +template struct ValueConverter>; template type::Type valueTypeToExpressionType>(); -template optional> fromExpressionValue>(const Value&); -template Value toExpressionValue(const std::vector&); +template struct ValueConverter>; template type::Type valueTypeToExpressionType>(); -template optional> fromExpressionValue>(const Value&); -template Value toExpressionValue(const std::vector&); +template struct ValueConverter>; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const AlignmentType&); +template struct ValueConverter; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const CirclePitchScaleType&); +template struct ValueConverter; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const IconTextFitType&); +template struct ValueConverter; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const LineCapType&); +template struct ValueConverter; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const LineJoinType&); +template struct ValueConverter; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const SymbolPlacementType&); +template struct ValueConverter; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const SymbolAnchorType&); +template struct ValueConverter; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const TextJustifyType&); +template struct ValueConverter; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const TextTransformType&); +template struct ValueConverter; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const TranslateAnchorType&); +template struct ValueConverter; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const RasterResamplingType&); +template struct ValueConverter; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const HillshadeIlluminationAnchorType&); +template struct ValueConverter; template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const LightAnchorType&); - -template type::Type valueTypeToExpressionType(); -template optional fromExpressionValue(const Value&); -template Value toExpressionValue(const Position&); +template struct ValueConverter; } // namespace expression } // namespace style -- cgit v1.2.1