Patrick Williams | 4dbdef7 | 2016-07-20 12:36:56 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Williams | 4dbdef7 | 2016-07-20 12:36:56 -0500 | [diff] [blame] | 3 | #include <array> |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 4 | #include <tuple> |
| 5 | #include <utility> |
Patrick Williams | 4dbdef7 | 2016-07-20 12:36:56 -0500 | [diff] [blame] | 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> |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 29 | constexpr auto tuple_to_array(std::tuple<V, Types...>&& tuple, |
| 30 | std::integer_sequence<std::size_t, I...>) |
Patrick Williams | 4dbdef7 | 2016-07-20 12:36:56 -0500 | [diff] [blame] | 31 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 32 | return std::array<V, sizeof...(I)>({ |
| 33 | std::get<I>(tuple)..., |
| 34 | }); |
Patrick Williams | 4dbdef7 | 2016-07-20 12:36:56 -0500 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | } // namespace details |
| 38 | |
| 39 | /** tuple_to_array - Create std::array from std::tuple. |
| 40 | * |
| 41 | * @tparam Types - Sequence of types of the tuple elements. |
| 42 | * @tparam I - std::integer_sequence of indexes (0..N) for each tuple element. |
| 43 | * |
| 44 | * @param tuple - Tuple of N same-typed elements. |
| 45 | * |
| 46 | * @return A std::array where each I-th element is tuple's I-th element. |
| 47 | */ |
| 48 | template <typename... Types, |
| 49 | typename I = std::make_index_sequence<sizeof...(Types)>> |
| 50 | constexpr auto tuple_to_array(std::tuple<Types...>&& tuple) |
| 51 | { |
| 52 | return details::tuple_to_array(std::move(tuple), I()); |
| 53 | } |
| 54 | |
| 55 | } // namespace utility |
| 56 | |
| 57 | } // namespace sdbusplus |