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

bblanchon / ArduinoJson / 3619649488

pending completion
3619649488

push

github

Benoit Blanchon
CI: use `ubuntu-20.04` for GCC

3373 of 3389 relevant lines covered (99.53%)

5926.06 hits per line

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

98.51
/src/ArduinoJson/Object/ObjectRef.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2022, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Object/MemberProxy.hpp>
8
#include <ArduinoJson/Object/ObjectConstRef.hpp>
9

10
namespace ARDUINOJSON_NAMESPACE {
11

12
class ArrayRef;
13

14
class ObjectRef : public VariantOperators<ObjectRef> {
15
  friend class VariantAttorney;
16

17
 public:
18
  typedef ObjectIterator iterator;
19

20
  FORCE_INLINE ObjectRef() : _data(0), _pool(0) {}
20✔
21
  FORCE_INLINE ObjectRef(MemoryPool* buf, CollectionData* data)
22
      : _data(data), _pool(buf) {}
386✔
23

24
  operator VariantRef() const {
8✔
25
    void* data = _data;  // prevent warning cast-align
8✔
26
    return VariantRef(_pool, reinterpret_cast<VariantData*>(data));
8✔
27
  }
28

29
  operator ObjectConstRef() const {
40✔
30
    return ObjectConstRef(_data);
40✔
31
  }
32

33
  operator VariantConstRef() const {
155✔
34
    return VariantConstRef(collectionToVariant(_data));
155✔
35
  }
36

37
  FORCE_INLINE bool isNull() const {
38
    return _data == 0;
13✔
39
  }
40

41
  FORCE_INLINE operator bool() const {
42
    return _data != 0;
2✔
43
  }
44

45
  FORCE_INLINE size_t memoryUsage() const {
46
    return _data ? _data->memoryUsage() : 0;
9✔
47
  }
48

49
  FORCE_INLINE size_t nesting() const {
50
    return variantNesting(collectionToVariant(_data));
5✔
51
  }
52

53
  FORCE_INLINE size_t size() const {
54
    return _data ? _data->size() : 0;
44✔
55
  }
56

57
  FORCE_INLINE iterator begin() const {
58
    if (!_data)
27✔
59
      return iterator();
2✔
60
    return iterator(_pool, _data->head());
25✔
61
  }
62

63
  FORCE_INLINE iterator end() const {
64
    return iterator();
6✔
65
  }
66

67
  void clear() const {
2✔
68
    if (!_data)
2✔
69
      return;
1✔
70
    _data->clear();
1✔
71
  }
72

73
  FORCE_INLINE bool set(ObjectConstRef src) {
74
    if (!_data || !src._data)
10✔
75
      return false;
2✔
76
    return _data->copyFrom(*src._data, _pool);
8✔
77
  }
78

79
  FORCE_INLINE bool operator==(ObjectRef rhs) const {
80
    return ObjectConstRef(_data) == ObjectConstRef(rhs._data);
32✔
81
  }
82

83
  template <typename TString>
84
  FORCE_INLINE typename enable_if<IsString<TString>::value,
85
                                  MemberProxy<ObjectRef, TString> >::type
86
  operator[](const TString& key) const {
87
    return MemberProxy<ObjectRef, TString>(*this, key);
29✔
88
  }
89

90
  template <typename TChar>
91
  FORCE_INLINE typename enable_if<IsString<TChar*>::value,
92
                                  MemberProxy<ObjectRef, TChar*> >::type
93
  operator[](TChar* key) const {
94
    return MemberProxy<ObjectRef, TChar*>(*this, key);
438✔
95
  }
96

97
  FORCE_INLINE void remove(iterator it) const {
98
    if (!_data)
4✔
99
      return;
1✔
100
    _data->removeSlot(it._slot);
3✔
101
  }
102

103
  // remove(const std::string&) const
104
  // remove(const String&) const
105
  template <typename TString>
106
  FORCE_INLINE void remove(const TString& key) const {
107
    removeMember(adaptString(key));
1✔
108
  }
1✔
109

110
  // remove(char*) const
111
  // remove(const char*) const
112
  // remove(const __FlashStringHelper*) const
113
  template <typename TChar>
114
  FORCE_INLINE void remove(TChar* key) const {
115
    removeMember(adaptString(key));
12✔
116
  }
12✔
117

118
  template <typename TString>
119
  FORCE_INLINE typename enable_if<IsString<TString>::value, bool>::type
120
  containsKey(const TString& key) const {
121
    return getMember(adaptString(key)) != 0;
1✔
122
  }
123

124
  template <typename TChar>
125
  FORCE_INLINE typename enable_if<IsString<TChar*>::value, bool>::type
126
  containsKey(TChar* key) const {
127
    return getMember(adaptString(key)) != 0;
5✔
128
  }
129

130
  template <typename TString>
131
  FORCE_INLINE ArrayRef createNestedArray(const TString& key) const;
132

133
  template <typename TChar>
134
  FORCE_INLINE ArrayRef createNestedArray(TChar* key) const;
135

136
  template <typename TString>
137
  ObjectRef createNestedObject(const TString& key) const {
1✔
138
    return operator[](key).template to<ObjectRef>();
1✔
139
  }
140

141
  template <typename TChar>
142
  ObjectRef createNestedObject(TChar* key) const {
10✔
143
    return operator[](key).template to<ObjectRef>();
10✔
144
  }
145

146
 private:
147
  MemoryPool* getPool() const {
954✔
148
    return _pool;
954✔
149
  }
150

151
  VariantData* getData() const {
234✔
152
    return collectionToVariant(_data);
234✔
153
  }
154

155
  VariantData* getOrCreateData() const {
312✔
156
    return collectionToVariant(_data);
312✔
157
  }
158

159
  template <typename TAdaptedString>
160
  inline VariantData* getMember(TAdaptedString key) const {
6✔
161
    if (!_data)
6✔
162
      return 0;
×
163
    return _data->getMember(key);
6✔
164
  }
165

166
  template <typename TAdaptedString>
167
  void removeMember(TAdaptedString key) const {
13✔
168
    if (!_data)
13✔
169
      return;
1✔
170
    _data->removeMember(key);
12✔
171
  }
172

173
  CollectionData* _data;
174
  MemoryPool* _pool;
175
};
176

177
template <>
178
struct Converter<ObjectRef> : private VariantAttorney {
179
  static void toJson(VariantConstRef src, VariantRef dst) {
13✔
180
    variantCopyFrom(getData(dst), getData(src), getPool(dst));
39✔
181
  }
13✔
182

183
  static ObjectRef fromJson(VariantRef src) {
111✔
184
    VariantData* data = getData(src);
111✔
185
    MemoryPool* pool = getPool(src);
111✔
186
    return ObjectRef(pool, data != 0 ? data->asObject() : 0);
222✔
187
  }
188

189
  static InvalidConversion<VariantConstRef, ObjectRef> fromJson(
190
      VariantConstRef);
191

192
  static bool checkJson(VariantConstRef) {
9✔
193
    return false;
9✔
194
  }
195

196
  static bool checkJson(VariantRef src) {
52✔
197
    VariantData* data = getData(src);
52✔
198
    return data && data->isObject();
52✔
199
  }
200
};
201
}  // namespace ARDUINOJSON_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