Skip to content

Commit 5def9a3

Browse files
committed
VariantImpl: move out-of-class definition back in the class
1 parent 2e20ce0 commit 5def9a3

File tree

1 file changed

+75
-87
lines changed

1 file changed

+75
-87
lines changed

src/ArduinoJson/Variant/VariantImpl.hpp

Lines changed: 75 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,68 @@ class VariantImpl {
441441
}
442442

443443
template <typename T>
444-
void setRawString(SerializedValue<T> value);
444+
void setRawString(SerializedValue<T> value) {
445+
if (!data_)
446+
return;
447+
auto dup = resources_->saveString(adaptString(value.data(), value.size()));
448+
if (dup)
449+
data_->setRawString(dup);
450+
}
445451

446452
template <typename TAdaptedString>
447-
bool setString(TAdaptedString value);
453+
bool setString(TAdaptedString value) {
454+
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
455+
456+
if (!data_)
457+
return false;
458+
459+
if (value.isNull())
460+
return false;
448461

449-
bool setLinkedString(const char* s);
462+
if (value.isStatic())
463+
return setLinkedString(value.data());
450464

451-
void empty();
465+
if (isTinyString(value, value.size())) {
466+
data_->setTinyString(value);
467+
return true;
468+
}
469+
470+
auto dup = resources_->saveString(value);
471+
if (dup) {
472+
data_->setOwnedString(dup);
473+
return true;
474+
}
475+
476+
return false;
477+
}
478+
479+
bool setLinkedString(const char* s) {
480+
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
481+
ARDUINOJSON_ASSERT(s);
482+
483+
auto slotId = resources_->saveStaticString(s);
484+
if (slotId == NULL_SLOT)
485+
return false;
486+
487+
data_->type = VariantType::LinkedString;
488+
data_->content.asSlotId = slotId;
489+
return true;
490+
}
491+
492+
void empty() {
493+
auto coll = getCollectionData();
494+
495+
auto next = coll->head;
496+
while (next != NULL_SLOT) {
497+
auto currId = next;
498+
auto slot = getVariant(next);
499+
next = slot->next;
500+
freeVariant({slot, currId});
501+
}
502+
503+
coll->head = NULL_SLOT;
504+
coll->tail = NULL_SLOT;
505+
}
452506

453507
size_t size() const;
454508

@@ -457,7 +511,23 @@ class VariantImpl {
457511
}
458512

459513
// Release the resources used by this variant and set it to null.
460-
void clear();
514+
void clear() {
515+
if (!data_)
516+
return;
517+
518+
if (data_->type & VariantTypeBits::OwnedStringBit)
519+
resources_->dereferenceString(data_->content.asOwnedString->data);
520+
521+
#if ARDUINOJSON_USE_8_BYTE_POOL
522+
if (data_->type & VariantTypeBits::EightByteBit)
523+
resources_->freeEightByte(data_->content.asSlotId);
524+
#endif
525+
526+
if (data_->type & VariantTypeBits::CollectionMask)
527+
empty();
528+
529+
data_->type = VariantType::Null;
530+
}
461531

462532
private:
463533
template <typename TAdaptedString>
@@ -495,86 +565,4 @@ class VariantImpl {
495565
}
496566
};
497567

498-
template <typename T>
499-
inline void VariantImpl::setRawString(SerializedValue<T> value) {
500-
if (!data_)
501-
return;
502-
auto dup = resources_->saveString(adaptString(value.data(), value.size()));
503-
if (dup)
504-
data_->setRawString(dup);
505-
}
506-
507-
inline bool VariantImpl::setLinkedString(const char* s) {
508-
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
509-
ARDUINOJSON_ASSERT(s);
510-
511-
auto slotId = resources_->saveStaticString(s);
512-
if (slotId == NULL_SLOT)
513-
return false;
514-
515-
data_->type = VariantType::LinkedString;
516-
data_->content.asSlotId = slotId;
517-
return true;
518-
}
519-
520-
template <typename TAdaptedString>
521-
inline bool VariantImpl::setString(TAdaptedString value) {
522-
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
523-
524-
if (!data_)
525-
return false;
526-
527-
if (value.isNull())
528-
return false;
529-
530-
if (value.isStatic())
531-
return setLinkedString(value.data());
532-
533-
if (isTinyString(value, value.size())) {
534-
data_->setTinyString(value);
535-
return true;
536-
}
537-
538-
auto dup = resources_->saveString(value);
539-
if (dup) {
540-
data_->setOwnedString(dup);
541-
return true;
542-
}
543-
544-
return false;
545-
}
546-
547-
inline void VariantImpl::clear() {
548-
if (!data_)
549-
return;
550-
551-
if (data_->type & VariantTypeBits::OwnedStringBit)
552-
resources_->dereferenceString(data_->content.asOwnedString->data);
553-
554-
#if ARDUINOJSON_USE_8_BYTE_POOL
555-
if (data_->type & VariantTypeBits::EightByteBit)
556-
resources_->freeEightByte(data_->content.asSlotId);
557-
#endif
558-
559-
if (data_->type & VariantTypeBits::CollectionMask)
560-
empty();
561-
562-
data_->type = VariantType::Null;
563-
}
564-
565-
inline void VariantImpl::empty() {
566-
auto coll = getCollectionData();
567-
568-
auto next = coll->head;
569-
while (next != NULL_SLOT) {
570-
auto currId = next;
571-
auto slot = getVariant(next);
572-
next = slot->next;
573-
freeVariant({slot, currId});
574-
}
575-
576-
coll->head = NULL_SLOT;
577-
coll->tail = NULL_SLOT;
578-
}
579-
580568
ARDUINOJSON_END_PRIVATE_NAMESPACE

0 commit comments

Comments
 (0)