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

bblanchon / ArduinoJson / 12027022725

25 Nov 2024 11:25AM UTC coverage: 99.324% (-0.003%) from 99.327%
12027022725

push

github

bblanchon
Remove unnecessary universal references

10 of 10 new or added lines in 5 files covered. (100.0%)

1 existing line in 1 file now uncovered.

3965 of 3992 relevant lines covered (99.32%)

10921.05 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
  using iterator = JsonArrayIterator;
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
  detail::enable_if_t<!detail::is_same<T, JsonVariant>::value, T> add() const {
36✔
48
    return add<JsonVariant>().to<T>();
72✔
49
  }
50

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

60
  // Appends a value to the array.
61
  // https://arduinojson.org/v7/api/jsonarray/add/
62
  template <typename T>
63
  bool add(const T& value) const {
66,545✔
64
    return detail::ArrayData::addValue(data_, value, resources_);
66,545✔
65
  }
66

67
  // Appends a value to the array.
68
  // https://arduinojson.org/v7/api/jsonarray/add/
69
  template <typename T,
70
            typename = detail::enable_if_t<!detail::is_const<T>::value>>
71
  bool add(T* value) const {
3✔
72
    return detail::ArrayData::addValue(data_, value, resources_);
3✔
73
  }
74

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

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

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

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

101
    return true;
12✔
102
  }
103

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

219
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