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

bblanchon / ArduinoJson / 4655935094

pending completion
4655935094

push

github

Benoit Blanchon
Remove unused `MemoryPool::_left`

5 of 5 new or added lines in 1 file covered. (100.0%)

3305 of 3322 relevant lines covered (99.49%)

6124.39 hits per line

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

99.17
/src/ArduinoJson/Document/JsonDocument.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/Memory/Allocator.hpp>
9
#include <ArduinoJson/Memory/MemoryPool.hpp>
10
#include <ArduinoJson/Object/JsonObject.hpp>
11
#include <ArduinoJson/Object/MemberProxy.hpp>
12
#include <ArduinoJson/Polyfills/utility.hpp>
13
#include <ArduinoJson/Strings/StoragePolicy.hpp>
14
#include <ArduinoJson/Variant/JsonVariantConst.hpp>
15
#include <ArduinoJson/Variant/VariantTo.hpp>
16

17
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
18

19
// A JSON document.
20
// https://arduinojson.org/v6/api/jsondocument/
21
class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
22
  friend class detail::VariantAttorney;
23

24
 public:
25
  explicit JsonDocument(size_t capa,
1,947✔
26
                        Allocator* alloc = detail::DefaultAllocator::instance())
27
      : _pool(capa, alloc) {}
1,947✔
28

29
  // Copy-constructor
30
  JsonDocument(const JsonDocument& src)
4✔
31
      : JsonDocument(src._pool.capacity(), src.allocator()) {
4✔
32
    set(src);
4✔
33
  }
4✔
34

35
  // Move-constructor
36
  JsonDocument(JsonDocument&& src) : _pool(0, src.allocator()) {
2✔
37
    // TODO: use the copy and swap idiom
38
    moveAssignFrom(src);
2✔
39
  }
2✔
40

41
  // Construct from variant, array, or object
42
  template <typename T>
43
  JsonDocument(const T& src,
3✔
44
               Allocator* alloc = detail::DefaultAllocator::instance(),
45
               typename detail::enable_if<
46
                   detail::is_same<T, JsonVariant>::value ||
47
                   detail::is_same<T, JsonVariantConst>::value ||
48
                   detail::is_same<T, JsonArray>::value ||
49
                   detail::is_same<T, JsonArrayConst>::value ||
50
                   detail::is_same<T, JsonObject>::value ||
51
                   detail::is_same<T, JsonObjectConst>::value>::type* = 0)
52
      : JsonDocument(src.memoryUsage(), alloc) {
3✔
53
    set(src);
3✔
54
  }
3✔
55

56
  // disambiguate
57
  // TODO: still needed?
58
  JsonDocument(JsonVariant src) : JsonDocument(src.memoryUsage()) {
59
    set(src);
60
  }
61

62
  JsonDocument& operator=(const JsonDocument& src) {
3✔
63
    // TODO: use the copy and swap idiom
64
    copyAssignFrom(src);
3✔
65
    return *this;
3✔
66
  }
67

68
  JsonDocument& operator=(JsonDocument&& src) {
3✔
69
    // TODO: use the copy and swap idiom
70
    moveAssignFrom(src);
3✔
71
    return *this;
3✔
72
  }
73

74
  template <typename T>
75
  JsonDocument& operator=(const T& src) {
5✔
76
    size_t requiredSize = src.memoryUsage();
5✔
77
    if (requiredSize > _pool.capacity())
5✔
78
      _pool.reallocPool(requiredSize);
×
79
    set(src);
5✔
80
    return *this;
5✔
81
  }
82

83
  Allocator* allocator() const {
6✔
84
    return _pool.allocator();
6✔
85
  }
86

87
  // Reduces the capacity of the memory pool to match the current usage.
88
  // https://arduinojson.org/v6/api/JsonDocument/shrinktofit/
89
  void shrinkToFit() {
15✔
90
    _pool.shrinkToFit(_data);
15✔
91
  }
15✔
92

93
  // Reclaims the memory leaked when removing and replacing values.
