Prechádzať zdrojové kódy

Implement `IsString` from `StringAdapter`

Benoit Blanchon 3 rokov pred
rodič
commit
c3d5e9382d

+ 1 - 0
extras/tests/Misc/StringAdapters.cpp

@@ -8,6 +8,7 @@
 #include "custom_string.hpp"
 #include "weird_strcmp.hpp"
 
+#include <ArduinoJson/Strings/IsString.hpp>
 #include <ArduinoJson/Strings/StringAdapters.hpp>
 
 #include <catch.hpp>

+ 1 - 1
src/ArduinoJson/Object/ObjectShortcuts.hpp

@@ -6,7 +6,7 @@
 
 #include <ArduinoJson/Polyfills/attributes.hpp>
 #include <ArduinoJson/Polyfills/type_traits.hpp>
-#include <ArduinoJson/Strings/StringAdapters.hpp>
+#include <ArduinoJson/Strings/IsString.hpp>
 
 namespace ARDUINOJSON_NAMESPACE {
 template <typename TSource>

+ 0 - 7
src/ArduinoJson/Strings/Adapters/ArduinoString.hpp

@@ -7,7 +7,6 @@
 #include <Arduino.h>
 
 #include <ArduinoJson/Strings/Adapters/RamString.hpp>
-#include <ArduinoJson/Strings/IsString.hpp>
 #include <ArduinoJson/Strings/StringAdapter.hpp>
 
 namespace ARDUINOJSON_NAMESPACE {
@@ -23,10 +22,4 @@ struct StringAdapter<
   }
 };
 
-template <>
-struct IsString< ::String> : true_type {};
-
-template <>
-struct IsString< ::StringSumHelper> : true_type {};
-
 }  // namespace ARDUINOJSON_NAMESPACE

+ 1 - 4
src/ArduinoJson/Strings/Adapters/FlashString.hpp

@@ -7,7 +7,7 @@
 #include <Arduino.h>
 
 #include <ArduinoJson/Polyfills/pgmspace.hpp>
-#include <ArduinoJson/Strings/IsString.hpp>
+#include <ArduinoJson/Strings/StringAdapter.hpp>
 
 namespace ARDUINOJSON_NAMESPACE {
 
@@ -88,7 +88,4 @@ struct SizedStringAdapter<const __FlashStringHelper*, void> {
   }
 };
 
-template <>
-struct IsString<const __FlashStringHelper*> : true_type {};
-
 }  // namespace ARDUINOJSON_NAMESPACE

+ 1 - 4
src/ArduinoJson/Strings/Adapters/JsonString.hpp

@@ -5,8 +5,8 @@
 #pragma once
 
 #include <ArduinoJson/Strings/Adapters/RamString.hpp>
-#include <ArduinoJson/Strings/IsString.hpp>
 #include <ArduinoJson/Strings/String.hpp>
+#include <ArduinoJson/Strings/StringAdapter.hpp>
 
 namespace ARDUINOJSON_NAMESPACE {
 
@@ -33,7 +33,4 @@ struct StringAdapter<String> {
   }
 };
 
-template <>
-struct IsString<String> : true_type {};
-
 }  // namespace ARDUINOJSON_NAMESPACE

+ 0 - 16
src/ArduinoJson/Strings/Adapters/RamString.hpp

@@ -8,7 +8,6 @@
 #include <string.h>  // strcmp
 
 #include <ArduinoJson/Polyfills/assert.hpp>
-#include <ArduinoJson/Strings/IsString.hpp>
 #include <ArduinoJson/Strings/StoragePolicy.hpp>
 #include <ArduinoJson/Strings/StringAdapter.hpp>
 
@@ -58,12 +57,6 @@ class ZeroTerminatedRamString {
   const char* _str;
 };
 
-template <>
-struct IsString<char*> : true_type {};
-
-template <>
-struct IsString<unsigned char*> : true_type {};
-
 template <typename TChar>
 struct StringAdapter<TChar*, typename enable_if<sizeof(TChar) == 1>::type> {
   typedef ZeroTerminatedRamString AdaptedString;
@@ -73,15 +66,6 @@ struct StringAdapter<TChar*, typename enable_if<sizeof(TChar) == 1>::type> {
   }
 };
 
-template <>
-struct IsString<signed char*> : true_type {};
-
-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;

+ 0 - 4
src/ArduinoJson/Strings/Adapters/StdString.hpp

@@ -20,8 +20,4 @@ struct StringAdapter<std::basic_string<char, TCharTraits, TAllocator>, void> {
   }
 };
 
-template <typename TCharTraits, typename TAllocator>
-struct IsString<std::basic_string<char, TCharTraits, TAllocator> > : true_type {
-};
-
 }  // namespace ARDUINOJSON_NAMESPACE

+ 0 - 3
src/ArduinoJson/Strings/Adapters/StringView.hpp

@@ -19,7 +19,4 @@ struct StringAdapter<std::string_view, void> {
   }
 };
 
-template <>
-struct IsString<std::string_view> : true_type {};
-
 }  // namespace ARDUINOJSON_NAMESPACE

+ 5 - 2
src/ArduinoJson/Strings/IsString.hpp

@@ -5,13 +5,16 @@
 #pragma once
 
 #include <ArduinoJson/Polyfills/type_traits.hpp>
+#include <ArduinoJson/Strings/StringAdapter.hpp>
 
 namespace ARDUINOJSON_NAMESPACE {
 
 template <typename T, typename Enable = void>
 struct IsString : false_type {};
 
-template <typename TChar>
-struct IsString<const TChar*> : IsString<TChar*> {};
+template <typename T>
+struct IsString<
+    T, typename make_void<typename StringAdapter<T>::AdaptedString>::type>
+    : true_type {};
 
 }  // namespace ARDUINOJSON_NAMESPACE