changeset 649:3129f59002d2

json_util: use std::optional
author David Demelier <markand@malikania.fr>
date Tue, 23 Oct 2018 20:13:00 +0200
parents 5bd9424a523a
children ff73f2dd1c82
files cpp/json_util/json_util.hpp cpp/json_util/test/main.cpp
diffstat 2 files changed, 40 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- 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 <cstdint>
 #include <limits>
+#include <optional>
 #include <string>
 #include <type_traits>
 
-#include <boost/optional.hpp>
-
 #include <json.hpp>
 
 /**
@@ -47,15 +46,15 @@
 template <typename Int>
 class parser_type_traits_uint : public std::true_type {
 public:
-	static boost::optional<Int> get(const nlohmann::json& value) noexcept
+	static std::optional<Int> get(const nlohmann::json& value) noexcept
 	{
 		if (!value.is_number_unsigned())
-			return boost::none;
+			return std::nullopt;
 
 		const auto ret = value.get<std::uint64_t>();
 
 		if (ret > std::numeric_limits<Int>::max())
-			return boost::none;
+			return std::nullopt;
 
 		return static_cast<Int>(ret);
 	}
@@ -64,15 +63,15 @@
 template <typename Int>
 class parser_type_traits_int : public std::true_type {
 public:
-	static boost::optional<Int> get(const nlohmann::json& value) noexcept
+	static std::optional<Int> get(const nlohmann::json& value) noexcept
 	{
 		if (!value.is_number_integer())
-			return boost::none;
+			return std::nullopt;
 
 		const auto ret = value.get<std::int64_t>();
 
 		if (ret < std::numeric_limits<Int>::min() || ret > std::numeric_limits<Int>::max())
-			return boost::none;
+			return std::nullopt;
 
 		return static_cast<Int>(ret);
 	}
@@ -93,7 +92,7 @@
  * You only need to implement the get function with the following signature:
  *
  * ```cpp
- * static boost::optional<T> get(const nlohmann::json& value);
+ * static std::optional<T> 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<bool> get(const nlohmann::json& value) noexcept
+	static std::optional<bool> get(const nlohmann::json& value) noexcept
 	{
 		if (!value.is_boolean())
-			return boost::none;
+			return std::nullopt;
 
 		return value.get<bool>();
 	}
@@ -143,10 +142,10 @@
 	 * \param value the value
 	 * \return the double or none if not a double type
 	 */
-	static boost::optional<double> get(const nlohmann::json& value) noexcept
+	static std::optional<double> get(const nlohmann::json& value) noexcept
 	{
 		if (!value.is_number_float())
-			return boost::none;
+			return std::nullopt;
 
 		return value.get<double>();
 	}
@@ -164,10 +163,10 @@
 	 * \param value the value
 	 * \return the string or none if not a string type
 	 */
-	static boost::optional<std::string> get(const nlohmann::json& value)
+	static std::optional<std::string> get(const nlohmann::json& value)
 	{
 		if (!value.is_string())
-			return boost::none;
+			return std::nullopt;
 
 		return value.get<std::string>();
 	}
@@ -206,10 +205,10 @@
 	 * \param value the value
 	 * \return the int or none if not a int type
 	 */
-	static boost::optional<std::int64_t> get(const nlohmann::json& value) noexcept
+	static std::optional<std::int64_t> get(const nlohmann::json& value) noexcept
 	{
 		if (!value.is_number_integer())
-			return boost::none;
+			return std::nullopt;
 
 		return value.get<std::int64_t>();
 	}
@@ -248,10 +247,10 @@
 	 * \param value the value
 	 * \return the int or none if not a int type
 	 */
-	static boost::optional<std::uint64_t> get(const nlohmann::json& value) noexcept
+	static std::optional<std::uint64_t> get(const nlohmann::json& value) noexcept
 	{
 		if (!value.is_number_unsigned())
-			return boost::none;
+			return std::nullopt;
 
 		return value.get<std::uint64_t>();
 	}
@@ -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 <typename Type>
-	inline boost::optional<Type> get(const std::string& key) const noexcept
+	inline std::optional<Type> get(const std::string& key) const noexcept
 	{
 		static_assert(parser_type_traits<Type>::value, "type not supported");
 
 		const auto it = find(key);
 
 		if (it == end())
-			return boost::none;
+			return std::nullopt;
 
 		return parser_type_traits<Type>::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 <typename Type, typename DefaultValue>
-	inline boost::optional<Type> optional(const std::string& key, DefaultValue&& def) const noexcept
+	inline std::optional<Type> optional(const std::string& key, DefaultValue&& def) const noexcept
 	{
 		static_assert(parser_type_traits<Type>::value, "type not supported");
 
 		const auto it = find(key);
 
 		if (it == end())
-			return boost::optional<Type>(std::forward<DefaultValue>(def));
+			return std::optional<Type>(std::forward<DefaultValue>(def));
 
 		return parser_type_traits<Type>::get(*it);
 	}
--- 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 <boost/test/unit_test.hpp>
-#include <boost/optional/optional_io.hpp>
 
 #include <json_util.hpp>
 
+namespace std {
+
+template <typename T>
+auto operator<<(ostream& out, const optional<T>& value) -> ostream&
+{
+	if (!value)
+		out << "<empty>";
+	else
+		out << *value;
+
+	return out;
+}
+
+} // !std
+
 namespace json_util {
 
 class test_fixture {