Jelajahi Sumber

Polyfills: add `decay`

Benoit Blanchon 1 tahun lalu
induk
melakukan
afc0a29c2c

+ 9 - 0
extras/tests/Misc/TypeTraits.cpp

@@ -211,6 +211,15 @@ TEST_CASE("Polyfills/type_traits") {
     CHECK(is_enum<bool>::value == false);
     CHECK(is_enum<double>::value == false);
   }
+
+  SECTION("decay") {
+    CHECK(is_same<decay_t<int>, int>::value);
+    CHECK(is_same<decay_t<int&>, int>::value);
+    CHECK(is_same<decay_t<int&&>, int>::value);
+    CHECK(is_same<decay_t<int[]>, int*>::value);
+    CHECK(is_same<decay_t<int[10]>, int*>::value);
+    CHECK(is_same<decay_t<decltype("toto")>, const char*>::value);
+  }
 }
 
 TEST_CASE("is_std_string") {

+ 1 - 0
src/ArduinoJson/Polyfills/type_traits.hpp

@@ -5,6 +5,7 @@
 #pragma once
 
 #include "type_traits/conditional.hpp"
+#include "type_traits/decay.hpp"
 #include "type_traits/enable_if.hpp"
 #include "type_traits/function_traits.hpp"
 #include "type_traits/integral_constant.hpp"

+ 33 - 0
src/ArduinoJson/Polyfills/type_traits/decay.hpp

@@ -0,0 +1,33 @@
+// ArduinoJson - https://arduinojson.org
+// Copyright © 2014-2024, Benoit BLANCHON
+// MIT License
+
+#pragma once
+
+#include <stddef.h>  // size_t
+
+#include <ArduinoJson/Namespace.hpp>
+
+ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
+
+template <typename T>
+struct decay {
+  using type = T;
+};
+
+template <typename T>
+struct decay<T&> : decay<T> {};
+
+template <typename T>
+struct decay<T&&> : decay<T> {};
+
+template <typename T>
+struct decay<T[]> : decay<T*> {};
+
+template <typename T, size_t N>
+struct decay<T[N]> : decay<T*> {};
+
+template <typename T>
+using decay_t = typename decay<T>::type;
+
+ARDUINOJSON_END_PRIVATE_NAMESPACE