• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //     __ _____ _____ _____
2 //  __|  |   __|     |   | |  JSON for Modern C++
3 // |  |  |__   |  |  | | | |  version 3.11.2
4 // |_____|_____|_____|_|___|  https://github.com/nlohmann/json
5 //
6 // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
7 // SPDX-License-Identifier: MIT
8 
9 #pragma once
10 
11 #include <initializer_list>
12 #include <utility>
13 
14 #include <nlohmann/detail/abi_macros.hpp>
15 #include <nlohmann/detail/meta/type_traits.hpp>
16 
17 NLOHMANN_JSON_NAMESPACE_BEGIN
18 namespace detail
19 {
20 
21 template<typename BasicJsonType>
22 class json_ref
23 {
24   public:
25     using value_type = BasicJsonType;
26 
json_ref(value_type && value)27     json_ref(value_type&& value)
28         : owned_value(std::move(value))
29     {}
30 
json_ref(const value_type & value)31     json_ref(const value_type& value)
32         : value_ref(&value)
33     {}
34 
json_ref(std::initializer_list<json_ref> init)35     json_ref(std::initializer_list<json_ref> init)
36         : owned_value(init)
37     {}
38 
39     template <
40         class... Args,
41         enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
json_ref(Args &&...args)42     json_ref(Args && ... args)
43         : owned_value(std::forward<Args>(args)...)
44     {}
45 
46     // class should be movable only
47     json_ref(json_ref&&) noexcept = default;
48     json_ref(const json_ref&) = delete;
49     json_ref& operator=(const json_ref&) = delete;
50     json_ref& operator=(json_ref&&) = delete;
51     ~json_ref() = default;
52 
moved_or_copied() const53     value_type moved_or_copied() const
54     {
55         if (value_ref == nullptr)
56         {
57             return std::move(owned_value);
58         }
59         return *value_ref;
60     }
61 
operator *() const62     value_type const& operator*() const
63     {
64         return value_ref ? *value_ref : owned_value;
65     }
66 
operator ->() const67     value_type const* operator->() const
68     {
69         return &** this;
70     }
71 
72   private:
73     mutable value_type owned_value = nullptr;
74     value_type const* value_ref = nullptr;
75 };
76 
77 }  // namespace detail
78 NLOHMANN_JSON_NAMESPACE_END
79