simdjson 5.0.2
Ridiculously Fast JSON
Loading...
Searching...
No Matches
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
11namespace simdjson {
12namespace SIMDJSON_IMPLEMENTATION {
13namespace ondemand {
14
15simdjson_inline array_iterator::array_iterator(const value_iterator &_iter) noexcept
16 : iter{_iter}
17{}
18
19#if SIMDJSON_DEVELOPMENT_CHECKS
20simdjson_inline array_iterator::array_iterator(const value_iterator &_iter, array* _parent) noexcept
21 : parent{_parent}, iter{_iter}
22{
23 if (parent) parent->set_locked(true);
24}
25
26simdjson_inline array_iterator::~array_iterator() noexcept
27{
28 if (parent) parent->set_locked(false);
29}
30
31simdjson_inline array_iterator::array_iterator(array_iterator&& other) noexcept
32 : has_been_referenced{other.has_been_referenced},
33 parent{other.parent},
34 iter{std::move(other.iter)}
35{
36 other.parent = nullptr;
37}
38
39simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other) noexcept {
40 if (this != &other)
41 {
42 if (parent)
43 parent->set_locked(false);
44 has_been_referenced = other.has_been_referenced;
45 parent = other.parent;
46 iter = std::move(other.iter);
47
48 other.parent = nullptr;
49 }
50 return *this;
51}
52
53simdjson_inline array_iterator::array_iterator(const array_iterator& other) noexcept
54 : has_been_referenced{other.has_been_referenced},
55 parent{nullptr},
56 iter{other.iter}
57{}
58
59simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& other) noexcept {
60 if (this != &other)
61 {
62 if (parent)
63 parent->set_locked(false);
64 has_been_referenced = other.has_been_referenced;
65 parent = nullptr;
66 iter = other.iter;
67 }
68 return *this;
69}
70#endif
71
73#if SIMDJSON_DEVELOPMENT_CHECKS
74 SIMDJSON_ASSUME(!has_been_referenced);
75 has_been_referenced = true;
76#endif
77 if (iter.error()) { iter.abandon(); return iter.error(); }
78 return value(iter.child());
79}
80simdjson_inline bool array_iterator::operator==(const array_iterator &other) const noexcept {
81 return !(*this != other);
82}
83simdjson_inline bool array_iterator::operator!=(const array_iterator &) const noexcept {
84 return iter.is_open();
85}
86simdjson_inline array_iterator &array_iterator::operator++() noexcept {
87#if SIMDJSON_DEVELOPMENT_CHECKS
88 has_been_referenced = false;
89#endif
90 error_code error;
91 // PERF NOTE this is a safety rail ... users should exit loops as soon as they receive an error, so we'll never get here.
92 // However, it does not seem to make a perf difference, so we add it out of an abundance of caution.
93 if (( error = iter.error() )) { return *this; }
94 if (( error = iter.skip_child() )) { return *this; }
95 if (( error = iter.has_next_element().error() )) { return *this; }
96 return *this;
97}
98
99simdjson_inline bool array_iterator::at_end() const noexcept {
100 return iter.at_end();
101}
102} // namespace ondemand
103} // namespace SIMDJSON_IMPLEMENTATION
104} // namespace simdjson
105
106namespace simdjson {
107
108simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::simdjson_result(
109 SIMDJSON_IMPLEMENTATION::ondemand::array_iterator &&value
110) noexcept
111 : SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>(value))
112{
113 first.iter.assert_is_valid();
114}
115simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::simdjson_result(error_code error) noexcept
116 : SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>({}, error)
117{
118}
119
120simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::operator*() noexcept {
121 if (error()) { return error(); }
122 return *first;
123}
124simdjson_inline bool simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::operator==(const simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> &other) const noexcept {
125 if (!first.iter.is_valid()) { return !error(); }
126 return first == other.first;
127}
128simdjson_inline bool simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::operator!=(const simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> &other) const noexcept {
129 if (!first.iter.is_valid()) { return error(); }
130 return first != other.first;
131}
132simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> &simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::operator++() noexcept {
133 // Clear the error if there is one, so we don't yield it twice
134 if (error()) { second = SUCCESS; return *this; }
135 ++(first);
136 return *this;
137}
138simdjson_inline bool simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::at_end() const noexcept {
139 return !first.iter.is_valid() || first.at_end();
140}
141} // namespace simdjson
142
143#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.
simdjson_warn_unused simdjson_inline bool at_end() const noexcept
Check if the array is at the end.
An ephemeral JSON value returned during iteration.
Definition value.h:22
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:281