mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Remove getElement(), getOrAddElement(), getMember(), and getOrAddMember()
				
					
				
			This commit is contained in:
		
							
								
								
									
										20
									
								
								CHANGELOG.md
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								CHANGELOG.md
									
									
									
									
									
								
							| @@ -9,6 +9,26 @@ HEAD | ||||
| * Fix comparison operators for `JsonArray`, `JsonArrayConst`, `JsonObject`, and `JsonObjectConst` | ||||
| * Remove undocumented `accept()` functions | ||||
| * Rename `addElement()` to `add()` | ||||
| * Remove `getElement()`, `getOrAddElement()`, `getMember()`, and `getOrAddMember()` | ||||
|  | ||||
| > ### BREAKING CHANGES | ||||
| > | ||||
| > This release hides `JsonVariant`'s functions that were only intended for internal use. | ||||
| > If you were using them in your programs, you must replace with `operator[]` and `to<JsonVariant>()`, like so: | ||||
| > | ||||
| > ```c++ | ||||
| > // before | ||||
| > JsonVariant a = variant.getElement(idx); | ||||
| > JsonVariant b = variant.getOrAddElement(idx); | ||||
| > JsonVariant c = variant.getMember(key); | ||||
| > JsonVariant d = variant.getOrAddMember(key); | ||||
| > | ||||
| > // after | ||||
| > JsonVariant a = variant[idx]; | ||||
| > JsonVariant b = variant[idx].to<JsonVariant>(); | ||||
| > JsonVariant c = variant[key]; | ||||
| > JsonVariant d = variant[key].to<JsonVariant>(); | ||||
| > ``` | ||||
|  | ||||
| v6.19.4 (2022-04-05) | ||||
| ------- | ||||
|   | ||||
| @@ -9,7 +9,6 @@ add_executable(JsonArrayTests | ||||
| 	copyArray.cpp | ||||
| 	createNested.cpp | ||||
| 	equals.cpp | ||||
| 	get.cpp | ||||
| 	isNull.cpp | ||||
| 	iterator.cpp | ||||
| 	memoryUsage.cpp | ||||
|   | ||||
| @@ -1,16 +0,0 @@ | ||||
| // ArduinoJson - https://arduinojson.org | ||||
| // Copyright © 2014-2022, Benoit BLANCHON | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonArray::get()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   deserializeJson(doc, "[1,2,3]"); | ||||
|   JsonArray array = doc.as<JsonArray>(); | ||||
|  | ||||
|   SECTION("Overflow") { | ||||
|     REQUIRE(array.getElement(3).isNull()); | ||||
|   } | ||||
| } | ||||
| @@ -96,10 +96,6 @@ class ArrayConstRef : public ArrayRefBase<const CollectionData>, | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantConstRef operator[](size_t index) const { | ||||
|     return getElementConst(index); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantConstRef getElementConst(size_t index) const { | ||||
|     return VariantConstRef(_data ? _data->getElement(index) : 0); | ||||
|   } | ||||
| }; | ||||
| @@ -152,21 +148,6 @@ class ArrayRef : public ArrayRefBase<CollectionData>, | ||||
|     return ArrayConstRef(_data) == ArrayConstRef(rhs._data); | ||||
|   } | ||||
|  | ||||
|   // Internal use | ||||
|   FORCE_INLINE VariantRef getOrAddElement(size_t index) const { | ||||
|     return VariantRef(_pool, _data ? _data->getOrAddElement(index, _pool) : 0); | ||||
|   } | ||||
|  | ||||
|   // Gets the value at the specified index. | ||||
|   FORCE_INLINE VariantRef getElement(size_t index) const { | ||||
|     return VariantRef(_pool, _data ? _data->getElement(index) : 0); | ||||
|   } | ||||
|  | ||||
|   // Gets the value at the specified index. | ||||
|   FORCE_INLINE VariantConstRef getElementConst(size_t index) const { | ||||
|     return VariantConstRef(_data ? _data->getElement(index) : 0); | ||||
|   } | ||||
|  | ||||
|   // Removes element at specified position. | ||||
|   FORCE_INLINE void remove(iterator it) const { | ||||
|     if (!_data) | ||||
| @@ -187,6 +168,18 @@ class ArrayRef : public ArrayRefBase<CollectionData>, | ||||
|     _data->clear(); | ||||
|   } | ||||
|  | ||||
|   MemoryPool* getPool() const { | ||||
|     return _pool; | ||||
|   } | ||||
|  | ||||
|   VariantData* getData() const { | ||||
|     return collectionToVariant(_data); | ||||
|   } | ||||
|  | ||||
|   VariantData* getOrCreateData() const { | ||||
|     return collectionToVariant(_data); | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   MemoryPool* _pool; | ||||
| }; | ||||
|   | ||||
| @@ -135,54 +135,12 @@ class ElementProxy : public VariantOperators<ElementProxy<TArray> >, | ||||
|     return getUpstreamElementConst().memoryUsage(); | ||||
|   } | ||||
|  | ||||
|   template <typename TNestedKey> | ||||
|   VariantRef getMember(TNestedKey* key) const { | ||||
|     return getUpstreamElement().getMember(key); | ||||
|   } | ||||
|  | ||||
|   template <typename TNestedKey> | ||||
|   VariantRef getMember(const TNestedKey& key) const { | ||||
|     return getUpstreamElement().getMember(key); | ||||
|   } | ||||
|  | ||||
|   template <typename TNestedKey> | ||||
|   VariantConstRef getMemberConst(TNestedKey* key) const { | ||||
|     return getUpstreamElementConst().getMemberConst(key); | ||||
|   } | ||||
|  | ||||
|   template <typename TNestedKey> | ||||
|   VariantConstRef getMemberConst(const TNestedKey& key) const { | ||||
|     return getUpstreamElementConst().getMemberConst(key); | ||||
|   } | ||||
|  | ||||
|   template <typename TNestedKey> | ||||
|   VariantRef getOrAddMember(TNestedKey* key) const { | ||||
|     return getOrAddUpstreamElement().getOrAddMember(key); | ||||
|   } | ||||
|  | ||||
|   template <typename TNestedKey> | ||||
|   VariantRef getOrAddMember(const TNestedKey& key) const { | ||||
|     return getOrAddUpstreamElement().getOrAddMember(key); | ||||
|   } | ||||
|  | ||||
|   VariantRef add() const { | ||||
|     return getOrAddUpstreamElement().add(); | ||||
|   } | ||||
|  | ||||
|   using ArrayShortcuts<ElementProxy<TArray> >::add; | ||||
|  | ||||
|   VariantRef getElement(size_t index) const { | ||||
|     return getOrAddUpstreamElement().getElement(index); | ||||
|   } | ||||
|  | ||||
|   VariantConstRef getElementConst(size_t index) const { | ||||
|     return getUpstreamElementConst().getElementConst(index); | ||||
|   } | ||||
|  | ||||
|   VariantRef getOrAddElement(size_t index) const { | ||||
|     return getOrAddUpstreamElement().getOrAddElement(index); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE void remove(size_t index) const { | ||||
|     getUpstreamElement().remove(index); | ||||
|   } | ||||
| @@ -202,17 +160,30 @@ class ElementProxy : public VariantOperators<ElementProxy<TArray> >, | ||||
|     getUpstreamElement().remove(key); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE MemoryPool* getPool() const { | ||||
|     return _array.getPool(); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantData* getData() const { | ||||
|     return variantGetElement(_array.getData(), _index); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantData* getOrCreateData() const { | ||||
|     return variantGetOrAddElement(_array.getOrCreateData(), _index, | ||||
|                                   _array.getPool()); | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   FORCE_INLINE VariantRef getUpstreamElement() const { | ||||
|     return _array.getElement(_index); | ||||
|     return VariantRef(getPool(), getData()); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantConstRef getUpstreamElementConst() const { | ||||
|     return _array.getElementConst(_index); | ||||
|     return VariantConstRef(getData()); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantRef getOrAddUpstreamElement() const { | ||||
|     return _array.getOrAddElement(_index); | ||||
|     return VariantRef(getPool(), getOrCreateData()); | ||||
|   } | ||||
|  | ||||
|   friend void convertToJson(const this_type& src, VariantRef dst) { | ||||
|   | ||||
| @@ -100,14 +100,14 @@ class JsonDocument : public VariantOperators<const JsonDocument&> { | ||||
|   // createNestedArray(const __FlashStringHelper*) | ||||
|   template <typename TChar> | ||||
|   ArrayRef createNestedArray(TChar* key) { | ||||
|     return getOrAddMember(key).template to<ArrayRef>(); | ||||
|     return operator[](key).template to<ArrayRef>(); | ||||
|   } | ||||
|  | ||||
|   // createNestedArray(const std::string&) | ||||
|   // createNestedArray(const String&) | ||||
|   template <typename TString> | ||||
|   ArrayRef createNestedArray(const TString& key) { | ||||
|     return getOrAddMember(key).template to<ArrayRef>(); | ||||
|     return operator[](key).template to<ArrayRef>(); | ||||
|   } | ||||
|  | ||||
|   ObjectRef createNestedObject() { | ||||
| @@ -119,14 +119,14 @@ class JsonDocument : public VariantOperators<const JsonDocument&> { | ||||
|   // createNestedObject(const __FlashStringHelper*) | ||||
|   template <typename TChar> | ||||
|   ObjectRef createNestedObject(TChar* key) { | ||||
|     return getOrAddMember(key).template to<ObjectRef>(); | ||||
|     return operator[](key).template to<ObjectRef>(); | ||||
|   } | ||||
|  | ||||
|   // createNestedObject(const std::string&) | ||||
|   // createNestedObject(const String&) | ||||
|   template <typename TString> | ||||
|   ObjectRef createNestedObject(const TString& key) { | ||||
|     return getOrAddMember(key).template to<ObjectRef>(); | ||||
|     return operator[](key).template to<ObjectRef>(); | ||||
|   } | ||||
|  | ||||
|   // containsKey(char*) const | ||||
| @@ -134,14 +134,14 @@ class JsonDocument : public VariantOperators<const JsonDocument&> { | ||||
|   // containsKey(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   bool containsKey(TChar* key) const { | ||||
|     return !getMemberConst(key).isUnbound(); | ||||
|     return _data.getMember(adaptString(key)) != 0; | ||||
|   } | ||||
|  | ||||
|   // containsKey(const std::string&) const | ||||
|   // containsKey(const String&) const | ||||
|   template <typename TString> | ||||
|   bool containsKey(const TString& key) const { | ||||
|     return !getMemberConst(key).isUnbound(); | ||||
|     return _data.getMember(adaptString(key)) != 0; | ||||
|   } | ||||
|  | ||||
|   // operator[](const std::string&) | ||||
| @@ -169,7 +169,7 @@ class JsonDocument : public VariantOperators<const JsonDocument&> { | ||||
|   FORCE_INLINE | ||||
|       typename enable_if<IsString<TString>::value, VariantConstRef>::type | ||||
|       operator[](const TString& key) const { | ||||
|     return getMemberConst(key); | ||||
|     return VariantConstRef(_data.getMember(adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   // operator[](char*) const | ||||
| @@ -179,7 +179,7 @@ class JsonDocument : public VariantOperators<const JsonDocument&> { | ||||
|   FORCE_INLINE | ||||
|       typename enable_if<IsString<TChar*>::value, VariantConstRef>::type | ||||
|       operator[](TChar* key) const { | ||||
|     return getMemberConst(key); | ||||
|     return VariantConstRef(_data.getMember(adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE ElementProxy<JsonDocument&> operator[](size_t index) { | ||||
| @@ -187,73 +187,9 @@ class JsonDocument : public VariantOperators<const JsonDocument&> { | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantConstRef operator[](size_t index) const { | ||||
|     return getElementConst(index); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantRef getElement(size_t index) { | ||||
|     return VariantRef(&_pool, _data.getElement(index)); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantConstRef getElementConst(size_t index) const { | ||||
|     return VariantConstRef(_data.getElement(index)); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantRef getOrAddElement(size_t index) { | ||||
|     return VariantRef(&_pool, _data.getOrAddElement(index, &_pool)); | ||||
|   } | ||||
|  | ||||
|   // JsonVariantConst getMemberConst(char*) const | ||||
|   // JsonVariantConst getMemberConst(const char*) const | ||||
|   // JsonVariantConst getMemberConst(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantConstRef getMemberConst(TChar* key) const { | ||||
|     return VariantConstRef(_data.getMember(adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   // JsonVariantConst getMemberConst(const std::string&) const | ||||
|   // JsonVariantConst getMemberConst(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE | ||||
|       typename enable_if<IsString<TString>::value, VariantConstRef>::type | ||||
|       getMemberConst(const TString& key) const { | ||||
|     return VariantConstRef(_data.getMember(adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   // JsonVariant getMember(char*) | ||||
|   // JsonVariant getMember(const char*) | ||||
|   // JsonVariant getMember(const __FlashStringHelper*) | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantRef getMember(TChar* key) { | ||||
|     return VariantRef(&_pool, _data.getMember(adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   // JsonVariant getMember(const std::string&) | ||||
|   // JsonVariant getMember(const String&) | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE typename enable_if<IsString<TString>::value, VariantRef>::type | ||||
|   getMember(const TString& key) { | ||||
|     return VariantRef(&_pool, _data.getMember(adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   // getOrAddMember(char*) | ||||
|   // getOrAddMember(const char*) | ||||
|   // getOrAddMember(const __FlashStringHelper*) | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantRef getOrAddMember(TChar* key) { | ||||
|     return VariantRef(&_pool, | ||||
|                       _data.getOrAddMember(adaptString(key), &_pool, | ||||
|                                            getStringStoragePolicy(key))); | ||||
|   } | ||||
|  | ||||
|   // getOrAddMember(const std::string&) | ||||
|   // getOrAddMember(const String&) | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE VariantRef getOrAddMember(const TString& key) { | ||||
|     return VariantRef(&_pool, | ||||
|                       _data.getOrAddMember(adaptString(key), &_pool, | ||||
|                                            getStringStoragePolicy(key))); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantRef add() { | ||||
|     return VariantRef(&_pool, _data.addElement(&_pool)); | ||||
|   } | ||||
| @@ -331,6 +267,23 @@ class JsonDocument : public VariantOperators<const JsonDocument&> { | ||||
|  private: | ||||
|   JsonDocument(const JsonDocument&); | ||||
|   JsonDocument& operator=(const JsonDocument&); | ||||
|  | ||||
|  public: | ||||
|   MemoryPool* getPool() { | ||||
|     return &_pool; | ||||
|   } | ||||
|  | ||||
|   VariantData* getData() { | ||||
|     return &_data; | ||||
|   } | ||||
|  | ||||
|   const VariantData* getData() const { | ||||
|     return &_data; | ||||
|   } | ||||
|  | ||||
|   VariantData* getOrCreateData() { | ||||
|     return &_data; | ||||
|   } | ||||
| }; | ||||
|  | ||||
| inline void convertToJson(const JsonDocument& src, VariantRef dst) { | ||||
|   | ||||
| @@ -160,74 +160,30 @@ class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >, | ||||
|  | ||||
|   using ArrayShortcuts<MemberProxy<TObject, TStringRef> >::add; | ||||
|  | ||||
|   FORCE_INLINE VariantRef getElement(size_t index) const { | ||||
|     return getUpstreamMember().getElement(index); | ||||
|   FORCE_INLINE MemoryPool *getPool() const { | ||||
|     return _object.getPool(); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantConstRef getElementConst(size_t index) const { | ||||
|     return getUpstreamMemberConst().getElementConst(index); | ||||
|   FORCE_INLINE VariantData *getData() const { | ||||
|     return variantGetMember(_object.getData(), adaptString(_key)); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantRef getOrAddElement(size_t index) const { | ||||
|     return getOrAddUpstreamMember().getOrAddElement(index); | ||||
|   } | ||||
|  | ||||
|   // getMember(char*) const | ||||
|   // getMember(const char*) const | ||||
|   // getMember(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantRef getMember(TChar *key) const { | ||||
|     return getUpstreamMember().getMember(key); | ||||
|   } | ||||
|  | ||||
|   // getMember(const std::string&) const | ||||
|   // getMember(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE VariantRef getMember(const TString &key) const { | ||||
|     return getUpstreamMember().getMember(key); | ||||
|   } | ||||
|  | ||||
|   // getMemberConst(char*) const | ||||
|   // getMemberConst(const char*) const | ||||
|   // getMemberConst(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantConstRef getMemberConst(TChar *key) const { | ||||
|     return getUpstreamMemberConst().getMemberConst(key); | ||||
|   } | ||||
|  | ||||
|   // getMemberConst(const std::string&) const | ||||
|   // getMemberConst(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE VariantConstRef getMemberConst(const TString &key) const { | ||||
|     return getUpstreamMemberConst().getMemberConst(key); | ||||
|   } | ||||
|  | ||||
|   // getOrAddMember(char*) const | ||||
|   // getOrAddMember(const char*) const | ||||
|   // getOrAddMember(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantRef getOrAddMember(TChar *key) const { | ||||
|     return getOrAddUpstreamMember().getOrAddMember(key); | ||||
|   } | ||||
|  | ||||
|   // getOrAddMember(const std::string&) const | ||||
|   // getOrAddMember(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE VariantRef getOrAddMember(const TString &key) const { | ||||
|     return getOrAddUpstreamMember().getOrAddMember(key); | ||||
|   FORCE_INLINE VariantData *getOrCreateData() const { | ||||
|     return variantGetOrAddMember(_object.getOrCreateData(), _key, | ||||
|                                  _object.getPool()); | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   FORCE_INLINE VariantRef getUpstreamMember() const { | ||||
|     return _object.getMember(_key); | ||||
|     return VariantRef(getPool(), getData()); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantConstRef getUpstreamMemberConst() const { | ||||
|     return _object.getMemberConst(_key); | ||||
|     return VariantConstRef(getData()); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantRef getOrAddUpstreamMember() const { | ||||
|     return _object.getOrAddMember(_key); | ||||
|     return VariantRef(getPool(), getOrCreateData()); | ||||
|   } | ||||
|  | ||||
|   friend void convertToJson(const this_type &src, VariantRef dst) { | ||||
|   | ||||
| @@ -22,14 +22,4 @@ void objectRemove(CollectionData *obj, TAdaptedString key) { | ||||
|     return; | ||||
|   obj->removeMember(key); | ||||
| } | ||||
|  | ||||
| template <typename TAdaptedString, typename TStoragePolicy> | ||||
| inline VariantData *objectGetOrAddMember(CollectionData *obj, | ||||
|                                          TAdaptedString key, MemoryPool *pool, | ||||
|                                          TStoragePolicy storage_policy) { | ||||
|   if (!obj) | ||||
|     return 0; | ||||
|  | ||||
|   return obj->getOrAddMember(key, pool, storage_policy); | ||||
| } | ||||
| }  // namespace ARDUINOJSON_NAMESPACE | ||||
|   | ||||
| @@ -13,41 +13,41 @@ template <typename TObject> | ||||
| template <typename TString> | ||||
| inline ArrayRef ObjectShortcuts<TObject>::createNestedArray( | ||||
|     const TString& key) const { | ||||
|   return impl()->getOrAddMember(key).template to<ArrayRef>(); | ||||
|   return impl()->operator[](key).template to<ArrayRef>(); | ||||
| } | ||||
|  | ||||
| template <typename TObject> | ||||
| template <typename TChar> | ||||
| inline ArrayRef ObjectShortcuts<TObject>::createNestedArray(TChar* key) const { | ||||
|   return impl()->getOrAddMember(key).template to<ArrayRef>(); | ||||
|   return impl()->operator[](key).template to<ArrayRef>(); | ||||
| } | ||||
|  | ||||
| template <typename TObject> | ||||
| template <typename TString> | ||||
| inline ObjectRef ObjectShortcuts<TObject>::createNestedObject( | ||||
|     const TString& key) const { | ||||
|   return impl()->getOrAddMember(key).template to<ObjectRef>(); | ||||
|   return impl()->operator[](key).template to<ObjectRef>(); | ||||
| } | ||||
|  | ||||
| template <typename TObject> | ||||
| template <typename TChar> | ||||
| inline ObjectRef ObjectShortcuts<TObject>::createNestedObject( | ||||
|     TChar* key) const { | ||||
|   return impl()->getOrAddMember(key).template to<ObjectRef>(); | ||||
|   return impl()->operator[](key).template to<ObjectRef>(); | ||||
| } | ||||
|  | ||||
| template <typename TObject> | ||||
| template <typename TString> | ||||
| inline typename enable_if<IsString<TString>::value, bool>::type | ||||
| ObjectShortcuts<TObject>::containsKey(const TString& key) const { | ||||
|   return !impl()->getMemberConst(key).isUnbound(); | ||||
|   return variantGetMember(impl()->getData(), adaptString(key)) != 0; | ||||
| } | ||||
|  | ||||
| template <typename TObject> | ||||
| template <typename TChar> | ||||
| inline typename enable_if<IsString<TChar*>::value, bool>::type | ||||
| ObjectShortcuts<TObject>::containsKey(TChar* key) const { | ||||
|   return !impl()->getMemberConst(key).isUnbound(); | ||||
|   return variantGetMember(impl()->getData(), adaptString(key)) != 0; | ||||
| } | ||||
|  | ||||
| template <typename TObject> | ||||
|   | ||||
| @@ -71,7 +71,7 @@ class ObjectConstRef : public ObjectRefBase<const CollectionData>, | ||||
|   // containsKey(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE bool containsKey(const TString& key) const { | ||||
|     return !getMemberConst(key).isUnbound(); | ||||
|     return objectGetMember(_data, adaptString(key)) != 0; | ||||
|   } | ||||
|  | ||||
|   // containsKey(char*) const | ||||
| @@ -79,22 +79,7 @@ class ObjectConstRef : public ObjectRefBase<const CollectionData>, | ||||
|   // containsKey(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE bool containsKey(TChar* key) const { | ||||
|     return !getMemberConst(key).isUnbound(); | ||||
|   } | ||||
|  | ||||
|   // getMemberConst(const std::string&) const | ||||
|   // getMemberConst(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE VariantConstRef getMemberConst(const TString& key) const { | ||||
|     return VariantConstRef(objectGetMember(_data, adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   // getMemberConst(char*) const | ||||
|   // getMemberConst(const char*) const | ||||
|   // getMemberConst(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantConstRef getMemberConst(TChar* key) const { | ||||
|     return VariantConstRef(objectGetMember(_data, adaptString(key))); | ||||
|     return objectGetMember(_data, adaptString(key)) != 0; | ||||
|   } | ||||
|  | ||||
|   // operator[](const std::string&) const | ||||
| @@ -103,7 +88,7 @@ class ObjectConstRef : public ObjectRefBase<const CollectionData>, | ||||
|   FORCE_INLINE | ||||
|       typename enable_if<IsString<TString>::value, VariantConstRef>::type | ||||
|       operator[](const TString& key) const { | ||||
|     return getMemberConst(key); | ||||
|     return VariantConstRef(objectGetMember(_data, adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   // operator[](char*) const | ||||
| @@ -113,7 +98,7 @@ class ObjectConstRef : public ObjectRefBase<const CollectionData>, | ||||
|   FORCE_INLINE | ||||
|       typename enable_if<IsString<TChar*>::value, VariantConstRef>::type | ||||
|       operator[](TChar* key) const { | ||||
|     return getMemberConst(key); | ||||
|     return VariantConstRef(objectGetMember(_data, adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE bool operator==(ObjectConstRef rhs) const { | ||||
| @@ -178,55 +163,6 @@ class ObjectRef : public ObjectRefBase<CollectionData>, | ||||
|     return _data->copyFrom(*src._data, _pool); | ||||
|   } | ||||
|  | ||||
|   // getMember(const std::string&) const | ||||
|   // getMember(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE VariantRef getMember(const TString& key) const { | ||||
|     return VariantRef(_pool, objectGetMember(_data, adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   // getMember(char*) const | ||||
|   // getMember(const char*) const | ||||
|   // getMember(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantRef getMember(TChar* key) const { | ||||
|     return VariantRef(_pool, objectGetMember(_data, adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   // getMemberConst(const std::string&) const | ||||
|   // getMemberConst(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE VariantConstRef getMemberConst(const TString& key) const { | ||||
|     return VariantConstRef(objectGetMember(_data, adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   // getMemberConst(char*) const | ||||
|   // getMemberConst(const char*) const | ||||
|   // getMemberConst(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantConstRef getMemberConst(TChar* key) const { | ||||
|     return VariantConstRef(objectGetMember(_data, adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   // getOrAddMember(const std::string&) const | ||||
|   // getOrAddMember(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE VariantRef getOrAddMember(const TString& key) const { | ||||
|     return VariantRef(_pool, | ||||
|                       objectGetOrAddMember(_data, adaptString(key), _pool, | ||||
|                                            getStringStoragePolicy(key))); | ||||
|   } | ||||
|  | ||||
|   // getOrAddMember(char*) const | ||||
|   // getOrAddMember(const char*) const | ||||
|   // getOrAddMember(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantRef getOrAddMember(TChar* key) const { | ||||
|     return VariantRef(_pool, | ||||
|                       objectGetOrAddMember(_data, adaptString(key), _pool, | ||||
|                                            getStringStoragePolicy(key))); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE bool operator==(ObjectRef rhs) const { | ||||
|     return ObjectConstRef(_data) == ObjectConstRef(rhs._data); | ||||
|   } | ||||
| @@ -252,6 +188,18 @@ class ObjectRef : public ObjectRefBase<CollectionData>, | ||||
|     objectRemove(_data, adaptString(key)); | ||||
|   } | ||||
|  | ||||
|   MemoryPool* getPool() const { | ||||
|     return _pool; | ||||
|   } | ||||
|  | ||||
|   VariantData* getData() const { | ||||
|     return collectionToVariant(_data); | ||||
|   } | ||||
|  | ||||
|   VariantData* getOrCreateData() const { | ||||
|     return collectionToVariant(_data); | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   MemoryPool* _pool; | ||||
| }; | ||||
|   | ||||
| @@ -59,6 +59,10 @@ inline CollectionData *variantToObject(VariantData *var) { | ||||
|   return &var->toObject(); | ||||
| } | ||||
|  | ||||
| inline VariantData *variantGetElement(const VariantData *var, size_t index) { | ||||
|   return var != 0 ? var->getElement(index) : 0; | ||||
| } | ||||
|  | ||||
| inline NO_INLINE VariantData *variantAddElement(VariantData *var, | ||||
|                                                 MemoryPool *pool) { | ||||
|   return var != 0 ? var->addElement(pool) : 0; | ||||
| @@ -70,8 +74,17 @@ inline NO_INLINE VariantData *variantGetOrAddElement(VariantData *var, | ||||
|   return var != 0 ? var->getOrAddElement(index, pool) : 0; | ||||
| } | ||||
|  | ||||
| template <typename AdaptedString> | ||||
| VariantData *variantGetMember(const VariantData *var, AdaptedString key) { | ||||
|   if (!var) | ||||
|     return 0; | ||||
|   return var->getMember(key); | ||||
| } | ||||
|  | ||||
| // TODO: this function is inconsitent with the others: | ||||
| // it should take an adapted string | ||||
| template <typename TChar> | ||||
| NO_INLINE VariantData *variantGetOrAddMember(VariantData *var, TChar *key, | ||||
| VariantData *variantGetOrAddMember(VariantData *var, TChar *key, | ||||
|                                    MemoryPool *pool) { | ||||
|   if (!var) | ||||
|     return 0; | ||||
| @@ -80,8 +93,7 @@ NO_INLINE VariantData *variantGetOrAddMember(VariantData *var, TChar *key, | ||||
| } | ||||
|  | ||||
| template <typename TString> | ||||
| NO_INLINE VariantData *variantGetOrAddMember(VariantData *var, | ||||
|                                              const TString &key, | ||||
| VariantData *variantGetOrAddMember(VariantData *var, const TString &key, | ||||
|                                    MemoryPool *pool) { | ||||
|   if (!var) | ||||
|     return 0; | ||||
|   | ||||
| @@ -116,27 +116,8 @@ class VariantConstRef : public VariantRefBase<const VariantData>, | ||||
|     return as<T>(); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantConstRef getElementConst(size_t index) const { | ||||
|     return VariantConstRef(_data != 0 ? _data->getElement(index) : 0); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantConstRef operator[](size_t index) const { | ||||
|     return getElementConst(index); | ||||
|   } | ||||
|  | ||||
|   // getMemberConst(const std::string&) const | ||||
|   // getMemberConst(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE VariantConstRef getMemberConst(const TString &key) const { | ||||
|     return VariantConstRef(_data ? _data->getMember(adaptString(key)) : 0); | ||||
|   } | ||||
|  | ||||
|   // getMemberConst(char*) const | ||||
|   // getMemberConst(const char*) const | ||||
|   // getMemberConst(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantConstRef getMemberConst(TChar *key) const { | ||||
|     return VariantConstRef(_data ? _data->getMember(adaptString(key)) : 0); | ||||
|     return VariantConstRef(variantGetElement(_data, index)); | ||||
|   } | ||||
|  | ||||
|   // operator[](const std::string&) const | ||||
| @@ -145,7 +126,7 @@ class VariantConstRef : public VariantRefBase<const VariantData>, | ||||
|   FORCE_INLINE | ||||
|       typename enable_if<IsString<TString>::value, VariantConstRef>::type | ||||
|       operator[](const TString &key) const { | ||||
|     return getMemberConst(key); | ||||
|     return VariantConstRef(variantGetMember(_data, adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   // operator[](char*) const | ||||
| @@ -155,7 +136,11 @@ class VariantConstRef : public VariantRefBase<const VariantData>, | ||||
|   FORCE_INLINE | ||||
|       typename enable_if<IsString<TChar *>::value, VariantConstRef>::type | ||||
|       operator[](TChar *key) const { | ||||
|     return getMemberConst(key); | ||||
|     return VariantConstRef(variantGetMember(_data, adaptString(key))); | ||||
|   } | ||||
|  | ||||
|   const VariantData *getData() const { | ||||
|     return _data; | ||||
|   } | ||||
| }; | ||||
|  | ||||
| @@ -275,62 +260,6 @@ class VariantRef : public VariantRefBase<VariantData>, | ||||
|  | ||||
|   using ArrayShortcuts<VariantRef>::add; | ||||
|  | ||||
|   FORCE_INLINE VariantConstRef getElementConst(size_t index) const { | ||||
|     return VariantConstRef(_data != 0 ? _data->getElement(index) : 0); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE VariantRef getOrAddElement(size_t index) const { | ||||
|     return VariantRef(_pool, variantGetOrAddElement(_data, index, _pool)); | ||||
|   } | ||||
|  | ||||
|   // getMember(const char*) const | ||||
|   // getMember(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantRef getMember(TChar *key) const { | ||||
|     return VariantRef(_pool, | ||||
|                       _data != 0 ? _data->getMember(adaptString(key)) : 0); | ||||
|   } | ||||
|  | ||||
|   // getMember(const std::string&) const | ||||
|   // getMember(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE typename enable_if<IsString<TString>::value, VariantRef>::type | ||||
|   getMember(const TString &key) const { | ||||
|     return VariantRef(_pool, | ||||
|                       _data != 0 ? _data->getMember(adaptString(key)) : 0); | ||||
|   } | ||||
|  | ||||
|   // getMemberConst(const char*) const | ||||
|   // getMemberConst(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantConstRef getMemberConst(TChar *key) const { | ||||
|     return VariantConstRef(_data ? _data->getMember(adaptString(key)) : 0); | ||||
|   } | ||||
|  | ||||
|   // getMemberConst(const std::string&) const | ||||
|   // getMemberConst(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE | ||||
|       typename enable_if<IsString<TString>::value, VariantConstRef>::type | ||||
|       getMemberConst(const TString &key) const { | ||||
|     return VariantConstRef(_data ? _data->getMember(adaptString(key)) : 0); | ||||
|   } | ||||
|  | ||||
|   // getOrAddMember(char*) const | ||||
|   // getOrAddMember(const char*) const | ||||
|   // getOrAddMember(const __FlashStringHelper*) const | ||||
|   template <typename TChar> | ||||
|   FORCE_INLINE VariantRef getOrAddMember(TChar *key) const { | ||||
|     return VariantRef(_pool, variantGetOrAddMember(_data, key, _pool)); | ||||
|   } | ||||
|  | ||||
|   // getOrAddMember(const std::string&) const | ||||
|   // getOrAddMember(const String&) const | ||||
|   template <typename TString> | ||||
|   FORCE_INLINE VariantRef getOrAddMember(const TString &key) const { | ||||
|     return VariantRef(_pool, variantGetOrAddMember(_data, key, _pool)); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE void remove(size_t index) const { | ||||
|     if (_data) | ||||
|       _data->remove(index); | ||||
| @@ -356,13 +285,25 @@ class VariantRef : public VariantRefBase<VariantData>, | ||||
|   inline void shallowCopy(VariantConstRef target) { | ||||
|     if (!_data) | ||||
|       return; | ||||
|     const VariantData *targetData = getData(target); | ||||
|     const VariantData *targetData = target.getData(); | ||||
|     if (targetData) | ||||
|       *_data = *targetData; | ||||
|     else | ||||
|       _data->setNull(); | ||||
|   } | ||||
|  | ||||
|   MemoryPool *getPool() const { | ||||
|     return _pool; | ||||
|   } | ||||
|  | ||||
|   VariantData *getData() const { | ||||
|     return _data; | ||||
|   } | ||||
|  | ||||
|   VariantData *getOrCreateData() const { | ||||
|     return _data; | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   MemoryPool *_pool; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user