From d0e3808dd0c79fe874243de31942d235780d4630 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Wed, 13 Apr 2022 18:13:28 +0200 Subject: [PATCH] Move declaration of `VariantConstRef` above `VariantRef` --- src/ArduinoJson/Variant/VariantRef.hpp | 349 +++++++++++++------------ 1 file changed, 175 insertions(+), 174 deletions(-) diff --git a/src/ArduinoJson/Variant/VariantRef.hpp b/src/ArduinoJson/Variant/VariantRef.hpp index e26ec2d5..d01ca751 100644 --- a/src/ArduinoJson/Variant/VariantRef.hpp +++ b/src/ArduinoJson/Variant/VariantRef.hpp @@ -57,188 +57,15 @@ class VariantRefBase : public VariantTag { } }; -// A variant that can be a any value serializable to a JSON value. -// -// It can be set to: -// - a boolean -// - a char, short, int or a long (signed or unsigned) -// - a string (const char*) -// - a reference to a ArrayRef or ObjectRef -class VariantRef : public VariantRefBase, - public VariantOperators, - public VariantShortcuts, - public Visitable { - typedef VariantRefBase base_type; - friend class VariantConstRef; - - public: - // Intenal use only - FORCE_INLINE VariantRef(MemoryPool *pool, VariantData *data) - : base_type(data), _pool(pool) {} - - // Creates an uninitialized VariantRef - FORCE_INLINE VariantRef() : base_type(0), _pool(0) {} - - FORCE_INLINE void clear() const { - return variantSetNull(_data); - } - - template - FORCE_INLINE bool set(const T &value) const { - Converter::toJson(value, *this); - return _pool && !_pool->overflowed(); - } - - bool ARDUINOJSON_DEPRECATED( - "Support for char is deprecated, use int8_t or uint8_t instead") - set(char value) const; - - template - FORCE_INLINE bool set(T *value) const { - Converter::toJson(value, *this); - return _pool && !_pool->overflowed(); - } - - template - FORCE_INLINE - typename enable_if::value && !is_same::value, - T>::type - as() const { - return Converter::fromJson(*this); - } - - template - FORCE_INLINE typename enable_if::value, const char *>::type - ARDUINOJSON_DEPRECATED("Replace as() with as()") - as() const { - return as(); - } - - template - FORCE_INLINE typename enable_if::value, char>::type - ARDUINOJSON_DEPRECATED( - "Support for char is deprecated, use int8_t or uint8_t instead") - as() const { - return static_cast(as()); - } - - template - FORCE_INLINE - typename enable_if::value && !is_same::value, - bool>::type - is() const { - return Converter::checkJson(*this); - } - - template - FORCE_INLINE typename enable_if::value, bool>::type - ARDUINOJSON_DEPRECATED("Replace is() with is()") - is() const { - return is(); - } - - template - FORCE_INLINE typename enable_if::value, bool>::type - ARDUINOJSON_DEPRECATED( - "Support for char is deprecated, use int8_t or uint8_t instead") - is() const { - return is(); - } - - template - FORCE_INLINE operator T() const { - return as(); - } - - template - typename TVisitor::result_type accept(TVisitor &visitor) const { - return variantAccept(_data, visitor); - } - - // Change the type of the variant - // - // ArrayRef to() - template - typename enable_if::value, ArrayRef>::type to() const; - // - // ObjectRef to() - template - typename enable_if::value, ObjectRef>::type to() const; - // - // ObjectRef to() - template - typename enable_if::value, VariantRef>::type to() - const; - - VariantRef addElement() const; - - FORCE_INLINE VariantRef getElement(size_t) const; - - FORCE_INLINE VariantRef getOrAddElement(size_t) const; - - // getMember(const char*) const - // getMember(const __FlashStringHelper*) const - template - FORCE_INLINE VariantRef getMember(TChar *) const; - - // getMember(const std::string&) const - // getMember(const String&) const - template - FORCE_INLINE typename enable_if::value, VariantRef>::type - getMember(const TString &) const; - - // getOrAddMember(char*) const - // getOrAddMember(const char*) const - // getOrAddMember(const __FlashStringHelper*) const - template - FORCE_INLINE VariantRef getOrAddMember(TChar *) const; - - // getOrAddMember(const std::string&) const - // getOrAddMember(const String&) const - template - FORCE_INLINE VariantRef getOrAddMember(const TString &) const; - - FORCE_INLINE void remove(size_t index) const { - if (_data) - _data->remove(index); - } - // remove(char*) const - // remove(const char*) const - // remove(const __FlashStringHelper*) const - template - FORCE_INLINE typename enable_if::value>::type remove( - TChar *key) const { - if (_data) - _data->remove(adaptString(key)); - } - // remove(const std::string&) const - // remove(const String&) const - template - FORCE_INLINE typename enable_if::value>::type remove( - const TString &key) const { - if (_data) - _data->remove(adaptString(key)); - } - - private: - MemoryPool *_pool; - - friend MemoryPool *getPool(const VariantRef &variant) { - return variant._pool; - } -}; - class VariantConstRef : public VariantRefBase, public VariantOperators, public VariantShortcuts, public Visitable { typedef VariantRefBase base_type; - friend class VariantRef; public: VariantConstRef() : base_type(0) {} - VariantConstRef(const VariantData *data) : base_type(data) {} - VariantConstRef(VariantRef var) : base_type(var._data) {} + explicit VariantConstRef(const VariantData *data) : base_type(data) {} template typename TVisitor::result_type accept(TVisitor &visitor) const { @@ -339,6 +166,180 @@ class VariantConstRef : public VariantRefBase, } }; +// A variant that can be a any value serializable to a JSON value. +// +// It can be set to: +// - a boolean +// - a char, short, int or a long (signed or unsigned) +// - a string (const char*) +// - a reference to a ArrayRef or ObjectRef +class VariantRef : public VariantRefBase, + public VariantOperators, + public VariantShortcuts, + public Visitable { + typedef VariantRefBase base_type; + + public: + // Intenal use only + FORCE_INLINE VariantRef(MemoryPool *pool, VariantData *data) + : base_type(data), _pool(pool) {} + + // Creates an uninitialized VariantRef + FORCE_INLINE VariantRef() : base_type(0), _pool(0) {} + + FORCE_INLINE void clear() const { + return variantSetNull(_data); + } + + template + FORCE_INLINE bool set(const T &value) const { + Converter::toJson(value, *this); + return _pool && !_pool->overflowed(); + } + + bool ARDUINOJSON_DEPRECATED( + "Support for char is deprecated, use int8_t or uint8_t instead") + set(char value) const; + + template + FORCE_INLINE bool set(T *value) const { + Converter::toJson(value, *this); + return _pool && !_pool->overflowed(); + } + + template + FORCE_INLINE + typename enable_if::value && !is_same::value, + T>::type + as() const { + return Converter::fromJson(*this); + } + + template + FORCE_INLINE typename enable_if::value, const char *>::type + ARDUINOJSON_DEPRECATED("Replace as() with as()") + as() const { + return as(); + } + + template + FORCE_INLINE typename enable_if::value, char>::type + ARDUINOJSON_DEPRECATED( + "Support for char is deprecated, use int8_t or uint8_t instead") + as() const { + return static_cast(as()); + } + + template + FORCE_INLINE + typename enable_if::value && !is_same::value, + bool>::type + is() const { + return Converter::checkJson(*this); + } + + template + FORCE_INLINE typename enable_if::value, bool>::type + ARDUINOJSON_DEPRECATED("Replace is() with is()") + is() const { + return is(); + } + + template + FORCE_INLINE typename enable_if::value, bool>::type + ARDUINOJSON_DEPRECATED( + "Support for char is deprecated, use int8_t or uint8_t instead") + is() const { + return is(); + } + + template + FORCE_INLINE operator T() const { + return as(); + } + + FORCE_INLINE operator VariantConstRef() const { + return VariantConstRef(_data); + } + + template + typename TVisitor::result_type accept(TVisitor &visitor) const { + return variantAccept(_data, visitor); + } + + // Change the type of the variant + // + // ArrayRef to() + template + typename enable_if::value, ArrayRef>::type to() const; + // + // ObjectRef to() + template + typename enable_if::value, ObjectRef>::type to() const; + // + // ObjectRef to() + template + typename enable_if::value, VariantRef>::type to() + const; + + VariantRef addElement() const; + + FORCE_INLINE VariantRef getElement(size_t) const; + + FORCE_INLINE VariantRef getOrAddElement(size_t) const; + + // getMember(const char*) const + // getMember(const __FlashStringHelper*) const + template + FORCE_INLINE VariantRef getMember(TChar *) const; + + // getMember(const std::string&) const + // getMember(const String&) const + template + FORCE_INLINE typename enable_if::value, VariantRef>::type + getMember(const TString &) const; + + // getOrAddMember(char*) const + // getOrAddMember(const char*) const + // getOrAddMember(const __FlashStringHelper*) const + template + FORCE_INLINE VariantRef getOrAddMember(TChar *) const; + + // getOrAddMember(const std::string&) const + // getOrAddMember(const String&) const + template + FORCE_INLINE VariantRef getOrAddMember(const TString &) const; + + FORCE_INLINE void remove(size_t index) const { + if (_data) + _data->remove(index); + } + // remove(char*) const + // remove(const char*) const + // remove(const __FlashStringHelper*) const + template + FORCE_INLINE typename enable_if::value>::type remove( + TChar *key) const { + if (_data) + _data->remove(adaptString(key)); + } + // remove(const std::string&) const + // remove(const String&) const + template + FORCE_INLINE typename enable_if::value>::type remove( + const TString &key) const { + if (_data) + _data->remove(adaptString(key)); + } + + private: + MemoryPool *_pool; + + friend MemoryPool *getPool(const VariantRef &variant) { + return variant._pool; + } +}; + template <> struct Converter { static void toJson(VariantRef src, VariantRef dst) {