mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Implemented JsonVariant comparisons with template friends
This commit is contained in:
		| @@ -10,7 +10,6 @@ | ||||
| #include "ArduinoJson/DynamicJsonBuffer.hpp" | ||||
| #include "ArduinoJson/JsonArray.hpp" | ||||
| #include "ArduinoJson/JsonObject.hpp" | ||||
| #include "ArduinoJson/JsonVariantComparisons.hpp" | ||||
| #include "ArduinoJson/StaticJsonBuffer.hpp" | ||||
|  | ||||
| #include "ArduinoJson/Deserialization/JsonParserImpl.hpp" | ||||
|   | ||||
| @@ -1,69 +0,0 @@ | ||||
| // Copyright Benoit Blanchon 2014-2017 | ||||
| // MIT License | ||||
| // | ||||
| // Arduino JSON library | ||||
| // https://bblanchon.github.io/ArduinoJson/ | ||||
| // If you like this project, please add a star! | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "../JsonVariantBase.hpp" | ||||
| #include "../StringTraits/StringTraits.hpp" | ||||
| #include "../TypeTraits/EnableIf.hpp" | ||||
|  | ||||
| namespace ArduinoJson { | ||||
| namespace Internals { | ||||
| template <typename TComparand, typename Enable = void> | ||||
| struct JsonVariantComparer {}; | ||||
|  | ||||
| template <typename TString> | ||||
| struct JsonVariantComparer< | ||||
|     TString, | ||||
|     typename TypeTraits::EnableIf<TypeTraits::IsString<TString>::value>::type> { | ||||
|   template <typename TVariant> | ||||
|   static bool equals(const JsonVariantBase<TVariant> &variant, | ||||
|                      const TString &comparand) { | ||||
|     const char *value = variant.template as<const char *>(); | ||||
|     return Internals::StringTraits<TString>::equals(comparand, value); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| template <typename TComparand> | ||||
| struct JsonVariantComparer< | ||||
|     TComparand, typename TypeTraits::EnableIf< | ||||
|                     !TypeTraits::IsVariant<TComparand>::value && | ||||
|                     !TypeTraits::IsString<TComparand>::value>::type> { | ||||
|   template <typename TVariant> | ||||
|   static bool equals(const JsonVariantBase<TVariant> &variant, | ||||
|                      const TComparand &comparand) { | ||||
|     return variant.template as<TComparand>() == comparand; | ||||
|   } | ||||
| }; | ||||
|  | ||||
| template <typename TVariant2> | ||||
| struct JsonVariantComparer<TVariant2, | ||||
|                            typename TypeTraits::EnableIf< | ||||
|                                TypeTraits::IsVariant<TVariant2>::value>::type> { | ||||
|   template <typename TVariant1> | ||||
|   static bool equals(const JsonVariantBase<TVariant1> &left, | ||||
|                      const TVariant2 &right) { | ||||
|     if (left.template is<bool>() && right.template is<bool>()) | ||||
|       return left.template as<bool>() == right.template as<bool>(); | ||||
|     if (left.template is<JsonInteger>() && right.template is<JsonInteger>()) | ||||
|       return left.template as<JsonInteger>() == | ||||
|              right.template as<JsonInteger>(); | ||||
|     if (left.template is<JsonFloat>() && right.template is<JsonFloat>()) | ||||
|       return left.template as<JsonFloat>() == right.template as<JsonFloat>(); | ||||
|     if (left.template is<JsonArray>() && right.template is<JsonArray>()) | ||||
|       return left.template as<JsonArray>() == right.template as<JsonArray>(); | ||||
|     if (left.template is<JsonObject>() && right.template is<JsonObject>()) | ||||
|       return left.template as<JsonObject>() == right.template as<JsonObject>(); | ||||
|     if (left.template is<char *>() && right.template is<char *>()) | ||||
|       return strcmp(left.template as<char *>(), right.template as<char *>()) == | ||||
|              0; | ||||
|  | ||||
|     return false; | ||||
|   } | ||||
| }; | ||||
| } | ||||
| } | ||||
| @@ -106,15 +106,16 @@ inline const JsonArraySubscript JsonArray::operator[](size_t index) const { | ||||
|   return JsonArraySubscript(*const_cast<JsonArray*>(this), index); | ||||
| } | ||||
|  | ||||
| template <typename TImplem> | ||||
| inline JsonArraySubscript JsonVariantBase<TImplem>::operator[](size_t index) { | ||||
|   return as<JsonArray>()[index]; | ||||
| template <typename TImpl> | ||||
| inline JsonArraySubscript JsonVariantSubscripts<TImpl>::operator[]( | ||||
|     size_t index) { | ||||
|   return impl()->template as<JsonArray>()[index]; | ||||
| } | ||||
|  | ||||
| template <typename TImplem> | ||||
| inline const JsonArraySubscript JsonVariantBase<TImplem>::operator[]( | ||||
| template <typename TImpl> | ||||
| inline const JsonArraySubscript JsonVariantSubscripts<TImpl>::operator[]( | ||||
|     size_t index) const { | ||||
|   return as<JsonArray>()[index]; | ||||
|   return impl()->template as<JsonArray>()[index]; | ||||
| } | ||||
|  | ||||
| }  // namespace ArduinoJson | ||||
|   | ||||
| @@ -7,133 +7,17 @@ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "Data/JsonVariantAs.hpp" | ||||
| #include "Polyfills/attributes.hpp" | ||||
| #include "JsonVariantCasts.hpp" | ||||
| #include "JsonVariantComparisons.hpp" | ||||
| #include "JsonVariantSubscripts.hpp" | ||||
| #include "Serialization/JsonPrintable.hpp" | ||||
|  | ||||
| namespace ArduinoJson { | ||||
|  | ||||
| // Forward declarations. | ||||
| class JsonArraySubscript; | ||||
| template <typename TKey> | ||||
| class JsonObjectSubscript; | ||||
|  | ||||
| template <typename TImpl> | ||||
| class JsonVariantBase : public Internals::JsonPrintable<TImpl> { | ||||
|  public: | ||||
| #if ARDUINOJSON_ENABLE_DEPRECATED | ||||
|   DEPRECATED("use as<JsonArray>() instead") | ||||
|   FORCE_INLINE JsonArray &asArray() const { | ||||
|     return as<JsonArray>(); | ||||
|   } | ||||
|  | ||||
|   DEPRECATED("use as<JsonObject>() instead") | ||||
|   FORCE_INLINE JsonObject &asObject() const { | ||||
|     return as<JsonObject>(); | ||||
|   } | ||||
|  | ||||
|   DEPRECATED("use as<char*>() instead") | ||||
|   FORCE_INLINE const char *asString() const { | ||||
|     return as<const char *>(); | ||||
|   } | ||||
| #endif | ||||
|  | ||||
|   // Gets the variant as an array. | ||||
|   // Returns a reference to the JsonArray or JsonArray::invalid() if the | ||||
|   // variant | ||||
|   // is not an array. | ||||
|   FORCE_INLINE operator JsonArray &() const { | ||||
|     return as<JsonArray &>(); | ||||
|   } | ||||
|  | ||||
|   // Gets the variant as an object. | ||||
|   // Returns a reference to the JsonObject or JsonObject::invalid() if the | ||||
|   // variant is not an object. | ||||
|   FORCE_INLINE operator JsonObject &() const { | ||||
|     return as<JsonObject &>(); | ||||
|   } | ||||
|  | ||||
|   template <typename T> | ||||
|   FORCE_INLINE operator T() const { | ||||
|     return as<T>(); | ||||
|   } | ||||
|  | ||||
|   template <typename T> | ||||
|   FORCE_INLINE const typename Internals::JsonVariantAs<T>::type as() const { | ||||
|     return impl()->template as<T>(); | ||||
|   } | ||||
|  | ||||
|   template <typename T> | ||||
|   FORCE_INLINE bool is() const { | ||||
|     return impl()->template is<T>(); | ||||
|   } | ||||
|  | ||||
|   // Mimics an array or an object. | ||||
|   // Returns the size of the array or object if the variant has that type. | ||||
|   // Returns 0 if the variant is neither an array nor an object | ||||
|   size_t size() const { | ||||
|     return as<JsonArray>().size() + as<JsonObject>().size(); | ||||
|   } | ||||
|  | ||||
|   // Mimics an array. | ||||
|   // Returns the element at specified index if the variant is an array. | ||||
|   // Returns JsonVariant::invalid() if the variant is not an array. | ||||
|   FORCE_INLINE const JsonArraySubscript operator[](size_t index) const; | ||||
|   FORCE_INLINE JsonArraySubscript operator[](size_t index); | ||||
|  | ||||
|   // Mimics an object. | ||||
|   // Returns the value associated with the specified key if the variant is | ||||
|   // an object. | ||||
|   // Return JsonVariant::invalid() if the variant is not an object. | ||||
|   // | ||||
|   // const JsonObjectSubscript operator[](TKey) const; | ||||
|   // TKey = const std::string&, const String& | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE typename TypeTraits::EnableIf< | ||||
|       Internals::StringTraits<TString>::has_equals, | ||||
|       const JsonObjectSubscript<const TString &> >::type | ||||
|   operator[](const TString &key) const { | ||||
|     return as<JsonObject>()[key]; | ||||
|   } | ||||
|   // | ||||
|   // const JsonObjectSubscript operator[](TKey) const; | ||||
|   // TKey = const std::string&, const String& | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE typename TypeTraits::EnableIf< | ||||
|       Internals::StringTraits<TString>::has_equals, | ||||
|       JsonObjectSubscript<const TString &> >::type | ||||
|   operator[](const TString &key) { | ||||
|     return as<JsonObject>()[key]; | ||||
|   } | ||||
|   // | ||||
|   // JsonObjectSubscript operator[](TKey); | ||||
|   // TKey = const char*, const char[N], const FlashStringHelper* | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE typename TypeTraits::EnableIf< | ||||
|       Internals::StringTraits<const TString *>::has_equals, | ||||
|       JsonObjectSubscript<const TString *> >::type | ||||
|   operator[](const TString *key) { | ||||
|     return as<JsonObject>()[key]; | ||||
|   } | ||||
|   // | ||||
|   // JsonObjectSubscript operator[](TKey); | ||||
|   // TKey = const char*, const char[N], const FlashStringHelper* | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE typename TypeTraits::EnableIf< | ||||
|       Internals::StringTraits<TString *>::has_equals, | ||||
|       const JsonObjectSubscript<const TString *> >::type | ||||
|   operator[](const TString *key) const { | ||||
|     return as<JsonObject>()[key]; | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   const TImpl *impl() const { | ||||
|     return static_cast<const TImpl *>(this); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| namespace TypeTraits { | ||||
| template <typename T> | ||||
| struct IsVariant : IsBaseOf<JsonVariantBase<T>, T> {}; | ||||
| } | ||||
| class JsonVariantBase : public Internals::JsonPrintable<TImpl>, | ||||
|                         public JsonVariantCasts<TImpl>, | ||||
|                         public JsonVariantComparisons<TImpl>, | ||||
|                         public JsonVariantSubscripts<TImpl>, | ||||
|                         public TypeTraits::JsonVariantTag {}; | ||||
| } | ||||
|   | ||||
							
								
								
									
										60
									
								
								src/ArduinoJson/JsonVariantCasts.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								src/ArduinoJson/JsonVariantCasts.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | ||||
| // Copyright Benoit Blanchon 2014-2017 | ||||
| // MIT License | ||||
| // | ||||
| // Arduino JSON library | ||||
| // https://bblanchon.github.io/ArduinoJson/ | ||||
| // If you like this project, please add a star! | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "Data/JsonVariantAs.hpp" | ||||
| #include "Polyfills/attributes.hpp" | ||||
|  | ||||
| namespace ArduinoJson { | ||||
|  | ||||
| template <typename TImpl> | ||||
| class JsonVariantCasts { | ||||
|  public: | ||||
| #if ARDUINOJSON_ENABLE_DEPRECATED | ||||
|   DEPRECATED("use as<JsonArray>() instead") | ||||
|   FORCE_INLINE JsonArray &asArray() const { | ||||
|     return impl()->template as<JsonArray>(); | ||||
|   } | ||||
|  | ||||
|   DEPRECATED("use as<JsonObject>() instead") | ||||
|   FORCE_INLINE JsonObject &asObject() const { | ||||
|     return impl()->template as<JsonObject>(); | ||||
|   } | ||||
|  | ||||
|   DEPRECATED("use as<char*>() instead") | ||||
|   FORCE_INLINE const char *asString() const { | ||||
|     return impl()->template as<const char *>(); | ||||
|   } | ||||
| #endif | ||||
|  | ||||
|   // Gets the variant as an array. | ||||
|   // Returns a reference to the JsonArray or JsonArray::invalid() if the | ||||
|   // variant | ||||
|   // is not an array. | ||||
|   FORCE_INLINE operator JsonArray &() const { | ||||
|     return impl()->template as<JsonArray &>(); | ||||
|   } | ||||
|  | ||||
|   // Gets the variant as an object. | ||||
|   // Returns a reference to the JsonObject or JsonObject::invalid() if the | ||||
|   // variant is not an object. | ||||
|   FORCE_INLINE operator JsonObject &() const { | ||||
|     return impl()->template as<JsonObject &>(); | ||||
|   } | ||||
|  | ||||
|   template <typename T> | ||||
|   FORCE_INLINE operator T() const { | ||||
|     return impl()->template as<T>(); | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   const TImpl *impl() const { | ||||
|     return static_cast<const TImpl *>(this); | ||||
|   } | ||||
| }; | ||||
| } | ||||
| @@ -7,82 +7,138 @@ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "Data/JsonVariantComparer.hpp" | ||||
| #include "StringTraits/StringTraits.hpp" | ||||
| #include "TypeTraits/EnableIf.hpp" | ||||
| #include "TypeTraits/IsVariant.hpp" | ||||
|  | ||||
| namespace ArduinoJson { | ||||
| template <typename TVariant, typename TComparand> | ||||
| inline bool operator==(const JsonVariantBase<TVariant> &variant, | ||||
|                        TComparand comparand) { | ||||
|   return Internals::JsonVariantComparer<TComparand>::equals(variant, comparand); | ||||
| } | ||||
|  | ||||
| template <typename TVariant, typename TComparand> | ||||
| inline typename TypeTraits::EnableIf<!TypeTraits::IsVariant<TComparand>::value, | ||||
|                                      bool>::type | ||||
| operator==(TComparand comparand, const JsonVariantBase<TVariant> &variant) { | ||||
|   return Internals::JsonVariantComparer<TComparand>::equals(variant, comparand); | ||||
| } | ||||
| template <typename TImpl> | ||||
| class JsonVariantComparisons { | ||||
|  public: | ||||
|   template <typename TComparand> | ||||
|   friend bool operator==(const JsonVariantComparisons &variant, | ||||
|                          TComparand comparand) { | ||||
|     return variant.equals(comparand); | ||||
|   } | ||||
|  | ||||
| template <typename TVariant, typename TComparand> | ||||
| inline bool operator!=(const JsonVariantBase<TVariant> &variant, | ||||
|                        TComparand comparand) { | ||||
|   return !Internals::JsonVariantComparer<TComparand>::equals(variant, | ||||
|                                                              comparand); | ||||
| } | ||||
|   template <typename TComparand> | ||||
|   friend | ||||
|       typename TypeTraits::EnableIf<!TypeTraits::IsVariant<TComparand>::value, | ||||
|                                     bool>::type | ||||
|       operator==(TComparand comparand, const JsonVariantComparisons &variant) { | ||||
|     return variant.equals(comparand); | ||||
|   } | ||||
|  | ||||
| template <typename TVariant, typename TComparand> | ||||
| inline typename TypeTraits::EnableIf<!TypeTraits::IsVariant<TComparand>::value, | ||||
|                                      bool>::type | ||||
| operator!=(TComparand comparand, const JsonVariantBase<TVariant> &variant) { | ||||
|   return !Internals::JsonVariantComparer<TComparand>::equals(variant, | ||||
|                                                              comparand); | ||||
| } | ||||
|   template <typename TComparand> | ||||
|   friend bool operator!=(const JsonVariantComparisons &variant, | ||||
|                          TComparand comparand) { | ||||
|     return !variant.equals(comparand); | ||||
|   } | ||||
|  | ||||
| template <typename TVariant, typename TComparand> | ||||
| inline bool operator<=(const JsonVariantBase<TVariant> &left, | ||||
|                        TComparand right) { | ||||
|   return left.template as<TComparand>() <= right; | ||||
| } | ||||
|   template <typename TComparand> | ||||
|   friend | ||||
|       typename TypeTraits::EnableIf<!TypeTraits::IsVariant<TComparand>::value, | ||||
|                                     bool>::type | ||||
|       operator!=(TComparand comparand, const JsonVariantComparisons &variant) { | ||||
|     return !variant.equals(comparand); | ||||
|   } | ||||
|  | ||||
| template <typename TVariant, typename TComparand> | ||||
| inline bool operator<=(TComparand comparand, | ||||
|                        const JsonVariantBase<TVariant> &variant) { | ||||
|   return comparand <= variant.template as<TComparand>(); | ||||
| } | ||||
|   template <typename TComparand> | ||||
|   friend bool operator<=(const JsonVariantComparisons &left, TComparand right) { | ||||
|     return left.as<TComparand>() <= right; | ||||
|   } | ||||
|  | ||||
| template <typename TVariant, typename TComparand> | ||||
| inline bool operator>=(const JsonVariantBase<TVariant> &variant, | ||||
|                        TComparand comparand) { | ||||
|   return variant.template as<TComparand>() >= comparand; | ||||
| } | ||||
|   template <typename TComparand> | ||||
|   friend bool operator<=(TComparand comparand, | ||||
|                          const JsonVariantComparisons &variant) { | ||||
|     return comparand <= variant.as<TComparand>(); | ||||
|   } | ||||
|  | ||||
| template <typename TVariant, typename TComparand> | ||||
| inline bool operator>=(TComparand comparand, | ||||
|                        const JsonVariantBase<TVariant> &variant) { | ||||
|   return comparand >= variant.template as<TComparand>(); | ||||
| } | ||||
|   template <typename TComparand> | ||||
|   friend bool operator>=(const JsonVariantComparisons &variant, | ||||
|                          TComparand comparand) { | ||||
|     return variant.as<TComparand>() >= comparand; | ||||
|   } | ||||
|  | ||||
| template <typename TVariant, typename TComparand> | ||||
| inline bool operator<(const JsonVariantBase<TVariant> &varian, | ||||
|                       TComparand comparand) { | ||||
|   return varian.template as<TComparand>() < comparand; | ||||
| } | ||||
|   template <typename TComparand> | ||||
|   friend bool operator>=(TComparand comparand, | ||||
|                          const JsonVariantComparisons &variant) { | ||||
|     return comparand >= variant.as<TComparand>(); | ||||
|   } | ||||
|  | ||||
| template <typename TVariant, typename TComparand> | ||||
| inline bool operator<(TComparand comparand, | ||||
|                       const JsonVariantBase<TVariant> &variant) { | ||||
|   return comparand < variant.template as<TComparand>(); | ||||
| } | ||||
|   template <typename TComparand> | ||||
|   friend bool operator<(const JsonVariantComparisons &varian, | ||||
|                         TComparand comparand) { | ||||
|     return varian.as<TComparand>() < comparand; | ||||
|   } | ||||
|  | ||||
| template <typename TVariant, typename TComparand> | ||||
| inline bool operator>(const JsonVariantBase<TVariant> &variant, | ||||
|                       TComparand comparand) { | ||||
|   return variant.template as<TComparand>() > comparand; | ||||
| } | ||||
|   template <typename TComparand> | ||||
|   friend bool operator<(TComparand comparand, | ||||
|                         const JsonVariantComparisons &variant) { | ||||
|     return comparand < variant.as<TComparand>(); | ||||
|   } | ||||
|  | ||||
| template <typename TVariant, typename TComparand> | ||||
| inline bool operator>(TComparand comparand, | ||||
|                       const JsonVariantBase<TVariant> &variant) { | ||||
|   return comparand > variant.template as<TComparand>(); | ||||
| } | ||||
|   template <typename TComparand> | ||||
|   friend bool operator>(const JsonVariantComparisons &variant, | ||||
|                         TComparand comparand) { | ||||
|     return variant.as<TComparand>() > comparand; | ||||
|   } | ||||
|  | ||||
|   template <typename TComparand> | ||||
|   friend bool operator>(TComparand comparand, | ||||
|                         const JsonVariantComparisons &variant) { | ||||
|     return comparand > variant.as<TComparand>(); | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   const TImpl *impl() const { | ||||
|     return static_cast<const TImpl *>(this); | ||||
|   } | ||||
|  | ||||
|   template <typename T> | ||||
|   const typename Internals::JsonVariantAs<T>::type as() const { | ||||
|     return impl()->template as<T>(); | ||||
|   } | ||||
|  | ||||
|   template <typename T> | ||||
|   bool is() const { | ||||
|     return impl()->template is<T>(); | ||||
|   } | ||||
|  | ||||
|   template <typename TString> | ||||
|   typename TypeTraits::EnableIf<TypeTraits::IsString<TString>::value, | ||||
|                                 bool>::type | ||||
|   equals(const TString &comparand) const { | ||||
|     const char *value = as<const char *>(); | ||||
|     return Internals::StringTraits<TString>::equals(comparand, value); | ||||
|   } | ||||
|  | ||||
|   template <typename TComparand> | ||||
|   typename TypeTraits::EnableIf<!TypeTraits::IsVariant<TComparand>::value && | ||||
|                                     !TypeTraits::IsString<TComparand>::value, | ||||
|                                 bool>::type | ||||
|   equals(const TComparand &comparand) const { | ||||
|     return as<TComparand>() == comparand; | ||||
|   } | ||||
|  | ||||
|   template <typename TVariant2> | ||||
|   bool equals(const JsonVariantComparisons<TVariant2> &right) const { | ||||
|     using namespace Internals; | ||||
|     if (is<bool>() && right.template is<bool>()) | ||||
|       return as<bool>() == right.template as<bool>(); | ||||
|     if (is<JsonInteger>() && right.template is<JsonInteger>()) | ||||
|       return as<JsonInteger>() == right.template as<JsonInteger>(); | ||||
|     if (is<JsonFloat>() && right.template is<JsonFloat>()) | ||||
|       return as<JsonFloat>() == right.template as<JsonFloat>(); | ||||
|     if (is<JsonArray>() && right.template is<JsonArray>()) | ||||
|       return as<JsonArray>() == right.template as<JsonArray>(); | ||||
|     if (is<JsonObject>() && right.template is<JsonObject>()) | ||||
|       return as<JsonObject>() == right.template as<JsonObject>(); | ||||
|     if (is<char *>() && right.template is<char *>()) | ||||
|       return strcmp(as<char *>(), right.template as<char *>()) == 0; | ||||
|  | ||||
|     return false; | ||||
|   } | ||||
| }; | ||||
| } | ||||
|   | ||||
							
								
								
									
										89
									
								
								src/ArduinoJson/JsonVariantSubscripts.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								src/ArduinoJson/JsonVariantSubscripts.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,89 @@ | ||||
| // Copyright Benoit Blanchon 2014-2017 | ||||
| // MIT License | ||||
| // | ||||
| // Arduino JSON library | ||||
| // https://bblanchon.github.io/ArduinoJson/ | ||||
| // If you like this project, please add a star! | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "Data/JsonVariantAs.hpp" | ||||
| #include "Polyfills/attributes.hpp" | ||||
| #include "StringTraits/StringTraits.hpp" | ||||
| #include "TypeTraits/EnableIf.hpp" | ||||
|  | ||||
| namespace ArduinoJson { | ||||
|  | ||||
| // Forward declarations. | ||||
| class JsonArraySubscript; | ||||
| template <typename TKey> | ||||
| class JsonObjectSubscript; | ||||
|  | ||||
| template <typename TImpl> | ||||
| class JsonVariantSubscripts { | ||||
|  public: | ||||
|   // Mimics an array or an object. | ||||
|   // Returns the size of the array or object if the variant has that type. | ||||
|   // Returns 0 if the variant is neither an array nor an object | ||||
|   size_t size() const { | ||||
|     return impl()->template as<JsonArray>().size() + | ||||
|            impl()->template as<JsonObject>().size(); | ||||
|   } | ||||
|  | ||||
|   // Mimics an array. | ||||
|   // Returns the element at specified index if the variant is an array. | ||||
|   // Returns JsonVariant::invalid() if the variant is not an array. | ||||
|   FORCE_INLINE const JsonArraySubscript operator[](size_t index) const; | ||||
|   FORCE_INLINE JsonArraySubscript operator[](size_t index); | ||||
|  | ||||
|   // Mimics an object. | ||||
|   // Returns the value associated with the specified key if the variant is | ||||
|   // an object. | ||||
|   // Return JsonVariant::invalid() if the variant is not an object. | ||||
|   // | ||||
|   // const JsonObjectSubscript operator[](TKey) const; | ||||
|   // TKey = const std::string&, const String& | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE typename TypeTraits::EnableIf< | ||||
|       Internals::StringTraits<TString>::has_equals, | ||||
|       const JsonObjectSubscript<const TString &> >::type | ||||
|   operator[](const TString &key) const { | ||||
|     return impl()->template as<JsonObject>()[key]; | ||||
|   } | ||||
|   // | ||||
|   // const JsonObjectSubscript operator[](TKey) const; | ||||
|   // TKey = const std::string&, const String& | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE typename TypeTraits::EnableIf< | ||||
|       Internals::StringTraits<TString>::has_equals, | ||||
|       JsonObjectSubscript<const TString &> >::type | ||||
|   operator[](const TString &key) { | ||||
|     return impl()->template as<JsonObject>()[key]; | ||||
|   } | ||||
|   // | ||||
|   // JsonObjectSubscript operator[](TKey); | ||||
|   // TKey = const char*, const char[N], const FlashStringHelper* | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE typename TypeTraits::EnableIf< | ||||
|       Internals::StringTraits<const TString *>::has_equals, | ||||
|       JsonObjectSubscript<const TString *> >::type | ||||
|   operator[](const TString *key) { | ||||
|     return impl()->template as<JsonObject>()[key]; | ||||
|   } | ||||
|   // | ||||
|   // JsonObjectSubscript operator[](TKey); | ||||
|   // TKey = const char*, const char[N], const FlashStringHelper* | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE typename TypeTraits::EnableIf< | ||||
|       Internals::StringTraits<TString *>::has_equals, | ||||
|       const JsonObjectSubscript<const TString *> >::type | ||||
|   operator[](const TString *key) const { | ||||
|     return impl()->template as<JsonObject>()[key]; | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   const TImpl *impl() const { | ||||
|     return static_cast<const TImpl *>(this); | ||||
|   } | ||||
| }; | ||||
| } | ||||
							
								
								
									
										20
									
								
								src/ArduinoJson/TypeTraits/IsVariant.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/ArduinoJson/TypeTraits/IsVariant.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| // Copyright Benoit Blanchon 2014-2017 | ||||
| // MIT License | ||||
| // | ||||
| // Arduino JSON library | ||||
| // https://bblanchon.github.io/ArduinoJson/ | ||||
| // If you like this project, please add a star! | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "IsBaseOf.hpp" | ||||
|  | ||||
| namespace ArduinoJson { | ||||
| namespace TypeTraits { | ||||
|  | ||||
| class JsonVariantTag {}; | ||||
|  | ||||
| template <typename T> | ||||
| struct IsVariant : IsBaseOf<JsonVariantTag, T> {}; | ||||
| } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user