94
  // https://arduinojson.org/v6/api/jsondocument/garbagecollect/
95
  bool garbageCollect() {
3✔
96
    // make a temporary clone and move assign
97
    JsonDocument tmp(*this);
6✔
98
    if (!tmp._pool.capacity())
3✔
99
      return false;
1✔
100
    moveAssignFrom(tmp);
2✔
101
    return true;
2✔
102
  }
103

104
  // Casts the root to the specified type.
105
  // https://arduinojson.org/v6/api/jsondocument/as/
106
  template <typename T>
107
  T as() {
502✔
108
    return getVariant().template as<T>();
1,004✔
109
  }
110

111
  // Casts the root to the specified type.
112
  // https://arduinojson.org/v6/api/jsondocument/as/
113
  template <typename T>
114
  T as() const {
10✔
115
    return getVariant().template as<T>();
20✔
116
  }
117

118
  // Empties the document and resets the memory pool
119
  // https://arduinojson.org/v6/api/jsondocument/clear/
120
  void clear() {
1,754✔
121
    _pool.clear();
1,754✔
122
    _data.setNull();
1,754✔
123
  }
1,754✔
124

125
  // Returns true if the root is of the specified type.
126
  // https://arduinojson.org/v6/api/jsondocument/is/
127
  template <typename T>
128
  bool is() {
93✔
129
    return getVariant().template is<T>();
186✔
130
  }
131

132
  // Returns true if the root is of the specified type.
133
  // https://arduinojson.org/v6/api/jsondocument/is/
134
  template <typename T>
135
  bool is() const {
136
    return getVariant().template is<T>();
137
  }
138

139
  // Returns true if the root is null.
140
  // https://arduinojson.org/v6/api/jsondocument/isnull/
141
  bool isNull() const {
12✔
142
    return getVariant().isNull();
24✔
143
  }
144

145
  // Returns the number of used bytes in the memory pool.
146
  // https://arduinojson.org/v6/api/jsondocument/memoryusage/
147
  size_t memoryUsage() const {
259✔
148
    return _pool.size();
259✔
149
  }
150

151
  // Returns trues if the memory pool was too small.
152
  // https://arduinojson.org/v6/api/jsondocument/overflowed/
153
  bool overflowed() const {
18✔
154
    return _pool.overflowed();
18✔
155
  }
156

157
  // Returns the depth (nesting level) of the array.
158
  // https://arduinojson.org/v6/api/jsondocument/nesting/
159
  size_t nesting() const {
4✔
160
    return variantNesting(&_data);
4✔
161
  }
162

163
  // Returns the number of elements in the root array or object.
164
  // https://arduinojson.org/v6/api/jsondocument/size/
165
  size_t size() const {
11✔
166
    return _data.size();
11✔
167
  }
168

169
  // Copies the specified document.
170
  // https://arduinojson.org/v6/api/jsondocument/set/
171
  bool set(const JsonDocument& src) {
7✔
172
    return to<JsonVariant>().set(src.as<JsonVariantConst>());
14✔
173
  }
174

175
  // Replaces the root with the specified value.
176
  // https://arduinojson.org/v6/api/jsondocument/set/
177
  template <typename T>
178
  typename detail::enable_if<!detail::is_base_of<JsonDocument, T>::value,
179
                             bool>::type
180
  set(const T& src) {
40✔
181
    return to<JsonVariant>().set(src);
80✔
182
  }
183

184
  // Clears the document and converts it to the specified type.
185
  // https://arduinojson.org/v6/api/jsondocument/to/
186
  template <typename T>
187
  typename detail::VariantTo<T>::type to() {
714✔
188
    clear();
714✔
189
    return getVariant().template to<T>();
1,428✔
190
  }
191

192
  // Creates an array and appends it to the root array.
193
  // https://arduinojson.org/v6/api/jsondocument/createnestedarray/
194
  JsonArray createNestedArray() {
25✔
195
    return add().to<JsonArray>();
50✔
196
  }
197

198
  // Creates an array and adds it to the root object.
