Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle many error predecessors in upon_error #543

Merged
merged 1 commit into from
Jun 25, 2023
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
4 changes: 2 additions & 2 deletions include/unifex/upon_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ struct _sender<Predecessor, Func>::type {
* This helper returns type_list<type_list<Result>> if invoking func returns
* Result else if func returns void then helper returns type_list<type_list<>>
*/
template <typename Error>
template <typename... Error>
using invoked_result_t =
type_list<detail::result_overload_t<std::invoke_result_t<Func, Error>>>;
type_list<detail::result_overload_t<std::invoke_result_t<Func, Error>>...>;

public:
template <
Expand Down
96 changes: 96 additions & 0 deletions test/upon_error_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unifex/sync_wait.hpp>

#include <gtest/gtest.h>
#include <tuple>
#include <type_traits>
#include <variant>

Expand Down Expand Up @@ -87,3 +88,98 @@ TEST(UponError, ExceptionHandling) {
}
EXPECT_EQ(val, 0);
}

struct single_value_sender {

template <
template <typename...> class Variant,
template <typename...> class Tuple>
using value_types = Variant<Tuple<int>>;

template <template <typename...> class Variant>
using error_types = Variant<>;

static constexpr bool sends_done = false;

template <class Receiver>
struct operation {
friend auto tag_invoke(tag_t<start>, operation& self) noexcept {
set_value(std::move(self.receiver), 0);
}

Receiver receiver;
};

template <class Receiver>
friend auto tag_invoke(tag_t<connect>, single_value_sender, Receiver&& receiver) {
return operation<Receiver>{std::forward<Receiver>(receiver)};
}
};

TEST(UponError, ZeroErrorSender) {
auto s = single_value_sender{} | upon_error([](auto) -> double { return 0.0; });
static_assert(std::is_same_v<
decltype(s)::value_types<std::variant, std::tuple>,
std::variant<std::tuple<int>>
>);
}

struct Error1{};
struct Error2{};
struct Error3{};
struct Error4{};

struct many_error_sender {

template <
template <typename...> class Variant,
template <typename...> class Tuple>
using value_types = Variant<Tuple<double>>;

template <template <typename...> class Variant>
using error_types = Variant<Error1, Error2, Error3>;

static constexpr bool sends_done = false;

template <class Receiver>
struct operation {
friend auto tag_invoke(tag_t<start>, operation& self) noexcept {
set_error(std::move(self.receiver), Error1{});
}

Receiver receiver;
};

template <class Receiver>
friend auto tag_invoke(tag_t<connect>, many_error_sender, Receiver&& receiver) {
return operation<Receiver>{std::forward<Receiver>(receiver)};
}
};

TEST(UponError, ManyErrorSender) {
auto s = many_error_sender{} | upon_error([](auto e) {
if constexpr (std::is_same_v<decltype(e), Error3>) {
return Error4{};
} else {
return e;
}
});
static_assert(std::is_same_v<
decltype(s)::value_types<std::variant, std::tuple>,
std::variant<std::tuple<double>, std::tuple<Error1>, std::tuple<Error2>, std::tuple<Error4>>
>);
}

TEST(UponError, ManyErrorSenderIntoVoid) {
auto s = many_error_sender{} | upon_error([](auto e) {
if constexpr (std::is_same_v<decltype(e), Error3>) {
return;
} else {
return e;
}
});
static_assert(std::is_same_v<
decltype(s)::value_types<std::variant, std::tuple>,
std::variant<std::tuple<double>, std::tuple<Error1>, std::tuple<Error2>, std::tuple<>>
>);
}