Skip to content

Commit d14eb2f

Browse files
author
Julian LALU
committed
Add Pair trivial and non trivial destructors and destructor test
1 parent c784d69 commit d14eb2f

3 files changed

Lines changed: 74 additions & 13 deletions

File tree

interface/core/containers/pair.h

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
namespace hud
3434
{
35-
3635
/**
3736
* This class couples together a pair of components, which may be of different types (first_type and second_type). The individual components can be accessed through its public members first and second.
3837
* pair is a particular case of Tuple.
@@ -58,8 +57,7 @@ namespace hud
5857
* If one of the component is explicitly default constructible, the pair is explicitly default constructible.
5958
* pair do not accept throwable default constructible components.
6059
*/
61-
constexpr explicit(!(hud::is_implicitly_default_constructible_v<first_type> && hud::is_implicitly_default_constructible_v<second_type>))
62-
pair() noexcept
60+
constexpr explicit(!(hud::is_implicitly_default_constructible_v<first_type> && hud::is_implicitly_default_constructible_v<second_type>)) pair() noexcept
6361
: first()
6462
, second()
6563
{
@@ -76,8 +74,7 @@ namespace hud
7674
* @param f An object of the same type as 'first' or some other type implicitly convertible to it.
7775
* @param s An object of the same type as 'second' or some other type implicitly convertible to it.
7876
*/
79-
constexpr explicit(!(hud::is_convertible_v<const first_type &, first_type> && hud::is_convertible_v<const second_type &, second_type>))
80-
pair(const first_type &f, const second_type &s) noexcept
77+
constexpr explicit(!(hud::is_convertible_v<const first_type &, first_type> && hud::is_convertible_v<const second_type &, second_type>)) pair(const first_type &f, const second_type &s) noexcept
8178
requires(hud::is_copy_constructible_v<first_type> && hud::is_copy_constructible_v<second_type>)
8279
: first(f)
8380
, second(s)
@@ -99,8 +96,7 @@ namespace hud
9996
*/
10097
template<typename u_type_t = first_type, typename v_type_t = second_type>
10198
requires(hud::is_move_constructible_v<first_type, u_type_t> && hud::is_move_constructible_v<second_type, v_type_t>)
102-
constexpr explicit(!(hud::is_convertible_v<u_type_t &&, first_type> && hud::is_convertible_v<v_type_t &&, second_type>))
103-
pair(u_type_t &&f, v_type_t &&s) noexcept
99+
constexpr explicit(!(hud::is_convertible_v<u_type_t &&, first_type> && hud::is_convertible_v<v_type_t &&, second_type>)) pair(u_type_t &&f, v_type_t &&s) noexcept
104100
: first(hud::forward<u_type_t>(f))
105101
, second(hud::forward<v_type_t>(s))
106102
{
@@ -116,8 +112,7 @@ namespace hud
116112
* pair does not accept throwable copy constructible components.
117113
* @param other Another pair object.
118114
*/
119-
constexpr explicit(!(hud::is_convertible_v<const first_type &, first_type> && hud::is_convertible_v<const second_type &, second_type>))
120-
pair(const pair &other) noexcept
115+
constexpr explicit(!(hud::is_convertible_v<const first_type &, first_type> && hud::is_convertible_v<const second_type &, second_type>)) pair(const pair &other) noexcept
121116
requires(hud::is_nothrow_copy_constructible_v<first_type> && hud::is_nothrow_copy_constructible_v<second_type>)
122117
= default;
123118

@@ -132,8 +127,7 @@ namespace hud
132127
*/
133128
template<typename u_type_t, typename v_type_t>
134129
requires(hud::is_copy_constructible_v<first_type, u_type_t> && hud::is_copy_constructible_v<second_type, v_type_t>)
135-
constexpr explicit(!(hud::is_convertible_v<const u_type_t &, first_type> && hud::is_convertible_v<const v_type_t &, second_type>))
136-
pair(const pair<u_type_t, v_type_t> &other) noexcept
130+
constexpr explicit(!(hud::is_convertible_v<const u_type_t &, first_type> && hud::is_convertible_v<const v_type_t &, second_type>)) pair(const pair<u_type_t, v_type_t> &other) noexcept
137131
: first(other.first)
138132
, second(other.second)
139133
{
@@ -165,8 +159,7 @@ namespace hud
165159
*/
166160
template<typename u_type_t, typename v_type_t>
167161
requires(hud::is_move_constructible_v<first_type, u_type_t> && hud::is_move_constructible_v<second_type, v_type_t>)
168-
constexpr explicit(!(hud::is_convertible_v<u_type_t, first_type> && hud::is_convertible_v<v_type_t, second_type>))
169-
pair(pair<u_type_t, v_type_t> &&other) noexcept
162+
constexpr explicit(!(hud::is_convertible_v<u_type_t, first_type> && hud::is_convertible_v<v_type_t, second_type>)) pair(pair<u_type_t, v_type_t> &&other) noexcept
170163
: first(hud::forward<u_type_t>(other.first))
171164
, second(hud::forward<v_type_t>(other.second))
172165
{
@@ -191,6 +184,22 @@ namespace hud
191184
return *this;
192185
}
193186

187+
/**
188+
* Destructor; Trivial if first_type and second_type are trivially destructible
189+
* This call first_type and second_type destructors.
190+
*/
191+
constexpr ~pair()
192+
requires(std::is_trivially_destructible_v<first_type> && std::is_trivially_destructible_v<second_type>)
193+
= default;
194+
195+
/**
196+
* Destructor; Trivial if first_type and second_type are trivially destructible
197+
* This call first_type and second_type destructors.
198+
*/
199+
constexpr ~pair()
200+
requires(!(std::is_trivially_destructible_v<first_type> && std::is_trivially_destructible_v<second_type>))
201+
= default;
202+
194203
/**
195204
* Assigns other as the new content for the pair object.
196205
* Perform copy assignments, with the elements of its argument preserving their values after the call.

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ set( src
112112
pair/pair_assignments.cpp
113113
pair/pair_comparison.cpp
114114
pair/pair_constructors.cpp
115+
pair/pair_destructor.cpp
115116
pair/pair_misc.cpp
116117
pair/pair_swap.cpp
117118
shared_pointer/multi_thread/shared_pointer_multi_thread_assignments.cpp

test/pair/pair_destructor.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <core/containers/pair.h>
2+
3+
GTEST_TEST(pair, destructor_call_inner_components_destructors)
4+
{
5+
using first_type = hud_test::SetBoolToTrueWhenDestroyed;
6+
using second_type = hud_test::SetBoolToTrueWhenDestroyed;
7+
8+
const auto test = []()
9+
{
10+
i32 destructor_counts[2] = {0};
11+
bool destructor_first_ok = true;
12+
bool destructor_second_ok = true;
13+
{
14+
hud::pair<first_type, second_type> pair {&destructor_counts[0], &destructor_counts[1]};
15+
destructor_first_ok &= (destructor_counts[0] == 0);
16+
destructor_second_ok &= (destructor_counts[1] == 0);
17+
}
18+
destructor_first_ok &= (destructor_counts[0] == 1);
19+
destructor_second_ok &= (destructor_counts[1] == 1);
20+
return std::tuple {destructor_first_ok, destructor_second_ok};
21+
};
22+
23+
// Non constant
24+
{
25+
const auto result = test();
26+
hud_assert_true(std::get<0>(result));
27+
hud_assert_true(std::get<1>(result));
28+
}
29+
30+
// Constant
31+
{
32+
constexpr auto result = test();
33+
hud_assert_true(std::get<0>(result));
34+
hud_assert_true(std::get<1>(result));
35+
}
36+
}
37+
38+
GTEST_TEST(pair, pair_is_trivially_destructible_if_inner_types_are_trivially_destructible)
39+
{
40+
static_assert(hud::is_trivially_destructible_v<hud::pair<i32, i64>>);
41+
hud_assert_true((hud::is_trivially_destructible_v<hud::pair<i32, i64>>));
42+
43+
static_assert(!hud::is_trivially_destructible_v<hud::pair<hud_test::non_bitwise_type, i64>>);
44+
hud_assert_true((!hud::is_trivially_destructible_v<hud::pair<hud_test::non_bitwise_type, i64>>));
45+
46+
static_assert(!hud::is_trivially_destructible_v<hud::pair<i32, hud_test::non_bitwise_type>>);
47+
hud_assert_true((!hud::is_trivially_destructible_v<hud::pair<i32, hud_test::non_bitwise_type>>));
48+
49+
static_assert(!hud::is_trivially_destructible_v<hud::pair<hud_test::non_bitwise_type, hud_test::non_bitwise_type>>);
50+
hud_assert_true((!hud::is_trivially_destructible_v<hud::pair<hud_test::non_bitwise_type, hud_test::non_bitwise_type>>));
51+
}

0 commit comments

Comments
 (0)