DAW JSON Link
Loading...
Searching...
No Matches
daw_json_parse_kv_array_iterator.h
Go to the documentation of this file.
1// Copyright( c ) Darrell Wright
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5//
6// Official repository: https://github.com/beached/daw_json_link
7//
8
9#pragma once
10
11#include "version.h"
12
14#include "daw_json_assert.h"
16#include "daw_json_traits.h"
17
18#include <daw/daw_attributes.h>
19#include <daw/daw_move.h>
20
21namespace daw::json {
22 inline namespace DAW_JSON_VER {
23 namespace json_details {
24 template<typename ParseState, bool>
25 struct json_parse_kv_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;
29 ParseState *parse_state = nullptr;
30 };
31
32 template<typename ParseState>
33 struct json_parse_kv_array_iterator_base<ParseState, true> {
34#if defined( DAW_JSON_HAS_CPP23_RANGE_CTOR )
35 using iterator_category = std::input_iterator_tag;
36#else
37 // We have to lie so that std::distance uses O(1) instead of O(N)
38 using iterator_category = std::random_access_iterator_tag;
39#endif
40 using difference_type = std::ptrdiff_t;
41 static constexpr bool has_counter = true;
42 ParseState *parse_state = nullptr;
43 difference_type counter = 0;
44
45 explicit json_parse_kv_array_iterator_base( ) = default;
46
47 explicit constexpr json_parse_kv_array_iterator_base(
48 daw::not_null<ParseState *> pd ) noexcept
49 : parse_state( pd )
50 , counter( static_cast<difference_type>( pd->counter ) ) {}
51
52 constexpr difference_type
53 operator-( json_parse_kv_array_iterator_base const &rhs ) const {
54 // rhs is the iterator with the parser in it. We should know how many
55 // items are in play because we already counted them in the skip_array
56 // call.
57 return rhs.counter;
58 }
59 };
60
61 template<typename JsonMember, typename ParseState, bool KnownBounds>
62 struct json_parse_kv_array_iterator final
63 : json_parse_kv_array_iterator_base<
64 ParseState, can_be_random_iterator_v<KnownBounds>> {
65
66 using base = json_parse_kv_array_iterator_base<
67 ParseState, can_be_random_iterator_v<KnownBounds>>;
68 using iterator_category = typename base::iterator_category;
69 using json_key_t = typename JsonMember::json_key_t;
70 using json_element_t = typename JsonMember::json_value_t;
71 using value_type = std::pair<json_result_t<json_key_t> const,
72 json_result_t<json_element_t>>;
73 using reference = value_type;
74 using pointer = arrow_proxy<value_type>;
75 using parse_state_t = ParseState;
76 using difference_type = typename base::difference_type;
77
78 using json_class_type = typename JsonMember::json_class_t;
79 explicit json_parse_kv_array_iterator( ) = default;
80
81 explicit constexpr json_parse_kv_array_iterator( parse_state_t &r )
82 : base{ &r } {
83 if( DAW_UNLIKELY( base::parse_state->front( ) == ']' ) ) {
84 if constexpr( not KnownBounds ) {
85 // Cleanup at end of value
86 base::parse_state->remove_prefix( );
87 base::parse_state->trim_left_checked( );
88 // Ensure we are equal to default
89 }
90 base::parse_state = nullptr;
91 }
92 }
93
94 static constexpr value_type
95 get_pair( json_result_t<json_class_type> &&v ) {
96 return value_type( std::get<0>( std::move( v.members ) ),
97 std::get<1>( std::move( v.members ) ) );
98 }
99
100 DAW_ATTRIB_NOINLINE value_type operator*( ) const {
101 // This is hear to satisfy indirectly_readable
102 daw_json_error( true, ErrorReason::UnexpectedEndOfData );
103 }
104
105 DAW_ATTRIB_INLINE constexpr value_type operator*( ) {
106 daw_json_assert_weak( base::parse_state and
107 base::parse_state->has_more( ),
108 ErrorReason::UnexpectedEndOfData,
109 *base::parse_state );
110
111 return get_pair(
112 parse_value<json_class_type, false, JsonParseTypes::Class>(
113 *base::parse_state ) );
114 }
115
116 DAW_ATTRIB_INLINE constexpr json_parse_kv_array_iterator &
117 operator++( ) {
118 daw_json_assert_weak( base::parse_state,
119 ErrorReason::UnexpectedEndOfData );
120 base::parse_state->trim_left( );
121
123 base::parse_state->has_more( ) and
124 base::parse_state->is_at_next_array_element( ),
125 ErrorReason::UnexpectedEndOfData,
126 *base::parse_state );
127
128 base::parse_state->move_next_member_or_end( );
129 daw_json_assert_weak( base::parse_state->has_more( ),
130 ErrorReason::UnexpectedEndOfData );
131 if( DAW_UNLIKELY( base::parse_state->front( ) == ']' ) ) {
132#if not defined( NDEBUG )
133 if constexpr( base::has_counter ) {
134 daw_json_assert_weak( base::counter == 0,
135 ErrorReason::UnexpectedEndOfData );
136 }
137#endif
138 if constexpr( not KnownBounds ) {
139 // Cleanup at end of value
140 base::parse_state->remove_prefix( );
141 base::parse_state->trim_left_checked( );
142 // Ensure we are equal to default
143 }
144 base::parse_state = nullptr;
145 }
146#if not defined( NDEBUG )
147 if constexpr( base::has_counter ) {
148 daw_json_assert_weak( base::counter > 0,
149 ErrorReason::UnexpectedEndOfData );
150 --base::counter;
151 }
152#endif
153 return *this;
154 }
155
156 DAW_ATTRIB_INLINE constexpr void operator++( int ) {
157 (void)operator++( );
158 }
159
160 friend constexpr bool
161 operator==( json_parse_kv_array_iterator const &lhs,
162 json_parse_kv_array_iterator const &rhs ) {
163 return lhs.parse_state == rhs.parse_state;
164 }
165
166 friend constexpr bool
167 operator!=( json_parse_kv_array_iterator const &lhs,
168 json_parse_kv_array_iterator const &rhs ) {
169 return not( lhs == rhs );
170 }
171 };
172 } // namespace json_details
173 } // namespace DAW_JSON_VER
174} // namespace daw::json
#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.
DAW_ATTRIB_NOINLINE void daw_json_error(bool b, ErrorReason reason)
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20