1// __ _____ _____ _____
2// __| | __| | | | JSON for Modern C++
3// | | |__ | | | | | | version 3.11.3
4// |_____|_____|_____|_|___| https://github.com/nlohmann/json
5//
6// SPDX-FileCopyrightText: 2013-2023 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
17NLOHMANN_JSON_NAMESPACE_BEGIN
18namespace detail
19{
20
21template<typename BasicJsonType>
22class json_ref
23{
24 public:
25 using value_type = BasicJsonType;
26
27 json_ref(value_type&& value)
28 : owned_value(std::move(value))
29 {}
30
31 json_ref(const value_type& value)
32 : value_ref(&value)
33 {}
34
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 >
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
53 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
62 value_type const& operator*() const
63 {
64 return value_ref ? *value_ref : owned_value;
65 }
66
67 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
78NLOHMANN_JSON_NAMESPACE_END
79