json/include/nlohmann/detail/json_ref.hpp

69 lines
1.4 KiB
C++
Raw Normal View History

2018-01-10 12:18:31 +03:00
#pragma once
2017-08-14 19:14:27 +03:00
#include <initializer_list>
#include <utility>
#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
{}
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
:rotating_light: add new CI and fix warnings (#2561) * :alembic: move CI targets to CMake * :recycle: add target for cpplint * :recycle: add target for self-contained binaries * :recycle: add targets for iwyu and infer * :loud_sound: add version output * :recycle: add target for oclint * :rotating_light: fix warnings * :recycle: rename targets * :recycle: use iwyu properly * :rotating_light: fix warnings * :recycle: use iwyu properly * :recycle: add target for benchmarks * :recycle: add target for CMake flags * :construction_worker: use GitHub Actions * :alembic: try to install Clang 11 * :alembic: try to install GCC 11 * :alembic: try to install Clang 11 * :alembic: try to install GCC 11 * :alembic: add clang analyze target * :fire: remove Google Benchmark * :arrow_up: Google Benchmark 1.5.2 * :fire: use fetchcontent * :penguin: add target to download a Linux version of CMake * :hammer: fix dependency * :rotating_light: fix includes * :rotating_light: fix comment * :wrench: adjust flags for GCC 11.0.0 20210110 (experimental) * :whale: user Docker image to run CI * :wrench: add target for Valgrind * :construction_worker: add target for Valgrind tests * :alembic: add Dart * :rewind: remove Dart * :alembic: do not call ctest in test subdirectory * :alembic: download test data explicitly * :alembic: only execute Valgrind tests * :alembic: fix labels * :fire: remove unneeded jobs * :hammer: cleanup * :bug: fix OCLint call * :white_check_mark: add targets for offline and git-independent tests * :white_check_mark: add targets for C++ language versions and reproducible tests * :hammer: clean up * :construction_worker: add CI steps for cppcheck and cpplint * :rotating_light: fix warnings from Clang-Tidy * :construction_worker: add CI steps for Clang-Tidy * :rotating_light: fix warnings * :wrench: select proper binary * :rotating_light: fix warnings * :rotating_light: suppress some unhelpful warnings * :rotating_light: fix warnings * :art: fix format * :rotating_light: fix warnings * :construction_worker: add CI steps for Sanitizers * :rotating_light: fix warnings * :zap: add optimization to sanitizer build * :rotating_light: fix warnings * :rotating_light: add missing header * :rotating_light: fix warnings * :construction_worker: add CI step for coverage * :construction_worker: add CI steps for disabled exceptions and implicit conversions * :rotating_light: fix warnings * :construction_worker: add CI steps for checking indentation * :bug: fix variable use * :green_heart: fix build * :heavy_minus_sign: remove CircleCI * :construction_worker: add CI step for diagnostics * :rotating_light: fix warning * :fire: clean Travis
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;
~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
};
} // namespace detail
} // namespace nlohmann