Nebula
Loading...
Searching...
No Matches
tupleutility.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
9//------------------------------------------------------------------------------
10#include <tuple>
11namespace Util
12{
13
14//------------------------------------------------------------------------------
18template <typename... T>
19using Tuple = std::tuple<T...>;
20
21
22//------------------------------------------------------------------------------
25template <typename... T>
26constexpr Tuple<T...>
27MakeTuple(const T&... args)
28{
29 return std::make_tuple(args...);
30}
31
32//------------------------------------------------------------------------------
35template <int INDEX, typename... T>
36constexpr const std::tuple_element_t<INDEX, std::tuple<T...>>&
37Get(const Tuple<T...>& tuple)
38{
39 return std::get<INDEX>(tuple);
40}
41
42//------------------------------------------------------------------------------
46template <typename A, typename B>
47using Pair = std::pair<A, B>;
48
49//------------------------------------------------------------------------------
52template <typename A, typename B>
53constexpr Pair<A, B>
54MakePair(const A& a, const B& b)
55{
56 return std::make_pair(a, b);
57}
58
59//------------------------------------------------------------------------------
62template <int INDEX, typename A, typename B>
63constexpr const std::tuple_element_t<INDEX, std::tuple<A, B>>&
64Get(const Pair<A, B>& pair)
65{
66 static_assert(INDEX == 0 || INDEX == 1, "Index has to be either 0 or 1");
67 return std::get<INDEX>(pair);
68}
69
70template <typename C>
72
73//------------------------------------------------------------------------------
77template <template <typename> class C, typename T>
79{
80 using type = T;
81};
82
83//------------------------------------------------------------------------------
87template <template <typename, int> class C, typename T, int I>
88struct get_template_type<C<T, I>>
89{
90 using type = T;
91};
92
93
94//------------------------------------------------------------------------------
98template <template <int, typename> class C, int I, typename T>
99struct get_template_type<C<I, T>>
100{
101 using type = T;
102};
103
104//------------------------------------------------------------------------------
108template <template <typename> class C, typename T>
109struct get_template_type<const C<T>&>
110{
111 using type = T;
112};
113
114//------------------------------------------------------------------------------
118template <template <int, typename> class C, int I, typename T>
119struct get_template_type<C<I, T>&>
120{
121 using type = T;
122};
123
124//------------------------------------------------------------------------------
128template <template <typename, int> class C, typename T, int I>
129struct get_template_type<C<T, I>&>
130{
131 using type = T;
132};
133
134//------------------------------------------------------------------------------
138template <typename C>
140
141//------------------------------------------------------------------------------
145template<class...Ts, std::size_t...Is> void
146alloc_for_each_in_tuple(std::tuple<Ts...>& tuple, std::index_sequence<Is...>)
147{
148 (std::get<Is>(tuple).Append(typename get_template_type<Ts>::type()), ...);
149}
150
151//------------------------------------------------------------------------------
155template<class...Ts> void
156alloc_for_each_in_tuple(std::tuple<Ts...>& tuple)
157{
158 alloc_for_each_in_tuple(tuple, std::make_index_sequence<sizeof...(Ts)>());
159}
160
161//------------------------------------------------------------------------------
165template<class...Ts, std::size_t...Is> void
166clear_for_each_in_tuple(std::tuple<Ts...>& tuple, std::index_sequence<Is...>)
167{
168 (std::get<Is>(tuple).Clear(), ...);
169}
170
171//------------------------------------------------------------------------------
175template<class...Ts> void
176clear_for_each_in_tuple(std::tuple<Ts...>& tuple)
177{
178 clear_for_each_in_tuple(tuple, std::make_index_sequence<sizeof...(Ts)>());
179}
180
181//------------------------------------------------------------------------------
185template <class...Ts, std::size_t...Is> void
186move_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t to, uint32_t from, std::index_sequence<Is...>)
187{
188 ((std::get<Is>(tuple)[to] = std::get<Is>(tuple)[from]), ...);
189}
190
191//------------------------------------------------------------------------------
195template <class...Ts> void
196move_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t to, uint32_t from)
197{
198 move_for_each_in_tuple(tuple, to, from, std::make_index_sequence<sizeof...(Ts)>());
199}
200
201//------------------------------------------------------------------------------
206template <class...Ts, std::size_t...Is> void
207erase_index_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t i, std::index_sequence<Is...>)
208{
209 (std::get<Is>(tuple).EraseIndex(i), ...);
210}
211
212//------------------------------------------------------------------------------
217template <class...Ts> void
218erase_index_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t i)
219{
220 erase_index_for_each_in_tuple(tuple, i, std::make_index_sequence<sizeof...(Ts)>());
221}
222
223//------------------------------------------------------------------------------
230template <class...Ts, std::size_t...Is> void
231erase_index_swap_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t i, std::index_sequence<Is...>)
232{
233 (std::get<Is>(tuple).EraseIndexSwap(i), ...);
234}
235
236//------------------------------------------------------------------------------
243template <class...Ts> void
244erase_index_swap_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t i)
245{
246 erase_index_swap_for_each_in_tuple(tuple, i, std::make_index_sequence<sizeof...(Ts)>());
247}
248
249//------------------------------------------------------------------------------
252template <class...Ts, std::size_t...Is> void
253erase_range_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t start, uint32_t end, std::index_sequence<Is...>)
254{
255 (std::get<Is>(tuple).EraseRange(start, end), ...);
256}
257
258//------------------------------------------------------------------------------
263template <class...Ts> void
264erase_range_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t start, uint32_t end)
265{
266 erase_range_for_each_in_tuple(tuple, start, end, std::make_index_sequence<sizeof...(Ts)>());
267}
268
269//------------------------------------------------------------------------------
273template <class...Ts, std::size_t...Is, class...TYPES> void
274set_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t i, std::index_sequence<Is...>, TYPES const& ... values)
275{
276 ((std::get<Is>(tuple)[i] = values), ...);
277}
278
279//------------------------------------------------------------------------------
283template <class...Ts, class...TYPES> void
284set_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t i, TYPES const& ... values)
285{
286 set_for_each_in_tuple(tuple, i, std::make_index_sequence<sizeof...(Ts)>(), values...);
287}
288
289//------------------------------------------------------------------------------
293template <class...Ts, std::size_t...Is> void
294reserve_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t size, std::index_sequence<Is...>)
295{
296 (std::get<Is>(tuple).Reserve(size), ...);
297}
298
299//------------------------------------------------------------------------------
303template <class...Ts> void
304reserve_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t size)
305{
306 reserve_for_each_in_tuple(tuple, size, std::make_index_sequence<sizeof...(Ts)>());
307}
308
309//------------------------------------------------------------------------------
313template <class...Ts, std::size_t...Is> void
314set_size_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t size, std::index_sequence<Is...>)
315{
316 (std::get<Is>(tuple).SetSize(size), ...);
317}
318
319//------------------------------------------------------------------------------
323template <class...Ts> void
324set_size_for_each_in_tuple(std::tuple<Ts...>& tuple, uint32_t size)
325{
326 set_size_for_each_in_tuple(tuple, size, std::make_index_sequence<sizeof...(Ts)>());
327}
328
329//------------------------------------------------------------------------------
333template <int MEMBER, class ... TYPES>
335}
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
void move_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t to, uint32_t from, std::index_sequence< Is... >)
Entry point for moving an element between two indices.
Definition tupleutility.h:186
void erase_range_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t start, uint32_t end, std::index_sequence< Is... >)
Definition tupleutility.h:253
void erase_index_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t i, std::index_sequence< Is... >)
Entry point for erasing an element.
Definition tupleutility.h:207
void reserve_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t size, std::index_sequence< Is... >)
Entry point for reserving in each array.
Definition tupleutility.h:294
constexpr Pair< A, B > MakePair(const A &a, const B &b)
Definition tupleutility.h:54
void alloc_for_each_in_tuple(std::tuple< Ts... > &tuple, std::index_sequence< Is... >)
Unpacks allocations for each member in a tuble.
Definition tupleutility.h:146
void clear_for_each_in_tuple(std::tuple< Ts... > &tuple, std::index_sequence< Is... >)
Unpacks allocations for each member in a tuple.
Definition tupleutility.h:166
constexpr const std::tuple_element_t< INDEX, std::tuple< T... > > & Get(const Tuple< T... > &tuple)
Definition tupleutility.h:37
constexpr Tuple< T... > MakeTuple(const T &... args)
Definition tupleutility.h:27
void set_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t i, std::index_sequence< Is... >, TYPES const &... values)
Entry point for setting values in each array at an index.
Definition tupleutility.h:274
get_template_type_t< std::tuple_element_t< MEMBER, std::tuple< Util::Array< TYPES >... > > > tuple_array_t
Get type of contained element in Util::Array stored in std::tuple.
Definition tupleutility.h:334
void erase_index_swap_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t i, std::index_sequence< Is... >)
Entry point for erasing an element by swapping with the last and reducing size.
Definition tupleutility.h:231
std::tuple< T... > Tuple
Wrap std::tuple.
Definition tupleutility.h:19
void set_size_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t size, std::index_sequence< Is... >)
Entry point for reserving in each array.
Definition tupleutility.h:314
typename get_template_type< C >::type get_template_type_t
Helper typedef so that the above expression can be used like decltype.
Definition tupleutility.h:139
std::pair< A, B > Pair
Wrap std::pair.
Definition tupleutility.h:47
T type
Definition tupleutility.h:101
T type
Definition tupleutility.h:121
T type
Definition tupleutility.h:90
T type
Definition tupleutility.h:131
T type
Definition tupleutility.h:80
T type
Definition tupleutility.h:111
Definition tupleutility.h:71