RflySimSDK v4.10
RflySimSDK说明文档
载入中...
搜索中...
未找到
iteration_proxy.hpp
1// __ _____ _____ _____
2// __| | __| | | | JSON for Modern C++
3// | | |__ | | | | | | version 3.12.0
4// |_____|_____|_____|_|___| https://github.com/nlohmann/json
5//
6// SPDX-FileCopyrightText: 2013-2025 Niels Lohmann <https://nlohmann.me>
7// SPDX-License-Identifier: MIT
8
9#pragma once
10
11#include <cstddef> // size_t
12#include <iterator> // forward_iterator_tag
13#include <tuple> // tuple_size, get, tuple_element
14#include <utility> // move
15
16#if JSON_HAS_RANGES
17 #include <ranges> // enable_borrowed_range
18#endif
19
20#include <nlohmann/detail/abi_macros.hpp>
21#include <nlohmann/detail/meta/type_traits.hpp>
22#include <nlohmann/detail/string_utils.hpp>
23#include <nlohmann/detail/value_t.hpp>
24
25NLOHMANN_JSON_NAMESPACE_BEGIN
26namespace detail
27{
28
29template<typename IteratorType> class iteration_proxy_value
30{
31 public:
32 using difference_type = std::ptrdiff_t;
33 using value_type = iteration_proxy_value;
34 using pointer = value_type *;
35 using reference = value_type &;
36 using iterator_category = std::forward_iterator_tag;
37 using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
38
39 private:
40 /// the iterator
41 IteratorType anchor{};
42 /// an index for arrays (used to create key names)
43 std::size_t array_index = 0;
44 /// last stringified array index
45 mutable std::size_t array_index_last = 0;
46 /// a string representation of the array index
47 mutable string_type array_index_str = "0";
48 /// an empty string (to return a reference for primitive values)
49 string_type empty_str{};
50
51 public:
52 explicit iteration_proxy_value() = default;
53 explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
54 noexcept(std::is_nothrow_move_constructible<IteratorType>::value
55 && std::is_nothrow_default_constructible<string_type>::value)
56 : anchor(std::move(it))
57 , array_index(array_index_)
58 {}
59
60 iteration_proxy_value(iteration_proxy_value const&) = default;
61 iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
62 // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
63 iteration_proxy_value(iteration_proxy_value&&)
64 noexcept(std::is_nothrow_move_constructible<IteratorType>::value
65 && std::is_nothrow_move_constructible<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
66 iteration_proxy_value& operator=(iteration_proxy_value&&)
67 noexcept(std::is_nothrow_move_assignable<IteratorType>::value
68 && std::is_nothrow_move_assignable<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
69 ~iteration_proxy_value() = default;
70
71 /// dereference operator (needed for range-based for)
72 const iteration_proxy_value& operator*() const
73 {
74 return *this;
75 }
76
77 /// increment operator (needed for range-based for)
78 iteration_proxy_value& operator++()
79 {
80 ++anchor;
81 ++array_index;
82
83 return *this;
84 }
85
86 iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
87 {
88 auto tmp = iteration_proxy_value(anchor, array_index);
89 ++anchor;
90 ++array_index;
91 return tmp;
92 }
93
94 /// equality operator (needed for InputIterator)
95 bool operator==(const iteration_proxy_value& o) const
96 {
97 return anchor == o.anchor;
98 }
99
100 /// inequality operator (needed for range-based for)
101 bool operator!=(const iteration_proxy_value& o) const
102 {
103 return anchor != o.anchor;
104 }
105
106 /// return key of the iterator
107 const string_type& key() const
108 {
109 JSON_ASSERT(anchor.m_object != nullptr);
110
111 switch (anchor.m_object->type())
112 {
113 // use integer array index as key
114 case value_t::array:
115 {
116 if (array_index != array_index_last)
117 {
118 int_to_string( array_index_str, array_index );
119 array_index_last = array_index;
120 }
121 return array_index_str;
122 }
123
124 // use key from the object
125 case value_t::object:
126 return anchor.key();
127
128 // use an empty key for all primitive types
129 case value_t::null:
130 case value_t::string:
131 case value_t::boolean:
135 case value_t::binary:
137 default:
138 return empty_str;
139 }
140 }
141
142 /// return value of the iterator
143 typename IteratorType::reference value() const
144 {
145 return anchor.value();
146 }
147};
148
149/// proxy class for the items() function
150template<typename IteratorType> class iteration_proxy
151{
152 private:
153 /// the container to iterate
154 typename IteratorType::pointer container = nullptr;
155
156 public:
157 explicit iteration_proxy() = default;
158
159 /// construct iteration proxy from a container
160 explicit iteration_proxy(typename IteratorType::reference cont) noexcept
161 : container(&cont) {}
162
163 iteration_proxy(iteration_proxy const&) = default;
164 iteration_proxy& operator=(iteration_proxy const&) = default;
165 iteration_proxy(iteration_proxy&&) noexcept = default;
166 iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
167 ~iteration_proxy() = default;
168
169 /// return iterator begin (needed for range-based for)
170 iteration_proxy_value<IteratorType> begin() const noexcept
171 {
172 return iteration_proxy_value<IteratorType>(container->begin());
173 }
174
175 /// return iterator end (needed for range-based for)
177 {
178 return iteration_proxy_value<IteratorType>(container->end());
179 }
180};
181
182// Structured Bindings Support
183// For further reference see https://blog.tartanllama.xyz/structured-bindings/
184// And see https://github.com/nlohmann/json/pull/1391
185template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
186auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
187{
188 return i.key();
189}
190// Structured Bindings Support
191// For further reference see https://blog.tartanllama.xyz/structured-bindings/
192// And see https://github.com/nlohmann/json/pull/1391
193template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
194auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
195{
196 return i.value();
197}
198
199} // namespace detail
200NLOHMANN_JSON_NAMESPACE_END
201
202// The Addition to the STD Namespace is required to add
203// Structured Bindings Support to the iteration_proxy_value class
204// For further reference see https://blog.tartanllama.xyz/structured-bindings/
205// And see https://github.com/nlohmann/json/pull/1391
206namespace std
207{
208
209#if defined(__clang__)
210 // Fix: https://github.com/nlohmann/json/issues/1401
211 #pragma clang diagnostic push
212 #pragma clang diagnostic ignored "-Wmismatched-tags"
213#endif
214template<typename IteratorType>
215class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
216 : public std::integral_constant<std::size_t, 2> {};
217
218template<std::size_t N, typename IteratorType>
219class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >> // NOLINT(cert-dcl58-cpp)
220{
221 public:
222 using type = decltype(
223 get<N>(std::declval <
224 ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
225};
226#if defined(__clang__)
227 #pragma clang diagnostic pop
228#endif
229
230} // namespace std
231
232#if JSON_HAS_RANGES
233 template <typename IteratorType>
234 inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
235#endif
定义 iteration_proxy.hpp:30
IteratorType::reference value() const
return value of the iterator
定义 iteration_proxy.hpp:143
iteration_proxy_value & operator++()
increment operator (needed for range-based for)
定义 iteration_proxy.hpp:78
const iteration_proxy_value & operator*() const
dereference operator (needed for range-based for)
定义 iteration_proxy.hpp:72
bool operator!=(const iteration_proxy_value &o) const
inequality operator (needed for range-based for)
定义 iteration_proxy.hpp:101
bool operator==(const iteration_proxy_value &o) const
equality operator (needed for InputIterator)
定义 iteration_proxy.hpp:95
const string_type & key() const
return key of the iterator
定义 iteration_proxy.hpp:107
proxy class for the items() function
定义 iteration_proxy.hpp:151
iteration_proxy_value< IteratorType > begin() const noexcept
return iterator begin (needed for range-based for)
定义 iteration_proxy.hpp:170
iteration_proxy(typename IteratorType::reference cont) noexcept
construct iteration proxy from a container
定义 iteration_proxy.hpp:160
iteration_proxy_value< IteratorType > end() const noexcept
return iterator end (needed for range-based for)
定义 iteration_proxy.hpp:176
detail namespace with internal helper functions
定义 from_json.hpp:43
@ null
null value
定义 value_t.hpp:55
@ number_integer
number value (signed integer)
定义 value_t.hpp:60
@ boolean
boolean value
定义 value_t.hpp:59
@ discarded
discarded by the parser callback function
定义 value_t.hpp:64
@ binary
binary array (ordered collection of bytes)
定义 value_t.hpp:63
@ object
object (unordered set of name/value pairs)
定义 value_t.hpp:56
@ string
string value
定义 value_t.hpp:58
@ number_float
number value (floating-point)
定义 value_t.hpp:62
@ number_unsigned
number value (unsigned integer)
定义 value_t.hpp:61
@ array
array (ordered collection of values)
定义 value_t.hpp:57