Add array_to_ptr template
C++ template to convert T[] type to T* type.
Change-Id: I91c63b5d14b77f82d3deea3772990912dfcd4277
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/sdbusplus/utility/type_traits.hpp b/sdbusplus/utility/type_traits.hpp
new file mode 100644
index 0000000..c5bf965
--- /dev/null
+++ b/sdbusplus/utility/type_traits.hpp
@@ -0,0 +1,27 @@
+#pragma once
+
+#include <type_traits>
+
+namespace sdbusplus
+{
+
+namespace utility
+{
+
+/** @brief Convert T[N] to T* if is_same<Tbase,T>
+ *
+ * @tparam Tbase - The base type expected.
+ * @tparam T - The type to convert.
+ */
+template <typename Tbase, typename T>
+using array_to_ptr_t = typename std::conditional_t<
+ std::is_array<T>::value,
+ std::conditional_t<std::is_same<Tbase,
+ std::remove_extent_t<T>>::value,
+ std::add_pointer_t<std::remove_extent_t<T>>,
+ T>,
+ T>;
+
+} // namespace utility
+
+} // namespace sdbusplus
diff --git a/test/utility/type_traits.cpp b/test/utility/type_traits.cpp
new file mode 100644
index 0000000..3a31d06
--- /dev/null
+++ b/test/utility/type_traits.cpp
@@ -0,0 +1,24 @@
+#include <sdbusplus/utility/type_traits.hpp>
+
+int main()
+{
+ using sdbusplus::utility::array_to_ptr_t;
+
+ static_assert(
+ std::is_same<char, array_to_ptr_t<char, char>>::value,
+ "array_to_ptr_t<char, char> != char");
+
+ static_assert(
+ std::is_same<char*, array_to_ptr_t<char, char*>>::value,
+ "array_to_ptr_t<char, char*> != char*");
+
+ static_assert(
+ std::is_same<char*, array_to_ptr_t<char, char[100]>>::value,
+ "array_to_ptr_t<char, char[100]> != char*");
+
+ static_assert(
+ std::is_same<char[100], array_to_ptr_t<int, char[100]>>::value,
+ "array_to_ptr_t<int, char[100]> != char[100]");
+
+ return 0;
+}