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