Skip to content

Commit

Permalink
Add hide_if_empty().
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadden committed Sep 20, 2020
1 parent 2fa0c26 commit b166963
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/alia/signals/containers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef ALIA_SIGNALS_CONTAINERS_HPP
#define ALIA_SIGNALS_CONTAINERS_HPP

#include <alia/signals/adaptors.hpp>
#include <alia/signals/application.hpp>

namespace alia {

// hide_if_empty(s), where :s is a signal, yields a wrapper for :s that will
// claim to have no value if that value would be empty. (This is determined by
// calling the empty() member function of the value, so it works on most
// containers (including strings).)
template<class Signal>
auto
hide_if_empty(Signal s)
{
return mask_reads(
s, lazy_apply([](auto const& x) { return !x.empty(); }, s));
}

} // namespace alia

#endif
32 changes: 32 additions & 0 deletions unit_tests/signals/containers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <alia/signals/containers.hpp>

#include <testing.hpp>

using namespace alia;

TEST_CASE("hide_if_empty (hidden)", "[signals][adaptors]")
{
std::string x;
auto wrapped = direct(x);
auto s = hide_if_empty(wrapped);

typedef decltype(s) signal_t;
REQUIRE(signal_is_readable<signal_t>::value);
REQUIRE(signal_is_writable<signal_t>::value);

REQUIRE(!signal_has_value(s));
}

TEST_CASE("hide_if_empty (not hidden)", "[signals][adaptors]")
{
std::string x("foo");
auto wrapped = direct(x);
auto s = hide_if_empty(wrapped);

typedef decltype(s) signal_t;
REQUIRE(signal_is_readable<signal_t>::value);
REQUIRE(signal_is_writable<signal_t>::value);

REQUIRE(signal_has_value(s));
REQUIRE(read_signal(s) == "foo");
}

0 comments on commit b166963

Please sign in to comment.