From 6a7474af025fba507dfbf29132babedf1e6bea10 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Mon, 14 Jul 2025 18:30:28 +0200 Subject: [PATCH] VariantImpl: move out-of-class definitions back in the class --- src/ArduinoJson/Variant/VariantImpl.hpp | 130 +++++++++++------------- 1 file changed, 59 insertions(+), 71 deletions(-) diff --git a/src/ArduinoJson/Variant/VariantImpl.hpp b/src/ArduinoJson/Variant/VariantImpl.hpp index c8edc453..bce11998 100644 --- a/src/ArduinoJson/Variant/VariantImpl.hpp +++ b/src/ArduinoJson/Variant/VariantImpl.hpp @@ -425,14 +425,52 @@ class VariantImpl { } template - void setRawString(SerializedValue value); + void setRawString(SerializedValue value) { + if (!data_) + return; + auto dup = resources_->saveString(adaptString(value.data(), value.size())); + if (dup) + data_->setRawString(dup); + } template - bool setString(TAdaptedString value); + bool setString(TAdaptedString value) { + ARDUINOJSON_ASSERT(isNull()); // must call clear() first - bool setLinkedString(const char* s); + if (!data_) + return false; - void empty(); + if (value.isNull()) + return false; + + if (isTinyString(value, value.size())) { + data_->setTinyString(value); + return true; + } + + auto dup = resources_->saveString(value); + if (dup) { + data_->setLongString(dup); + return true; + } + + return false; + } + + void empty() { + auto coll = getCollectionData(); + + auto next = coll->head; + while (next != NULL_SLOT) { + auto currId = next; + auto slot = getVariant(next); + next = slot->next; + freeVariant({slot, currId}); + } + + coll->head = NULL_SLOT; + coll->tail = NULL_SLOT; + } size_t size() const; @@ -441,7 +479,23 @@ class VariantImpl { } // Release the resources used by this variant and set it to null. - void clear(); + void clear() { + if (!data_) + return; + + if (data_->type & VariantTypeBits::OwnedStringBit) + resources_->dereferenceString(data_->content.asStringNode->data); + +#if ARDUINOJSON_USE_8_BYTE_POOL + if (data_->type & VariantTypeBits::EightByteBit) + resources_->freeEightByte(data_->content.asSlotId); +#endif + + if (data_->type & VariantTypeBits::CollectionMask) + empty(); + + data_->type = VariantType::Null; + } private: template @@ -479,70 +533,4 @@ class VariantImpl { } }; -template -inline void VariantImpl::setRawString(SerializedValue value) { - if (!data_) - return; - auto dup = resources_->saveString(adaptString(value.data(), value.size())); - if (dup) - data_->setRawString(dup); -} - -template -inline bool VariantImpl::setString(TAdaptedString value) { - ARDUINOJSON_ASSERT(isNull()); // must call clear() first - - if (!data_) - return false; - - if (value.isNull()) - return false; - - if (isTinyString(value, value.size())) { - data_->setTinyString(value); - return true; - } - - auto dup = resources_->saveString(value); - if (dup) { - data_->setLongString(dup); - return true; - } - - return false; -} - -inline void VariantImpl::clear() { - if (!data_) - return; - - if (data_->type & VariantTypeBits::OwnedStringBit) - resources_->dereferenceString(data_->content.asStringNode->data); - -#if ARDUINOJSON_USE_8_BYTE_POOL - if (data_->type & VariantTypeBits::EightByteBit) - resources_->freeEightByte(data_->content.asSlotId); -#endif - - if (data_->type & VariantTypeBits::CollectionMask) - empty(); - - data_->type = VariantType::Null; -} - -inline void VariantImpl::empty() { - auto coll = getCollectionData(); - - auto next = coll->head; - while (next != NULL_SLOT) { - auto currId = next; - auto slot = getVariant(next); - next = slot->next; - freeVariant({slot, currId}); - } - - coll->head = NULL_SLOT; - coll->tail = NULL_SLOT; -} - ARDUINOJSON_END_PRIVATE_NAMESPACE