[core] Simplify Value conversions

Use a single template function for each of {to,from}ExpressionValue, always dispatching to ValueConverter.

Remove unused ValueConverter::ExpressionType typedefs.
This commit is contained in:
John Firebaugh
2018-07-11 16:44:09 -07:00
parent e16c964465
commit f7e2fe5e38
2 changed files with 42 additions and 107 deletions

View File

@@ -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,12 +118,21 @@ 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;