Skip to content

Commit c1cc1b9

Browse files
author
Julian LALU
committed
Add tuple piecewise construction
1 parent 891416c commit c1cc1b9

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

interface/core/containers/pair.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ namespace hud
107107
/**
108108
* Piecewise constructor for `pair` using two tuples of arguments to construct each element.
109109
* This constructor forwards the elements of the two tuples into the respective constructors of `first` and `second`.
110-
* Internally, it uses index sequences to unpack the tuple elements and delegates to the other constructor.
111110
*
112111
* @tparam u_type_t Parameter pack for constructing the `first_type`.
113112
* @tparam v_type_t Parameter pack for constructing the `second_type`.

interface/core/containers/tuple.h

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "../traits/is_not_same.h"
3232
#include "../traits/is_swappable.h"
3333
#include "../templates/tag_init.h"
34+
#include "../templates/tag_piecewise_constrcut.h"
3435

3536
namespace hud
3637
{
@@ -96,6 +97,20 @@ namespace hud
9697
static_assert(hud::is_nothrow_move_constructible_v<type_t, UType>, "type_t(UType&&) move constructor is throwable. tuple is not designed to allow throwable move constructible components");
9798
}
9899

100+
/**
101+
* Piecewise constructor.
102+
* @tparam type_t Parameter pack for constructing.
103+
* @tparam indexes Index sequence for elements to extract from the tuple.
104+
* @param tuple_type Reference to the tuple to move from.
105+
* @param ... Index sequences used to unpack the tuple elements.
106+
*/
107+
template<typename tuple_type, usize... indexes>
108+
constexpr tuple_leaf(tuple_type &tuple, hud::index_sequence<indexes...>) noexcept
109+
: content(hud::get<indexes>(hud::move(tuple))...)
110+
{
111+
static_assert(hud::is_nothrow_constructible_v<type_t, hud::get<indexes>(hud::move(tuple))...>, "type_t(hud::get<indexes>(tuple)&&...) constructor is throwable. pair is not designed to allow throwable constructible components");
112+
}
113+
99114
/**
100115
* Assigns operator.
101116
* @tparam UType type_t of other tuple_leaf parameter
@@ -132,7 +147,7 @@ namespace hud
132147
* Default constructor that calls all tuple leafs default constructors.
133148
* Value-initializes all elements, if any.
134149
*/
135-
HD_FORCEINLINE constexpr tuple_impl(tag_init_t) noexcept
150+
constexpr tuple_impl(tag_init_t) noexcept
136151
: tuple_leaf<indices, types_t>(hud::tag_init)...
137152
{
138153
}
@@ -141,7 +156,7 @@ namespace hud
141156
* Default constructor that calls all tuple leafs default constructors.
142157
* Do not initializes elements, if any.
143158
*/
144-
HD_FORCEINLINE constexpr tuple_impl() noexcept
159+
constexpr tuple_impl() noexcept
145160
: tuple_leaf<indices, types_t>()...
146161
{
147162
}
@@ -150,7 +165,7 @@ namespace hud
150165
* Initialization copy constructor that calls all tuple leafs initialization copy constructors.
151166
* @param args List of objects to copy into the tuple
152167
*/
153-
HD_FORCEINLINE constexpr tuple_impl(const types_t &...args) noexcept
168+
constexpr tuple_impl(const types_t &...args) noexcept
154169
: tuple_leaf<indices, types_t>(args)...
155170
{
156171
}
@@ -160,11 +175,24 @@ namespace hud
160175
* @param args List of objects to move into the tuple
161176
*/
162177
template<typename... u_types_t>
163-
HD_FORCEINLINE constexpr tuple_impl(u_types_t &&...args) noexcept
178+
constexpr tuple_impl(u_types_t &&...args) noexcept
164179
: tuple_leaf<indices, types_t>(hud::forward<u_types_t>(args))...
165180
{
166181
}
167182

183+
/**
184+
* Piecewise constructor for `tuple` using tuples of arguments to construct each tuple leafs.
185+
* This constructor forwards the elements of the tuples into the respective constructors.
186+
*
187+
* @param hud::tag_piecewise_construct_t Tag to indicate piecewise construction.
188+
* @param tuples Tuples containing arguments to forward to the constructor of each leafs.
189+
*/
190+
template<typename... tuple_types>
191+
constexpr tuple_impl(hud::tag_piecewise_construct_t, tuple_types... tuples) noexcept
192+
: tuple_leaf<indices, types_t>(hud::get<indices>(hud::move(tuples))...)
193+
{
194+
}
195+
168196
/** Copy constructor */
169197
constexpr tuple_impl(const tuple_impl &) = default;
170198

@@ -585,11 +613,11 @@ namespace hud
585613
}
586614

587615
/**
588-
Initialization copy constructor.
589-
This involves individually copy constructs all components, with an initialization that depends on the constructor.
590-
tuple do not accept throwable copy constructible components.
591-
@param args List of objects to copy construct into the tuple
592-
*/
616+
* Initialization copy constructor.
617+
* This involves individually copy constructs all components, with an initialization that depends on the constructor.
618+
* tuple do not accept throwable copy constructible components.
619+
* @param args List of objects to copy construct into the tuple
620+
*/
593621
explicit(hud::disjunction_v<hud::is_explicitly_copy_constructible<types_t>...>) constexpr tuple(const types_t &...args) noexcept
594622
requires(hud::conjunction_v<
595623
hud::bool_constant<sizeof...(types_t) >= 1>,
@@ -614,6 +642,19 @@ namespace hud
614642
{
615643
}
616644

645+
/**
646+
* Piecewise constructor for `tuple` using tuples of arguments to construct each element.
647+
* This constructor forwards the elements of the tuples into the respective constructors.
648+
*
649+
* @param hud::tag_piecewise_construct_t Tag to indicate piecewise construction.
650+
* @param tuples Tuples containing arguments to forward to the constructor of each element.
651+
*/
652+
template<typename... tuple_types>
653+
constexpr tuple(hud::tag_piecewise_construct_t, tuple_types... tuples) noexcept
654+
: super_type(hud::tag_piecewise_construct, hud::forward<tuple_types>(tuples)...)
655+
{
656+
}
657+
617658
/**
618659
* Initialization copy constructor from a pair.
619660
* This involves individually copy constructs pair elements in tuple, with an initialization that depends on the constructor.

0 commit comments

Comments
 (0)