From 8e9714fe3dae9725011083096bdd1b115e9c1a61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20DELRIEU?= Date: Mon, 14 Aug 2017 18:14:27 +0200 Subject: [PATCH] add detail/json_ref.hpp --- Makefile | 3 +- src/detail/json_ref.hpp | 66 +++++++++++++++++++++++++++++++++++++++++ src/json.hpp | 58 +----------------------------------- 3 files changed, 69 insertions(+), 58 deletions(-) create mode 100644 src/detail/json_ref.hpp diff --git a/Makefile b/Makefile index b646081d7..a2c73dbfb 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,8 @@ SRCS = ${SRCDIR}/json.hpp \ ${SRCDIR}/detail/parsing/output_adapters.hpp \ ${SRCDIR}/detail/parsing/binary_reader.hpp \ ${SRCDIR}/detail/parsing/binary_writer.hpp \ - ${SRCDIR}/detail/serializer.hpp + ${SRCDIR}/detail/serializer.hpp \ + ${SRCDIR}/detail/json_ref.hpp # main target all: diff --git a/src/detail/json_ref.hpp b/src/detail/json_ref.hpp new file mode 100644 index 000000000..ec120c026 --- /dev/null +++ b/src/detail/json_ref.hpp @@ -0,0 +1,66 @@ +#ifndef NLOHMANN_JSON_DETAIL_JSON_REF_HPP +#define NLOHMANN_JSON_DETAIL_JSON_REF_HPP + +#include +#include + +namespace nlohmann +{ +namespace detail +{ +template +class json_ref +{ + public: + using value_type = BasicJsonType; + + json_ref(value_type&& value) + : owned_value(std::move(value)), value_ref(&owned_value), is_rvalue(true) + {} + + json_ref(const value_type& value) + : value_ref(const_cast(&value)), is_rvalue(false) + {} + + json_ref(std::initializer_list init) + : owned_value(init), value_ref(&owned_value), is_rvalue(true) + {} + + template + json_ref(Args&& ... args) + : owned_value(std::forward(args)...), value_ref(&owned_value), is_rvalue(true) + {} + + // class should be movable only + json_ref(json_ref&&) = default; + json_ref(const json_ref&) = delete; + json_ref& operator=(const json_ref&) = delete; + + value_type moved_or_copied() const + { + if (is_rvalue) + { + return std::move(*value_ref); + } + return *value_ref; + } + + value_type const& operator*() const + { + return *static_cast(value_ref); + } + + value_type const* operator->() const + { + return static_cast(value_ref); + } + + private: + mutable value_type owned_value = nullptr; + value_type* value_ref = nullptr; + const bool is_rvalue; +}; +} +} + +#endif diff --git a/src/json.hpp b/src/json.hpp index d52d6e838..32051d7e0 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -69,6 +69,7 @@ SOFTWARE. #include "detail/parsing/binary_reader.hpp" #include "detail/parsing/binary_writer.hpp" #include "detail/serializer.hpp" +#include "detail/json_ref.hpp" /*! @brief namespace for Niels Lohmann @@ -77,63 +78,6 @@ SOFTWARE. */ namespace nlohmann { -namespace detail -{ -template -class json_ref -{ - public: - using value_type = BasicJsonType; - - json_ref(value_type&& value) - : owned_value(std::move(value)), value_ref(&owned_value), is_rvalue(true) - {} - - json_ref(const value_type& value) - : value_ref(const_cast(&value)), is_rvalue(false) - {} - - json_ref(std::initializer_list init) - : owned_value(init), value_ref(&owned_value), is_rvalue(true) - {} - - template - json_ref(Args&& ... args) - : owned_value(std::forward(args)...), value_ref(&owned_value), is_rvalue(true) - {} - - // class should be movable only - json_ref(json_ref&&) = default; - json_ref(const json_ref&) = delete; - json_ref& operator=(const json_ref&) = delete; - - value_type moved_or_copied() const - { - if (is_rvalue) - { - return std::move(*value_ref); - } - return *value_ref; - } - - value_type const& operator*() const - { - return *static_cast(value_ref); - } - - value_type const* operator->() const - { - return static_cast(value_ref); - } - - private: - mutable value_type owned_value = nullptr; - value_type* value_ref = nullptr; - const bool is_rvalue; -}; - -} // namespace detail - template struct adl_serializer { -- GitLab