blob: b5ab2a5d55bf7b233347221ad74d02a0373e1096 [file] [log] [blame]
Patrick Williams4dbdef72016-07-20 12:36:56 -05001#pragma once
2
3#include <utility>
4#include <tuple>
5#include <array>
6
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>
29constexpr 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 */
47template <typename... Types,
48 typename I = std::make_index_sequence<sizeof...(Types)>>
49constexpr 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