diff options
| author | Patrick Williams <patrick@stwcx.xyz> | 2016-07-20 12:34:24 -0500 |
|---|---|---|
| committer | Patrick Williams <patrick@stwcx.xyz> | 2016-07-26 14:08:32 -0500 |
| commit | 0c841a17f53aca94cc4f66943c86169def9e5186 (patch) | |
| tree | f903489164cead1d1f82c232c3d66c25e199927a | |
| parent | 8aaac21e3f820237d3acbedb45e52ce318fd986a (diff) | |
| download | sdbusplus-0c841a17f53aca94cc4f66943c86169def9e5186.tar.gz sdbusplus-0c841a17f53aca94cc4f66943c86169def9e5186.zip | |
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>
| -rw-r--r-- | sdbusplus/utility/type_traits.hpp | 27 | ||||
| -rw-r--r-- | test/utility/type_traits.cpp | 24 |
2 files changed, 51 insertions, 0 deletions
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; +} |

