• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bblanchon / ArduinoJson / 5645229289

pending completion
5645229289

push

github

bblanchon
Remains

3427 of 3441 relevant lines covered (99.59%)

8803.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.87
/src/ArduinoJson/Array/JsonArray.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2023, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Array/ElementProxy.hpp>
8
#include <ArduinoJson/Array/JsonArrayConst.hpp>
9

10
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
11

12
class JsonObject;
13

14
// A reference to an array in a JsonDocument
15
// https://arduinojson.org/v6/api/jsonarray/
16
class JsonArray : public detail::VariantOperators<JsonArray> {
17
  friend class detail::VariantAttorney;
18

19
 public:
20
  typedef JsonArrayIterator iterator;
21

22
  // Constructs an unbound reference.
23
  FORCE_INLINE JsonArray() : data_(0), resources_(0) {}
18✔
24

25
  // INTERNAL USE ONLY
26
  FORCE_INLINE JsonArray(detail::ArrayData* data,
27
                         detail::ResourceManager* resources)
28
      : data_(data), resources_(resources) {}
146✔
29

30
  // Returns a JsonVariant pointing to the array.
31
  // https://arduinojson.org/v6/api/jsonvariant/
32
  operator JsonVariant() {
8✔
33
    void* data = data_;  // prevent warning cast-align
8✔
34
    return JsonVariant(reinterpret_cast<detail::VariantData*>(data),
8✔
35
                       resources_);
8✔
36
  }
37

38
  // Returns a read-only reference to the array.
39
  // https://arduinojson.org/v6/api/jsonarrayconst/
40
  operator JsonArrayConst() const {
35✔
41
    return JsonArrayConst(data_, resources_);
35✔
42
  }
43

44
  // Appends a new (null) element to the array.
45
  // Returns a reference to the new element.
46
  // https://arduinojson.org/v6/api/jsonarray/add/
47
  JsonVariant add() const {
66,199✔
48
    return JsonVariant(detail::ArrayData::addElement(data_, resources_),
66,199✔
49
                       resources_);
132,398✔
50
  }
51

52
  // Appends a value to the array.
53
  // https://arduinojson.org/v6/api/jsonarray/add/
54
  template <typename T>
55
  FORCE_INLINE bool add(const T& value) const {
56
    return add().set(value);
1,067✔
57
  }
58

59
  // Appends a value to the array.
60
  // https://arduinojson.org/v6/api/jsonarray/add/
61
  template <typename T>
62
  FORCE_INLINE bool add(T* value) const {
63
    return add().set(value);
131,204✔
64
  }
65

66
  // Returns an iterator to the first element of the array.
67
  // https://arduinojson.org/v6/api/jsonarray/begin/
68
  FORCE_INLINE iterator begin() const {
69
    if (!data_)
7✔
70
      return iterator();
2✔
71
    return iterator(data_->createIterator(resources_), resources_);
5✔
72
  }
73

74
  // Returns an iterator following the last element of the array.
75
  // https://arduinojson.org/v6/api/jsonarray/end/
76
  FORCE_INLINE iterator end() const {
77
    return iterator();
6✔
78
  }
79

80
  // Copies an array.
81
  // https://arduinojson.org/v6/api/jsonarray/set/
82
  FORCE_INLINE bool set(JsonArrayConst src) const {
83
    if (!data_)
13✔
84
      return false;
×
85

86
    clear();
13✔
87
    for (auto element : src) {
31✔
88
      if (!add(element))
6✔
89
        return false;
1✔
90
    }
91

92
    return true;
12✔
93
  }
94

95
  // Removes the element at the specified iterator.
96
  // ⚠️ Doesn't release the memory associated with the removed element.
97
  // https://arduinojson.org/v6/api/jsonarray/remove/
98
  FORCE_INLINE void remove(iterator it) const {
99
    detail::ArrayData::remove(data_, it.iterator_, resources_);
5✔
100
  }
5✔
101

102
  // Removes the element at the specified index.
103
  // ⚠️ Doesn't release the memory associated with the removed element.
104
  // https://arduinojson.org/v6/api/jsonarray/remove/
105
  FORCE_INLINE void remove(size_t index) const {
106
    detail::ArrayData::removeElement(data_, index, resources_);
5✔
107
  }
5✔
108

109
  // Removes all the elements of the array.
110
  // ⚠️ Doesn't release the memory associated with the removed elements.
111
  // https://arduinojson.org/v6/api/jsonarray/clear/
112
  void clear() const {
16✔
113
    detail::ArrayData::clear(data_, resources_);
16✔
114
  }
16✔
115

116
  // Gets or sets the element at the specified index.
117
  // https://arduinojson.org/v6/api/jsonarray/subscript/
118
  FORCE_INLINE detail::ElementProxy<JsonArray> operator[](size_t index) const {
119
    return {*this, index};
232✔
120
  }
121

122
  // Creates an object and appends it to the array.
123
  // https://arduinojson.org/v6/api/jsonarray/createnestedobject/
124
  FORCE_INLINE JsonObject createNestedObject() const;
125

126
  // Creates an array and appends it to the array.
127
  // https://arduinojson.org/v6/api/jsonarray/createnestedarray/
128
  FORCE_INLINE JsonArray createNestedArray() const {
129
    return add().to<JsonArray>();
14✔
130
  }
131

132
  operator JsonVariantConst() const {
272✔
133
    return JsonVariantConst(collectionToVariant(data_), resources_);
272✔
134
  }
135

136
  // Returns true if the reference is unbound.
137
  // https://arduinojson.org/v6/api/jsonarray/isnull/
138
  FORCE_INLINE bool isNull() const {
139
    return data_ == 0;
25✔
140
  }
141

142
  // Returns true if the reference is bound.
143
  // https://arduinojson.org/v6/api/jsonarray/isnull/
144
  FORCE_INLINE operator bool() const {
145
    return data_ != 0;
2✔
146
  }
147

148
  // Returns the depth (nesting level) of the array.
149
  // https://arduinojson.org/v6/api/jsonarray/nesting/
150
  FORCE_INLINE size_t nesting() const {
151
    return detail::VariantData::nesting(collectionToVariant(data_), resources_);
5✔
152
  }
153

154
  // Returns the number of elements in the array.
155
  // https://arduinojson.org/v6/api/jsonarray/size/
156
  FORCE_INLINE size_t size() const {
157
    return data_ ? data_->size(resources_) : 0;
76✔
158
  }
159

160
 private:
161
  detail::ResourceManager* getResourceManager() const {
512✔
162
    return resources_;
512✔
163
  }
164

165
  detail::VariantData* getData() const {
225✔
166
    return collectionToVariant(data_);
225✔
167
  }
168

169
  detail::VariantData* getOrCreateData() const {
23✔
170
    return collectionToVariant(data_);
23✔
171
  }
172

173
  detail::ArrayData* data_;
174
  detail::ResourceManager* resources_;
175
};
176

177
ARDUINOJSON_END_PUBLIC_NAMESPACE
178

179
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
180

181
template <typename TDerived>
182
template <typename T>
183
inline typename enable_if<is_same<T, JsonArray>::value, JsonArray>::type
184
VariantRefBase<TDerived>::to() const {
220✔
185
  return JsonArray(
186
      VariantData::toArray(getOrCreateData(), getResourceManager()),
187
      getResourceManager());
660✔
188
}
189

190
ARDUINOJSON_END_PRIVATE_NAMESPACE
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc