Patrick Williams | 4dbdef7 | 2016-07-20 12:36:56 -0500 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | #include <utility> |
| 4 | #include <tuple> |
| 5 | #include <array> |
| 6 | |
| 7 | namespace sdbusplus |
| 8 | { |
| 9 | |
| 10 | namespace utility |
| 11 | { |
| 12 | |
| 13 | namespace details |
| 14 | { |
| 15 | |
| 16 | /** tuple_to_array - Create std::array from std::tuple. |
| 17 | * |
| 18 | * @tparam V - Type of the first tuple element. |
| 19 | * @tparam Types - Sequence of types for remaining elements, which must |
| 20 | * be automatically castable to V. |
| 21 | * @tparam I - Sequence of integral indexes (0...N) for each tuple elemeent. |
| 22 | * |
| 23 | * @param tuple - Tuple of N same-typed elements. |
| 24 | * @param [unnamed] - std::integer_sequence of tuple's index values. |
| 25 | * |
| 26 | * @return A std::array where each I-th element is tuple's I-th element. |
| 27 | */ |
| 28 | template <typename V, typename... Types, std::size_t... I> |
| 29 | constexpr auto tuple_to_array( |
| 30 | std::tuple<V, Types...>&& tuple, |
| 31 | std::integer_sequence<std::size_t, I...>) |
| 32 | { |
| 33 | return std::array<V, sizeof...(I) >({ std::get<I>(tuple)..., }); |
| 34 | } |
| 35 | |
| 36 | } // namespace details |
| 37 | |
| 38 | /** tuple_to_array - Create std::array from std::tuple. |
| 39 | * |
| 40 | * @tparam Types - Sequence of types of the tuple elements. |
| 41 | * @tparam I - std::integer_sequence of indexes (0..N) for each tuple element. |
| 42 | * |
| 43 | * @param tuple - Tuple of N same-typed elements. |
| 44 | * |
| 45 | * @return A std::array where each I-th element is tuple's I-th element. |
| 46 | */ |
| 47 | template <typename... Types, |
| 48 | typename I = std::make_index_sequence<sizeof...(Types)>> |
| 49 | constexpr auto tuple_to_array(std::tuple<Types...>&& tuple) |
| 50 | { |
| 51 | return details::tuple_to_array(std::move(tuple), I()); |
| 52 | } |
| 53 | |
| 54 | } // namespace utility |
| 55 | |
| 56 | } // namespace sdbusplus |