diff options
author | Daniel Friesel <derf@finalrewind.org> | 2018-09-17 11:10:12 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2018-09-17 11:10:12 +0200 |
commit | 276b9a93c2e3b9ac58a9ddb5fff4c8299c459222 (patch) | |
tree | 8b39c09a58a8c3225b234b2b69af08a76d31f9a8 /include/lib/modernjson/detail/json_ref.hpp | |
parent | 99cea89d798b61398adf33cbc57c11d985ddab77 (diff) |
add nlohmann modenjson
Diffstat (limited to 'include/lib/modernjson/detail/json_ref.hpp')
-rw-r--r-- | include/lib/modernjson/detail/json_ref.hpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/include/lib/modernjson/detail/json_ref.hpp b/include/lib/modernjson/detail/json_ref.hpp new file mode 100644 index 0000000..8285be2 --- /dev/null +++ b/include/lib/modernjson/detail/json_ref.hpp @@ -0,0 +1,63 @@ +#pragma once + +#include <initializer_list> +#include <utility> + +namespace nlohmann +{ +namespace detail +{ +template<typename BasicJsonType> +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_type*>(&value)), is_rvalue(false) + {} + + json_ref(std::initializer_list<json_ref> init) + : owned_value(init), value_ref(&owned_value), is_rvalue(true) + {} + + template<class... Args> + json_ref(Args&& ... args) + : owned_value(std::forward<Args>(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_type const*>(value_ref); + } + + value_type const* operator->() const + { + return static_cast<value_type const*>(value_ref); + } + + private: + mutable value_type owned_value = nullptr; + value_type* value_ref = nullptr; + const bool is_rvalue; +}; +} +} |