DAW JSON Link
daw_json_parse_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 "daw_json_arrow_proxy.h"
12 #include "daw_json_assert.h"
14 
15 #include <ciso646>
16 
17 namespace daw::json::json_details {
18  template<typename Range, bool>
19  struct json_parse_array_iterator_base {
20  using iterator_category = std::input_iterator_tag;
21  using difference_type = std::ptrdiff_t;
22  static constexpr bool has_counter = false;
23  Range *rng = nullptr;
24  };
25 
26  template<typename Range>
27  struct json_parse_array_iterator_base<Range, true> {
28  // We have to lie so that std::distance uses O(1) instead of O(N)
29  using iterator_category = std::random_access_iterator_tag;
30  using difference_type = std::ptrdiff_t;
31  static constexpr bool has_counter = true;
32  Range *rng = nullptr;
33 
34  constexpr difference_type
35  operator-( json_parse_array_iterator_base const &rhs ) const {
36  if( rhs.rng ) {
37  return static_cast<difference_type>( rhs.rng->counter ) + 1;
38  }
39  return 0;
40  }
41  };
42 
43  template<typename JsonMember, typename Range, bool KnownBounds>
44  struct json_parse_array_iterator
45  : json_parse_array_iterator_base<Range, can_random_v<KnownBounds>> {
46 
47  using base =
48  json_parse_array_iterator_base<Range, can_random_v<KnownBounds>>;
49  using iterator_category = typename base::iterator_category;
50  using element_t = typename JsonMember::json_element_t;
51  using value_type = typename element_t::parse_to_t;
52  using reference = value_type;
53  using pointer = arrow_proxy<value_type>;
54  using iterator_range_t = Range;
55  using difference_type = typename base::difference_type;
56  bool at_first = true;
57  inline constexpr json_parse_array_iterator( ) = default;
58 
59  inline constexpr explicit json_parse_array_iterator( iterator_range_t &r )
60  : base{ &r } {
61  if( base::rng->front( ) == ']' ) {
62  if constexpr( not KnownBounds ) {
63  // Cleanup at end of value
64  base::rng->remove_prefix( );
65  base::rng->trim_left_checked( );
66  // Ensure we are equal to default
67  }
68  base::rng = nullptr;
69  }
70  }
71 
72  inline constexpr value_type operator*( ) {
73  daw_json_assert_weak( base::rng and base::rng->has_more( ),
74  ErrorReason::UnexpectedEndOfData, *base::rng );
75  at_first = false;
76  return parse_value<element_t>( ParseTag<element_t::expected_type>{ },
77  *base::rng );
78  }
79 
80  inline constexpr json_parse_array_iterator &operator++( ) {
81  // daw_json_assert_weak( base::rng, "Unexpected increment", *base::rng );
82  base::rng->template clean_end_of_value<']'>( at_first );
83  daw_json_assert_weak( base::rng->has_more( ),
84  ErrorReason::UnexpectedEndOfData, *base::rng );
85  if( base::rng->front( ) == ']' ) {
86 #ifndef NDEBUG
87  if constexpr( base::has_counter ) {
88  daw_json_assert_weak( base::rng->counter == 0,
89  ErrorReason::AttemptToAccessPastEndOfValue,
90  *base::rng );
91  }
92 #endif
93  if constexpr( not KnownBounds ) {
94  // Cleanup at end of value
95  base::rng->remove_prefix( );
96  base::rng->trim_left_checked( );
97  // Ensure we are equal to default
98  }
99  base::rng = nullptr;
100  }
101 #ifndef NDEBUG
102  if constexpr( base::has_counter ) {
103  if( base::rng ) {
104  daw_json_assert_weak( base::rng->counter > 0,
105  ErrorReason::AttemptToAccessPastEndOfValue,
106  *base::rng );
107  base::rng->counter--;
108  }
109  }
110 #endif
111  return *this;
112  }
113 
114  inline constexpr bool
115  operator==( json_parse_array_iterator const &rhs ) const {
116  return base::rng == rhs.base::rng;
117  }
118 
119  inline constexpr bool
120  operator!=( json_parse_array_iterator const &rhs ) const {
121  return base::rng != rhs.base::rng;
122  }
123  };
124 } // namespace daw::json::json_details
daw_json_assert_weak
#define daw_json_assert_weak(Bool,...)
Definition: daw_json_assert.h:206
daw_json_parse_value_fwd.h
daw_json_arrow_proxy.h
daw_json_assert.h