Skip to content

Commit fd5d52b

Browse files
author
Julian LALU
committed
Add tuple piecewise_construct
1 parent 9a939f7 commit fd5d52b

3 files changed

Lines changed: 73 additions & 82 deletions

File tree

interface/core/containers/tuple.h

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,29 @@ namespace hud
9999
}
100100

101101
/**
102-
* Piecewise constructor.
103-
* @tparam type_t Parameter pack for constructing.
104-
* @tparam indexes Index sequence for elements to extract from the tuple.
105-
* @param tuple_type Reference to the tuple to move from.
106-
* @param ... Index sequences used to unpack the tuple elements.
102+
* Piecewise constructor forwarding a tuple of arguments.
103+
* @tparam Args Types of the elements in the tuple used for construction.
104+
* @param tuple Tuple of arguments to forward to the constructor of type_t.
107105
*/
108-
// template<typename tuple_type>
109-
// constexpr tuple_leaf(hud::tag_piecewise_construct_t, tuple_type tuple) noexcept
110-
// : content()
111-
// {
112-
// // static_assert(hud::is_nothrow_constructible_v<type_t, hud::get<indexes>(tuple)...>, "type_t(hud::get<indexes>(tuple)&&...) constructor is throwable. pair is not designed to allow throwable constructible components");
113-
// }
106+
template<typename... Args>
107+
constexpr tuple_leaf(hud::tag_piecewise_construct_t, hud::tuple<Args...> &&tuple) noexcept
108+
: tuple_leaf(hud::forward<hud::tuple<Args...>>(tuple), hud::make_index_sequence_for<Args...> {})
109+
{
110+
}
111+
112+
/**
113+
* Piecewise constructor that unpacks a tuple into individual arguments.
114+
*
115+
* @tparam Args Types of the elements in the tuple used for construction.
116+
* @tparam indices Index sequence used to extract each element from the tuple.
117+
* @param tuple Tuple whose elements are forwarded to the constructor of type_t.
118+
*/
119+
template<typename... Args, usize... indices>
120+
constexpr tuple_leaf(hud::tuple<Args...> &&tuple, hud::index_sequence<indices...>) noexcept
121+
: content(hud::get<indices>(tuple)...)
122+
{
123+
static_assert(hud::is_nothrow_constructible_v<type_t, Args...>, "type_t(Args&&...) constructor is throwable. pair is not designed to allow throwable constructible components");
124+
}
114125

115126
/**
116127
* Assigns operator.
@@ -181,36 +192,16 @@ namespace hud
181192
{
182193
}
183194

184-
// /**
185-
// * Piecewise constructor for `tuple` using tuples of arguments to construct each tuple leafs.
186-
// * This constructor forwards the elements of the tuples into the respective constructors.
187-
// *
188-
// * @param hud::tag_piecewise_construct_t Tag to indicate piecewise construction.
189-
// * @param tuples Tuples containing arguments to forward to the constructor of each leafs.
190-
// */
191-
// template<typename... tuple_types>
192-
// constexpr tuple_impl(hud::tag_piecewise_construct_t, tuple_types &&...tuples) noexcept
193-
// : tuple_leaf<indices, types_t>(tuples..., hud::make_index_sequence<hud::tuple_size_v<tuples>...> {})...
194-
// {
195-
// }
196-
197-
// template<typename... tuple_types>
198-
// constexpr tuple_impl(hud::tag_piecewise_construct_t, tuple_type tuple) noexcept
199-
// : tuple_leaf<indices, types_t>()
200-
// {
201-
// }
202-
203-
// template<typename... types_t, typename... tuple_rest>
204-
// constexpr tuple_impl(hud::tag_piecewise_construct_t, tuple_impl<types_t...> tuple, tuple_rest... rest) noexcept
205-
// : tuple_leaf<indices, types_t>(tuple, hud::make_index_sequence_for<types_t...>)...
206-
// {
207-
// }
208-
209-
// template<typename... types_t>
210-
// constexpr tuple_impl(hud::tag_piecewise_construct_t, tuple_impl<types_t...> tuple) noexcept
211-
// : tuple_leaf<indices, types_t>(tuple, hud::make_index_sequence_for<types_t...>)...
212-
// {
213-
// }
195+
/**
196+
* Piecewise constructor for `tuple` using tuples of arguments to construct each tuple leafs.
197+
* @param hud::tag_piecewise_construct_t Tag to indicate piecewise construction.
198+
* @param tuples Tuples containing arguments to forward to the constructor of each leafs.
199+
*/
200+
template<typename... tuple_types>
201+
constexpr tuple_impl(hud::tag_piecewise_construct_t, tuple_types &&...tuples) noexcept
202+
: tuple_leaf<indices, types_t>(hud::tag_piecewise_construct, hud::forward<tuple_types>(tuples))...
203+
{
204+
}
214205