199
  // https://arduinojson.org/v6/api/jsondocument/createnestedarray/
200
  template <typename TChar>
201
  JsonArray createNestedArray(TChar* key) {
1✔
202
    return operator[](key).template to<JsonArray>();
1✔
203
  }
204

205
  // Creates an array and adds it to the root object.
206
  // https://arduinojson.org/v6/api/jsondocument/createnestedarray/
207
  template <typename TString>
208
  JsonArray createNestedArray(const TString& key) {
1✔
209
    return operator[](key).template to<JsonArray>();
1✔
210
  }
211

212
  // Creates an object and appends it to the root array.
213
  // https://arduinojson.org/v6/api/jsondocument/createnestedobject/
214
  JsonObject createNestedObject() {
25✔
215
    return add().to<JsonObject>();
50✔
216
  }
217

218
  // Creates an object and adds it to the root object.
219
  // https://arduinojson.org/v6/api/jsondocument/createnestedobject/
220
  template <typename TChar>
221
  JsonObject createNestedObject(TChar* key) {
1✔
222
    return operator[](key).template to<JsonObject>();
1✔
223
  }
224

225
  // Creates an object and adds it to the root object.
226
  // https://arduinojson.org/v6/api/jsondocument/createnestedobject/
227
  template <typename TString>
228
  JsonObject createNestedObject(const TString& key) {
1✔
229
    return operator[](key).template to<JsonObject>();
1✔
230
  }
231

232
  // Returns true if the root object contains the specified key.
233
  // https://arduinojson.org/v6/api/jsondocument/containskey/
234
  template <typename TChar>
235
  bool containsKey(TChar* key) const {
5✔
236
    return _data.getMember(detail::adaptString(key)) != 0;
5✔
237
  }
238

239
  // Returns true if the root object contains the specified key.
240
  // https://arduinojson.org/v6/api/jsondocument/containskey/
241
  template <typename TString>
242
  bool containsKey(const TString& key) const {
1✔
243
    return _data.getMember(detail::adaptString(key)) != 0;
1✔
244
  }
245

246
  // Gets or sets a root object's member.
247
  // https://arduinojson.org/v6/api/jsondocument/subscript/
248
  template <typename TString>
249
  FORCE_INLINE typename detail::enable_if<
250
      detail::IsString<TString>::value,
251
      detail::MemberProxy<JsonDocument&, TString>>::type
252
  operator[](const TString& key) {
253
    return {*this, key};
16✔
254
  }
255

256
  // Gets or sets a root object's member.
257
  // https://arduinojson.org/v6/api/jsondocument/subscript/
258
  template <typename TChar>
259
  FORCE_INLINE typename detail::enable_if<
260
      detail::IsString<TChar*>::value,
261
      detail::MemberProxy<JsonDocument&, TChar*>>::type
262
  operator[](TChar* key) {
263
    return {*this, key};
421✔
264
  }
265

266
  // Gets a root object's member.
267
  // https://arduinojson.org/v6/api/jsondocument/subscript/
268
  template <typename TString>
269
  FORCE_INLINE typename detail::enable_if<detail::IsString<TString>::value,
270
                                          JsonVariantConst>::type
271
  operator[](const TString& key) const {
272
    return JsonVariantConst(_data.getMember(detail::adaptString(key)));
1✔
273
  }
274

275
  // Gets a root object's member.
276
  // https://arduinojson.org/v6/api/jsondocument/subscript/
277
  template <typename TChar>
278
  FORCE_INLINE typename detail::enable_if<detail::IsString<TChar*>::value,
279
                                          JsonVariantConst>::type
280
  operator[](TChar* key) const {
281
    return JsonVariantConst(_data.getMember(detail::adaptString(key)));
1✔
282
  }
283

284
  // Gets or sets a root array's element.
285
  // https://arduinojson.org/v6/api/jsondocument/subscript/
286
  FORCE_INLINE detail::ElementProxy<JsonDocument&> operator[](size_t index) {
287
    return {*this, index};
123✔
288
  }
