/src/arduinojson/src/ArduinoJson/Array/JsonArrayConst.hpp
Line | Count | Source |
1 | | // ArduinoJson - https://arduinojson.org |
2 | | // Copyright © 2014-2025, Benoit BLANCHON |
3 | | // MIT License |
4 | | |
5 | | #pragma once |
6 | | |
7 | | #include <ArduinoJson/Array/JsonArrayIterator.hpp> |
8 | | #include <ArduinoJson/Variant/VariantAttorney.hpp> |
9 | | #include <ArduinoJson/Variant/VariantData.hpp> |
10 | | |
11 | | ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE |
12 | | |
13 | | class JsonObject; |
14 | | |
15 | | // A read-only reference to an array in a JsonDocument |
16 | | // https://arduinojson.org/v7/api/jsonarrayconst/ |
17 | | class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> { |
18 | | friend class JsonArray; |
19 | | friend class detail::VariantAttorney; |
20 | | |
21 | | public: |
22 | | using iterator = JsonArrayConstIterator; |
23 | | |
24 | | // Returns an iterator to the first element of the array. |
25 | | // https://arduinojson.org/v7/api/jsonarrayconst/begin/ |
26 | 0 | iterator begin() const { |
27 | 0 | return iterator(impl_.createIterator(), impl_.resources()); |
28 | 0 | } |
29 | | |
30 | | // Returns an iterator to the element following the last element of the array. |
31 | | // https://arduinojson.org/v7/api/jsonarrayconst/end/ |
32 | 0 | iterator end() const { |
33 | 0 | return iterator(); |
34 | 0 | } |
35 | | |
36 | | // Creates an unbound reference. |
37 | 0 | JsonArrayConst() {} |
38 | | |
39 | | // INTERNAL USE ONLY |
40 | | JsonArrayConst(detail::VariantData* data, detail::ResourceManager* resources) |
41 | 0 | : impl_(data, resources) {} |
42 | | |
43 | | // INTERNAL USE ONLY |
44 | 0 | JsonArrayConst(const detail::VariantImpl& impl) : impl_(impl) {} |
45 | | |
46 | | // Returns the element at the specified index. |
47 | | // https://arduinojson.org/v7/api/jsonarrayconst/subscript/ |
48 | | template <typename T, |
49 | | detail::enable_if_t<detail::is_integral<T>::value, int> = 0> |
50 | | JsonVariantConst operator[](T index) const { |
51 | | return JsonVariantConst(impl_.getElement(size_t(index)), impl_.resources()); |
52 | | } |
53 | | |
54 | | // Returns the element at the specified index. |
55 | | // https://arduinojson.org/v7/api/jsonarrayconst/subscript/ |
56 | | template <typename TVariant, |
57 | | detail::enable_if_t<detail::IsVariant<TVariant>::value, int> = 0> |
58 | | JsonVariantConst operator[](const TVariant& variant) const { |
59 | | if (variant.template is<size_t>()) |
60 | | return operator[](variant.template as<size_t>()); |
61 | | else |
62 | | return JsonVariantConst(); |
63 | | } |
64 | | |
65 | 0 | operator JsonVariantConst() const { |
66 | 0 | return JsonVariantConst(impl_.data(), impl_.resources()); |
67 | 0 | } |
68 | | |
69 | | // Returns true if the reference is unbound. |
70 | | // https://arduinojson.org/v7/api/jsonarrayconst/isnull/ |
71 | 0 | bool isNull() const { |
72 | 0 | return impl_.isNull(); |
73 | 0 | } |
74 | | |
75 | | // Returns true if the reference is bound. |
76 | | // https://arduinojson.org/v7/api/jsonarrayconst/isnull/ |
77 | 0 | operator bool() const { |
78 | 0 | return !isNull(); |
79 | 0 | } |
80 | | |
81 | | // Returns the depth (nesting level) of the array. |
82 | | // https://arduinojson.org/v7/api/jsonarrayconst/nesting/ |
83 | 0 | size_t nesting() const { |
84 | 0 | return impl_.nesting(); |
85 | 0 | } |
86 | | |
87 | | // Returns the number of elements in the array. |
88 | | // https://arduinojson.org/v7/api/jsonarrayconst/size/ |
89 | 0 | size_t size() const { |
90 | 0 | return impl_.size(); |
91 | 0 | } |
92 | | |
93 | | // DEPRECATED: always returns zero |
94 | | ARDUINOJSON_DEPRECATED("always returns zero") |
95 | 0 | size_t memoryUsage() const { |
96 | 0 | return 0; |
97 | 0 | } |
98 | | |
99 | | private: |
100 | 0 | const detail::VariantData* getData() const { |
101 | 0 | return impl_.data(); |
102 | 0 | } |
103 | | |
104 | | detail::VariantImpl impl_; |
105 | | }; |
106 | | |
107 | | // Compares the content of two arrays. |
108 | | // Returns true if the two arrays are equal. |
109 | 0 | inline bool operator==(JsonArrayConst lhs, JsonArrayConst rhs) { |
110 | 0 | if (!lhs && !rhs) |
111 | 0 | return true; |
112 | 0 | if (!lhs || !rhs) |
113 | 0 | return false; |
114 | 0 |
|
115 | 0 | auto a = lhs.begin(); |
116 | 0 | auto b = rhs.begin(); |
117 | 0 |
|
118 | 0 | for (;;) { |
119 | 0 | if (a == b) // same pointer or both null |
120 | 0 | return true; |
121 | 0 | if (a == lhs.end() || b == rhs.end()) |
122 | 0 | return false; |
123 | 0 | if (*a != *b) |
124 | 0 | return false; |
125 | 0 | ++a; |
126 | 0 | ++b; |
127 | 0 | } |
128 | 0 | } |
129 | | |
130 | | ARDUINOJSON_END_PUBLIC_NAMESPACE |