kokkos-execution
0.0.1
Toggle main menu visibility
Loading...
Searching...
No Matches
test_lifetime.cpp
Go to the documentation of this file.
1
#include "gtest/gtest.h"
2
3
#include "
kokkos-execution/utils/ignore_warnings.hpp
"
4
PRAGMA_DIAGNOSTIC_PUSH
5
PRAGMA_DIAGNOSTIC_IGNORED(
"-Wdeprecated-declarations"
)
6
PRAGMA_DIAGNOSTIC_IGNORED(
"-Wshadow"
)
7
#include "kokkos-execution/stdexec/P3955/lifetime.hpp"
8
PRAGMA_DIAGNOSTIC_POP
9
10
#include "
tests/utils/sink_receiver.hpp
"
11
12
namespace
Tests
{
13
15
template
<
typename
T>
requires
( std::constructible_from<T, size_t*> &&
requires
(T& obj) {
16
{obj.exit() } -> std::same_as<void>;
17
})
18
struct
scope_object_with_exit
{
19
using
type
= T;
20
21
size_t
*
counter
;
22
23
exec::enter_scope_sender
auto
operator()
(
type
* storage)
const
noexcept
{
24
return
stdexec::just() | stdexec::then([
counter
=
counter
, storage = storage]() {
25
auto
* ptr = std::construct_at(storage,
counter
);
26
return
stdexec::just() | stdexec::then([ptr]()
noexcept
{ ptr->exit(); });
27
});
28
}
29
};
30
31
struct
DoSomethingOnExit
{
32
size_t
*
counter
;
33
34
void
exit
() noexcept {
35
std::printf(
"I just reached the finish line of my lifetime.\n"
);
// NOLINT(modernize-use-std-print)
36
++*
counter
;
37
}
38
};
39
41
TEST(lifetime,
scope_object_with_exit
) {
42
size_t
counter = 0;
43
44
stdexec::sync_wait(
45
exec::lifetime(
46
[&counter](
DoSomethingOnExit
&) {
47
EXPECT_EQ(counter, 0);
48
return
stdexec::just();
49
},
50
scope_object_with_exit<DoSomethingOnExit>
{.counter = std::addressof(counter)}));
51
52
ASSERT_EQ(counter, 1);
53
}
54
55
}
// namespace Tests
ignore_warnings.hpp
Tests
Definition
test_any_sender.cpp:39
sink_receiver.hpp
Tests::DoSomethingOnExit
Definition
test_lifetime.cpp:31
Tests::DoSomethingOnExit::counter
size_t * counter
Definition
test_lifetime.cpp:32
Tests::DoSomethingOnExit::exit
void exit() noexcept
Definition
test_lifetime.cpp:34
Tests::scope_object_with_exit
Inspired by the examples from p3955r2. It calls T::exit when the scope ends.
Definition
test_lifetime.cpp:18
Tests::scope_object_with_exit::counter
size_t * counter
Definition
test_lifetime.cpp:21
Tests::scope_object_with_exit::operator()
exec::enter_scope_sender auto operator()(type *storage) const noexcept
Definition
test_lifetime.cpp:23
Tests::scope_object_with_exit::type
T type
Definition
test_lifetime.cpp:19
tests
stdexec
test_lifetime.cpp
Generated on
for kokkos-execution by
1.18.0