Skip to content

Commit

Permalink
Add actionize().
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadden committed Sep 19, 2020
1 parent 4800982 commit 4be79e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/alia/flow/actions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,24 @@ lambda_action(Perform perform)
return lambda_action([]() { return true; }, perform);
}

// actionize(x) returns the action form of x (if it isn't already one).
// Specifically, if x is a callable object, this returns lambda_action(x).
// If x is an action, this returns x itself.
template<class Action>
std::enable_if_t<is_action_type<Action>::value, Action>
actionize(Action x)
{
return x;
}
template<
class Callable,
std::enable_if_t<!is_action_type<Callable>::value, int> = 0>
auto
actionize(Callable x)
{
return lambda_action(std::move(x));
}

} // namespace alia

#endif
18 changes: 18 additions & 0 deletions unit_tests/flow/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,21 @@ TEST_CASE("action binding", "[flow][actions]")
REQUIRE(!(lambda_action([]() { return false; }, [&](int) {}) << value(0))
.is_ready());
}

TEST_CASE("actionize an action", "[flow][actions]")
{
int x = 0;
auto a = actionize(direct(x) <<= 1);
REQUIRE(a.is_ready());
perform_action(a);
REQUIRE(x == 1);
}

TEST_CASE("actionize a lambda", "[flow][actions]")
{
int x = 0;
auto a = actionize([&] { x = 1; });
REQUIRE(a.is_ready());
perform_action(a);
REQUIRE(x == 1);
}

0 comments on commit 4be79e7

Please sign in to comment.