simdjson 4.3.1
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
20#if SIMDJSON_DEVELOPMENT_CHECKS
21 SIMDJSON_ASSUME(!has_been_referenced);
22 has_been_referenced = true;
23#endif
24 if (iter.error()) { iter.abandon(); return iter.error(); }
25 return value(iter.child());
26}
27simdjson_inline bool array_iterator::operator==(const array_iterator &other) const noexcept {
28 return !(*this != other);
29}
30simdjson_inline bool array_iterator::operator!=(const array_iterator &) const noexcept {
31 return iter.is_open();
32}
33simdjson_inline array_iterator &array_iterator::operator++() noexcept {
34#if SIMDJSON_DEVELOPMENT_CHECKS
35 has_been_referenced = false;
36#endif
37 error_code error;
38 // PERF NOTE this is a safety rail ... users should exit loops as soon as they receive an error, so we'll never get here.
39 // However, it does not seem to make a perf difference, so we add it out of an abundance of caution.
40 if (( error = iter.error() )) { return *this; }
41 if (( error = iter.skip_child() )) { return *this; }
42 if (( error = iter.has_next_element().error() )) { return *this; }
43 return *this;
44}
45
46simdjson_inline bool array_iterator::at_end() const noexcept {
47 return iter.at_end();
48}
49} // namespace ondemand
50} // namespace SIMDJSON_IMPLEMENTATION
51} // namespace simdjson
52
53namespace simdjson {
54
55simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::simdjson_result(
56 SIMDJSON_IMPLEMENTATION::ondemand::array_iterator &&value
57) noexcept
58 : SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>(value))
59{
60 first.iter.assert_is_valid();
61}
62simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::simdjson_result(error_code error) noexcept
63 : SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>({}, error)
64{
65}
66
67simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::operator*() noexcept {
68 if (error()) { return error(); }
69 return *first;
70}
71simdjson_inline bool simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::operator==(const simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> &other) const noexcept {
72 if (!first.iter.is_valid()) { return !error(); }
73 return first == other.first;
74}
75simdjson_inline bool simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::operator!=(const simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> &other) const noexcept {
76 if (!first.iter.is_valid()) { return error(); }
77 return first != other.first;
78}
79simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> &simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::operator++() noexcept {
80 // Clear the error if there is one, so we don't yield it twice
81 if (error()) { second = SUCCESS; return *this; }
82 ++(first);
83 return *this;
84}
85simdjson_inline bool simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator>::at_end() const noexcept {
86 return !first.iter.is_valid() || first.at_end();
87}
88} // namespace simdjson
89
90#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:280