2018-01-10 12:18:31 +03:00
|
|
|
#pragma once
|
2017-08-14 19:14:27 +03:00
|
|
|
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-10-12 11:54:58 +03:00
|
|
|
#include <nlohmann/detail/meta/type_traits.hpp>
|
|
|
|
|
2017-08-14 19:14:27 +03:00
|
|
|
namespace nlohmann
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
class json_ref
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using value_type = BasicJsonType;
|
|
|
|
|
|
|
|
json_ref(value_type&& value)
|
2020-06-22 23:32:21 +03:00
|
|
|
: owned_value(std::move(value))
|
2017-08-14 19:14:27 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
json_ref(const value_type& value)
|
2020-09-27 08:45:21 +03:00
|
|
|
: value_ref(&value)
|
2017-08-14 19:14:27 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
json_ref(std::initializer_list<json_ref> init)
|
2020-06-22 23:32:21 +03:00
|
|
|
: owned_value(init)
|
2017-08-14 19:14:27 +03:00
|
|
|
{}
|
|
|
|
|
2018-10-12 11:54:58 +03:00
|
|
|
template <
|
|
|
|
class... Args,
|
|
|
|
enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
|
|
|
|
json_ref(Args && ... args)
|
2020-06-22 23:32:21 +03:00
|
|
|
: owned_value(std::forward<Args>(args)...)
|
|
|
|
{}
|
2017-08-14 19:14:27 +03:00
|
|
|
|
|
|
|
// class should be movable only
|
2021-03-24 09:15:18 +03:00
|
|
|
json_ref(json_ref&&) noexcept = default;
|
2017-08-14 19:14:27 +03:00
|
|
|
json_ref(const json_ref&) = delete;
|
|
|
|
json_ref& operator=(const json_ref&) = delete;
|
2018-10-08 07:54:51 +03:00
|
|
|
json_ref& operator=(json_ref&&) = delete;
|
2018-10-07 19:39:18 +03:00
|
|
|
~json_ref() = default;
|
2017-08-14 19:14:27 +03:00
|
|
|
|
|
|
|
value_type moved_or_copied() const
|
|
|
|
{
|
2020-09-27 08:45:21 +03:00
|
|
|
if (value_ref == nullptr)
|
2017-08-14 19:14:27 +03:00
|
|
|
{
|
2020-09-27 08:45:21 +03:00
|
|
|
return std::move(owned_value);
|
2017-08-14 19:14:27 +03:00
|
|
|
}
|
|
|
|
return *value_ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_type const& operator*() const
|
|
|
|
{
|
2020-09-27 08:45:21 +03:00
|
|
|
return value_ref ? *value_ref : owned_value;
|
2017-08-14 19:14:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
value_type const* operator->() const
|
|
|
|
{
|
2020-12-14 12:38:49 +03:00
|
|
|
return &** this;
|
2017-08-14 19:14:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable value_type owned_value = nullptr;
|
2020-09-27 08:45:21 +03:00
|
|
|
value_type const* value_ref = nullptr;
|
2017-08-14 19:14:27 +03:00
|
|
|
};
|
2018-10-07 19:39:18 +03:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace nlohmann
|