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

bblanchon / ArduinoJson / 9131102872

17 May 2024 03:10PM CUT coverage: 99.553% (-0.001%) from 99.554%
9131102872

push

github

bblanchon
Move `CollectionData::releaseSlot()` to `ResourceManager::freeSlot()`

8 of 8 new or added lines in 2 files covered. (100.0%)

3790 of 3807 relevant lines covered (99.55%)

10947.29 hits per line

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

98.63
/src/ArduinoJson/Array/JsonArray.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2024, 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/v7/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
  JsonArray() : data_(0), resources_(0) {}
18✔
24

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

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

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

43
  // Appends a new (empty) element to the array.
44
  // Returns a reference to the new element.
45
  // https://arduinojson.org/v7/api/jsonarray/add/
46
  template <typename T>
47
  typename detail::enable_if<!detail::is_same<T, JsonVariant>::value, T>::type
48
  add() const {
36✔
49
    return add<JsonVariant>().to<T>();
72✔
50
  }
51

52
  // Appends a new (null) element to the array.
53
  // Returns a reference to the new element.
54
  // https://arduinojson.org/v7/api/jsonarray/add/
55
  template <typename T>
56
  typename detail::enable_if<detail::is_same<T, JsonVariant>::value, T>::type
57
  add() const {
66,227✔
58
    return JsonVariant(detail::ArrayData::addElement(data_, resources_),
66,227✔
59
                       resources_);
66,227✔
60
  }
61

62
  // Appends a value to the array.
63
  // https://arduinojson.org/v7/api/jsonarray/add/
64
  template <typename T>
65
  bool add(const T& value) const {
553✔
66
    return add<JsonVariant>().set(value);
553✔
67
  }
68

69
  // Appends a value to the array.
70
  // https://arduinojson.org/v7/api/jsonarray/add/
71
  template <typename T>
72
  bool add(T* value) const {
65,609✔
73
    return add<JsonVariant>().set(value);
65,609✔
74
  }
75

76
  // Returns an iterator to the first element of the array.
77
  // https://arduinojson.org/v7/api/jsonarray/begin/
78
  iterator begin() const {
7✔
79
    if (!data_)
7✔
80
      return iterator();
2✔
81
    return iterator(data_->createIterator(resources_), resources_);
5✔
82
  }
83

84
  // Returns an iterator following the last element of the array.
85
  // https://arduinojson.org/v7/api/jsonarray/end/
86
  iterator end() const {
6✔
87
    return iterator();
6✔
88
  }
89

90
  // Copies an array.
91
  // https://arduinojson.org/v7/api/jsonarray/set/
92
  bool set(JsonArrayConst src) const {
13✔
93
    if (!data_)
13✔
94
      return false;
×
95

96
    clear();
13✔
97
    for (auto element : src) {
19✔
98
      if (!add(element))
7✔
99
        return false;
1✔
100
    }
101

102
    return true;
12✔
103
  }
104

105
  // Removes the element at the specified iterator.
106
  // https://arduinojson.org/v7/api/jsonarray/remove/
107
  void remove(iterator it) const {
5✔
108
    detail::ArrayData::remove(data_, it.iterator_, resources_);
5✔
109
  }
5✔
110

111
  // Removes the element at the specified index.
112
  // https://arduinojson.org/v7/api/jsonarray/remove/
113
  void remove(size_t index) const {
6✔
114
    detail::ArrayData::removeElement(data_, index, resources_);
6✔
115
  }
6✔
116

117
  // Removes the element at the specified index.
118
  // https://arduinojson.org/v7/api/jsonarray/remove/
119
  template <typename TVariant>
120
  typename detail::enable_if<detail::IsVariant<TVariant>::value>::type remove(
2✔
121
      TVariant variant) const {
122
    if (variant.template is<size_t>())
2✔
123
      remove(variant.template as<size_t>());
1✔
124
  }
2✔
125

126
  // Removes all the elements of the array.
127
  // https://arduinojson.org/v7/api/jsonarray/clear/
128
  void clear() const {
16✔
129
    detail::ArrayData::clear(data_, resources_);
16✔
130
  }
16✔
131

132
  // Gets or sets the element at the specified index.
133
  // https://arduinojson.org/v7/api/jsonarray/subscript/
134
  template <typename T>
135
  typename detail::enable_if<detail::is_integral<T>::value,
136
                             detail::ElementProxy<JsonArray>>::type
137
  operator[](T index) const {
255✔
138
    return {*this, size_t(index)};
255✔
139
  }
140

141
  // Gets or sets the element at the specified index.
142
  // https://arduinojson.org/v7/api/jsonarray/subscript/
143
  template <typename TVariant>
144
  typename detail::enable_if<detail::IsVariant<TVariant>::value,
145
                             detail::ElementProxy<JsonArray>>::type
146
  operator[](const TVariant& variant) const {
2✔
147
    if (variant.template is<size_t>())
2✔
148
      return operator[](variant.template as<size_t>());
1✔
149
    else
150
      return {*this, size_t(-1)};
1✔
151
  }
152

153
  operator JsonVariantConst() const {
271✔
154
    return JsonVariantConst(collectionToVariant(data_), resources_);
271✔
155
  }
156

157
  // Returns true if the reference is unbound.
158
  // https://arduinojson.org/v7/api/jsonarray/isnull/
159
  bool isNull() const {
19✔
160
    return data_ == 0;
19✔
161
  }
162

163
  // Returns true if the reference is bound.
164
  // https://arduinojson.org/v7/api/jsonarray/isnull/
165
  operator bool() const {
2✔
166
    return data_ != 0;
2✔
167
  }
168

169
  // Returns the depth (nesting level) of the array.
170
  // https://arduinojson.org/v7/api/jsonarray/nesting/
171
  size_t nesting() const {
5✔
172
    return detail::VariantData::nesting(collectionToVariant(data_), resources_);
5✔
173
  }
174

175
  // Returns the number of elements in the array.
176
  // https://arduinojson.org/v7/api/jsonarray/size/
177
  size_t size() const {
77✔
178
    return data_ ? data_->size(resources_) : 0;
77✔
179
  }
180

181
  // DEPRECATED: use add<JsonVariant>() instead
182
  ARDUINOJSON_DEPRECATED("use add<JsonVariant>() instead")
183
  JsonVariant add() const {
1✔
184
    return add<JsonVariant>();
1✔
185
  }
186

187
  // DEPRECATED: use add<JsonArray>() instead
188
  ARDUINOJSON_DEPRECATED("use add<JsonArray>() instead")
189
  JsonArray createNestedArray() const {
1✔
190
    return add<JsonArray>();
1✔
191
  }
192

193
  // DEPRECATED: use add<JsonObject>() instead
194
  ARDUINOJSON_DEPRECATED("use add<JsonObject>() instead")
195
  JsonObject createNestedObject() const;
196

197
  // DEPRECATED: always returns zero
198
  ARDUINOJSON_DEPRECATED("always returns zero")
199
  size_t memoryUsage() const {
1✔
200
    return 0;
1✔
201
  }
202

203
 private:
204
  detail::ResourceManager* getResourceManager() const {
534✔
205
    return resources_;
534✔
206
  }
207

208
  detail::VariantData* getData() const {
233✔
209
    return collectionToVariant(data_);
233✔
210
  }
211

212
  detail::VariantData* getOrCreateData() const {
25✔
213
    return collectionToVariant(data_);
25✔
214
  }
215

216
  detail::ArrayData* data_;
217
  detail::ResourceManager* resources_;
218
};
219

220
ARDUINOJSON_END_PUBLIC_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