mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Refactored string adapters: only one IsString<T> and adaptString()
				
					
				
			This commit is contained in:
		| @@ -12,7 +12,8 @@ | |||||||
|   // Add the IDs of extensions you want installed when the container is created. |   // Add the IDs of extensions you want installed when the container is created. | ||||||
|   "extensions": [ |   "extensions": [ | ||||||
| 	"ms-vscode.cmake-tools", | 	"ms-vscode.cmake-tools", | ||||||
| 	"ms-vscode.cpptools" | 	"ms-vscode.cpptools", | ||||||
|  | 	"xaver.clang-format" | ||||||
| ], | ], | ||||||
|  |  | ||||||
|   // Use 'forwardPorts' to make a list of ports inside the container available locally. |   // Use 'forwardPorts' to make a list of ports inside the container available locally. | ||||||
|   | |||||||
| @@ -2,23 +2,22 @@ | |||||||
| // Copyright Benoit Blanchon 2014-2021 | // Copyright Benoit Blanchon 2014-2021 | ||||||
| // MIT License | // MIT License | ||||||
|  |  | ||||||
|  | #define ARDUINOJSON_ENABLE_PROGMEM 1 | ||||||
|  | #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1 | ||||||
|  |  | ||||||
| #include "custom_string.hpp" | #include "custom_string.hpp" | ||||||
| #include "progmem_emulation.hpp" | #include "progmem_emulation.hpp" | ||||||
| #include "weird_strcmp.hpp" | #include "weird_strcmp.hpp" | ||||||
|  |  | ||||||
| #include <ArduinoJson/Strings/ArduinoStringAdapter.hpp> | #include <ArduinoJson/Strings/StringAdapters.hpp> | ||||||
| #include <ArduinoJson/Strings/ConstRamStringAdapter.hpp> |  | ||||||
| #include <ArduinoJson/Strings/FlashStringAdapter.hpp> |  | ||||||
| #include <ArduinoJson/Strings/SizedRamStringAdapter.hpp> |  | ||||||
| #include <ArduinoJson/Strings/StdStringAdapter.hpp> |  | ||||||
|  |  | ||||||
| #include <catch.hpp> | #include <catch.hpp> | ||||||
|  |  | ||||||
| using namespace ARDUINOJSON_NAMESPACE; | using namespace ARDUINOJSON_NAMESPACE; | ||||||
|  |  | ||||||
| TEST_CASE("ConstRamStringAdapter") { | TEST_CASE("const char*") { | ||||||
|   SECTION("null") { |   SECTION("null") { | ||||||
|     ConstRamStringAdapter adapter(NULL); |     StringAdapter<const char*> adapter(NULL); | ||||||
|  |  | ||||||
|     CHECK(adapter.compare("bravo") < 0); |     CHECK(adapter.compare("bravo") < 0); | ||||||
|     CHECK(adapter.compare(NULL) == 0); |     CHECK(adapter.compare(NULL) == 0); | ||||||
| @@ -30,7 +29,7 @@ TEST_CASE("ConstRamStringAdapter") { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   SECTION("non-null") { |   SECTION("non-null") { | ||||||
|     ConstRamStringAdapter adapter("bravo"); |     StringAdapter<const char*> adapter("bravo"); | ||||||
|  |  | ||||||
|     CHECK(adapter.compare(NULL) > 0); |     CHECK(adapter.compare(NULL) > 0); | ||||||
|     CHECK(adapter.compare("alpha") > 0); |     CHECK(adapter.compare("alpha") > 0); | ||||||
| @@ -44,9 +43,9 @@ TEST_CASE("ConstRamStringAdapter") { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| TEST_CASE("SizedRamStringAdapter") { | TEST_CASE("const char* + size") { | ||||||
|   SECTION("null") { |   SECTION("null") { | ||||||
|     SizedRamStringAdapter adapter(NULL, 10); |     StringAdapter<const char*, true> adapter(NULL, 10); | ||||||
|  |  | ||||||
|     CHECK(adapter.compare("bravo") < 0); |     CHECK(adapter.compare("bravo") < 0); | ||||||
|     CHECK(adapter.compare(NULL) == 0); |     CHECK(adapter.compare(NULL) == 0); | ||||||
| @@ -58,7 +57,7 @@ TEST_CASE("SizedRamStringAdapter") { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   SECTION("non-null") { |   SECTION("non-null") { | ||||||
|     SizedRamStringAdapter adapter("bravo", 5); |     StringAdapter<const char*, true> adapter("bravo", 5); | ||||||
|  |  | ||||||
|     CHECK(adapter.compare(NULL) > 0); |     CHECK(adapter.compare(NULL) > 0); | ||||||
|     CHECK(adapter.compare("alpha") > 0); |     CHECK(adapter.compare("alpha") > 0); | ||||||
| @@ -72,9 +71,9 @@ TEST_CASE("SizedRamStringAdapter") { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| TEST_CASE("FlashStringAdapter") { | TEST_CASE("const __FlashStringHelper*") { | ||||||
|   SECTION("null") { |   SECTION("null") { | ||||||
|     FlashStringAdapter adapter(NULL); |     StringAdapter<const __FlashStringHelper*> adapter(NULL); | ||||||
|  |  | ||||||
|     CHECK(adapter.compare("bravo") < 0); |     CHECK(adapter.compare("bravo") < 0); | ||||||
|     CHECK(adapter.compare(NULL) == 0); |     CHECK(adapter.compare(NULL) == 0); | ||||||
| @@ -86,7 +85,7 @@ TEST_CASE("FlashStringAdapter") { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   SECTION("non-null") { |   SECTION("non-null") { | ||||||
|     FlashStringAdapter adapter = adaptString(F("bravo")); |     StringAdapter<const __FlashStringHelper*> adapter = adaptString(F("bravo")); | ||||||
|  |  | ||||||
|     CHECK(adapter.compare(NULL) > 0); |     CHECK(adapter.compare(NULL) > 0); | ||||||
|     CHECK(adapter.compare("alpha") > 0); |     CHECK(adapter.compare("alpha") > 0); | ||||||
| @@ -102,7 +101,7 @@ TEST_CASE("FlashStringAdapter") { | |||||||
|  |  | ||||||
| TEST_CASE("std::string") { | TEST_CASE("std::string") { | ||||||
|   std::string str("bravo"); |   std::string str("bravo"); | ||||||
|   StdStringAdapter<std::string> adapter = adaptString(str); |   StringAdapter<std::string> adapter(str); | ||||||
|  |  | ||||||
|   CHECK(adapter.compare(NULL) > 0); |   CHECK(adapter.compare(NULL) > 0); | ||||||
|   CHECK(adapter.compare("alpha") > 0); |   CHECK(adapter.compare("alpha") > 0); | ||||||
| @@ -117,7 +116,7 @@ TEST_CASE("std::string") { | |||||||
|  |  | ||||||
| TEST_CASE("Arduino String") { | TEST_CASE("Arduino String") { | ||||||
|   ::String str("bravo"); |   ::String str("bravo"); | ||||||
|   ArduinoStringAdapter adapter = adaptString(str); |   StringAdapter< ::String> adapter(str); | ||||||
|  |  | ||||||
|   CHECK(adapter.compare(NULL) > 0); |   CHECK(adapter.compare(NULL) > 0); | ||||||
|   CHECK(adapter.compare("alpha") > 0); |   CHECK(adapter.compare("alpha") > 0); | ||||||
| @@ -132,7 +131,7 @@ TEST_CASE("Arduino String") { | |||||||
|  |  | ||||||
| TEST_CASE("custom_string") { | TEST_CASE("custom_string") { | ||||||
|   custom_string str("bravo"); |   custom_string str("bravo"); | ||||||
|   StdStringAdapter<custom_string> adapter = adaptString(str); |   StringAdapter<custom_string> adapter(str); | ||||||
|  |  | ||||||
|   CHECK(adapter.compare(NULL) > 0); |   CHECK(adapter.compare(NULL) > 0); | ||||||
|   CHECK(adapter.compare("alpha") > 0); |   CHECK(adapter.compare("alpha") > 0); | ||||||
|   | |||||||
| @@ -62,9 +62,9 @@ inline bool CollectionData::copyFrom(const CollectionData& src, | |||||||
|     VariantData* var; |     VariantData* var; | ||||||
|     if (s->key() != 0) { |     if (s->key() != 0) { | ||||||
|       if (s->ownsKey()) |       if (s->ownsKey()) | ||||||
|         var = addMember(RamStringAdapter(s->key()), pool); |         var = addMember(adaptString(const_cast<char*>(s->key())), pool); | ||||||
|       else |       else | ||||||
|         var = addMember(ConstRamStringAdapter(s->key()), pool); |         var = addMember(adaptString(s->key()), pool); | ||||||
|     } else { |     } else { | ||||||
|       var = addElement(pool); |       var = addElement(pool); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -20,5 +20,6 @@ | |||||||
| #include "type_traits/is_signed.hpp" | #include "type_traits/is_signed.hpp" | ||||||
| #include "type_traits/is_unsigned.hpp" | #include "type_traits/is_unsigned.hpp" | ||||||
| #include "type_traits/make_unsigned.hpp" | #include "type_traits/make_unsigned.hpp" | ||||||
|  | #include "type_traits/make_void.hpp" | ||||||
| #include "type_traits/remove_const.hpp" | #include "type_traits/remove_const.hpp" | ||||||
| #include "type_traits/remove_reference.hpp" | #include "type_traits/remove_reference.hpp" | ||||||
|   | |||||||
							
								
								
									
										14
									
								
								src/ArduinoJson/Polyfills/type_traits/make_void.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/ArduinoJson/Polyfills/type_traits/make_void.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | |||||||
|  | // ArduinoJson - https://arduinojson.org | ||||||
|  | // Copyright Benoit Blanchon 2014-2021 | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
|  | template <class = void> | ||||||
|  | struct make_void { | ||||||
|  |   typedef void type; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
| @@ -7,14 +7,15 @@ | |||||||
| #include <Arduino.h> | #include <Arduino.h> | ||||||
| 
 | 
 | ||||||
| #include <ArduinoJson/Polyfills/safe_strcmp.hpp> | #include <ArduinoJson/Polyfills/safe_strcmp.hpp> | ||||||
| #include <ArduinoJson/Strings/IsString.hpp> |  | ||||||
| #include <ArduinoJson/Strings/StoragePolicy.hpp> | #include <ArduinoJson/Strings/StoragePolicy.hpp> | ||||||
|  | #include <ArduinoJson/Strings/StringAdapter.hpp> | ||||||
| 
 | 
 | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
| 
 | 
 | ||||||
| class ArduinoStringAdapter { | template <> | ||||||
|  | class StringAdapter< ::String> { | ||||||
|  public: |  public: | ||||||
|   ArduinoStringAdapter(const ::String& str) : _str(&str) {} |   StringAdapter(const ::String& str) : _str(&str) {} | ||||||
| 
 | 
 | ||||||
|   void copyTo(char* p, size_t n) const { |   void copyTo(char* p, size_t n) const { | ||||||
|     memcpy(p, _str->c_str(), n); |     memcpy(p, _str->c_str(), n); | ||||||
| @@ -46,13 +47,10 @@ class ArduinoStringAdapter { | |||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| template <> | template <> | ||||||
| struct IsString< ::String> : true_type {}; | class StringAdapter< ::StringSumHelper> : public StringAdapter< ::String> { | ||||||
| 
 |  public: | ||||||
| template <> |   StringAdapter< ::StringSumHelper>(const ::String& s) | ||||||
| struct IsString< ::StringSumHelper> : true_type {}; |       : StringAdapter< ::String>(s) {} | ||||||
| 
 | }; | ||||||
| inline ArduinoStringAdapter adaptString(const ::String& str) { |  | ||||||
|   return ArduinoStringAdapter(str); |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE
 | }  // namespace ARDUINOJSON_NAMESPACE
 | ||||||
| @@ -8,14 +8,15 @@ | |||||||
| #include <string.h>  // strcmp
 | #include <string.h>  // strcmp
 | ||||||
| 
 | 
 | ||||||
| #include <ArduinoJson/Polyfills/safe_strcmp.hpp> | #include <ArduinoJson/Polyfills/safe_strcmp.hpp> | ||||||
| #include <ArduinoJson/Strings/IsString.hpp> |  | ||||||
| #include <ArduinoJson/Strings/StoragePolicy.hpp> | #include <ArduinoJson/Strings/StoragePolicy.hpp> | ||||||
|  | #include <ArduinoJson/Strings/StringAdapter.hpp> | ||||||
| 
 | 
 | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
| 
 | 
 | ||||||
| class ConstRamStringAdapter { | template <> | ||||||
|  | class StringAdapter<const char*> { | ||||||
|  public: |  public: | ||||||
|   ConstRamStringAdapter(const char* str = 0) : _str(str) {} |   StringAdapter(const char* str = 0) : _str(str) {} | ||||||
| 
 | 
 | ||||||
|   int compare(const char* other) const { |   int compare(const char* other) const { | ||||||
|     return safe_strcmp(_str, other); |     return safe_strcmp(_str, other); | ||||||
| @@ -45,14 +46,10 @@ class ConstRamStringAdapter { | |||||||
|   const char* _str; |   const char* _str; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| template <> |  | ||||||
| struct IsString<const char*> : true_type {}; |  | ||||||
| 
 |  | ||||||
| template <int N> | template <int N> | ||||||
| struct IsString<const char[N]> : true_type {}; | class StringAdapter<const char[N]> : public StringAdapter<const char*> { | ||||||
| 
 |  public: | ||||||
| inline ConstRamStringAdapter adaptString(const char* str) { |   StringAdapter<const char[N]>(const char* s) : StringAdapter<const char*>(s) {} | ||||||
|   return ConstRamStringAdapter(str); | }; | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE
 | }  // namespace ARDUINOJSON_NAMESPACE
 | ||||||
| @@ -5,14 +5,15 @@ | |||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
| #include <ArduinoJson/Polyfills/pgmspace.hpp> | #include <ArduinoJson/Polyfills/pgmspace.hpp> | ||||||
| #include <ArduinoJson/Strings/IsString.hpp> |  | ||||||
| #include <ArduinoJson/Strings/StoragePolicy.hpp> | #include <ArduinoJson/Strings/StoragePolicy.hpp> | ||||||
|  | #include <ArduinoJson/Strings/StringAdapter.hpp> | ||||||
| 
 | 
 | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
| 
 | 
 | ||||||
| class FlashStringAdapter { | template <> | ||||||
|  | class StringAdapter<const __FlashStringHelper*> { | ||||||
|  public: |  public: | ||||||
|   FlashStringAdapter(const __FlashStringHelper* str) : _str(str) {} |   StringAdapter(const __FlashStringHelper* str) : _str(str) {} | ||||||
| 
 | 
 | ||||||
|   int compare(const char* other) const { |   int compare(const char* other) const { | ||||||
|     if (!other && !_str) |     if (!other && !_str) | ||||||
| @@ -48,10 +49,4 @@ class FlashStringAdapter { | |||||||
|   const __FlashStringHelper* _str; |   const __FlashStringHelper* _str; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| inline FlashStringAdapter adaptString(const __FlashStringHelper* str) { |  | ||||||
|   return FlashStringAdapter(str); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| template <> |  | ||||||
| struct IsString<const __FlashStringHelper*> : true_type {}; |  | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE
 | }  // namespace ARDUINOJSON_NAMESPACE
 | ||||||
							
								
								
									
										27
									
								
								src/ArduinoJson/Strings/Adapters/JsonStringAdapter.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/ArduinoJson/Strings/Adapters/JsonStringAdapter.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | |||||||
|  | // ArduinoJson - https://arduinojson.org | ||||||
|  | // Copyright Benoit Blanchon 2014-2021 | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <ArduinoJson/Strings/Adapters/RamStringAdapter.hpp> | ||||||
|  | #include <ArduinoJson/Strings/String.hpp> | ||||||
|  |  | ||||||
|  | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
|  | template <> | ||||||
|  | class StringAdapter<String> : public StringAdapter<char*> { | ||||||
|  |  public: | ||||||
|  |   StringAdapter(const String& str) | ||||||
|  |       : StringAdapter<char*>(str.c_str()), _isStatic(str.isStatic()) {} | ||||||
|  |  | ||||||
|  |   bool isStatic() const { | ||||||
|  |     return _isStatic; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   typedef storage_policies::decide_at_runtime storage_policy; | ||||||
|  |  | ||||||
|  |  private: | ||||||
|  |   bool _isStatic; | ||||||
|  | }; | ||||||
|  | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
							
								
								
									
										29
									
								
								src/ArduinoJson/Strings/Adapters/RamStringAdapter.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/ArduinoJson/Strings/Adapters/RamStringAdapter.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | |||||||
|  | // ArduinoJson - https://arduinojson.org | ||||||
|  | // Copyright Benoit Blanchon 2014-2021 | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <ArduinoJson/Polyfills/type_traits.hpp> | ||||||
|  | #include <ArduinoJson/Strings/StoragePolicy.hpp> | ||||||
|  | #include <ArduinoJson/Strings/StringAdapter.hpp> | ||||||
|  |  | ||||||
|  | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
|  | template <typename TChar> | ||||||
|  | class StringAdapter<TChar*, false, | ||||||
|  |                     typename enable_if<sizeof(TChar) == 1 && | ||||||
|  |                                        !is_same<TChar, void>::value>::type> | ||||||
|  |     : public StringAdapter<const char*> { | ||||||
|  |  public: | ||||||
|  |   StringAdapter(const TChar* str) | ||||||
|  |       : StringAdapter<const char*>(reinterpret_cast<const char*>(str)) {} | ||||||
|  |  | ||||||
|  |   void copyTo(char* p, size_t n) const { | ||||||
|  |     memcpy(p, _str, n); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   typedef ARDUINOJSON_NAMESPACE::storage_policies::store_by_copy storage_policy; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
| @@ -5,14 +5,15 @@ | |||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
| #include <ArduinoJson/Namespace.hpp> | #include <ArduinoJson/Namespace.hpp> | ||||||
| #include <ArduinoJson/Strings/IsString.hpp> |  | ||||||
| #include <ArduinoJson/Strings/StoragePolicy.hpp> | #include <ArduinoJson/Strings/StoragePolicy.hpp> | ||||||
|  | #include <ArduinoJson/Strings/StringAdapter.hpp> | ||||||
| 
 | 
 | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
| 
 | 
 | ||||||
| class SizedFlashStringAdapter { | template <> | ||||||
|  | class StringAdapter<const __FlashStringHelper*, true> { | ||||||
|  public: |  public: | ||||||
|   SizedFlashStringAdapter(const __FlashStringHelper* str, size_t sz) |   StringAdapter(const __FlashStringHelper* str, size_t sz) | ||||||
|       : _str(str), _size(sz) {} |       : _str(str), _size(sz) {} | ||||||
| 
 | 
 | ||||||
|   int compare(const char* other) const { |   int compare(const char* other) const { | ||||||
| @@ -48,8 +49,4 @@ class SizedFlashStringAdapter { | |||||||
|   size_t _size; |   size_t _size; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| inline SizedFlashStringAdapter adaptString(const __FlashStringHelper* str, |  | ||||||
|                                            size_t sz) { |  | ||||||
|   return SizedFlashStringAdapter(str, sz); |  | ||||||
| } |  | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE
 | }  // namespace ARDUINOJSON_NAMESPACE
 | ||||||
| @@ -5,16 +5,17 @@ | |||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
| #include <ArduinoJson/Namespace.hpp> | #include <ArduinoJson/Namespace.hpp> | ||||||
| #include <ArduinoJson/Strings/IsString.hpp> |  | ||||||
| #include <ArduinoJson/Strings/StoragePolicy.hpp> | #include <ArduinoJson/Strings/StoragePolicy.hpp> | ||||||
|  | #include <ArduinoJson/Strings/StringAdapter.hpp> | ||||||
| 
 | 
 | ||||||
| #include <string.h>  // strcmp
 | #include <string.h>  // strcmp
 | ||||||
| 
 | 
 | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
| 
 | 
 | ||||||
| class SizedRamStringAdapter { | template <typename TChar> | ||||||
|  | class StringAdapter<TChar*, true> { | ||||||
|  public: |  public: | ||||||
|   SizedRamStringAdapter(const char* str, size_t n) : _str(str), _size(n) {} |   StringAdapter(const char* str, size_t n) : _str(str), _size(n) {} | ||||||
| 
 | 
 | ||||||
|   int compare(const char* other) const { |   int compare(const char* other) const { | ||||||
|     return safe_strncmp(_str, other, _size); |     return safe_strncmp(_str, other, _size); | ||||||
| @@ -43,9 +44,4 @@ class SizedRamStringAdapter { | |||||||
|   size_t _size; |   size_t _size; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| template <typename TChar> |  | ||||||
| inline SizedRamStringAdapter adaptString(const TChar* str, size_t size) { |  | ||||||
|   return SizedRamStringAdapter(reinterpret_cast<const char*>(str), size); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE
 | }  // namespace ARDUINOJSON_NAMESPACE
 | ||||||
| @@ -5,17 +5,19 @@ | |||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
| #include <ArduinoJson/Namespace.hpp> | #include <ArduinoJson/Namespace.hpp> | ||||||
| #include <ArduinoJson/Strings/IsString.hpp> |  | ||||||
| #include <ArduinoJson/Strings/StoragePolicy.hpp> | #include <ArduinoJson/Strings/StoragePolicy.hpp> | ||||||
|  | #include <ArduinoJson/Strings/StringAdapter.hpp> | ||||||
| 
 | 
 | ||||||
| #include <string> | #include <string> | ||||||
| 
 | 
 | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
| 
 | 
 | ||||||
| template <typename TString> | template <typename TCharTraits, typename TAllocator> | ||||||
| class StdStringAdapter { | class StringAdapter<std::basic_string<char, TCharTraits, TAllocator> > { | ||||||
|  public: |  public: | ||||||
|   StdStringAdapter(const TString& str) : _str(&str) {} |   typedef std::basic_string<char, TCharTraits, TAllocator> string_type; | ||||||
|  | 
 | ||||||
|  |   StringAdapter(const string_type& str) : _str(&str) {} | ||||||
| 
 | 
 | ||||||
|   void copyTo(char* p, size_t n) const { |   void copyTo(char* p, size_t n) const { | ||||||
|     memcpy(p, _str->c_str(), n); |     memcpy(p, _str->c_str(), n); | ||||||
| @@ -44,18 +46,7 @@ class StdStringAdapter { | |||||||
|   typedef storage_policies::store_by_copy storage_policy; |   typedef storage_policies::store_by_copy storage_policy; | ||||||
| 
 | 
 | ||||||
|  private: |  private: | ||||||
|   const TString* _str; |   const string_type* _str; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| template <typename TCharTraits, typename TAllocator> |  | ||||||
| struct IsString<std::basic_string<char, TCharTraits, TAllocator> > : true_type { |  | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| template <typename TCharTraits, typename TAllocator> |  | ||||||
| inline StdStringAdapter<std::basic_string<char, TCharTraits, TAllocator> > |  | ||||||
| adaptString(const std::basic_string<char, TCharTraits, TAllocator>& str) { |  | ||||||
|   return StdStringAdapter<std::basic_string<char, TCharTraits, TAllocator> >( |  | ||||||
|       str); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE
 | }  // namespace ARDUINOJSON_NAMESPACE
 | ||||||
| @@ -5,16 +5,17 @@ | |||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
| #include <ArduinoJson/Namespace.hpp> | #include <ArduinoJson/Namespace.hpp> | ||||||
| #include <ArduinoJson/Strings/IsString.hpp> |  | ||||||
| #include <ArduinoJson/Strings/StoragePolicy.hpp> | #include <ArduinoJson/Strings/StoragePolicy.hpp> | ||||||
|  | #include <ArduinoJson/Strings/StringAdapter.hpp> | ||||||
| 
 | 
 | ||||||
| #include <string_view> | #include <string_view> | ||||||
| 
 | 
 | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
| 
 | 
 | ||||||
| class StringViewAdapter { | template <> | ||||||
|  | class StringAdapter<std::string_view> { | ||||||
|  public: |  public: | ||||||
|   StringViewAdapter(std::string_view str) : _str(str) {} |   StringAdapter(std::string_view str) : _str(str) {} | ||||||
| 
 | 
 | ||||||
|   void copyTo(char* p, size_t n) const { |   void copyTo(char* p, size_t n) const { | ||||||
|     memcpy(p, _str.data(), n); |     memcpy(p, _str.data(), n); | ||||||
| @@ -46,11 +47,4 @@ class StringViewAdapter { | |||||||
|   std::string_view _str; |   std::string_view _str; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| template <> |  | ||||||
| struct IsString<std::string_view> : true_type {}; |  | ||||||
| 
 |  | ||||||
| inline StringViewAdapter adaptString(const std::string_view& str) { |  | ||||||
|   return StringViewAdapter(str); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE
 | }  // namespace ARDUINOJSON_NAMESPACE
 | ||||||
| @@ -1,18 +0,0 @@ | |||||||
| // ArduinoJson - https://arduinojson.org |  | ||||||
| // Copyright Benoit Blanchon 2014-2021 |  | ||||||
| // MIT License |  | ||||||
|  |  | ||||||
| #pragma once |  | ||||||
|  |  | ||||||
| #include <ArduinoJson/Polyfills/type_traits.hpp> |  | ||||||
|  |  | ||||||
| namespace ARDUINOJSON_NAMESPACE { |  | ||||||
| template <typename> |  | ||||||
| struct IsString : false_type {}; |  | ||||||
|  |  | ||||||
| template <typename T> |  | ||||||
| struct IsString<const T> : IsString<T> {}; |  | ||||||
|  |  | ||||||
| template <typename T> |  | ||||||
| struct IsString<T&> : IsString<T> {}; |  | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE |  | ||||||
| @@ -1,43 +0,0 @@ | |||||||
| // ArduinoJson - https://arduinojson.org |  | ||||||
| // Copyright Benoit Blanchon 2014-2021 |  | ||||||
| // MIT License |  | ||||||
|  |  | ||||||
| #pragma once |  | ||||||
|  |  | ||||||
| #include <ArduinoJson/Strings/ConstRamStringAdapter.hpp> |  | ||||||
| #include <ArduinoJson/Strings/IsString.hpp> |  | ||||||
| #include <ArduinoJson/Strings/StoragePolicy.hpp> |  | ||||||
|  |  | ||||||
| namespace ARDUINOJSON_NAMESPACE { |  | ||||||
|  |  | ||||||
| class RamStringAdapter : public ConstRamStringAdapter { |  | ||||||
|  public: |  | ||||||
|   RamStringAdapter(const char* str) : ConstRamStringAdapter(str) {} |  | ||||||
|  |  | ||||||
|   void copyTo(char* p, size_t n) const { |  | ||||||
|     memcpy(p, _str, n); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   typedef ARDUINOJSON_NAMESPACE::storage_policies::store_by_copy storage_policy; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| template <typename TChar> |  | ||||||
| inline RamStringAdapter adaptString(const TChar* str) { |  | ||||||
|   return RamStringAdapter(reinterpret_cast<const char*>(str)); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| inline RamStringAdapter adaptString(char* str) { |  | ||||||
|   return RamStringAdapter(str); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| template <typename TChar> |  | ||||||
| struct IsString<TChar*> { |  | ||||||
|   static const bool value = sizeof(TChar) == 1; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| template <> |  | ||||||
| struct IsString<void*> { |  | ||||||
|   static const bool value = false; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE |  | ||||||
| @@ -4,10 +4,6 @@ | |||||||
|  |  | ||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #include <ArduinoJson/Strings/ConstRamStringAdapter.hpp> |  | ||||||
| #include <ArduinoJson/Strings/IsString.hpp> |  | ||||||
| #include <ArduinoJson/Strings/StoragePolicy.hpp> |  | ||||||
|  |  | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
| class String { | class String { | ||||||
| @@ -53,25 +49,4 @@ class String { | |||||||
|   bool _isStatic; |   bool _isStatic; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| class StringAdapter : public RamStringAdapter { |  | ||||||
|  public: |  | ||||||
|   StringAdapter(const String& str) |  | ||||||
|       : RamStringAdapter(str.c_str()), _isStatic(str.isStatic()) {} |  | ||||||
|  |  | ||||||
|   bool isStatic() const { |  | ||||||
|     return _isStatic; |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   typedef storage_policies::decide_at_runtime storage_policy; |  | ||||||
|  |  | ||||||
|  private: |  | ||||||
|   bool _isStatic; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| template <> |  | ||||||
| struct IsString<String> : true_type {}; |  | ||||||
|  |  | ||||||
| inline StringAdapter adaptString(const String& str) { |  | ||||||
|   return StringAdapter(str); |  | ||||||
| } |  | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
|   | |||||||
							
								
								
									
										32
									
								
								src/ArduinoJson/Strings/StringAdapter.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/ArduinoJson/Strings/StringAdapter.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | |||||||
|  | // ArduinoJson - https://arduinojson.org | ||||||
|  | // Copyright Benoit Blanchon 2014-2021 | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <ArduinoJson/Polyfills/type_traits.hpp> | ||||||
|  |  | ||||||
|  | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
|  | template <typename T, bool bounded = false, typename Enable = void> | ||||||
|  | class StringAdapter; | ||||||
|  |  | ||||||
|  | template <typename T> | ||||||
|  | inline StringAdapter<T, false> adaptString(const T& str) { | ||||||
|  |   return StringAdapter<T, false>(str); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | template <typename T> | ||||||
|  | inline StringAdapter<T, true> adaptString(const T& str, size_t sz) { | ||||||
|  |   return StringAdapter<T, true>(str, sz); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | template <typename T, typename Enable = void> | ||||||
|  | struct IsString : false_type {}; | ||||||
|  |  | ||||||
|  | template <typename T> | ||||||
|  | struct IsString< | ||||||
|  |     T, typename make_void<typename StringAdapter<T>::storage_policy>::type> | ||||||
|  |     : true_type {}; | ||||||
|  |  | ||||||
|  | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
| @@ -4,23 +4,24 @@ | |||||||
|  |  | ||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #include <ArduinoJson/Strings/ConstRamStringAdapter.hpp> | #include <ArduinoJson/Strings/Adapters/ConstRamStringAdapter.hpp> | ||||||
| #include <ArduinoJson/Strings/RamStringAdapter.hpp> | #include <ArduinoJson/Strings/Adapters/JsonStringAdapter.hpp> | ||||||
| #include <ArduinoJson/Strings/SizedRamStringAdapter.hpp> | #include <ArduinoJson/Strings/Adapters/RamStringAdapter.hpp> | ||||||
|  | #include <ArduinoJson/Strings/Adapters/SizedRamStringAdapter.hpp> | ||||||
|  |  | ||||||
| #if ARDUINOJSON_ENABLE_STD_STRING | #if ARDUINOJSON_ENABLE_STD_STRING | ||||||
| #  include <ArduinoJson/Strings/StdStringAdapter.hpp> | #  include <ArduinoJson/Strings/Adapters/StdStringAdapter.hpp> | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #if ARDUINOJSON_ENABLE_STRING_VIEW | #if ARDUINOJSON_ENABLE_STRING_VIEW | ||||||
| #  include <ArduinoJson/Strings/StringViewAdapter.hpp> | #  include <ArduinoJson/Strings/Adapters/StringViewAdapter.hpp> | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #if ARDUINOJSON_ENABLE_ARDUINO_STRING | #if ARDUINOJSON_ENABLE_ARDUINO_STRING | ||||||
| #  include <ArduinoJson/Strings/ArduinoStringAdapter.hpp> | #  include <ArduinoJson/Strings/Adapters/ArduinoStringAdapter.hpp> | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #if ARDUINOJSON_ENABLE_PROGMEM | #if ARDUINOJSON_ENABLE_PROGMEM | ||||||
| #  include <ArduinoJson/Strings/FlashStringAdapter.hpp> | #  include <ArduinoJson/Strings/Adapters/FlashStringAdapter.hpp> | ||||||
| #  include <ArduinoJson/Strings/SizedFlashStringAdapter.hpp> | #  include <ArduinoJson/Strings/Adapters/SizedFlashStringAdapter.hpp> | ||||||
| #endif | #endif | ||||||
|   | |||||||
| @@ -8,7 +8,7 @@ | |||||||
| #include <ArduinoJson/Misc/Visitable.hpp> | #include <ArduinoJson/Misc/Visitable.hpp> | ||||||
| #include <ArduinoJson/Numbers/arithmeticCompare.hpp> | #include <ArduinoJson/Numbers/arithmeticCompare.hpp> | ||||||
| #include <ArduinoJson/Polyfills/type_traits.hpp> | #include <ArduinoJson/Polyfills/type_traits.hpp> | ||||||
| #include <ArduinoJson/Strings/IsString.hpp> | #include <ArduinoJson/Strings/StringAdapter.hpp> | ||||||
| #include <ArduinoJson/Variant/Visitor.hpp> | #include <ArduinoJson/Variant/Visitor.hpp> | ||||||
|  |  | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
|   | |||||||
| @@ -7,7 +7,7 @@ | |||||||
| #include <ArduinoJson/Memory/MemoryPool.hpp> | #include <ArduinoJson/Memory/MemoryPool.hpp> | ||||||
| #include <ArduinoJson/Misc/SerializedValue.hpp> | #include <ArduinoJson/Misc/SerializedValue.hpp> | ||||||
| #include <ArduinoJson/Numbers/convertNumber.hpp> | #include <ArduinoJson/Numbers/convertNumber.hpp> | ||||||
| #include <ArduinoJson/Strings/RamStringAdapter.hpp> | #include <ArduinoJson/Strings/StringAdapters.hpp> | ||||||
| #include <ArduinoJson/Variant/VariantContent.hpp> | #include <ArduinoJson/Variant/VariantContent.hpp> | ||||||
|  |  | ||||||
| // VariantData can't have a constructor (to be a POD), so we have no way to fix | // VariantData can't have a constructor (to be a POD), so we have no way to fix | ||||||
| @@ -103,7 +103,8 @@ class VariantData { | |||||||
|       case VALUE_IS_OBJECT: |       case VALUE_IS_OBJECT: | ||||||
|         return toObject().copyFrom(src._content.asCollection, pool); |         return toObject().copyFrom(src._content.asCollection, pool); | ||||||
|       case VALUE_IS_OWNED_STRING: |       case VALUE_IS_OWNED_STRING: | ||||||
|         return setString(RamStringAdapter(src._content.asString), pool); |         return setString(adaptString(const_cast<char *>(src._content.asString)), | ||||||
|  |                          pool); | ||||||
|       case VALUE_IS_OWNED_RAW: |       case VALUE_IS_OWNED_RAW: | ||||||
|         return setOwnedRaw( |         return setOwnedRaw( | ||||||
|             serialized(src._content.asRaw.data, src._content.asRaw.size), pool); |             serialized(src._content.asRaw.data, src._content.asRaw.size), pool); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user