MushMachine/external/json/include/nlohmann/detail/json_ref.hpp

79 lines
1.8 KiB
C++
Raw Normal View History

2022-08-02 14:28:39 +02:00
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
2022-10-30 02:36:30 +02:00
// | | |__ | | | | | | version 3.11.2
2022-08-02 14:28:39 +02:00
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
#pragma once
#include <initializer_list>
#include <utility>
2022-08-02 14:28:39 +02:00
#include <nlohmann/detail/abi_macros.hpp>
#include <nlohmann/detail/meta/type_traits.hpp>
2022-08-02 14:28:39 +02:00
NLOHMANN_JSON_NAMESPACE_BEGIN
namespace detail
{
2022-08-02 14:28:39 +02:00
template<typename BasicJsonType>
class json_ref
{
public:
using value_type = BasicJsonType;
json_ref(value_type&& value)
: owned_value(std::move(value))
{}
json_ref(const value_type& value)
2021-12-11 23:32:21 +01:00
: value_ref(&value)
{}
json_ref(std::initializer_list<json_ref> init)
: owned_value(init)
{}
template <
class... Args,
enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
json_ref(Args && ... args)
: owned_value(std::forward<Args>(args)...)
{}
// class should be movable only
2021-12-11 23:32:21 +01:00
json_ref(json_ref&&) noexcept = default;
json_ref(const json_ref&) = delete;
json_ref& operator=(const json_ref&) = delete;
json_ref& operator=(json_ref&&) = delete;
~json_ref() = default;
value_type moved_or_copied() const
{
2021-12-11 23:32:21 +01:00
if (value_ref == nullptr)
{
2021-12-11 23:32:21 +01:00
return std::move(owned_value);
}
return *value_ref;
}
value_type const& operator*() const
{
2021-12-11 23:32:21 +01:00
return value_ref ? *value_ref : owned_value;
}
value_type const* operator->() const
{
2021-12-11 23:32:21 +01:00
return &** this;
}
private:
mutable value_type owned_value = nullptr;
2021-12-11 23:32:21 +01:00
value_type const* value_ref = nullptr;
};
2022-08-02 14:28:39 +02:00
} // namespace detail
2022-08-02 14:28:39 +02:00
NLOHMANN_JSON_NAMESPACE_END