From f210bbbb6c9532c5bdcf176e191bef9a0a9858c7 Mon Sep 17 00:00:00 2001 From: Ander Conselvan de Oliveira Date: Wed, 8 May 2019 14:00:48 +0300 Subject: [PATCH] [core] Don't use optional unique_ptr in function coversion code Since unique_ptr is already nullable there is no need to wrap it in an optional<>. --- include/mbgl/style/expression/dsl.hpp | 14 ++++++------- src/mbgl/style/conversion/function.cpp | 27 ++++++++++++++------------ src/mbgl/style/expression/dsl.cpp | 20 +++++++++---------- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/include/mbgl/style/expression/dsl.hpp b/include/mbgl/style/expression/dsl.hpp index dc0f1ab90..bcab999ab 100644 --- a/include/mbgl/style/expression/dsl.hpp +++ b/include/mbgl/style/expression/dsl.hpp @@ -32,20 +32,20 @@ std::unique_ptr literal(std::initializer_list value); std::unique_ptr literal(std::initializer_list value); std::unique_ptr assertion(type::Type, std::unique_ptr, - optional> def = {}); + std::unique_ptr def = nullptr); std::unique_ptr number(std::unique_ptr, - optional> def = {}); + std::unique_ptr def = nullptr); std::unique_ptr string(std::unique_ptr, - optional> def = {}); + std::unique_ptr def = nullptr); std::unique_ptr boolean(std::unique_ptr, - optional> def = {}); + std::unique_ptr def = nullptr); std::unique_ptr toColor(std::unique_ptr, - optional> def = {}); + std::unique_ptr def = nullptr); std::unique_ptr toString(std::unique_ptr, - optional> def = {}); + std::unique_ptr def = nullptr); std::unique_ptr toFormatted(std::unique_ptr, - optional> def = {}); + std::unique_ptr def = nullptr); std::unique_ptr get(const char* value); std::unique_ptr get(std::unique_ptr); diff --git a/src/mbgl/style/conversion/function.cpp b/src/mbgl/style/conversion/function.cpp index e6d23268b..086bc4e27 100644 --- a/src/mbgl/style/conversion/function.cpp +++ b/src/mbgl/style/conversion/function.cpp @@ -452,7 +452,7 @@ template std::unique_ptr categorical(type::Type type, const std::string& property, std::map> branches, - optional> def) { + std::unique_ptr def) { std::unordered_map> convertedBranches; for (auto& b : branches) { convertedBranches[b.first] = std::move(b.second); @@ -460,14 +460,14 @@ std::unique_ptr categorical(type::Type type, return std::make_unique>(type, get(literal(property)), std::move(convertedBranches), - def ? std::move(*def) : error("replaced with default")); + def ? std::move(def) : error("replaced with default")); } template <> std::unique_ptr categorical(type::Type type, const std::string& property, std::map> branches, - optional> def) { + std::unique_ptr def) { auto it = branches.find(true); std::unique_ptr trueCase = it == branches.end() ? error("replaced with default") : @@ -483,13 +483,13 @@ std::unique_ptr categorical(type::Type type, convertedBranches.emplace_back(eq(get(literal(property)), literal(Value(false))), std::move(falseCase)); return std::make_unique(type, std::move(convertedBranches), - def ? std::move(*def) : error("replaced with default")); + def ? std::move(def) : error("replaced with default")); } static std::unique_ptr numberOrDefault(type::Type type, std::unique_ptr get, std::unique_ptr expr, - optional> def) { + std::unique_ptr def) { if (!def) { return expr; } @@ -497,14 +497,14 @@ static std::unique_ptr numberOrDefault(type::Type type, std::vector branches; branches.emplace_back(eq(compound("typeof", std::move(get)), literal("number")), std::move(expr)); - return std::make_unique(type, std::move(branches), std::move(*def)); + return std::make_unique(type, std::move(branches), std::move(def)); } static optional> convertIntervalFunction(type::Type type, const Convertible& value, Error& error, std::function (bool)> makeInput, - optional> def, + std::unique_ptr def, bool convertTokens = false) { auto stops = convertStops(type, value, error, convertTokens); if (!stops) { @@ -522,7 +522,7 @@ static optional> convertExponentialFunction(type::Ty const Convertible& value, Error& error, std::function (bool)> makeInput, - optional> def, + std::unique_ptr def, bool convertTokens = false) { auto stops = convertStops(type, value, error, convertTokens); if (!stops) { @@ -543,7 +543,7 @@ static optional> convertCategoricalFunction(type::Ty const Convertible& value, Error& err, const std::string& property, - optional> def) { + std::unique_ptr def) { auto stopsValue = objectMember(value, "stops"); if (!stopsValue) { err.message = "function value must specify stops"; @@ -709,12 +709,15 @@ optional> convertFunctionToExpression(type::Type typ } } - auto defaultExpr = [&]() -> optional> { + auto defaultExpr = [&]() -> std::unique_ptr { auto member = objectMember(value, "default"); if (member) { - return convertLiteral(type, *member, err); + auto literal = convertLiteral(type, *member, err); + if (literal) { + return std::move(*literal); + } } - return {}; + return nullptr; }; if (!objectMember(value, "property")) { diff --git a/src/mbgl/style/expression/dsl.cpp b/src/mbgl/style/expression/dsl.cpp index dd4aa7d27..70442de96 100644 --- a/src/mbgl/style/expression/dsl.cpp +++ b/src/mbgl/style/expression/dsl.cpp @@ -52,51 +52,51 @@ std::unique_ptr literal(std::initializer_list value) { std::unique_ptr assertion(type::Type type, std::unique_ptr value, - optional> def) { + std::unique_ptr def) { std::vector> v = vec(std::move(value)); if (def) { - v.push_back(std::move(*def)); + v.push_back(std::move(def)); } return std::make_unique(type, std::move(v)); } std::unique_ptr number(std::unique_ptr value, - optional> def) { + std::unique_ptr def) { return assertion(type::Number, std::move(value), std::move(def)); } std::unique_ptr string(std::unique_ptr value, - optional> def) { + std::unique_ptr def) { return assertion(type::String, std::move(value), std::move(def)); } std::unique_ptr boolean(std::unique_ptr value, - optional> def) { + std::unique_ptr def) { return assertion(type::Boolean, std::move(value), std::move(def)); } std::unique_ptr coercion(type::Type type, std::unique_ptr value, - optional> def) { + std::unique_ptr def) { std::vector> v = vec(std::move(value)); if (def) { - v.push_back(std::move(*def)); + v.push_back(std::move(def)); } return std::make_unique(type, std::move(v)); } std::unique_ptr toColor(std::unique_ptr value, - optional> def) { + std::unique_ptr def) { return coercion(type::Color, std::move(value), std::move(def)); } std::unique_ptr toString(std::unique_ptr value, - optional> def) { + std::unique_ptr def) { return coercion(type::String, std::move(value), std::move(def)); } std::unique_ptr toFormatted(std::unique_ptr value, - optional> def) { + std::unique_ptr def) { return coercion(type::Formatted, std::move(value), std::move(def)); }