WPILibC++ 2024.1.1-beta-4
json_ref.h
Go to the documentation of this file.
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
16
18namespace detail
19{
20
21template<typename BasicJsonType>
23{
24 public:
25 using value_type = BasicJsonType;
26
28 : owned_value(std::move(value))
29 {}
30
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
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
#define WPI_JSON_NAMESPACE_END
Definition: abi_macros.h:59
#define WPI_JSON_NAMESPACE_BEGIN
Definition: abi_macros.h:53
Definition: json_ref.h:23
json_ref(json_ref &&) noexcept=default
json_ref(const value_type &value)
Definition: json_ref.h:31
json_ref(value_type &&value)
Definition: json_ref.h:27
value_type const & operator*() const
Definition: json_ref.h:62
value_type const * operator->() const
Definition: json_ref.h:67
json_ref(std::initializer_list< json_ref > init)
Definition: json_ref.h:35
json_ref(Args &&... args)
Definition: json_ref.h:42
value_type moved_or_copied() const
Definition: json_ref.h:53
BasicJsonType value_type
Definition: json_ref.h:25
Definition: core.h:1238
detail namespace with internal helper functions
Definition: ranges.h:23
typename std::enable_if< B, T >::type enable_if_t
Definition: cpp_future.h:38
Definition: array.h:89