Skip to content

Commit 45e1268

Browse files
committed
Extract VariantImpl
1 parent c25824e commit 45e1268

35 files changed

+501
-603
lines changed

extras/tests/ResourceManager/StringBuffer.cpp

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

24-
REQUIRE(variant.type() == VariantType::TinyString);
25-
REQUIRE(variant.asString(&resources) == "hi!");
24+
REQUIRE(variant.type == VariantType::TinyString);
25+
REQUIRE(VariantImpl(&variant, &resources).asString() == "hi!");
2626
}
2727

2828
SECTION("Tiny string can't contain NUL") {
2929
auto ptr = sb.reserve(3);
3030
memcpy(ptr, "a\0b", 3);
3131
sb.save(&variant);
3232

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

35-
auto str = variant.asString(&resources);
35+
auto str = VariantImpl(&variant, &resources).asString();
3636
REQUIRE(str.size() == 3);
3737
REQUIRE(str.c_str()[0] == 'a');
3838
REQUIRE(str.c_str()[1] == 0);
@@ -44,7 +44,7 @@ TEST_CASE("StringBuffer") {
4444
strcpy(ptr, "alfa");
4545
sb.save(&variant);
4646

47-
REQUIRE(variant.type() == VariantType::OwnedString);
48-
REQUIRE(variant.asString(&resources) == "alfa");
47+
REQUIRE(variant.type == VariantType::OwnedString);
48+
REQUIRE(VariantImpl(&variant, &resources).asString() == "alfa");
4949
}
5050
}

extras/tests/ResourceManager/StringBuilder.cpp

