simdjson  3.11.0
Ridiculously Fast JSON
array_iterator-inl.h
1 #ifndef SIMDJSON_GENERIC_ONDEMAND_ARRAY_ITERATOR_INL_H
2 
3 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
4 #define SIMDJSON_GENERIC_ONDEMAND_ARRAY_ITERATOR_INL_H
5 #include "simdjson/generic/ondemand/base.h"
6 #include "simdjson/generic/ondemand/array_iterator.h"
7 #include "simdjson/generic/ondemand/value-inl.h"
8 #include "simdjson/generic/ondemand/value_iterator-inl.h"
9 #endif // SIMDJSON_CONDITIONAL_INCLUDE
10 
11 namespace simdjson {
12 namespace SIMDJSON_IMPLEMENTATION {
13 namespace ondemand {
14 
15 simdjson_inline array_iterator::array_iterator(const value_iterator &_iter) noexcept
16  : iter{_iter}
17 {}
18 
20  if (iter.error()) { iter.abandon(); return iter.error(); }
21  return value(iter.child());
22 }
23 simdjson_inline bool array_iterator::operator==(const array_iterator &other) const noexcept {
24  return !(*this != other);
25 }
26 simdjson_inline bool array_iterator::operator!=(const array_iterator &) const noexcept {
27  return iter.is_open();
28 }
29 simdjson_inline array_iterator &array_iterator::operator++() noexcept {
30  error_code error;
31  // PERF NOTE this is a safety rail ... users should exit loops as soon as they receive an error, so we'll never get here.
32  // However, it does not seem to make a perf difference, so we add it out of an abundance of caution.
33  if (( error = iter.error() )) { return *this; }
34  if (( error = iter.skip_child() )) { return *this; }
35  if (( error = iter.has_next_element().error() )) { return *this; }
36  return *this;
37 }
38 
39 } // namespace ondemand
40 } // namespace SIMDJSON_IMPLEMENTATION
41 } // namespace simdjson
42 
43 namespace simdjson {
44 
45 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::simdjson_result(
46  SIMDJSON_IMPLEMENTATION::ondemand::array_iterator &&value
47 ) noexcept
48  : SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>(value))
49 {
50  first.iter.assert_is_valid();
51 }
52 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::simdjson_result(error_code error) noexcept
53  : SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>({}, error)
54 {
55 }
56 
57 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::operator*() noexcept {
58  if (error()) { return error(); }
59  return *first;
60 }
61 simdjson_inline bool simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::operator==(const simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> &other) const noexcept {
62  if (!first.iter.is_valid()) { return !error(); }
63  return first == other.first;
64 }
65 simdjson_inline bool simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::operator!=(const simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> &other) const noexcept {
66  if (!first.iter.is_valid()) { return error(); }
67  return first != other.first;
68 }
69 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> &simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::operator++() noexcept {
70  // Clear the error if there is one, so we don't yield it twice
71  if (error()) { second = SUCCESS; return *this; }
72  ++(first);
73  return *this;
74 }
75 
76 } // namespace simdjson
77 
78 #endif // SIMDJSON_GENERIC_ONDEMAND_ARRAY_ITERATOR_INL_H
simdjson_inline simdjson_result< value > operator*() noexcept
Get the current element.
simdjson_inline bool operator==(const array_iterator &) const noexcept
Check if we are at the end of the JSON.
simdjson_inline array_iterator & operator++() noexcept
Move to the next element.
simdjson_inline array_iterator() noexcept=default
Create a new, invalid array iterator.
simdjson_inline bool operator!=(const array_iterator &) const noexcept
Check if there are more elements in the JSON array.
An ephemeral JSON value returned during iteration.
Definition: value.h:21
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8
error_code
All possible errors returned by simdjson.
Definition: error.h:19
@ SUCCESS
No error.
Definition: error.h:20
The result of a simdjson operation that could fail.
Definition: error.h:215
simdjson_inline error_code error() const noexcept
The error.
Definition: error-inl.h:131