Skip to content

Commit

Permalink
cleanup/condense Terminal::send* implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
evaleev committed Apr 9, 2024
1 parent ca4c976 commit 28bbbb8
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions ttg/ttg/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,16 @@ namespace ttg {
}

template <typename Key = keyT, typename Value = valueT>
std::enable_if_t<meta::is_none_void_v<Key, Value>, void> send(const Key &key, const Value &value) {
if (!send_callback) throw std::runtime_error("send callback not initialized");
send_callback(key, value);
}

template <typename Key = keyT, typename Value = valueT>
std::enable_if_t<meta::is_none_void_v<Key, Value> && std::is_same_v<Value, std::remove_reference_t<Value>>, void>
std::enable_if_t<meta::is_none_void_v<Key, Value>, void>
send(const Key &key, Value &&value) {
if (!move_callback) throw std::runtime_error("move callback not initialized");
move_callback(key, std::forward<valueT>(value));
constexpr auto value_is_rvref = !std::is_reference_v<Value>;
if constexpr (value_is_rvref) {
if (!move_callback) throw std::runtime_error("move callback not initialized");
move_callback(key, std::move(value));
} else {
if (!send_callback) throw std::runtime_error("send callback not initialized");
send_callback(key, value);
}
}

template <typename Key = keyT>
Expand All @@ -221,16 +221,17 @@ namespace ttg {
}

template <typename Value = valueT>
std::enable_if_t<!meta::is_void_v<Value>, void> sendv(const Value &value) {
if (!send_callback) throw std::runtime_error("send callback not initialized");
send_callback(value);
}

template <typename Value = valueT>
std::enable_if_t<!meta::is_void_v<Value> && std::is_same_v<Value, std::remove_reference_t<Value>>, void> sendv(
std::enable_if_t<!meta::is_void_v<Value>, void> sendv(
Value &&value) {
if (!move_callback) throw std::runtime_error("move callback not initialized");
move_callback(std::forward<valueT>(value));
constexpr auto value_is_rvref = !std::is_reference_v<Value>;
if constexpr (value_is_rvref) {
if (!move_callback) throw std::runtime_error("move callback not initialized");
move_callback(std::move(value));
}
else {
if (!send_callback) throw std::runtime_error("send callback not initialized");
send_callback(value);
}
}

void send() {
Expand Down Expand Up @@ -458,18 +459,6 @@ namespace ttg {
in->connect_pull(this);
}

template<typename Key = keyT, typename Value = valueT>
std::enable_if_t<meta::is_none_void_v<Key,Value>,void> send(const Key &key, const Value &value) {
for (auto && successor : this->successors()) {
assert(successor->get_type() != TerminalBase::Type::Write);
if (successor->get_type() == TerminalBase::Type::Read) {
static_cast<In<keyT, std::add_const_t<valueT>> *>(successor)->send(key, value);
} else if (successor->get_type() == TerminalBase::Type::Consume) {
static_cast<In<keyT, valueT> *>(successor)->send(key, value);
}
}
}

template <typename Key = keyT, typename Value = valueT>
std::enable_if_t<!meta::is_void_v<Key> && meta::is_void_v<Value>, void> sendk(const Key &key) {
for (auto &&successor : this->successors()) {
Expand Down Expand Up @@ -512,15 +501,20 @@ namespace ttg {
}

template <typename Key = keyT, typename Value = valueT>
std::enable_if_t<meta::is_none_void_v<Key, Value> && std::is_same_v<Value, std::remove_reference_t<Value>>, void>
std::enable_if_t<meta::is_none_void_v<Key, Value>, void>
send(const Key &key, Value &&value) {
const std::size_t N = this->nsuccessors();
TerminalBase *move_successor = nullptr;
// send copies to every terminal except the one we will move the results to
for (std::size_t i = 0; i != N; ++i) {
TerminalBase *successor = this->successors().at(i);
if (successor->get_type() == TerminalBase::Type::Read) {
static_cast<In<keyT, std::add_const_t<valueT>> *>(successor)->send(key, value);
// if only have 1 successor forward value even if successor is read-only, so we can deal with move-only types
auto* read_successor = static_cast<In<keyT, std::add_const_t<valueT>> *>(successor);
if (N != 1)
read_successor->send(key, value);
else
read_successor->send(key, std::forward<Value>(value));
} else if (successor->get_type() == TerminalBase::Type::Consume) {
if (nullptr == move_successor) {
move_successor = successor;
Expand Down

0 comments on commit 28bbbb8

Please sign in to comment.