Skip to content

Commit 8d17ba3

Browse files
committed
Extract VariantImpl
1 parent ed6b470 commit 8d17ba3

33 files changed

+590
-546
lines changed

extras/tests/ResourceManager/StringBuffer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ TEST_CASE("StringBuffer") {
2121
strcpy(ptr, "hi!");
2222
sb.save(&variant);
2323

24-
REQUIRE(variant.type() == VariantType::TinyString);
24+
REQUIRE(variant.type == VariantType::TinyString);
2525
REQUIRE(variant.asString() == "hi!");
2626
}
2727

@@ -30,7 +30,7 @@ TEST_CASE("StringBuffer") {
3030
memcpy(ptr, "a\0b", 3);
3131
sb.save(&variant);
3232

33-
REQUIRE(variant.type() == VariantType::LongString);
33+
REQUIRE(variant.type == VariantType::LongString);
3434

3535
auto str = variant.asString();
3636
REQUIRE(str.size() == 3);
@@ -44,7 +44,7 @@ TEST_CASE("StringBuffer") {
4444
strcpy(ptr, "alfa");
4545
sb.save(&variant);
4646

47-
REQUIRE(variant.type() == VariantType::LongString);
47+
REQUIRE(variant.type == VariantType::LongString);
4848
REQUIRE(variant.asString() == "alfa");
4949
}
5050
}

extras/tests/ResourceManager/StringBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ TEST_CASE("StringBuilder") {
2626
REQUIRE(spyingAllocator.log() == AllocatorLog{
2727
Allocate(sizeofStringBuffer()),
2828
});
29-
REQUIRE(data.type() == VariantType::TinyString);
29+
REQUIRE(data.type == VariantType::TinyString);
3030
}
3131

3232
SECTION("Tiny string") {
@@ -45,7 +45,7 @@ TEST_CASE("StringBuilder") {
4545
str.save(&data);
4646

4747
REQUIRE(resources.overflowed() == false);
48-
REQUIRE(data.type() == VariantType::TinyString);
48+
REQUIRE(data.type == VariantType::TinyString);
4949
REQUIRE(data.asString() == "url");
5050
}
5151

src/ArduinoJson/Array/ElementProxy.hpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,12 @@ class ElementProxy : public VariantRefBase<ElementProxy<TUpstream>>,
5555
}
5656

5757
FORCE_INLINE VariantData* getData() const {
58-
auto data = VariantAttorney::getData(upstream_);
59-
auto resources = VariantAttorney::getResourceManager(upstream_);
60-
return VariantData::asArray(data, resources).getElement(index_);
58+
return VariantAttorney::getVariantImpl(upstream_).getElement(index_);
6159
}
6260

6361
VariantData* getOrCreateData() const {
64-
auto data = VariantAttorney::getOrCreateData(upstream_);
65-
if (!data)
66-
return nullptr;
67-
return data->getOrAddElement(
68-
index_, VariantAttorney::getResourceManager(upstream_));
62+
return VariantAttorney::getOrCreateVariantImpl(upstream_).getOrAddElement(
63+
index_);
6964
}
7065

7166
TUpstream upstream_;

src/ArduinoJson/Array/JsonArray.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
2424

2525
// INTERNAL USE ONLY
2626
JsonArray(detail::VariantData* data, detail::ResourceManager* resources)
27-
: impl_(detail::VariantData::asArray(data, resources)) {}
27+
: impl_(detail::VariantImpl::asArray(data, resources)) {}
2828

2929
// INTERNAL USE ONLY
3030
JsonArray(const detail::ArrayImpl& impl) : impl_(impl) {}

src/ArduinoJson/Array/JsonArrayConst.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
3838

3939
// INTERNAL USE ONLY
4040
JsonArrayConst(detail::VariantData* data, detail::ResourceManager* resources)
41-
: impl_(detail::VariantData::asArray(data, resources)) {}
41+
: impl_(detail::VariantImpl::asArray(data, resources)) {}
4242

4343
// INTERNAL USE ONLY
4444
JsonArrayConst(const detail::ArrayImpl& impl) : impl_(impl) {}

src/ArduinoJson/Collection/CollectionData.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
1414

