WPILibC++ 2024.3.2
iter_impl.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 <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
12#include <type_traits> // conditional, is_const, remove_const
13
20#include <wpi/detail/value_t.h>
21
23namespace detail
24{
25
26// forward declare, to be able to friend it later on
27template<typename IteratorType> class iteration_proxy;
28template<typename IteratorType> class iteration_proxy_value;
29
30/*!
31@brief a template for a bidirectional iterator for the @ref basic_json class
32This class implements a both iterators (iterator and const_iterator) for the
33@ref basic_json class.
34@note An iterator is called *initialized* when a pointer to a JSON value has
35 been set (e.g., by a constructor or a copy assignment). If the iterator is
36 default-constructed, it is *uninitialized* and most methods are undefined.
37 **The library uses assertions to detect calls on uninitialized iterators.**
38@requirement The class satisfies the following concept requirements:
39-
40[BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
41 The iterator that can be moved can be moved in both directions (i.e.
42 incremented and decremented).
43@since version 1.0.0, simplified in version 2.0.9, change to bidirectional
44 iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
45*/
46template<typename BasicJsonType>
47class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
48{
49 /// the iterator with BasicJsonType of different const-ness
51 /// allow basic_json to access private members
52 friend other_iter_impl;
53 friend BasicJsonType;
56
57 using object_t = typename BasicJsonType::object_t;
58 using array_t = typename BasicJsonType::array_t;
59 // make sure BasicJsonType is basic_json or const basic_json
61 "iter_impl only accepts (const) basic_json");
62 // superficial check for the LegacyBidirectionalIterator named requirement
63 static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
64 && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
65 "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
66
67 public:
68 /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
69 /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
70 /// A user-defined iterator should provide publicly accessible typedefs named
71 /// iterator_category, value_type, difference_type, pointer, and reference.
72 /// Note that value_type is required to be non-const, even for constant iterators.
73 using iterator_category = std::bidirectional_iterator_tag;
74
75 /// the type of the values when the iterator is dereferenced
76 using value_type = typename BasicJsonType::value_type;
77 /// a type to represent differences between iterators
78 using difference_type = typename BasicJsonType::difference_type;
79 /// defines a pointer to the type iterated over (value_type)
80 using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
81 typename BasicJsonType::const_pointer,
82 typename BasicJsonType::pointer>::type;
83 /// defines a reference to the type iterated over (value_type)
84 using reference =
85 typename std::conditional<std::is_const<BasicJsonType>::value,
86 typename BasicJsonType::const_reference,
87 typename BasicJsonType::reference>::type;
88
89 iter_impl() = default;
90 ~iter_impl() = default;
91 iter_impl(iter_impl&&) noexcept = default;
92 iter_impl& operator=(iter_impl&&) noexcept = default;
93
94 /*!
95 @brief constructor for a given JSON instance
96 @param[in] object pointer to a JSON object for this iterator
97 @pre object != nullptr
98 @post The iterator is initialized; i.e. `m_object != nullptr`.
99 */
100 explicit iter_impl(pointer object) noexcept : m_object(object)
101 {
102 JSON_ASSERT(m_object != nullptr);
103
104 switch (m_object->m_type)
105 {
106 case value_t::object:
107 {
108 m_it.object_iterator = typename object_t::iterator();
109 break;
110 }
111
112 case value_t::array:
113 {
114 m_it.array_iterator = typename array_t::iterator();
115 break;
116 }
117
118 case value_t::null:
119 case value_t::string:
120 case value_t::boolean:
124 case value_t::binary:
126 default:
127 {
129 break;
130 }
131 }
132 }
133
134 /*!
135 @note The conventional copy constructor and copy assignment are implicitly
136 defined. Combined with the following converting constructor and
137 assignment, they support: (1) copy from iterator to iterator, (2)
138 copy from const iterator to const iterator, and (3) conversion from
139 iterator to const iterator. However conversion from const iterator
140 to iterator is not defined.
141 */
142
143 /*!
144 @brief const copy constructor
145 @param[in] other const iterator to copy from
146 @note This copy constructor had to be defined explicitly to circumvent a bug
147 occurring on msvc v19.0 compiler (VS 2015) debug build. For more
148 information refer to: https://github.com/nlohmann/json/issues/1608
149 */
151 : m_object(other.m_object), m_it(other.m_it)
152 {}
153
154 /*!
155 @brief converting assignment
156 @param[in] other const iterator to copy from
157 @return const/non-const iterator
158 @note It is not checked whether @a other is initialized.
159 */
161 {
162 if (&other != this)
163 {
164 m_object = other.m_object;
165 m_it = other.m_it;
166 }
167 return *this;
168 }
169
170 /*!
171 @brief converting constructor
172 @param[in] other non-const iterator to copy from
173 @note It is not checked whether @a other is initialized.
174 */
176 : m_object(other.m_object), m_it(other.m_it)
177 {}
178
179 /*!
180 @brief converting assignment
181 @param[in] other non-const iterator to copy from
182 @return const/non-const iterator
183 @note It is not checked whether @a other is initialized.
184 */
185 iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
186 {
187 m_object = other.m_object;
188 m_it = other.m_it;
189 return *this;
190 }
191
193 /*!
194 @brief set the iterator to the first value
195 @pre The iterator is initialized; i.e. `m_object != nullptr`.
196 */
197 void set_begin() noexcept
198 {
199 JSON_ASSERT(m_object != nullptr);
200
201 switch (m_object->m_type)
202 {
203 case value_t::object:
204 {
205 m_it.object_iterator = m_object->m_value.object->begin();
206 break;
207 }
208
209 case value_t::array:
210 {
211 m_it.array_iterator = m_object->m_value.array->begin();
212 break;
213 }
214
215 case value_t::null:
216 {
217 // set to end so begin()==end() is true: null is empty
219 break;
220 }
221
222 case value_t::string:
223 case value_t::boolean:
227 case value_t::binary:
229 default:
230 {
232 break;
233 }
234 }
235 }
236
237 /*!
238 @brief set the iterator past the last value
239 @pre The iterator is initialized; i.e. `m_object != nullptr`.
240 */
241 void set_end() noexcept
242 {
243 JSON_ASSERT(m_object != nullptr);
244
245 switch (m_object->m_type)
246 {
247 case value_t::object:
248 {
249 m_it.object_iterator = m_object->m_value.object->end();
250 break;
251 }
252
253 case value_t::array:
254 {
255 m_it.array_iterator = m_object->m_value.array->end();
256 break;
257 }
258
259 case value_t::null:
260 case value_t::string:
261 case value_t::boolean:
265 case value_t::binary:
267 default:
268 {
270 break;
271 }
272 }
273 }
274
275 public:
276 /*!
277 @brief return a reference to the value pointed to by the iterator
278 @pre The iterator is initialized; i.e. `m_object != nullptr`.
279 */
281 {
282 JSON_ASSERT(m_object != nullptr);
283
284 switch (m_object->m_type)
285 {
286 case value_t::object:
287 {
288 JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
289 return m_it.object_iterator->second;
290 }
291
292 case value_t::array:
293 {
294 JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
295 return *m_it.array_iterator;
296 }
297
298 case value_t::null:
299 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
300
301 case value_t::string:
302 case value_t::boolean:
306 case value_t::binary:
308 default:
309 {
311 {
312 return *m_object;
313 }
314
315 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
316 }
317 }
318 }
319
320 /*!
321 @brief dereference the iterator
322 @pre The iterator is initialized; i.e. `m_object != nullptr`.
323 */
325 {
326 JSON_ASSERT(m_object != nullptr);
327
328 switch (m_object->m_type)
329 {
330 case value_t::object:
331 {
332 JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
333 return &(m_it.object_iterator->second);
334 }
335
336 case value_t::array:
337 {
338 JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
339 return &*m_it.array_iterator;
340 }
341
342 case value_t::null:
343 case value_t::string:
344 case value_t::boolean:
348 case value_t::binary:
350 default:
351 {
353 {
354 return m_object;
355 }
356
357 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
358 }
359 }
360 }
361
362 /*!
363 @brief post-increment (it++)
364 @pre The iterator is initialized; i.e. `m_object != nullptr`.
365 */
366 iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
367 {
368 auto result = *this;
369 ++(*this);
370 return result;
371 }
372
373 /*!
374 @brief pre-increment (++it)
375 @pre The iterator is initialized; i.e. `m_object != nullptr`.
376 */
378 {
379 JSON_ASSERT(m_object != nullptr);
380
381 switch (m_object->m_type)
382 {
383 case value_t::object:
384 {
385 std::advance(m_it.object_iterator, 1);
386 break;
387 }
388
389 case value_t::array:
390 {
391 std::advance(m_it.array_iterator, 1);
392 break;
393 }
394
395 case value_t::null:
396 case value_t::string:
397 case value_t::boolean:
401 case value_t::binary:
403 default:
404 {
406 break;
407 }
408 }
409
410 return *this;
411 }
412
413 /*!
414 @brief post-decrement (it--)
415 @pre The iterator is initialized; i.e. `m_object != nullptr`.
416 */
417 iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
418 {
419 auto result = *this;
420 --(*this);
421 return result;
422 }
423
424 /*!
425 @brief pre-decrement (--it)
426 @pre The iterator is initialized; i.e. `m_object != nullptr`.
427 */
429 {
430 JSON_ASSERT(m_object != nullptr);
431
432 switch (m_object->m_type)
433 {
434 case value_t::object:
435 {
436 std::advance(m_it.object_iterator, -1);
437 break;
438 }
439
440 case value_t::array:
441 {
442 std::advance(m_it.array_iterator, -1);
443 break;
444 }
445
446 case value_t::null:
447 case value_t::string:
448 case value_t::boolean:
452 case value_t::binary:
454 default:
455 {
457 break;
458 }
459 }
460
461 return *this;
462 }
463
464 /*!
465 @brief comparison: equal
466 @pre The iterator is initialized; i.e. `m_object != nullptr`.
467 */
468 template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
469 bool operator==(const IterImpl& other) const
470 {
471 // if objects are not the same, the comparison is undefined
472 if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
473 {
474 JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
475 }
476
477 JSON_ASSERT(m_object != nullptr);
478
479 switch (m_object->m_type)
480 {
481 case value_t::object:
482 return (m_it.object_iterator == other.m_it.object_iterator);
483
484 case value_t::array:
485 return (m_it.array_iterator == other.m_it.array_iterator);
486
487 case value_t::null:
488 case value_t::string:
489 case value_t::boolean:
493 case value_t::binary:
495 default:
496 return (m_it.primitive_iterator == other.m_it.primitive_iterator);
497 }
498 }
499
500 /*!
501 @brief comparison: not equal
502 @pre The iterator is initialized; i.e. `m_object != nullptr`.
503 */
504 template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
505 bool operator!=(const IterImpl& other) const
506 {
507 return !operator==(other);
508 }
509
510 /*!
511 @brief comparison: smaller
512 @pre The iterator is initialized; i.e. `m_object != nullptr`.
513 */
514 bool operator<(const iter_impl& other) const
515 {
516 // if objects are not the same, the comparison is undefined
517 if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
518 {
519 JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
520 }
521
522 JSON_ASSERT(m_object != nullptr);
523
524 switch (m_object->m_type)
525 {
526 case value_t::object:
527 JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
528
529 case value_t::array:
530 return (m_it.array_iterator < other.m_it.array_iterator);
531
532 case value_t::null:
533 case value_t::string:
534 case value_t::boolean:
538 case value_t::binary:
540 default:
542 }
543 }
544
545 /*!
546 @brief comparison: less than or equal
547 @pre The iterator is initialized; i.e. `m_object != nullptr`.
548 */
549 bool operator<=(const iter_impl& other) const
550 {
551 return !other.operator < (*this);
552 }
553
554 /*!
555 @brief comparison: greater than
556 @pre The iterator is initialized; i.e. `m_object != nullptr`.
557 */
558 bool operator>(const iter_impl& other) const
559 {
560 return !operator<=(other);
561 }
562
563 /*!
564 @brief comparison: greater than or equal
565 @pre The iterator is initialized; i.e. `m_object != nullptr`.
566 */
567 bool operator>=(const iter_impl& other) const
568 {
569 return !operator<(other);
570 }
571
572 /*!
573 @brief add to iterator
574 @pre The iterator is initialized; i.e. `m_object != nullptr`.
575 */
577 {
578 JSON_ASSERT(m_object != nullptr);
579
580 switch (m_object->m_type)
581 {
582 case value_t::object:
583 JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
584
585 case value_t::array:
586 {
587 std::advance(m_it.array_iterator, i);
588 break;
589 }
590
591 case value_t::null:
592 case value_t::string:
593 case value_t::boolean:
597 case value_t::binary:
599 default:
600 {
602 break;
603 }
604 }
605
606 return *this;
607 }
608
609 /*!
610 @brief subtract from iterator
611 @pre The iterator is initialized; i.e. `m_object != nullptr`.
612 */
614 {
615 return operator+=(-i);
616 }
617
618 /*!
619 @brief add to iterator
620 @pre The iterator is initialized; i.e. `m_object != nullptr`.
621 */
623 {
624 auto result = *this;
625 result += i;
626 return result;
627 }
628
629 /*!
630 @brief addition of distance and iterator
631 @pre The iterator is initialized; i.e. `m_object != nullptr`.
632 */
634 {
635 auto result = it;
636 result += i;
637 return result;
638 }
639
640 /*!
641 @brief subtract from iterator
642 @pre The iterator is initialized; i.e. `m_object != nullptr`.
643 */
645 {
646 auto result = *this;
647 result -= i;
648 return result;
649 }
650
651 /*!
652 @brief return difference
653 @pre The iterator is initialized; i.e. `m_object != nullptr`.
654 */
656 {
657 JSON_ASSERT(m_object != nullptr);
658
659 switch (m_object->m_type)
660 {
661 case value_t::object:
662 JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
663
664 case value_t::array:
665 return m_it.array_iterator - other.m_it.array_iterator;
666
667 case value_t::null:
668 case value_t::string:
669 case value_t::boolean:
673 case value_t::binary:
675 default:
677 }
678 }
679
680 /*!
681 @brief access to successor
682 @pre The iterator is initialized; i.e. `m_object != nullptr`.
683 */
685 {
686 JSON_ASSERT(m_object != nullptr);
687
688 switch (m_object->m_type)
689 {
690 case value_t::object:
691 JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
692
693 case value_t::array:
694 return *std::next(m_it.array_iterator, n);
695
696 case value_t::null:
697 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
698
699 case value_t::string:
700 case value_t::boolean:
704 case value_t::binary:
706 default:
707 {
709 {
710 return *m_object;
711 }
712
713 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
714 }
715 }
716 }
717
718 /*!
719 @brief return the key of an object iterator
720 @pre The iterator is initialized; i.e. `m_object != nullptr`.
721 */
722 const typename object_t::key_type& key() const
723 {
724 JSON_ASSERT(m_object != nullptr);
725
726 if (JSON_HEDLEY_LIKELY(m_object->is_object()))
727 {
728 return m_it.object_iterator->first;
729 }
730
731 JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
732 }
733
734 /*!
735 @brief return the value of an iterator
736 @pre The iterator is initialized; i.e. `m_object != nullptr`.
737 */
739 {
740 return operator*();
741 }
742
744 /// associated JSON instance
745 pointer m_object = nullptr;
746 /// the actual iterator of the associated instance
748};
749
750} // namespace detail
#define WPI_JSON_NAMESPACE_END
Definition: abi_macros.h:59
#define WPI_JSON_NAMESPACE_BEGIN
Definition: abi_macros.h:53
static invalid_iterator create(int id_, const std::string &what_arg, BasicJsonContext context)
Definition: exceptions.h:191
a template for a bidirectional iterator for the basic_json class This class implements a both iterato...
Definition: iter_impl.h:48
~iter_impl()=default
iter_impl operator+(difference_type i) const
add to iterator
Definition: iter_impl.h:622
iter_impl & operator=(const iter_impl< typename std::remove_const< BasicJsonType >::type > &other) noexcept
converting assignment
Definition: iter_impl.h:185
bool operator>=(const iter_impl &other) const
comparison: greater than or equal
Definition: iter_impl.h:567
iter_impl(const iter_impl< typename std::remove_const< BasicJsonType >::type > &other) noexcept
converting constructor
Definition: iter_impl.h:175
bool operator<(const iter_impl &other) const
comparison: smaller
Definition: iter_impl.h:514
bool operator<=(const iter_impl &other) const
comparison: less than or equal
Definition: iter_impl.h:549
iter_impl & operator-=(difference_type i)
subtract from iterator
Definition: iter_impl.h:613
iter_impl & operator--()
pre-decrement (–it)
Definition: iter_impl.h:428
const object_t::key_type & key() const
return the key of an object iterator
Definition: iter_impl.h:722
bool operator==(const IterImpl &other) const
comparison: equal
Definition: iter_impl.h:469
iter_impl operator++(int) &
post-increment (it++)
Definition: iter_impl.h:366
iter_impl & operator+=(difference_type i)
add to iterator
Definition: iter_impl.h:576
reference operator[](difference_type n) const
access to successor
Definition: iter_impl.h:684
typename BasicJsonType::difference_type difference_type
a type to represent differences between iterators
Definition: iter_impl.h:78
pointer operator->() const
dereference the iterator
Definition: iter_impl.h:324
internal_iterator< typename std::remove_const< BasicJsonType >::type > m_it
the actual iterator of the associated instance
Definition: iter_impl.h:747
difference_type operator-(const iter_impl &other) const
return difference
Definition: iter_impl.h:655
iter_impl(iter_impl &&) noexcept=default
std::bidirectional_iterator_tag iterator_category
The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
Definition: iter_impl.h:73
friend iter_impl operator+(difference_type i, const iter_impl &it)
addition of distance and iterator
Definition: iter_impl.h:633
reference value() const
return the value of an iterator
Definition: iter_impl.h:738
bool operator>(const iter_impl &other) const
comparison: greater than
Definition: iter_impl.h:558
typename std::conditional< std::is_const< BasicJsonType >::value, typename BasicJsonType::const_pointer, typename BasicJsonType::pointer >::type pointer
defines a pointer to the type iterated over (value_type)
Definition: iter_impl.h:82
iter_impl & operator++()
pre-increment (++it)
Definition: iter_impl.h:377
typename BasicJsonType::value_type value_type
the type of the values when the iterator is dereferenced
Definition: iter_impl.h:76
reference operator*() const
return a reference to the value pointed to by the iterator
Definition: iter_impl.h:280
iter_impl operator-(difference_type i) const
subtract from iterator
Definition: iter_impl.h:644
iter_impl()=default
iter_impl & operator=(const iter_impl< const BasicJsonType > &other) noexcept
converting assignment
Definition: iter_impl.h:160
bool operator!=(const IterImpl &other) const
comparison: not equal
Definition: iter_impl.h:505
iter_impl operator--(int) &
post-decrement (it–)
Definition: iter_impl.h:417
typename std::conditional< std::is_const< BasicJsonType >::value, typename BasicJsonType::const_reference, typename BasicJsonType::reference >::type reference
defines a reference to the type iterated over (value_type)
Definition: iter_impl.h:87
iter_impl(const iter_impl< const BasicJsonType > &other) noexcept
const copy constructor
Definition: iter_impl.h:150
void set_end() noexcept
set the iterator past the last value
Definition: iter_impl.h:241
Definition: iteration_proxy.h:37
proxy class for the items() function
Definition: iteration_proxy.h:158
Definition: primitive_iterator.h:30
constexpr bool is_begin() const noexcept
return whether the iterator can be dereferenced
Definition: primitive_iterator.h:59
void set_end() noexcept
set iterator to a defined past the end
Definition: primitive_iterator.h:53
constexpr difference_type get_value() const noexcept
Definition: primitive_iterator.h:41
void set_begin() noexcept
set iterator to a defined beginning
Definition: primitive_iterator.h:47
#define JSON_HEDLEY_LIKELY(expr)
Definition: hedley.h:1395
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: hedley.h:1396
#define JSON_PRIVATE_UNLESS_TESTED
Definition: macro_scope.h:199
#define JSON_ASSERT(x)
Definition: macro_scope.h:192
#define JSON_THROW(exception)
Definition: macro_scope.h:163
detail namespace with internal helper functions
Definition: xchar.h:20
@ null
null value
@ number_integer
number value (signed integer)
@ boolean
boolean value
@ discarded
discarded by the parser callback function
@ binary
binary array (ordered collection of bytes)
@ object
object (unordered set of name/value pairs)
@ string
string value
@ number_float
number value (floating-point)
@ number_unsigned
number value (unsigned integer)
@ array
array (ordered collection of values)
type
Definition: core.h:556
an iterator value
Definition: internal_iterator.h:25
BasicJsonType::array_t::iterator array_iterator
iterator for JSON arrays
Definition: internal_iterator.h:29
primitive_iterator_t primitive_iterator
generic iterator for all other types
Definition: internal_iterator.h:31
BasicJsonType::object_t::iterator object_iterator
iterator for JSON objects
Definition: internal_iterator.h:27
Definition: type_traits.h:49