289

290
  // Gets a root array's member.
291
  // https://arduinojson.org/v6/api/jsondocument/subscript/
292
  FORCE_INLINE JsonVariantConst operator[](size_t index) const {
293
    return JsonVariantConst(_data.getElement(index));
1✔
294
  }
295

296
  // Appends a new (null) element to the root array.
297
  // Returns a reference to the new element.
298
  // https://arduinojson.org/v6/api/jsondocument/add/
299
  FORCE_INLINE JsonVariant add() {
300
    return JsonVariant(&_pool, _data.addElement(&_pool));
167✔
301
  }
302

303
  // Appends a value to the root array.
304
  // https://arduinojson.org/v6/api/jsondocument/add/
305
  template <typename TValue>
306
  FORCE_INLINE bool add(const TValue& value) {
307
    return add().set(value);
86✔
308
  }
309

310
  // Appends a value to the root array.
311
  // https://arduinojson.org/v6/api/jsondocument/add/
312
  template <typename TChar>
313
  FORCE_INLINE bool add(TChar* value) {
314
    return add().set(value);
24✔
315
  }
316

317
  // Removes an element of the root array.
318
  // ⚠️ Doesn't release the memory associated with the removed element.
319
  // https://arduinojson.org/v6/api/jsondocument/remove/
320
  FORCE_INLINE void remove(size_t index) {
321
    _data.remove(index, getPool());
1✔
322
  }
1✔
323

324
  // Removes a member of the root object.
325
  // ⚠️ Doesn't release the memory associated with the removed element.
326
  // https://arduinojson.org/v6/api/jsondocument/remove/
327
  template <typename TChar>
328
  FORCE_INLINE typename detail::enable_if<detail::IsString<TChar*>::value>::type
329
  remove(TChar* key) {
330
    _data.remove(detail::adaptString(key), getPool());
4✔
331
  }
4✔
332

333
  // Removes a member of the root object.
334
  // ⚠️ Doesn't release the memory associated with the removed element.
335
  // https://arduinojson.org/v6/api/jsondocument/remove/
336
  template <typename TString>
337
  FORCE_INLINE
338
      typename detail::enable_if<detail::IsString<TString>::value>::type
339
      remove(const TString& key) {
340
    _data.remove(detail::adaptString(key), &_pool);
1✔
341
  }
1✔
342

343
  FORCE_INLINE operator JsonVariant() {
344
    return getVariant();
1✔
345
  }
346

347
  FORCE_INLINE operator JsonVariantConst() const {
348
    return getVariant();
297✔
349
  }
350

351
 private:
352
  JsonVariant getVariant() {
1,310✔
353
    return JsonVariant(&_pool, &_data);
1,310✔
354
  }
355

356
  JsonVariantConst getVariant() const {
319✔
357
    return JsonVariantConst(&_data);
319✔
358
  }
359

360
  void copyAssignFrom(const JsonDocument& src) {
3✔
361
    _pool.reallocPool(src._pool.capacity());
3✔
362
    set(src);
3✔
363
  }
3✔
364

365
  void moveAssignFrom(JsonDocument& src) {
7✔
366
    _data = src._data;
7✔
367
    src._data.setNull();
7✔
368
    _pool = move(src._pool);
7✔
369
  }
7✔
370

371
  detail::MemoryPool* getPool() {
2,337✔
372
    return &_pool;
2,337✔
373
  }
374

375
  detail::VariantData* getData() {
1,279✔
376
    return &_data;
1,279✔
377
  }
378

379
  const detail::VariantData* getData() const {
6✔
380
    return &_data;
6✔
381
  }
382

383
  detail::VariantData* getOrCreateData() {
390✔
384
    return &_data;
390✔
385
  }
386

387
  detail::MemoryPool _pool;
388
  detail::VariantData _data;
389
};
390

391
inline void convertToJson(const JsonDocument& src, JsonVariant dst) {
1✔
392
  dst.set(src.as<JsonVariantConst>());
1✔
393
}
1✔
394

395
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