Skip to content

Commit fe1c824

Browse files
committed
VariantImpl: add generic set()
1 parent 43548db commit fe1c824

File tree

6 files changed

+54
-39
lines changed

6 files changed

+54
-39
lines changed

src/ArduinoJson/Array/ArrayImpl.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ inline bool VariantImpl::addValue(const T& value) {
7171
auto slot = allocVariant();
7272
if (!slot)
7373
return false;
74-
JsonVariant variant(slot.ptr(), resources_);
75-
if (!variant.set(value)) {
74+
if (!VariantImpl(slot.ptr(), resources_).set(value)) {
7675
freeVariant(slot);
7776
return false;
7877
}

src/ArduinoJson/Document/JsonDocument.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
132132
// Copies the specified document.
133133
// https://arduinojson.org/v7/api/jsondocument/set/
134134
bool set(const JsonDocument& src) {
135-
return to<JsonVariant>().set(src.as<JsonVariantConst>());
135+
return getImpl().set(src.as<JsonVariantConst>());
136136
}
137137

138138
// Replaces the root with the specified value.
@@ -141,15 +141,15 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
141141
typename T,
142142
detail::enable_if_t<!detail::is_base_of<JsonDocument, T>::value, int> = 0>
143143
bool set(const T& src) {
144-
return to<JsonVariant>().set(src);
144+
return getImpl().set(src);
145145
}
146146

147147
// Replaces the root with the specified value.
148148
// https://arduinojson.org/v7/api/jsondocument/set/
149149
template <typename TChar,
150150
detail::enable_if_t<!detail::is_const<TChar>::value, int> = 0>
151151
bool set(TChar* src) {
152-
return to<JsonVariant>().set(src);
152+
return getImpl().set(src);
153153
}
154154

155155
// Clears the document and converts it to the specified type.

src/ArduinoJson/Variant/ConverterImpl.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,36 @@ struct Converter<JsonObject> : private detail::VariantAttorney {
362362
}
363363
};
364364

365+
template <typename TConverter, typename T>
366+
bool detail::VariantImpl::setWithConverter(const T& value) {
367+
return setWithConverter<TConverter>(
368+
value,
369+
is_same<
370+
typename function_traits<decltype(&TConverter::toJson)>::return_type,
371+
bool>{});
372+
}
373+
374+
template <typename T>
375+
inline bool detail::VariantImpl::set(const T& value) {
376+
using TypeForConverter = conditional_t<IsStringLiteral<T>::value, T,
377+
remove_cv_t<remove_reference_t<T>>>;
378+
return setWithConverter<Converter<TypeForConverter>>(value);
379+
}
380+
381+
template <typename T, detail::enable_if_t<!detail::is_const<T>::value, int> = 0>
382+
inline bool detail::VariantImpl::set(T* value) {
383+
return setWithConverter<Converter<T*>>(value);
384+
}
385+
386+
template <typename TConverter, typename T>
387+
inline bool detail::VariantImpl::setWithConverter(const T& value, false_type) {
388+
TConverter::toJson(value, JsonVariant(*this));
389+
return resources_ && !resources_->overflowed();
390+
}
391+
392+
template <typename TConverter, typename T>
393+
inline bool detail::VariantImpl::setWithConverter(const T& value, true_type) {
394+
return TConverter::toJson(value, JsonVariant(*this));
395+
}
396+
365397
ARDUINOJSON_END_PUBLIC_NAMESPACE

src/ArduinoJson/Variant/VariantImpl.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,13 @@ class VariantImpl {
489489
return true;
490490
}
491491

492+
template <typename T>
493+
bool set(const T& value);
494+
495+
template <typename T,
496+
detail::enable_if_t<!detail::is_const<T>::value, int> = 0>
497+
bool set(T* value);
498+
492499
void empty() {
493500
auto coll = getCollectionData();
494501

@@ -541,6 +548,15 @@ class VariantImpl {
541548
void removeOne(iterator it);
542549
void removePair(iterator it);
543550

551+
template <typename TConverter, typename T>
552+
bool setWithConverter(const T& value);
553+
554+
template <typename TConverter, typename T>
555+
bool setWithConverter(const T& value, false_type);
556+
557+
template <typename TConverter, typename T>
558+
bool setWithConverter(const T& value, true_type);
559+
544560
VariantData* getVariant(SlotId id) const {
545561
ARDUINOJSON_ASSERT(resources_ != nullptr);
546562
return resources_->getVariant(id);

src/ArduinoJson/Variant/VariantRefBase.hpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,15 @@ class VariantRefBase : public VariantTag {
7777
// https://arduinojson.org/v7/api/jsonvariant/set/
7878
template <typename T>
7979
bool set(const T& value) const {
80-
using TypeForConverter = conditional_t<IsStringLiteral<T>::value, T,
81-
remove_cv_t<remove_reference_t<T>>>;
82-
return doSet<Converter<TypeForConverter>>(value);
80+
return getOrCreateImpl().set(value);
8381
}
8482

8583
// Copies the specified value.
8684
// https://arduinojson.org/v7/api/jsonvariant/set/
8785
template <typename T,
8886
detail::enable_if_t<!detail::is_const<T>::value, int> = 0>
8987
bool set(T* value) const {
90-
return doSet<Converter<T*>>(value);
88+
return getOrCreateImpl().set(value);
9189
}
9290

9391
// Returns the size of the array or object.
@@ -292,20 +290,6 @@ class VariantRefBase : public VariantTag {
292290
const {
293291
return getVariant();
294292
}
295-
296-
template <typename TConverter, typename T>
297-
bool doSet(const T& value) const {
298-
return doSet<TConverter>(
299-
value, is_same<typename function_traits<
300-
decltype(&TConverter::toJson)>::return_type,
301-
bool>{});
302-
}
303-
304-
template <typename TConverter, typename T>
305-
bool doSet(const T& value, false_type) const;
306-
307-
template <typename TConverter, typename T>
308-
bool doSet(const T& value, true_type) const;
309293
};
310294

311295
ARDUINOJSON_END_PRIVATE_NAMESPACE

src/ArduinoJson/Variant/VariantRefBaseImpl.hpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,6 @@ VariantRefBase<TDerived>::operator[](const TString& key) const {
125125
return {derived(), adaptString(key)};
126126
}
127127

128-
template <typename TDerived>
129-
template <typename TConverter, typename T>
130-
inline bool VariantRefBase<TDerived>::doSet(const T& value, false_type) const {
131-
auto impl = getOrCreateImpl();
132-
TConverter::toJson(value, JsonVariant(impl));
133-
auto resources = impl.resources();
134-
return resources && !resources->overflowed();
135-
}
136-
137-
template <typename TDerived>
138-
template <typename TConverter, typename T>
139-
inline bool VariantRefBase<TDerived>::doSet(const T& value, true_type) const {
140-
auto impl = getOrCreateImpl();
141-
return TConverter::toJson(value, JsonVariant(impl));
142-
}
143-
144128
template <typename TDerived>
145129
template <typename T, enable_if_t<is_same<T, JsonArray>::value, int>>
146130
inline JsonArray VariantRefBase<TDerived>::to() const {

0 commit comments

Comments
 (0)