diff options
| author | Patrick Williams <patrick@stwcx.xyz> | 2016-07-20 12:36:56 -0500 |
|---|---|---|
| committer | Patrick Williams <patrick@stwcx.xyz> | 2016-07-26 16:19:28 -0500 |
| commit | 4dbdef708e9e3149dab25ea377c2d959300382a4 (patch) | |
| tree | 103dee9a650cb6e85c836c7917c7f094e23a3243 | |
| parent | 0c841a17f53aca94cc4f66943c86169def9e5186 (diff) | |
| download | sdbusplus-4dbdef708e9e3149dab25ea377c2d959300382a4.tar.gz sdbusplus-4dbdef708e9e3149dab25ea377c2d959300382a4.zip | |
Add tuple_to_array template
C++ template to convert tuple<T...> to array<T, N>.
Change-Id: Ia9dff382f9c57d7608edaec281cf7d867aeed4b2
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
| -rw-r--r-- | sdbusplus/utility/tuple_to_array.hpp | 56 | ||||
| -rw-r--r-- | test/utility/tuple_to_array.cpp | 17 |
2 files changed, 73 insertions, 0 deletions
diff --git a/sdbusplus/utility/tuple_to_array.hpp b/sdbusplus/utility/tuple_to_array.hpp new file mode 100644 index 0000000..b5ab2a5 --- /dev/null +++ b/sdbusplus/utility/tuple_to_array.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include <utility> +#include <tuple> +#include <array> + +namespace sdbusplus +{ + +namespace utility +{ + +namespace details +{ + +/** tuple_to_array - Create std::array from std::tuple. + * + * @tparam V - Type of the first tuple element. + * @tparam Types - Sequence of types for remaining elements, which must + * be automatically castable to V. + * @tparam I - Sequence of integral indexes (0...N) for each tuple elemeent. + * + * @param tuple - Tuple of N same-typed elements. + * @param [unnamed] - std::integer_sequence of tuple's index values. + * + * @return A std::array where each I-th element is tuple's I-th element. + */ +template <typename V, typename... Types, std::size_t... I> +constexpr auto tuple_to_array( + std::tuple<V, Types...>&& tuple, + std::integer_sequence<std::size_t, I...>) +{ + return std::array<V, sizeof...(I) >({ std::get<I>(tuple)..., }); +} + +} // namespace details + +/** tuple_to_array - Create std::array from std::tuple. + * + * @tparam Types - Sequence of types of the tuple elements. + * @tparam I - std::integer_sequence of indexes (0..N) for each tuple element. + * + * @param tuple - Tuple of N same-typed elements. + * + * @return A std::array where each I-th element is tuple's I-th element. + */ +template <typename... Types, + typename I = std::make_index_sequence<sizeof...(Types)>> +constexpr auto tuple_to_array(std::tuple<Types...>&& tuple) +{ + return details::tuple_to_array(std::move(tuple), I()); +} + +} // namespace utility + +} // namespace sdbusplus diff --git a/test/utility/tuple_to_array.cpp b/test/utility/tuple_to_array.cpp new file mode 100644 index 0000000..e7472f9 --- /dev/null +++ b/test/utility/tuple_to_array.cpp @@ -0,0 +1,17 @@ +#include <sdbusplus/utility/tuple_to_array.hpp> +#include <cassert> + +int main() +{ + std::array<char, 3> a{'a', 'b', 'c'}; + auto t = std::make_tuple('a', 'b', 'c'); + + assert(a == sdbusplus::utility::tuple_to_array(std::move(t))); + + std::array<int, 4> b{1, 2, 3, 4}; + auto t2 = std::make_tuple(1, 2, 3, 4); + + assert(b == sdbusplus::utility::tuple_to_array(std::move(t2))); + + return 0; +} |

