Skip to content

Commit 783f7ac

Browse files
committed
Merge ArrayImpl into VariantImpl
1 parent 33c5c3e commit 783f7ac

File tree

13 files changed

+84
-139
lines changed

13 files changed

+84
-139
lines changed

src/ArduinoJson/Array/ArrayData.hpp

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/ArduinoJson/Array/ArrayImpl.hpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
#pragma once
66

7-
#include <ArduinoJson/Array/ArrayData.hpp>
87
#include <ArduinoJson/Variant/VariantCompare.hpp>
9-
#include <ArduinoJson/Variant/VariantData.hpp>
8+
#include <ArduinoJson/Variant/VariantImpl.hpp>
109

1110
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
1211

13-
inline ArrayImpl::iterator ArrayImpl::at(size_t index) const {
14-
if (isNull())
12+
inline VariantImpl::iterator VariantImpl::at(size_t index) const {
13+
if (!isArray())
1514
return iterator();
1615

1716
auto it = createIterator();
@@ -22,8 +21,8 @@ inline ArrayImpl::iterator ArrayImpl::at(size_t index) const {
2221
return it;
2322
}
2423

25-
inline VariantData* ArrayImpl::addElement(VariantData* data,
26-
ResourceManager* resources) {
24+
inline VariantData* VariantImpl::addElement(VariantData* data,
25+
ResourceManager* resources) {
2726
ARDUINOJSON_ASSERT(data != nullptr);
2827
ARDUINOJSON_ASSERT(data->isArray());
2928
ARDUINOJSON_ASSERT(resources != nullptr);
@@ -35,7 +34,7 @@ inline VariantData* ArrayImpl::addElement(VariantData* data,
3534
return slot.ptr();
3635
}
3736

38-
inline VariantData* ArrayImpl::getOrAddElement(size_t index) {
37+
inline VariantData* VariantImpl::getOrAddElement(size_t index) {
3938
auto it = createIterator();
4039
while (!it.done() && index > 0) {
4140
it.next(resources_);
@@ -53,17 +52,17 @@ inline VariantData* ArrayImpl::getOrAddElement(size_t index) {
5352
return element;
5453
}
5554

56-
inline VariantData* ArrayImpl::getElement(size_t index) const {
55+
inline VariantData* VariantImpl::getElement(size_t index) const {
5756
return at(index).data();
5857
}
5958

60-
inline void ArrayImpl::removeElement(size_t index) {
59+
inline void VariantImpl::removeElement(size_t index) {
6160
remove(at(index));
6261
}
6362

6463
template <typename T>
65-
inline bool ArrayImpl::addValue(const T& value, VariantData* data,
66-
ResourceManager* resources) {
64+
inline bool VariantImpl::addValue(const T& value, VariantData* data,
65+
ResourceManager* resources) {
6766
ARDUINOJSON_ASSERT(data != nullptr);
6867
ARDUINOJSON_ASSERT(resources != nullptr);
6968
auto slot = resources->allocVariant();

src/ArduinoJson/Array/ElementProxy.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ class ElementProxy : public VariantRefBase<ElementProxy<TUpstream>>,
5959
}
6060

6161
VariantData* getOrCreateData() const {
62-
return VariantAttorney::getOrCreateVariantImpl(upstream_).getOrAddElement(
63-
index_);
62+
auto data = VariantAttorney::getOrCreateData(upstream_);
63+
auto resources = VariantAttorney::getResourceManager(upstream_);
64+
if (data && data->type == VariantType::Null)
65+
data->toArray();
66+
return VariantImpl(data, resources).getOrAddElement(index_);
6467
}
6568

6669
TUpstream upstream_;

src/ArduinoJson/Array/JsonArray.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
2727
: impl_(data, resources) {}
2828

2929
// INTERNAL USE ONLY
30-
JsonArray(const detail::ArrayImpl& impl) : impl_(impl) {}
30+
JsonArray(const detail::VariantImpl& impl) : impl_(impl) {}
3131

3232
// Returns a JsonVariant pointing to the array.
3333
// https://arduinojson.org/v7/api/jsonvariant/
@@ -89,9 +89,6 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
8989
// Copies an array.
9090
// https://arduinojson.org/v7/api/jsonarray/set/
9191
bool set(JsonArrayConst src) const {
92-
if (isNull())
93-
return false;
94-
9592
clear();
9693
for (auto element : src) {
9794
if (!add(element))
@@ -125,7 +122,7 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
125122
// Removes all the elements of the array.
126123
// https://arduinojson.org/v7/api/jsonarray/clear/
127124
void clear() const {
128-
impl_.clear();
125+
impl_.empty();
129126
}
130127

131128
// Gets or sets the element at the specified index.
@@ -210,7 +207,7 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
210207
return impl_.getData();
211208
}
212209

213-
mutable detail::ArrayImpl impl_;
210+
mutable detail::VariantImpl impl_;
214211
};
215212

216213
ARDUINOJSON_END_PUBLIC_NAMESPACE

src/ArduinoJson/Array/JsonArrayConst.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
4141
: impl_(data, resources) {}
4242

4343
// INTERNAL USE ONLY
44-
JsonArrayConst(const detail::ArrayImpl& impl) : impl_(impl) {}
44+
JsonArrayConst(const detail::VariantImpl& impl) : impl_(impl) {}
4545

4646
// Returns the element at the specified index.
4747
// https://arduinojson.org/v7/api/jsonarrayconst/subscript/
@@ -102,7 +102,7 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
102102
return impl_.getData();
103103
}
104104

105-
detail::ArrayImpl impl_;
105+
detail::VariantImpl impl_;
106106
};
107107

108108
// Compares the content of two arrays.

src/ArduinoJson/Array/JsonArrayIterator.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class JsonArrayIterator {
3030

3131
public:
3232
JsonArrayIterator() {}
33-
explicit JsonArrayIterator(detail::ArrayImpl::iterator iterator,
33+
explicit JsonArrayIterator(detail::CollectionIterator iterator,
3434
detail::ResourceManager* resources)
3535
: iterator_(iterator), resources_(resources) {}
3636

@@ -55,7 +55,7 @@ class JsonArrayIterator {
5555
}
5656

5757
private:
58-
detail::ArrayImpl::iterator iterator_;
58+
detail::CollectionIterator iterator_;
5959
detail::ResourceManager* resources_;
6060
};
6161

@@ -64,7 +64,7 @@ class JsonArrayConstIterator {
6464

6565
public:
6666
JsonArrayConstIterator() {}
67-
explicit JsonArrayConstIterator(detail::ArrayImpl::iterator iterator,
67+
explicit JsonArrayConstIterator(detail::CollectionIterator iterator,
6868
detail::ResourceManager* resources)
6969
: iterator_(iterator), resources_(resources) {}
7070

@@ -89,7 +89,7 @@ class JsonArrayConstIterator {
8989
}
9090

9191
private:
92-
mutable detail::ArrayImpl::iterator iterator_;
92+
mutable detail::CollectionIterator iterator_;
9393
mutable detail::ResourceManager* resources_;
9494
};
9595

src/ArduinoJson/Collection/CollectionData.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ class CollectionImpl {
120120
return data_->content.asCollection.head;
121121
}
122122

123-
protected:
124123
static void appendOne(Slot<VariantData> slot, VariantData*, ResourceManager*);
125124

126125
static void appendPair(Slot<VariantData> key, Slot<VariantData> value,

src/ArduinoJson/Document/JsonDocument.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,22 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
287287
template <typename T, detail::enable_if_t<
288288
detail::is_same<T, JsonVariant>::value, int> = 0>
289289
JsonVariant add() {
290-
return JsonVariant(getVariantImpl().addElement(), &resources_);
290+
return JsonVariant(getOrCreateArray().addElement(), &resources_);
291291
}
292292

293293
// Appends a value to the root array.
294294
// https://arduinojson.org/v7/api/jsondocument/add/
295295
template <typename TValue>
296296
bool add(const TValue& value) {
297-
return getVariantImpl().addValue(value);
297+
return getOrCreateArray().addValue(value);
298298
}
299299

300300
// Appends a value to the root array.
301301
// https://arduinojson.org/v7/api/jsondocument/add/
302302
template <typename TChar,
303303
detail::enable_if_t<!detail::is_const<TChar>::value, int> = 0>
304304
bool add(TChar* value) {
305-
return getVariantImpl().addValue(value);
305+
return getOrCreateArray().addValue(value);
306306
}
307307

308308
// Removes an element of the root array.
@@ -412,6 +412,12 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
412412
return detail::VariantImpl(&data_, &resources_);
413413
}
414414

415+
detail::VariantImpl getOrCreateArray() const {
416+
if (data_.type == detail::VariantType::Null)
417+
data_.toArray();
418+
return detail::VariantImpl(&data_, &resources_);
419+
}
420+
415421
JsonVariant getVariant() {
416422
return JsonVariant(&data_, &resources_);
417423
}

src/ArduinoJson/Json/JsonDeserializer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class JsonDeserializer {
174174
for (;;) {
175175
if (elementFilter.allow()) {
176176
// Allocate slot in array
177-
VariantData* value = ArrayImpl::addElement(array, resources_);
177+
VariantData* value = VariantImpl::addElement(array, resources_);
178178
if (!value)
179179
return DeserializationError::NoMemory;
180180

src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ class MsgPackDeserializer {
360360
VariantData* value;
361361

362362
if (elementFilter.allow()) {
363-
value = ArrayImpl::addElement(variant, resources_);
363+
value = VariantImpl::addElement(variant, resources_);
364364
if (!value)
365365
return DeserializationError::NoMemory;
366366
} else {

0 commit comments

Comments
 (0)