17 #include <daw/daw_attributes.h>
19 #include <type_traits>
23 namespace json_details {
24 template<
typename ParseState,
bool>
25 struct json_parse_array_iterator_base {
26 using iterator_category = std::input_iterator_tag;
27 using difference_type = std::ptrdiff_t;
28 static constexpr
bool has_counter =
false;
30 ParseState *parse_state =
nullptr;
33 template<
typename ParseState>
34 struct json_parse_array_iterator_base<ParseState, true> {
35 #if defined( DAW_JSON_HAS_CPP23_RANGE_CTOR )
36 using iterator_category = std::input_iterator_tag;
38 using iterator_category = std::random_access_iterator_tag;
40 using difference_type = std::ptrdiff_t;
41 static constexpr
bool has_counter =
true;
43 ParseState *parse_state =
nullptr;
44 difference_type counter = 0;
46 explicit json_parse_array_iterator_base( ) =
default;
49 explicit inline constexpr
50 json_parse_array_iterator_base( ParseState *pd ) noexcept
52 , counter(
static_cast<difference_type
>( pd->counter ) ) {}
54 constexpr difference_type
55 operator-( json_parse_array_iterator_base
const &rhs )
const {
64 template<
typename JsonMember,
typename ParseState,
bool KnownBounds>
65 struct json_parse_array_iterator
66 : json_parse_array_iterator_base<
67 ParseState, can_be_random_iterator_v<KnownBounds>> {
70 json_parse_array_iterator_base<ParseState,
71 can_be_random_iterator_v<KnownBounds>>;
73 using iterator_category =
typename base::iterator_category;
74 using element_t =
typename JsonMember::json_element_t;
75 using value_type = json_result_t<element_t>;
76 using reference = value_type;
77 using pointer = arrow_proxy<value_type>;
78 using parse_state_t = ParseState;
79 using difference_type =
typename base::difference_type;
80 using size_type = std::size_t;
82 json_parse_array_iterator( ) =
default;
83 #if defined( DAW_JSON_USE_FULL_DEBUG_ITERATORS )
86 json_parse_array_iterator( json_parse_array_iterator
const & ) =
88 json_parse_array_iterator &
89 operator=( json_parse_array_iterator
const & ) =
default;
90 json_parse_array_iterator( json_parse_array_iterator && ) =
default;
91 json_parse_array_iterator &
92 operator=( json_parse_array_iterator && ) =
default;
94 if constexpr( base::has_counter ) {
96 ErrorReason::AttemptToAccessPastEndOfValue );
100 constexpr
explicit json_parse_array_iterator( parse_state_t &r )
102 if( DAW_UNLIKELY( base::parse_state->front( ) ==
']' ) ) {
103 if constexpr( not KnownBounds ) {
105 base::parse_state->remove_prefix( );
106 base::parse_state->trim_left_checked( );
109 base::parse_state =
nullptr;
113 [[noreturn]] DAW_ATTRIB_NOINLINE value_type operator*( )
const {
119 DAW_ATTRIB_INLINE constexpr value_type operator*( ) {
121 base::parse_state and base::parse_state->has_more( ),
122 ErrorReason::UnexpectedEndOfData, *base::parse_state );
124 return parse_value<element_t, false, element_t::expected_type>(
125 *base::parse_state );
128 DAW_ATTRIB_INLINE constexpr json_parse_array_iterator &operator++( ) {
130 ErrorReason::UnexpectedEndOfData,
131 *base::parse_state );
132 base::parse_state->trim_left( );
135 base::parse_state->has_more( ) and
136 base::parse_state->is_at_next_array_element( ),
137 ErrorReason::UnexpectedEndOfData, *base::parse_state );
139 base::parse_state->move_next_member_or_end( );
141 ErrorReason::UnexpectedEndOfData,
142 *base::parse_state );
143 if( base::parse_state->front( ) ==
']' ) {
144 #if not defined( NDEBUG )
145 if constexpr( base::has_counter ) {
147 ErrorReason::AttemptToAccessPastEndOfValue,
148 *base::parse_state );
151 if constexpr( not KnownBounds ) {
153 base::parse_state->remove_prefix( );
154 base::parse_state->trim_left_checked( );
157 base::parse_state =
nullptr;
159 #if not defined( NDEBUG )
160 if constexpr( base::has_counter ) {
162 ErrorReason::AttemptToAccessPastEndOfValue,
163 *base::parse_state );
171 DAW_ATTRIB_INLINE constexpr
void operator++(
int ) {
175 friend inline constexpr
bool
176 operator==( json_parse_array_iterator
const &lhs,
177 json_parse_array_iterator
const &rhs ) {
178 return lhs.parse_state == rhs.parse_state;
181 friend inline constexpr
bool
182 operator!=( json_parse_array_iterator
const &lhs,
183 json_parse_array_iterator
const &rhs ) {
184 return not( lhs == rhs );
187 constexpr json_parse_array_iterator &begin( ) {
191 constexpr json_parse_array_iterator end( )
const {
#define daw_json_assert_weak(Bool,...)
Assert that Bool is true when in Checked Input mode If false pass rest of args to daw_json_error.
#define DAW_JSON_CPP20_CX_DTOR
DAW_ATTRIB_NOINLINE void daw_json_error(ErrorReason reason)
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.