mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Add StringAdapter<T>
				
					
				
			This commit is contained in:
		| @@ -8,12 +8,20 @@ | |||||||
|  |  | ||||||
| #include <ArduinoJson/Strings/Adapters/RamString.hpp> | #include <ArduinoJson/Strings/Adapters/RamString.hpp> | ||||||
| #include <ArduinoJson/Strings/IsString.hpp> | #include <ArduinoJson/Strings/IsString.hpp> | ||||||
|  | #include <ArduinoJson/Strings/StringAdapter.hpp> | ||||||
|  |  | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
| inline SizedRamString adaptString(const ::String& s) { | template <typename T> | ||||||
|   return SizedRamString(s.c_str(), s.length()); | struct StringAdapter< | ||||||
|  |     T, typename enable_if<is_same<T, ::String>::value || | ||||||
|  |                           is_same<T, ::StringSumHelper>::value>::type> { | ||||||
|  |   typedef SizedRamString AdaptedString; | ||||||
|  |  | ||||||
|  |   static AdaptedString adapt(const ::String& s) { | ||||||
|  |     return AdaptedString(s.c_str(), s.length()); | ||||||
|   } |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
| template <> | template <> | ||||||
| struct IsString< ::String> : true_type {}; | struct IsString< ::String> : true_type {}; | ||||||
|   | |||||||
| @@ -70,13 +70,23 @@ class FlashString { | |||||||
|   size_t _size; |   size_t _size; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| inline FlashString adaptString(const __FlashStringHelper* s) { | template <> | ||||||
|   return FlashString(s, s ? strlen_P(reinterpret_cast<const char*>(s)) : 0); | struct StringAdapter<const __FlashStringHelper*, void> { | ||||||
| } |   typedef FlashString AdaptedString; | ||||||
|  |  | ||||||
| inline FlashString adaptString(const __FlashStringHelper* s, size_t n) { |   static AdaptedString adapt(const __FlashStringHelper* s) { | ||||||
|   return FlashString(s, n); |     return AdaptedString(s, s ? strlen_P(reinterpret_cast<const char*>(s)) : 0); | ||||||
|   } |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | template <> | ||||||
|  | struct SizedStringAdapter<const __FlashStringHelper*, void> { | ||||||
|  |   typedef FlashString AdaptedString; | ||||||
|  |  | ||||||
|  |   static AdaptedString adapt(const __FlashStringHelper* s, size_t n) { | ||||||
|  |     return AdaptedString(s, n); | ||||||
|  |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
| template <> | template <> | ||||||
| struct IsString<const __FlashStringHelper*> : true_type {}; | struct IsString<const __FlashStringHelper*> : true_type {}; | ||||||
|   | |||||||
| @@ -24,9 +24,14 @@ class JsonStringAdapter : public SizedRamString { | |||||||
|   bool _linked; |   bool _linked; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| inline JsonStringAdapter adaptString(const String& s) { | template <> | ||||||
|   return JsonStringAdapter(s); | struct StringAdapter<String> { | ||||||
|  |   typedef JsonStringAdapter AdaptedString; | ||||||
|  |  | ||||||
|  |   static AdaptedString adapt(const String& s) { | ||||||
|  |     return AdaptedString(s); | ||||||
|   } |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
| template <> | template <> | ||||||
| struct IsString<String> : true_type {}; | struct IsString<String> : true_type {}; | ||||||
|   | |||||||
| @@ -10,6 +10,7 @@ | |||||||
| #include <ArduinoJson/Polyfills/assert.hpp> | #include <ArduinoJson/Polyfills/assert.hpp> | ||||||
| #include <ArduinoJson/Strings/IsString.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 { | ||||||
|  |  | ||||||
| @@ -60,23 +61,35 @@ class ZeroTerminatedRamString { | |||||||
| template <> | template <> | ||||||
| struct IsString<char*> : true_type {}; | struct IsString<char*> : true_type {}; | ||||||
|  |  | ||||||
| inline ZeroTerminatedRamString adaptString(char* s) { |  | ||||||
|   return ZeroTerminatedRamString(s); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| template <> | template <> | ||||||
| struct IsString<unsigned char*> : true_type {}; | struct IsString<unsigned char*> : true_type {}; | ||||||
|  |  | ||||||
| inline ZeroTerminatedRamString adaptString(const unsigned char* s) { | template <typename TChar> | ||||||
|   return ZeroTerminatedRamString(reinterpret_cast<const char*>(s)); | struct StringAdapter<TChar*, typename enable_if<sizeof(TChar) == 1>::type> { | ||||||
|  |   typedef ZeroTerminatedRamString AdaptedString; | ||||||
|  |  | ||||||
|  |   static AdaptedString adapt(const TChar* p) { | ||||||
|  |     return AdaptedString(reinterpret_cast<const char*>(p)); | ||||||
|   } |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
| template <> | template <> | ||||||
| struct IsString<signed char*> : true_type {}; | struct IsString<signed char*> : true_type {}; | ||||||
|  |  | ||||||
| inline ZeroTerminatedRamString adaptString(const signed char* s) { | template <size_t N> | ||||||
|   return ZeroTerminatedRamString(reinterpret_cast<const char*>(s)); | struct IsString<char[N]> : true_type {}; | ||||||
|  |  | ||||||
|  | template <size_t N> | ||||||
|  | struct IsString<const char[N]> : true_type {}; | ||||||
|  |  | ||||||
|  | template <typename TChar, size_t N> | ||||||
|  | struct StringAdapter<TChar[N], typename enable_if<sizeof(TChar) == 1>::type> { | ||||||
|  |   typedef ZeroTerminatedRamString AdaptedString; | ||||||
|  |  | ||||||
|  |   static AdaptedString adapt(const TChar* p) { | ||||||
|  |     return AdaptedString(reinterpret_cast<const char*>(p)); | ||||||
|   } |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
| class StaticStringAdapter : public ZeroTerminatedRamString { | class StaticStringAdapter : public ZeroTerminatedRamString { | ||||||
|  public: |  public: | ||||||
| @@ -87,9 +100,14 @@ class StaticStringAdapter : public ZeroTerminatedRamString { | |||||||
|   } |   } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| inline StaticStringAdapter adaptString(const char* s) { | template <> | ||||||
|   return StaticStringAdapter(s); | struct StringAdapter<const char*, void> { | ||||||
|  |   typedef StaticStringAdapter AdaptedString; | ||||||
|  |  | ||||||
|  |   static AdaptedString adapt(const char* p) { | ||||||
|  |     return AdaptedString(p); | ||||||
|   } |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
| class SizedRamString { | class SizedRamString { | ||||||
|  public: |  public: | ||||||
| @@ -124,18 +142,14 @@ class SizedRamString { | |||||||
|   size_t _size; |   size_t _size; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| inline SizedRamString adaptString(const char* s, size_t n) { | template <typename TChar> | ||||||
|   return SizedRamString(s, n); | struct SizedStringAdapter<TChar*, | ||||||
|  |                           typename enable_if<sizeof(TChar) == 1>::type> { | ||||||
|  |   typedef SizedRamString AdaptedString; | ||||||
|  |  | ||||||
|  |   static AdaptedString adapt(const TChar* p, size_t n) { | ||||||
|  |     return AdaptedString(reinterpret_cast<const char*>(p), n); | ||||||
|   } |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
| template <size_t N> |  | ||||||
| struct IsString<char[N]> : true_type {}; |  | ||||||
|  |  | ||||||
| template <size_t N> |  | ||||||
| struct IsString<const char[N]> : true_type {}; |  | ||||||
|  |  | ||||||
| template <size_t N> |  | ||||||
| inline SizedRamString adaptString(char s[N]) { |  | ||||||
|   return SizedRamString(s, strlen(s)); |  | ||||||
| } |  | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
|   | |||||||
| @@ -11,10 +11,14 @@ | |||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
| template <typename TCharTraits, typename TAllocator> | template <typename TCharTraits, typename TAllocator> | ||||||
| inline SizedRamString adaptString( | struct StringAdapter<std::basic_string<char, TCharTraits, TAllocator>, void> { | ||||||
|  |   typedef SizedRamString AdaptedString; | ||||||
|  |  | ||||||
|  |   static AdaptedString adapt( | ||||||
|       const std::basic_string<char, TCharTraits, TAllocator>& s) { |       const std::basic_string<char, TCharTraits, TAllocator>& s) { | ||||||
|   return SizedRamString(s.c_str(), s.size()); |     return AdaptedString(s.c_str(), s.size()); | ||||||
|   } |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
| template <typename TCharTraits, typename TAllocator> | template <typename TCharTraits, typename TAllocator> | ||||||
| struct IsString<std::basic_string<char, TCharTraits, TAllocator> > : true_type { | struct IsString<std::basic_string<char, TCharTraits, TAllocator> > : true_type { | ||||||
|   | |||||||
| @@ -10,9 +10,14 @@ | |||||||
|  |  | ||||||
| namespace ARDUINOJSON_NAMESPACE { | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
| inline SizedRamString adaptString(const std::string_view& s) { | template <> | ||||||
|   return SizedRamString(s.data(), s.size()); | struct StringAdapter<std::string_view, void> { | ||||||
|  |   typedef SizedRamString AdaptedString; | ||||||
|  |  | ||||||
|  |   static AdaptedString adapt(const std::string_view& s) { | ||||||
|  |     return AdaptedString(s.data(), s.size()); | ||||||
|   } |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
| template <> | template <> | ||||||
| struct IsString<std::string_view> : true_type {}; | struct IsString<std::string_view> : true_type {}; | ||||||
|   | |||||||
							
								
								
									
										31
									
								
								src/ArduinoJson/Strings/StringAdapter.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/ArduinoJson/Strings/StringAdapter.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | |||||||
|  | // ArduinoJson - https://arduinojson.org | ||||||
|  | // Copyright © 2014-2022, Benoit BLANCHON | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
|  | template <typename TString, typename Enable = void> | ||||||
|  | struct StringAdapter; | ||||||
|  |  | ||||||
|  | template <typename TString, typename Enable = void> | ||||||
|  | struct SizedStringAdapter; | ||||||
|  |  | ||||||
|  | template <typename TString> | ||||||
|  | typename StringAdapter<TString>::AdaptedString adaptString(const TString& s) { | ||||||
|  |   return StringAdapter<TString>::adapt(s); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | template <typename TChar> | ||||||
|  | typename StringAdapter<TChar*>::AdaptedString adaptString(TChar* p) { | ||||||
|  |   return StringAdapter<TChar*>::adapt(p); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | template <typename TChar> | ||||||
|  | typename SizedStringAdapter<TChar*>::AdaptedString adaptString(TChar* p, | ||||||
|  |                                                                size_t n) { | ||||||
|  |   return SizedStringAdapter<TChar*>::adapt(p, n); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
		Reference in New Issue
	
	Block a user