WPILibC++ 2024.3.2
primitive_iterator.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 <cstddef> // ptrdiff_t
12#include <limits> // numeric_limits
13
15
17namespace detail
18{
19
20/*
21@brief an iterator for primitive JSON types
22
23This class models an iterator for primitive JSON types (boolean, number,
24string). It's only purpose is to allow the iterator/const_iterator classes
25to "iterate" over primitive values. Internally, the iterator is modeled by
26a `difference_type` variable. Value begin_value (`0`) models the begin,
27end_value (`1`) models past the end.
28*/
30{
31 private:
32 using difference_type = std::ptrdiff_t;
33 static constexpr difference_type begin_value = 0;
34 static constexpr difference_type end_value = begin_value + 1;
35
37 /// iterator as signed integer type
38 difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
39
40 public:
41 constexpr difference_type get_value() const noexcept
42 {
43 return m_it;
44 }
45
46 /// set iterator to a defined beginning
47 void set_begin() noexcept
48 {
49 m_it = begin_value;
50 }
51
52 /// set iterator to a defined past the end
53 void set_end() noexcept
54 {
55 m_it = end_value;
56 }
57
58 /// return whether the iterator can be dereferenced
59 constexpr bool is_begin() const noexcept
60 {
61 return m_it == begin_value;
62 }
63
64 /// return whether the iterator is at end
65 constexpr bool is_end() const noexcept
66 {
67 return m_it == end_value;
68 }
69
70 friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
71 {
72 return lhs.m_it == rhs.m_it;
73 }
74
75 friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
76 {
77 return lhs.m_it < rhs.m_it;
78 }
79
80 primitive_iterator_t operator+(difference_type n) noexcept
81 {
82 auto result = *this;
83 result += n;
84 return result;
85 }
86
87 friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
88 {
89 return lhs.m_it - rhs.m_it;
90 }
91
93 {
94 ++m_it;
95 return *this;
96 }
97
98 primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
99 {
100 auto result = *this;
101 ++m_it;
102 return result;
103 }
104
106 {
107 --m_it;
108 return *this;
109 }
110
111 primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp)
112 {
113 auto result = *this;
114 --m_it;
115 return result;
116 }
117
118 primitive_iterator_t& operator+=(difference_type n) noexcept
119 {
120 m_it += n;
121 return *this;
122 }
123
124 primitive_iterator_t& operator-=(difference_type n) noexcept
125 {
126 m_it -= n;
127 return *this;
128 }
129};
130
131} // namespace detail
#define WPI_JSON_NAMESPACE_END
Definition: abi_macros.h:59
#define WPI_JSON_NAMESPACE_BEGIN
Definition: abi_macros.h:53
Definition: primitive_iterator.h:30
primitive_iterator_t & operator++() noexcept
Definition: primitive_iterator.h:92
primitive_iterator_t & operator-=(difference_type n) noexcept
Definition: primitive_iterator.h:124
constexpr bool is_begin() const noexcept
return whether the iterator can be dereferenced
Definition: primitive_iterator.h:59
primitive_iterator_t & operator--() noexcept
Definition: primitive_iterator.h:105
void set_end() noexcept
set iterator to a defined past the end
Definition: primitive_iterator.h:53
friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
Definition: primitive_iterator.h:75
constexpr bool is_end() const noexcept
return whether the iterator is at end
Definition: primitive_iterator.h:65
primitive_iterator_t operator++(int) &noexcept
Definition: primitive_iterator.h:98
primitive_iterator_t & operator+=(difference_type n) noexcept
Definition: primitive_iterator.h:118
friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
Definition: primitive_iterator.h:70
constexpr difference_type get_value() const noexcept
Definition: primitive_iterator.h:41
friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
Definition: primitive_iterator.h:87
primitive_iterator_t operator+(difference_type n) noexcept
Definition: primitive_iterator.h:80
void set_begin() noexcept
set iterator to a defined beginning
Definition: primitive_iterator.h:47
primitive_iterator_t operator--(int) &noexcept
Definition: primitive_iterator.h:111
#define JSON_PRIVATE_UNLESS_TESTED
Definition: macro_scope.h:199
detail namespace with internal helper functions
Definition: xchar.h:20
UnitTypeLhs() min(const UnitTypeLhs &lhs, const UnitTypeRhs &rhs)
Definition: base.h:3409