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

bblanchon / ArduinoJson / 5807441580

pending completion
5807441580

push

github

bblanchon
Extract `VariantRefBaseImpl.hpp`

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

3414 of 3430 relevant lines covered (99.53%)

8822.85 hits per line

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

97.83
/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) {}
16✔
24

25
  // INTERNAL USE ONLY
26
  FORCE_INLINE JsonArray(detail::ArrayData* data,
27
                         detail::ResourceManager* resources)
28
      : data_(data), resources_(resources) {}
284✔
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 (empty) element to the array.
45
  // Returns a reference to the new element.
46
  // https://arduinojson.org/v6/api/jsonarray/add/
47
  template <typename T>
48
  typename detail::enable_if<!detail::is_same<T, JsonVariant>::value, T>::type
49
  add() const {
34✔
50
    return add<JsonVariant>().to<T>();
68✔
51
  }
52

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

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

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

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

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

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

97
    clear();
12✔
98
    for (auto element : src) {
29✔
99
      if (!add(element))
6✔
100
        return false;
1✔
101
    }
102

103
    return true;
11✔
104
  }
105

106
  // Removes the element at the specified iterator.
107
  // ⚠️ Doesn't release the memory associated with the removed element.
108
  // https://arduinojson.org/v6/api/jsonarray/remove/
109
  FORCE_INLINE void remove(iterator it) const {
110
    detail::ArrayData::remove(data_, it.iterator_, resources_);
5✔
111
  }
5✔
112

113
  // Removes the element at the specified index.
114
  // ⚠️ Doesn't release the memory associated with the removed element.
115
  // https://arduinojson.org/v6/api/jsonarray/remove/
116
  FORCE_INLINE void remove(size_t index) const {
117
    detail::ArrayData::removeElement(data_, index, resources_);
5✔
118
  }
5✔
119

120
  // Removes all the elements of the array.
121
  // ⚠️ Doesn't release the memory associated with the removed elements.
122
  // https://arduinojson.org/v6/api/jsonarray/clear/
123
  void clear() const {
15✔
124
    detail::ArrayData::clear(data_, resources_);
15✔
125
  }
15✔
126

127
  // Gets or sets the element at the specified index.
128
  // https://arduinojson.org/v6/api/jsonarray/subscript/
129
  FORCE_INLINE detail::ElementProxy<JsonArray> operator[](size_t index) const {
130
    return {*this, index};
245✔
131
  }
132

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

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

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

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

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

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

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

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

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

178
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