mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 00:32:37 +01:00 
			
		
		
		
	Fixed operator|(MemberProxy, JsonObject) (fixes #1415)
				
					
				
			This commit is contained in:
		| @@ -5,6 +5,7 @@ HEAD | |||||||
| ---- | ---- | ||||||
|  |  | ||||||
| * Fixed error `ambiguous overload for 'operator|'` (issue #1411) | * Fixed error `ambiguous overload for 'operator|'` (issue #1411) | ||||||
|  | * Fixed `operator|(MemberProxy, JsonObject)` (issue #1415) | ||||||
|  |  | ||||||
| v6.17.0 (2020-10-19) | v6.17.0 (2020-10-19) | ||||||
| ------- | ------- | ||||||
|   | |||||||
| @@ -121,6 +121,17 @@ TEST_CASE("MemberProxy::operator|()") { | |||||||
|  |  | ||||||
|     REQUIRE(sensor == std::string("gps")); |     REQUIRE(sensor == std::string("gps")); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   SECTION("Issue #1415") { | ||||||
|  |     JsonObject object = doc.to<JsonObject>(); | ||||||
|  |     object["hello"] = "world"; | ||||||
|  |  | ||||||
|  |     StaticJsonDocument<0> emptyDoc; | ||||||
|  |     JsonObject anotherObject = object["hello"] | emptyDoc.to<JsonObject>(); | ||||||
|  |  | ||||||
|  |     REQUIRE(anotherObject.isNull() == false); | ||||||
|  |     REQUIRE(anotherObject.size() == 0); | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| TEST_CASE("MemberProxy::remove()") { | TEST_CASE("MemberProxy::remove()") { | ||||||
|   | |||||||
| @@ -19,10 +19,13 @@ namespace ARDUINOJSON_NAMESPACE { | |||||||
| template <typename TArray> | template <typename TArray> | ||||||
| class ElementProxy : public VariantOperators<ElementProxy<TArray> >, | class ElementProxy : public VariantOperators<ElementProxy<TArray> >, | ||||||
|                      public VariantShortcuts<ElementProxy<TArray> >, |                      public VariantShortcuts<ElementProxy<TArray> >, | ||||||
|                      public Visitable { |                      public Visitable, | ||||||
|  |                      public VariantTag { | ||||||
|   typedef ElementProxy<TArray> this_type; |   typedef ElementProxy<TArray> this_type; | ||||||
|  |  | ||||||
|  public: |  public: | ||||||
|  |   typedef VariantRef variant_type; | ||||||
|  |  | ||||||
|   FORCE_INLINE ElementProxy(TArray array, size_t index) |   FORCE_INLINE ElementProxy(TArray array, size_t index) | ||||||
|       : _array(array), _index(index) {} |       : _array(array), _index(index) {} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -21,10 +21,13 @@ namespace ARDUINOJSON_NAMESPACE { | |||||||
| template <typename TObject, typename TStringRef> | template <typename TObject, typename TStringRef> | ||||||
| class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >, | class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >, | ||||||
|                     public VariantShortcuts<MemberProxy<TObject, TStringRef> >, |                     public VariantShortcuts<MemberProxy<TObject, TStringRef> >, | ||||||
|                     public Visitable { |                     public Visitable, | ||||||
|  |                     public VariantTag { | ||||||
|   typedef MemberProxy<TObject, TStringRef> this_type; |   typedef MemberProxy<TObject, TStringRef> this_type; | ||||||
|  |  | ||||||
|  public: |  public: | ||||||
|  |   typedef VariantRef variant_type; | ||||||
|  |  | ||||||
|   FORCE_INLINE MemberProxy(TObject variant, TStringRef key) |   FORCE_INLINE MemberProxy(TObject variant, TStringRef key) | ||||||
|       : _object(variant), _key(key) {} |       : _object(variant), _key(key) {} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -9,6 +9,7 @@ | |||||||
| #include <ArduinoJson/Polyfills/attributes.hpp> | #include <ArduinoJson/Polyfills/attributes.hpp> | ||||||
| #include <ArduinoJson/Polyfills/type_traits.hpp> | #include <ArduinoJson/Polyfills/type_traits.hpp> | ||||||
| #include <ArduinoJson/Variant/VariantAs.hpp> | #include <ArduinoJson/Variant/VariantAs.hpp> | ||||||
|  | #include <ArduinoJson/Variant/VariantTag.hpp> | ||||||
|  |  | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
| @@ -17,15 +18,24 @@ CompareResult compare(const T1 &lhs, const T2 &rhs);  // VariantCompare.cpp | |||||||
|  |  | ||||||
| template <typename TVariant> | template <typename TVariant> | ||||||
| struct VariantOperators { | struct VariantOperators { | ||||||
|   // Returns the default value if the VariantRef is undefined of incompatible |   // Returns the default value if the VariantRef is undefined or incompatible | ||||||
|   template <typename T> |   template <typename T> | ||||||
|   friend typename enable_if<!IsVisitable<T>::value, T>::type operator|( |   friend typename enable_if<!IsVariant<T>::value, T>::type operator|( | ||||||
|       const TVariant &variant, T defaultValue) { |       const TVariant &variant, T defaultValue) { | ||||||
|     if (variant.template is<T>()) |     if (variant.template is<T>()) | ||||||
|       return variant.template as<T>(); |       return variant.template as<T>(); | ||||||
|     else |     else | ||||||
|       return defaultValue; |       return defaultValue; | ||||||
|   } |   } | ||||||
|  |   // Returns the default value if the VariantRef is undefined or incompatible | ||||||
|  |   template <typename T> | ||||||
|  |   friend typename enable_if<IsVariant<T>::value, typename T::variant_type>::type | ||||||
|  |   operator|(const TVariant &variant, T defaultValue) { | ||||||
|  |     if (variant) | ||||||
|  |       return variant; | ||||||
|  |     else | ||||||
|  |       return defaultValue; | ||||||
|  |   } | ||||||
|  |  | ||||||
|   // value == TVariant |   // value == TVariant | ||||||
|   template <typename T> |   template <typename T> | ||||||
|   | |||||||
| @@ -16,6 +16,7 @@ | |||||||
| #include <ArduinoJson/Variant/VariantOperators.hpp> | #include <ArduinoJson/Variant/VariantOperators.hpp> | ||||||
| #include <ArduinoJson/Variant/VariantRef.hpp> | #include <ArduinoJson/Variant/VariantRef.hpp> | ||||||
| #include <ArduinoJson/Variant/VariantShortcuts.hpp> | #include <ArduinoJson/Variant/VariantShortcuts.hpp> | ||||||
|  | #include <ArduinoJson/Variant/VariantTag.hpp> | ||||||
|  |  | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
| @@ -23,12 +24,9 @@ namespace ARDUINOJSON_NAMESPACE { | |||||||
| class ArrayRef; | class ArrayRef; | ||||||
| class ObjectRef; | class ObjectRef; | ||||||
|  |  | ||||||
| template <typename, typename> |  | ||||||
| class MemberProxy; |  | ||||||
|  |  | ||||||
| // Contains the methods shared by VariantRef and VariantConstRef | // Contains the methods shared by VariantRef and VariantConstRef | ||||||
| template <typename TData> | template <typename TData> | ||||||
| class VariantRefBase { | class VariantRefBase : public VariantTag { | ||||||
|  public: |  public: | ||||||
|   // Tells wether the variant has the specified type. |   // Tells wether the variant has the specified type. | ||||||
|   // Returns true if the variant has type type T, false otherwise. |   // Returns true if the variant has type type T, false otherwise. | ||||||
|   | |||||||
							
								
								
									
										16
									
								
								src/ArduinoJson/Variant/VariantTag.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/ArduinoJson/Variant/VariantTag.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | |||||||
|  | // ArduinoJson - arduinojson.org | ||||||
|  | // Copyright Benoit Blanchon 2014-2020 | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <ArduinoJson/Namespace.hpp> | ||||||
|  |  | ||||||
|  | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
|  | struct VariantTag {}; | ||||||
|  |  | ||||||
|  | template <typename T> | ||||||
|  | struct IsVariant : is_base_of<VariantTag, T> {}; | ||||||
|  |  | ||||||
|  | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
		Reference in New Issue
	
	Block a user