Dune Core Modules (unstable)

tuplevector.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_COMMON_TUPLEVECTOR_HH
6#define DUNE_COMMON_TUPLEVECTOR_HH
7
8#include <tuple>
9#include <utility>
10
11#include <dune/common/indices.hh>
13#include <dune/common/std/type_traits.hh>
14
15
16
23namespace Dune
24{
25
26
27
33template<class... T>
34class TupleVector : public std::tuple<T...>
35{
36 using Base = std::tuple<T...>;
37
38public:
39
46 template<class... TT,
47 class = std::void_t<decltype(Base(std::declval<TT&&>()...))>>
48 constexpr TupleVector(TT&&... tt) :
49 Base(std::forward<TT>(tt)...)
50 {}
51
54 constexpr TupleVector()
55 {}
56
59 template<std::size_t i,
60 std::enable_if_t<(i < sizeof...(T)), int> = 0>
61 constexpr decltype(auto) operator[](const Dune::index_constant<i>&) const
62 {
63 return std::get<i>(*this);
64 }
65
68 template<std::size_t i,
69 std::enable_if_t<(i < sizeof...(T)), int> = 0>
70 decltype(auto) operator[](const Dune::index_constant<i>&)
71 {
72 return std::get<i>(*this);
73 }
74
76 static constexpr std::size_t size()
77 {
78 return std::tuple_size<Base>::value;
79 }
80};
81
85
86template<class... T>
87TupleVector(T...) -> TupleVector<T...>;
88
89template<class T1, class T2>
90TupleVector(std::pair<T1, T2>) -> TupleVector<T1, T2>;
91
92template<class... T>
93TupleVector(std::tuple<T...>) -> TupleVector<T...>;
94
95template <class Alloc, class... T>
96TupleVector(std::allocator_arg_t, Alloc, T...) -> TupleVector<T...>;
97
98template<class Alloc, class T1, class T2>
99TupleVector(std::allocator_arg_t, Alloc, std::pair<T1, T2>) -> TupleVector<T1, T2>;
100
101template<class Alloc, class... T>
102TupleVector(std::allocator_arg_t, Alloc, std::tuple<T...>) -> TupleVector<T...>;
103
105
106
107template<class... T>
108constexpr auto makeTupleVector(T&&... t)
109{
110 // The std::decay_t<T> is is a slight simplification,
111 // because std::reference_wrapper needs special care.
112 return TupleVector<std::decay_t<T>...>(std::forward<T>(t)...);
113}
114
115
116
117} // namespace Dune
118
119namespace std
120{
125 template <size_t i, typename... Args>
126 struct tuple_element<i,Dune::TupleVector<Args...> >
127 {
128 using type = typename std::tuple_element<i, std::tuple<Args...> >::type;
129 };
130
135 template <typename... Args>
136 struct tuple_size<Dune::TupleVector<Args...> >
137 : std::integral_constant<std::size_t, sizeof...(Args)>
138 {};
139}
140
141#endif // DUNE_COMMON_TUPLEVECTOR_HH
A class augmenting std::tuple by element access via operator[].
Definition: tuplevector.hh:35
static constexpr std::size_t size()
Number of elements of the tuple.
Definition: tuplevector.hh:76
constexpr TupleVector(TT &&... tt)
Construct from a set of arguments.
Definition: tuplevector.hh:48
constexpr TupleVector()
Default constructor.
Definition: tuplevector.hh:54
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:29
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)