Lines changed: 17 additions & 15 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,8 +45,8 @@ TEST_CASE("StringBuilder") {
4545
str.save(&data);
4646

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

5252
SECTION("Short string fits in first allocation") {
@@ -134,10 +134,10 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
134134
auto s2 = saveString(builder, "world");
135135
auto s3 = saveString(builder, "hello");
136136

137-
REQUIRE(s1.asString(&resources) == "hello");
138-
REQUIRE(s2.asString(&resources) == "world");
139-
REQUIRE(+s1.asString(&resources).c_str() ==
140-
+s3.asString(&resources).c_str()); // same address
137+
REQUIRE(VariantImpl(&s1, &resources).asString() == "hello");
138+
REQUIRE(VariantImpl(&s2, &resources).asString() == "world");
139+
REQUIRE(+VariantImpl(&s1, &resources).asString().c_str() ==
140+
+VariantImpl(&s3, &resources).asString().c_str()); // same address
141141

142142
REQUIRE(spy.log() ==
143143
AllocatorLog{
@@ -153,10 +153,11 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
153153
auto s1 = saveString(builder, "hello world");
154154
auto s2 = saveString(builder, "hello");
155155

156-
REQUIRE(s1.asString(&resources) == "hello world");
157-
REQUIRE(s2.asString(&resources) == "hello");
158-
REQUIRE(+s2.asString(&resources).c_str() !=
159-
+s1.asString(&resources).c_str()); // different address
156+
REQUIRE(VariantImpl(&s1, &resources).asString() == "hello world");
157+
REQUIRE(VariantImpl(&s2, &resources).asString() == "hello");
158+
REQUIRE(
159+
+VariantImpl(&s1, &resources).asString().c_str() !=
160+
+VariantImpl(&s2, &resources).asString().c_str()); // different address
160161

161162
REQUIRE(spy.log() ==
162163
AllocatorLog{
@@ -171,10 +172,11 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
171172
auto s1 = saveString(builder, "hello world");
172173
auto s2 = saveString(builder, "worl");
173174

174-
REQUIRE(s1.asString(&resources) == "hello world");
175-
REQUIRE(s2.asString(&resources) == "worl");
176-
REQUIRE(s2.asString(&resources).c_str() !=
177-
s1.asString(&resources).c_str()); // different address
175+
REQUIRE(VariantImpl(&s1, &resources).asString() == "hello world");
176+
REQUIRE(VariantImpl(&s2, &resources).asString() == "worl");
177+
REQUIRE(
178+
VariantImpl(&s1, &resources).asString().c_str() !=
179+
VariantImpl(&s2, &resources).asString().c_str()); // different address
178180

179181
REQUIRE(spy.log() ==
180182
AllocatorLog{

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(data, resources).asArray()) {}
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(data, resources).asArray()) {}
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: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
1414

1515
inline CollectionIterator::CollectionIterator(VariantData* slot, SlotId slotId)
1616
: slot_(slot), currentId_(slotId) {
17-
nextId_ = slot_ ? slot_->next() : NULL_SLOT;
17+
nextId_ = slot_ ? slot_->next : NULL_SLOT;
1818
}
1919

2020
inline void CollectionIterator::next(const ResourceManager* resources) {
2121
ARDUINOJSON_ASSERT(currentId_ != NULL_SLOT);
2222
slot_ = resources->getVariant(nextId_);
2323
currentId_ = nextId_;
2424
if (slot_)
25-
nextId_ = slot_->next();
25+
nextId_ = slot_->next;
2626
}
2727

2828
inline CollectionImpl::iterator CollectionImpl::createIterator() const {
@@ -37,7 +37,7 @@ inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
3737

3838
if (data_->tail != NULL_SLOT) {
3939
auto tail = resources_->getVariant(data_->tail);
40-
tail->setNext(slot.id());
40+
tail->next = slot.id();
4141
data_->tail = slot.id();
4242
} else {
4343
data_->head = slot.id();
@@ -50,11 +50,11 @@ inline void CollectionImpl::appendPair(Slot<VariantData> key,
5050
ARDUINOJSON_ASSERT(data_ != nullptr);
5151
ARDUINOJSON_ASSERT(resources_ != nullptr);
5252

53-
key->setNext(value.id());
53+
key->next = value.id();
5454

5555
if (data_->tail != NULL_SLOT) {
5656
auto tail = resources_->getVariant(data_->tail);
57-
tail->setNext(key.id());
57+
tail->next = key.id();
5858
data_->tail = value.id();
5959
} else {
6060
data_->head = key.id();
@@ -69,7 +69,7 @@ inline void CollectionImpl::clear() {
6969
while (next != NULL_SLOT) {
7070
auto currId = next;
7171
auto slot = resources_->getVariant(next);
72-
next = slot->next();
72+
next = slot->next;
7373
resources_->freeVariant({slot, currId});
7474
}
7575

@@ -86,7 +86,7 @@ inline Slot<VariantData> CollectionImpl::getPreviousSlot(
8686
if (currentSlot == target)
8787
break;
8888
prev = Slot<VariantData>(currentSlot, currentId);
89-
currentId = currentSlot->next();
89+
currentId = currentSlot->next;
9090
}
9191
return prev;
9292
}
@@ -96,9 +96,9 @@ inline void CollectionImpl::removeOne(iterator it) {
9696
return;
9797
auto curr = it.slot_;
9898
auto prev = getPreviousSlot(curr);
99-
auto next = curr->next();
99+
auto next = curr->next;
100100
if (prev)
101-
prev->setNext(next);
101+
prev->next = next;
102102
else
103103
data_->head = next;
104104
if (next == NULL_SLOT)
@@ -116,7 +116,7 @@ inline void CollectionImpl::removePair(ObjectImpl::iterator it) {
116116
auto valueSlot = resources_->getVariant(valueId);
117117

118118
// remove value slot
119-
keySlot->setNext(valueSlot->next());
119+
keySlot->next = valueSlot->next;
120120
resources_->freeVariant({valueSlot, valueId});
121121

122122
// remove key slot
@@ -128,7 +128,8 @@ inline size_t CollectionImpl::nesting() const {
128128
return 0;
129129
size_t maxChildNesting = 0;
130130
for (auto it = createIterator(); !it.done(); it.next(resources_)) {
131-
size_t childNesting = it->nesting(resources_);
131+
VariantImpl variant(it.data(), resources_);
132+
size_t childNesting = variant.nesting();
132133
if (childNesting > maxChildNesting)
133134
maxChildNesting = childNesting;
134135
}

src/ArduinoJson/Deserialization/deserialize.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ DeserializationError doDeserialize(TDestination&& dst, TReader reader,
5050
auto resources = VariantAttorney::getResourceManager(dst);
5151
dst.clear();
5252
auto err = TDeserializer<TReader>(resources, reader)
53-
.parse(*data, options.filter, options.nestingLimit);
53+
.parse(data, options.filter, options.nestingLimit);
5454
shrinkJsonDocument(dst);
5555
return err;
5656
}

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
}

0 commit comments

Comments
 (0)