blob: dfaf60a24ecf8ff431505ed4548bc9750327d290 [file] [log] [blame]
Patrick Williams4dbdef72016-07-20 12:36:56 -05001#pragma once
2
Patrick Williams4dbdef72016-07-20 12:36:56 -05003#include <array>
Patrick Venture95269db2018-08-31 09:19:17 -07004#include <tuple>
5#include <utility>
Patrick Williams4dbdef72016-07-20 12:36:56 -05006
7namespace sdbusplus
8{
9
10namespace utility
11{
12
13namespace 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 */
28template <typename V, typename... Types, std::size_t... I>
Andrew Geissler072da3e2018-01-18 07:21:42 -080029constexpr auto tuple_to_array(std::tuple<V, Types...>&& tuple,
30 std::integer_sequence<std::size_t, I...>)
Patrick Williams4dbdef72016-07-20 12:36:56 -050031{
Andrew Geissler072da3e2018-01-18 07:21:42 -080032 return std::array<V, sizeof...(I)>({
33 std::get<I>(tuple)...,
34 });
Patrick Williams4dbdef72016-07-20 12:36:56 -050035}
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 */
48template <typename... Types,
49 typename I = std::make_index_sequence<sizeof...(Types)>>
50constexpr 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