kokkos-execution 0.0.1
Loading...
Searching...
No Matches
test_lifetime.cpp
Go to the documentation of this file.
1#include "gtest/gtest.h"
2
4PRAGMA_DIAGNOSTIC_PUSH
5PRAGMA_DIAGNOSTIC_IGNORED("-Wdeprecated-declarations")
6PRAGMA_DIAGNOSTIC_IGNORED("-Wshadow")
7#include "kokkos-execution/stdexec/P3955/lifetime.hpp"
8PRAGMA_DIAGNOSTIC_POP
9
11
12namespace Tests {
13
15template <typename T> requires ( std::constructible_from<T, size_t*> && requires (T& obj) {
16 {obj.exit() } -> std::same_as<void>;
17})
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
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
41TEST(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
Inspired by the examples from p3955r2. It calls T::exit when the scope ends.
exec::enter_scope_sender auto operator()(type *storage) const noexcept