Skip to content

Commit

Permalink
Add noop and unready actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadden committed Sep 18, 2020
1 parent 20a20ae commit 127e537
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
26 changes: 25 additions & 1 deletion docs/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ found in
Action 'Library'
----------------

alia provides a small 'library' of actions that are generally useful:
alia provides a small library of actions that are generally useful:

<dl>

Expand All @@ -205,6 +205,30 @@ the back of `container`.

</dd>

<dt>noop_action()</dt><dd>

`noop_action()` creates an action that is always ready to perform but does
nothing.

This can be useful when you are mocking up a UI and aren't ready to fill in
real actions.

You can optionally provide type parameters to indicate the types of the
action's parameters. e.g., `noop_action<int>()` creates an action that takes a
single parameter of type `int`.

</dd>

<dt>unready_action()</dt><dd>

`unready_action()` creates an action that is never ready to perform.

Similar to `noop_action`, you can provide optional type parameters.
`unready_action<int>()` creates an action that takes a single parameter of type
`int`.

</dd>

</dl>

'Consuming' Actions
Expand Down
57 changes: 57 additions & 0 deletions src/alia/flow/actions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,63 @@ operator<<=(Sink sink, Source source)
return sink <<= value(source);
}

// The noop_action is always ready to perform but does nothing.
template<class... Args>
struct noop_action_type : action_interface<Args...>
{
noop_action_type()
{
}

bool
is_ready() const
{
return true;
}

void
perform(function_view<void()> const& intermediary, Args...) const
{
intermediary();
}
};
template<class... Args>
noop_action_type<Args...>
noop_action()
{
return noop_action_type<Args...>();
}

// The unready_action is never ready to perform.
template<class... Args>
struct unready_action_type : action_interface<Args...>
{
unready_action_type()
{
}

bool
is_ready() const
{
return false;
}

// LCOV_EXCL_START
void
perform(function_view<void()> const&, Args...) const
{
// This action is never supposed to be performed!
assert(0);
}
// LCOV_EXCL_STOP
};
template<class... Args>
unready_action_type<Args...>
unready_action()
{
return unready_action_type<Args...>();
}

// toggle(flag), where :flag is a signal to a boolean, creates an action
// that will toggle the value of :flag between true and false.
//
Expand Down
14 changes: 14 additions & 0 deletions unit_tests/flow/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ TEST_CASE("action parameter passing", "[flow][actions]")
f(a);
}

TEST_CASE("unready action", "[flow][actions]")
{
REQUIRE(!unready_action().is_ready());
REQUIRE(!unready_action<int>().is_ready());
}

TEST_CASE("noop action", "[flow][actions]")
{
REQUIRE(noop_action().is_ready());
perform_action(noop_action());
REQUIRE(noop_action<int>().is_ready());
perform_action(noop_action<int>(), 1);
}

TEST_CASE("toggle action", "[flow][actions]")
{
bool x = false;
Expand Down

0 comments on commit 127e537

Please sign in to comment.