215206
/** Copy constructor */
216207
constexpr tuple_impl(const tuple_impl &) = default;
@@ -661,18 +652,18 @@ namespace hud
661652
{
662653
}
663654

664-
// /**
665-
// * Piecewise constructor for `tuple` using tuples of arguments to construct each element.
666-
// * This constructor forwards the elements of the tuples into the respective constructors.
667-
// *
668-
// * @param hud::tag_piecewise_construct_t Tag to indicate piecewise construction.
669-
// * @param tuples Tuples containing arguments to forward to the constructor of each element.
670-
// */
671-
// template<typename... tuple_types>
672-
// constexpr tuple(hud::tag_piecewise_construct_t, tuple_types &&...tuples) noexcept
673-
// : super_type(hud::tag_piecewise_construct, tuples...)
674-
// {
675-
// }
655+
/**
656+
* Piecewise constructor for `tuple` using tuples of arguments to construct each element.
657+
* This constructor forwards the elements of the tuples into the respective constructors.
658+
*
659+
* @param hud::tag_piecewise_construct_t Tag to indicate piecewise construction.
660+
* @param tuples Tuples containing arguments to forward to the constructor of each element.
661+
*/
662+
template<typename... tuple_types>
663+
constexpr tuple(hud::tag_piecewise_construct_t, tuple_types &&...tuples) noexcept
664+
: super_type(hud::tag_piecewise_construct, hud::forward<tuple_types>(tuples)...)
665+
{
666+
}
676667

677668
/**
678669
* Initialization copy constructor from a pair.

test/hashmap/hashmap_destructeur.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ GTEST_TEST(hashmap, destructor_call_elements_destructors)
6464

6565
// Constant
6666
{
67-
// constexpr auto result = test();
67+
constexpr auto result = test();
6868
// hud_assert_true(std::get<0>(result));
6969
// hud_assert_true(std::get<1>(result));
7070
}

test/tuple/tuple_constructors.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,34 +2270,34 @@ GTEST_TEST(tuple, move_constructor_moveable_different_types)
22702270

22712271
GTEST_TEST(tuple, piecewise_constructor_trivial_type_same_type)
22722272
{
2273-
// using tuple_type = hud::tuple<i32, u32, i64, u64>;
2274-
2275-
// const auto test = []()
2276-
// {
2277-
// const tuple_type tuple {hud::tag_piecewise_construct, hud::forward_as_tuple(1), hud::forward_as_tuple(2), hud::forward_as_tuple(3), hud::forward_as_tuple(4)};
2278-
// return std::tuple {
2279-
// hud::get<0>(tuple) == 1,
2280-
// hud::get<1>(tuple) == 2,
2281-
// hud::get<2>(tuple) == 3,
2282-
// hud::get<3>(tuple) == 4,
2283-
// };
2284-
// };
2285-
2286-
// // Non constant
2287-
// {
2288-
// const auto result = test();
2289-
// hud_assert_true(std::get<0>(result));
2290-
// hud_assert_true(std::get<1>(result));
2291-
// hud_assert_true(std::get<2>(result));
2292-
// hud_assert_true(std::get<3>(result));
2293-
// }
2273+
using tuple_type = hud::tuple<i32, u32, i64, u64>;
2274+
2275+
const auto test = []()
2276+
{
2277+
const tuple_type tuple {hud::tag_piecewise_construct, hud::forward_as_tuple(1), hud::forward_as_tuple(2), hud::forward_as_tuple(3), hud::forward_as_tuple(4)};
2278+
return std::tuple {
2279+
hud::get<0>(tuple) == 1,
2280+
hud::get<1>(tuple) == 2,
2281+
hud::get<2>(tuple) == 3,
2282+
hud::get<3>(tuple) == 4,
2283+
};
2284+
};
2285+
2286+
// Non constant
2287+
{
2288+
const auto result = test();
2289+
hud_assert_true(std::get<0>(result));
2290+
hud_assert_true(std::get<1>(result));
2291+
hud_assert_true(std::get<2>(result));
2292+
hud_assert_true(std::get<3>(result));
2293+
}
22942294

22952295
// Constant
2296-
// {
2297-
// constexpr auto result = test();
2298-
// hud_assert_true(std::get<0>(result));
2299-
// hud_assert_true(std::get<1>(result));
2300-
// hud_assert_true(std::get<2>(result));
2301-
// hud_assert_true(std::get<3>(result));
2302-
// }
2296+
{
2297+
constexpr auto result = test();
2298+
hud_assert_true(std::get<0>(result));
2299+
hud_assert_true(std::get<1>(result));
2300+
hud_assert_true(std::get<2>(result));
2301+
hud_assert_true(std::get<3>(result));
2302+
}
23032303
}

0 commit comments

Comments
 (0)