Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/exec/at_coroutine_exit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ namespace experimental::execution
public:
template <class _Action, class... _Ts>
requires __callable<__decay_t<_Action>, __decay_t<_Ts>...>
auto operator()(_Action&& __action, _Ts&&... __ts) const -> __task<_Ts...>
auto operator()(_Action&& __action, _Ts&&... __ts) const -> __task<__decay_t<_Ts>...>
{
return __co_impl(static_cast<_Action&&>(__action), static_cast<_Ts&&>(__ts)...);
}
Expand Down
2 changes: 1 addition & 1 deletion include/exec/on_coro_disposition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ namespace experimental::execution
public:
template <class _Action, class... _Ts>
requires __callable<__decay_t<_Action>, __decay_t<_Ts>...>
auto operator()(_Action&& __action, _Ts&&... __ts) const -> __task<_Ts...>
auto operator()(_Action&& __action, _Ts&&... __ts) const -> __task<__decay_t<_Ts>...>
{
return __impl(static_cast<_Action&&>(__action), static_cast<_Ts&&>(__ts)...);
}
Expand Down
83 changes: 83 additions & 0 deletions test/exec/test_at_coroutine_exit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
# include "../test_common/require_terminate.hpp"
# include "../test_common/schedulers.hpp"

# include <memory>
# include <type_traits>
# include <utility>

using namespace exec;
using STDEXEC::sync_wait;

Expand Down Expand Up @@ -271,6 +275,46 @@ namespace
i *= i;
}

template <class Cleanup>
auto test_cleanup_action_with_lvalue_arguments(Cleanup cleanup,
int& result,
task_disposition disposition) -> task<void>
{
int argument = 3;
int const const_argument = 4;
auto action = [&result](int&& i, int&& j, std::unique_ptr<int>&& p) -> task<void>
{
result = i + j + *p;
co_return;
};

auto payload = std::make_unique<int>(5);
auto&& [i, j, p] = co_await cleanup(action, argument, const_argument, std::move(payload));
CHECK_FALSE(payload);
STATIC_REQUIRE(std::is_same_v<decltype(i), int&>);
STATIC_REQUIRE(std::is_same_v<decltype(j), int&>);
STATIC_REQUIRE(std::is_same_v<decltype(p), std::unique_ptr<int>&>);
CHECK(i == 3);
CHECK(j == 4);
CHECK(*p == 5);
CHECK(result == 0);

argument = 100;
CHECK(i == 3);
i = 6;
j = 7;
*p = 8;

if (disposition == task_disposition::stopped)
{
co_await stop();
}
else if (disposition == task_disposition::failed)
{
throw 42;
}
}

auto with_continuation(int& result, task<void> next) -> task<void>
{
co_await std::move(next);
Expand Down Expand Up @@ -498,6 +542,45 @@ namespace
REQUIRE(result == 10);
}

TEMPLATE_TEST_CASE("CoroutineCleanupWithLvalueArguments",
"[task][at_coroutine_exit]",
decltype(at_coroutine_exit),
decltype(on_coroutine_succeeded),
decltype(on_coroutine_stopped),
decltype(on_coroutine_failed))
{
int result = 0;
constexpr bool always_runs = std::is_same_v<TestType, decltype(at_coroutine_exit)>;

SECTION("success")
{
REQUIRE(sync_wait(test_cleanup_action_with_lvalue_arguments(TestType{},
result,
task_disposition::succeeded)));
constexpr bool runs = always_runs
|| std::is_same_v<TestType, decltype(on_coroutine_succeeded)>;
CHECK(result == (runs ? 21 : 0));
}

SECTION("stop")
{
CHECK_FALSE(sync_wait(
test_cleanup_action_with_lvalue_arguments(TestType{}, result, task_disposition::stopped)));
constexpr bool runs = always_runs || std::is_same_v<TestType, decltype(on_coroutine_stopped)>;
CHECK(result == (runs ? 21 : 0));
}

SECTION("error")
{
CHECK_THROWS_AS(
sync_wait(
test_cleanup_action_with_lvalue_arguments(TestType{}, result, task_disposition::failed)),
int);
constexpr bool runs = always_runs || std::is_same_v<TestType, decltype(on_coroutine_failed)>;
CHECK(result == (runs ? 21 : 0));
}
}

# ifdef REQUIRE_TERMINATE

TEST_CASE("CancelInCleanupActionCallsTerminate", "[task][at_coroutine_exit]")
Expand Down
Loading