# HG changeset patch # User David Demelier # Date 1540318380 -7200 # Node ID 3129f59002d23c0cd555df0702e677dddea8deba # Parent 5bd9424a523ab39bf94f29596bdea753d30d5db9 json_util: use std::optional diff -r 5bd9424a523a -r 3129f59002d2 cpp/json_util/json_util.hpp --- a/cpp/json_util/json_util.hpp Thu Oct 04 21:17:55 2018 +0200 +++ b/cpp/json_util/json_util.hpp Tue Oct 23 20:13:00 2018 +0200 @@ -26,11 +26,10 @@ #include #include +#include #include #include -#include - #include /** @@ -47,15 +46,15 @@ template class parser_type_traits_uint : public std::true_type { public: - static boost::optional get(const nlohmann::json& value) noexcept + static std::optional get(const nlohmann::json& value) noexcept { if (!value.is_number_unsigned()) - return boost::none; + return std::nullopt; const auto ret = value.get(); if (ret > std::numeric_limits::max()) - return boost::none; + return std::nullopt; return static_cast(ret); } @@ -64,15 +63,15 @@ template class parser_type_traits_int : public std::true_type { public: - static boost::optional get(const nlohmann::json& value) noexcept + static std::optional get(const nlohmann::json& value) noexcept { if (!value.is_number_integer()) - return boost::none; + return std::nullopt; const auto ret = value.get(); if (ret < std::numeric_limits::min() || ret > std::numeric_limits::max()) - return boost::none; + return std::nullopt; return static_cast(ret); } @@ -93,7 +92,7 @@ * You only need to implement the get function with the following signature: * * ```cpp - * static boost::optional get(const nlohmann::json& value); + * static std::optional get(const nlohmann::json& value); * ``` * * The implementation should not throw an exception but return a null optional @@ -122,10 +121,10 @@ * \param value the value * \return the bool or none if not a boolean type */ - static boost::optional get(const nlohmann::json& value) noexcept + static std::optional get(const nlohmann::json& value) noexcept { if (!value.is_boolean()) - return boost::none; + return std::nullopt; return value.get(); } @@ -143,10 +142,10 @@ * \param value the value * \return the double or none if not a double type */ - static boost::optional get(const nlohmann::json& value) noexcept + static std::optional get(const nlohmann::json& value) noexcept { if (!value.is_number_float()) - return boost::none; + return std::nullopt; return value.get(); } @@ -164,10 +163,10 @@ * \param value the value * \return the string or none if not a string type */ - static boost::optional get(const nlohmann::json& value) + static std::optional get(const nlohmann::json& value) { if (!value.is_string()) - return boost::none; + return std::nullopt; return value.get(); } @@ -206,10 +205,10 @@ * \param value the value * \return the int or none if not a int type */ - static boost::optional get(const nlohmann::json& value) noexcept + static std::optional get(const nlohmann::json& value) noexcept { if (!value.is_number_integer()) - return boost::none; + return std::nullopt; return value.get(); } @@ -248,10 +247,10 @@ * \param value the value * \return the int or none if not a int type */ - static boost::optional get(const nlohmann::json& value) noexcept + static std::optional get(const nlohmann::json& value) noexcept { if (!value.is_number_unsigned()) - return boost::none; + return std::nullopt; return value.get(); } @@ -274,17 +273,17 @@ * Get a value from the document object. * * \param key the property key - * \return the value or boost::none if not found or not convertible + * \return the value or std::nullopt if not found or not convertible */ template - inline boost::optional get(const std::string& key) const noexcept + inline std::optional get(const std::string& key) const noexcept { static_assert(parser_type_traits::value, "type not supported"); const auto it = find(key); if (it == end()) - return boost::none; + return std::nullopt; return parser_type_traits::get(*it); } @@ -293,21 +292,21 @@ * Get an optional value from the document object. * * If the value is undefined, the default value is returned. Otherwise, if - * the value is not in the given type, boost::none is returned. + * the value is not in the given type, std::nullopt is returned. * * \param key the property key * \param def the default value if property is undefined - * \return the value, boost::none or def + * \return the value, std::nullopt or def */ template - inline boost::optional optional(const std::string& key, DefaultValue&& def) const noexcept + inline std::optional optional(const std::string& key, DefaultValue&& def) const noexcept { static_assert(parser_type_traits::value, "type not supported"); const auto it = find(key); if (it == end()) - return boost::optional(std::forward(def)); + return std::optional(std::forward(def)); return parser_type_traits::get(*it); } diff -r 5bd9424a523a -r 3129f59002d2 cpp/json_util/test/main.cpp --- a/cpp/json_util/test/main.cpp Thu Oct 04 21:17:55 2018 +0200 +++ b/cpp/json_util/test/main.cpp Tue Oct 23 20:13:00 2018 +0200 @@ -18,10 +18,24 @@ #define BOOST_TEST_MODULE "json_util" #include -#include #include +namespace std { + +template +auto operator<<(ostream& out, const optional& value) -> ostream& +{ + if (!value) + out << ""; + else + out << *value; + + return out; +} + +} // !std + namespace json_util { class test_fixture {