15-
class VariantData;
15+
struct VariantData;
1616
class ResourceManager;
1717

1818
class CollectionIterator {

src/ArduinoJson/Collection/CollectionImpl.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
1414

1515
inline void CollectionIterator::next(const ResourceManager* resources) {
1616
ARDUINOJSON_ASSERT(slot_);
17-
auto nextId = slot_->next();
17+
auto nextId = slot_->next;
1818
slot_ = resources->getVariant(nextId);
1919
currentId_ = nextId;
2020
}
@@ -31,7 +31,7 @@ inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
3131

3232
if (data_->tail != NULL_SLOT) {
3333
auto tail = resources_->getVariant(data_->tail);
34-
tail->setNext(slot.id());
34+
tail->next = slot.id();
3535
data_->tail = slot.id();
3636
} else {
3737
data_->head = slot.id();
@@ -44,11 +44,11 @@ inline void CollectionImpl::appendPair(Slot<VariantData> key,
4444
ARDUINOJSON_ASSERT(data_ != nullptr);
4545
ARDUINOJSON_ASSERT(resources_ != nullptr);
4646

47-
key->setNext(value.id());
47+
key->next = value.id();
4848

4949
if (data_->tail != NULL_SLOT) {
5050
auto tail = resources_->getVariant(data_->tail);
51-
tail->setNext(key.id());
51+
tail->next = key.id();
5252
data_->tail = value.id();
5353
} else {
5454
data_->head = key.id();
@@ -63,7 +63,7 @@ inline void CollectionImpl::clear() {
6363
while (next != NULL_SLOT) {
6464
auto currId = next;
6565
auto slot = resources_->getVariant(next);
66-
next = slot->next();
66+
next = slot->next;
6767
resources_->freeVariant({slot, currId});
6868
}
6969

@@ -80,7 +80,7 @@ inline Slot<VariantData> CollectionImpl::getPreviousSlot(
8080
if (currentSlot == target)
8181
break;
8282
prev = Slot<VariantData>(currentSlot, currentId);
83-
currentId = currentSlot->next();
83+
currentId = currentSlot->next;
8484
}
8585
return prev;
8686
}
@@ -90,9 +90,9 @@ inline void CollectionImpl::removeOne(iterator it) {
9090
return;
9191
auto curr = it.slot_;
9292
auto prev = getPreviousSlot(curr);
93-
auto next = curr->next();
93+
auto next = curr->next;
9494
if (prev)
95-
prev->setNext(next);
95+
prev->next = next;
9696
else
9797
data_->head = next;
9898
if (next == NULL_SLOT)
@@ -106,11 +106,11 @@ inline void CollectionImpl::removePair(ObjectImpl::iterator it) {
106106

107107
auto keySlot = it.slot_;
108108

109-
auto valueId = keySlot->next();
109+
auto valueId = keySlot->next;
110110
auto valueSlot = resources_->getVariant(valueId);
111111

112112
// remove value slot
113-
keySlot->setNext(valueSlot->next());
113+
keySlot->next = valueSlot->next;
114114
resources_->freeVariant({valueSlot, valueId});
115115

116116
// remove key slot
@@ -122,7 +122,7 @@ inline size_t CollectionImpl::nesting() const {
122122
return 0;
123123
size_t maxChildNesting = 0;
124124
for (auto it = createIterator(); !it.done(); it.next(resources_)) {
125-
size_t childNesting = it->nesting(resources_);
125+
size_t childNesting = VariantImpl(it.data(), resources_).nesting();
126126
if (childNesting > maxChildNesting)
127127
maxChildNesting = childNesting;
128128
}

src/ArduinoJson/Document/JsonDocument.hpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
8888
// https://arduinojson.org/v7/api/jsondocument/clear/
8989
void clear() {
9090
resources_.clear();
91-
data_.reset();
91+
data_.type = detail::VariantType::Null;
9292
}
9393

9494
// Returns true if the root is of the specified type.
@@ -120,13 +120,13 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
120120
// Returns the depth (nesting level) of the array.
121121
// https://arduinojson.org/v7/api/jsondocument/nesting/
122122
size_t nesting() const {
123-
return data_.nesting(&resources_);
123+
return getVariantImpl().nesting();
124124
}
125125

126126
// Returns the number of elements in the root array or object.
127127
// https://arduinojson.org/v7/api/jsondocument/size/
128128
size_t size() const {
129-
return data_.size(&resources_);
129+
return getVariantImpl().size();
130130
}
131131

132132
// Copies the specified document.
@@ -165,7 +165,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
165165
template <typename TChar>
166166
ARDUINOJSON_DEPRECATED("use doc[\"key\"].is<T>() instead")
167167
bool containsKey(TChar* key) const {
168-
return data_.getMember(detail::adaptString(key), &resources_) != 0;
168+
return getVariantImpl().getMember(detail::adaptString(key)) != 0;
169169
}
170170

171171
// DEPRECATED: use obj[key].is<T>() instead
@@ -174,7 +174,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
174174
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
175175
ARDUINOJSON_DEPRECATED("use doc[key].is<T>() instead")
176176
bool containsKey(const TString& key) const {
177-
return data_.getMember(detail::adaptString(key), &resources_) != 0;
177+
return getVariantImpl().getMember(detail::adaptString(key)) != 0;
178178
}
179179

180180
// DEPRECATED: use obj[key].is<T>() instead
@@ -212,7 +212,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
212212
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
213213
JsonVariantConst operator[](const TString& key) const {
214214
return JsonVariantConst(
215-
data_.getMember(detail::adaptString(key), &resources_), &resources_);
215+
getVariantImpl().getMember(detail::adaptString(key)), &resources_);
216216
}
217217

218218
// Gets a root object's member.
@@ -223,7 +223,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
223223
int> = 0>
224224
JsonVariantConst operator[](TChar* key) const {
225225
return JsonVariantConst(
226-
data_.getMember(detail::adaptString(key), &resources_), &resources_);
226+
getVariantImpl().getMember(detail::adaptString(key)), &resources_);
227227
}
228228

229229
// Gets or sets a root array's element.
@@ -237,7 +237,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
237237
// Gets a root array's member.
238238
// https://arduinojson.org/v7/api/jsondocument/subscript/
239239
JsonVariantConst operator[](size_t index) const {
240-
return JsonVariantConst(data_.getElement(index, &resources_), &resources_);
240+
return JsonVariantConst(getVariantImpl().getElement(index), &resources_);
241241
}
242242

243243
// Gets or sets a root object's member.
@@ -267,31 +267,30 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
267267
template <typename T, detail::enable_if_t<
268268
detail::is_same<T, JsonVariant>::value, int> = 0>
269269
JsonVariant add() {
270-
return JsonVariant(data_.addElement(&resources_), &resources_);
270+
return JsonVariant(getVariantImpl().addElement(), &resources_);
271271
}
272272

273273
// Appends a value to the root array.
274274
// https://arduinojson.org/v7/api/jsondocument/add/
275275
template <typename TValue>
276276
bool add(const TValue& value) {
277-
return data_.addValue(value, &resources_);
277+
return getVariantImpl().addValue(value);
278278
}
279279

280280
// Appends a value to the root array.
281281
// https://arduinojson.org/v7/api/jsondocument/add/
282282
template <typename TChar,
283283
detail::enable_if_t<!detail::is_const<TChar>::value, int> = 0>
284284
bool add(TChar* value) {
285-
return data_.addValue(value, &resources_);
285+
return getVariantImpl().addValue(value);
286286
}
287287

288288
// Removes an element of the root array.
289289
// https://arduinojson.org/v7/api/jsondocument/remove/
290290
template <typename T,
291291
detail::enable_if_t<detail::is_integral<T>::value, int> = 0>
292292
void remove(T index) {
293-
detail::VariantData::removeElement(getData(), size_t(index),
294-
getResourceManager());
293+
getVariantImpl().removeElement(size_t(index));
295294
}
296295

297296
// Removes a member of the root object.
@@ -301,17 +300,15 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
301300
!detail::is_const<TChar>::value,
302301
int> = 0>
303302
void remove(TChar* key) {
304-
detail::VariantData::removeMember(getData(), detail::adaptString(key),
305-
getResourceManager());
303+
getVariantImpl().removeMember(detail::adaptString(key));
306304
}
307305

308306
// Removes a member of the root object.
309307
// https://arduinojson.org/v7/api/jsondocument/remove/
310308
template <typename TString,
311309
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
312310
void remove(const TString& key) {
313-
detail::VariantData::removeMember(getData(), detail::adaptString(key),
314-
getResourceManager());
311+
getVariantImpl().removeMember(detail::adaptString(key));
315312
}
316313

317314
// Removes a member of the root object or an element of the root array.
@@ -391,6 +388,10 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
391388
}
392389

393390
private:
391+
detail::VariantImpl getVariantImpl() const {
392+
return detail::VariantImpl(&data_, &resources_);
393+
}
394+
394395
JsonVariant getVariant() {
395396
return JsonVariant(&data_, &resources_);
396397
}

src/ArduinoJson/Json/JsonDeserializer.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ class JsonDeserializer {
7171
switch (current()) {
7272
case '[':
7373
if (filter.allowArray())
74-
return parseArray(variant->toArray(resources_), filter, nestingLimit);
74+
return parseArray(VariantImpl::toArray(variant, resources_), filter,
75+
nestingLimit);
7576
else
7677
return skipArray(nestingLimit);
7778

7879
case '{':
7980
if (filter.allowObject())
80-
return parseObject(variant->toObject(resources_), filter,
81+
return parseObject(VariantImpl::toObject(variant, resources_), filter,
8182
nestingLimit);
8283
else
8384
return skipObject(nestingLimit);
@@ -282,7 +283,7 @@ class JsonDeserializer {
282283

283284
stringBuilder_.save(keyVariant);
284285
} else {
285-
member->clear(resources_);
286+
VariantImpl::clear(member, resources_);
286287
}
287288

288289
// Parse value
@@ -519,26 +520,28 @@ class JsonDeserializer {
519520
auto number = parseNumber(buffer_);
520521
switch (number.type()) {
521522
case NumberType::UnsignedInteger:
522-
if (result->setInteger(number.asUnsignedInteger(), resources_))
523+
if (VariantImpl::setInteger(number.asUnsignedInteger(), result,
524+
resources_))
523525
return DeserializationError::Ok;
524526
else
525527
return DeserializationError::NoMemory;
526528

527529
case NumberType::SignedInteger:
528-
if (result->setInteger(number.asSignedInteger(), resources_))
530+
if (VariantImpl::setInteger(number.asSignedInteger(), result,
531+
resources_))
529532
return DeserializationError::Ok;
530533
else
531534
return DeserializationError::NoMemory;
532535

533536
case NumberType::Float:
534-
if (result->setFloat(number.asFloat(), resources_))
537+
if (VariantImpl::setFloat(number.asFloat(), result, resources_))
535538
return DeserializationError::Ok;
536539
else
537540
return DeserializationError::NoMemory;
538541

539542
#if ARDUINOJSON_USE_DOUBLE
540543
case NumberType::Double:
541-
if (result->setFloat(number.asDouble(), resources_))
544+
if (VariantImpl::setFloat(number.asDouble(), result, resources_))
542545
return DeserializationError::Ok;
543546
else
544547
return DeserializationError::NoMemory;

src/ArduinoJson/Json/JsonSerializer.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
2727
while (slotId != NULL_SLOT) {
2828
auto slot = resources_->getVariant(slotId);
2929

30-
slot->accept(*this, resources_);
30+
VariantImpl::accept(*this, slot, resources_);
3131

32-
slotId = slot->next();
32+
slotId = slot->next;
3333

3434
if (slotId != NULL_SLOT)
3535
write(',');
@@ -48,9 +48,9 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
4848

4949
while (slotId != NULL_SLOT) {
5050
auto slot = resources_->getVariant(slotId);
51-
slot->accept(*this, resources_);
51+
VariantImpl::accept(*this, slot, resources_);
5252

53-
slotId = slot->next();
53+
slotId = slot->next;
5454

5555
if (slotId != NULL_SLOT)
5656
write(isKey ? ':' : ',');

0 commit comments

Comments
 (0)