Unverified Commit 41df839f authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #930

Use fold expression in the data_processor if available
parents d6ebaac3 193c39fb
......@@ -475,6 +475,43 @@ public:
return none;
}
#if defined(__cpp_fold_expressions) && defined(__cpp_if_constexpr)
template <class... Ts>
error operator()(Ts&&... xs) {
error result;
auto f = [&result, this](auto&& x) {
using type = detail::decay_t<decltype(x)>;
if constexpr (meta::is_save_callback<type>::value) {
if constexpr (Derived::reads_state)
if (auto err = x.fun()) {
result = std::move(err);
return false;
}
} else if constexpr (meta::is_load_callback<type>::value) {
if constexpr (Derived::writes_state)
if (auto err = x.fun()) {
result = std::move(err);
return false;
}
} else if constexpr (meta::is_annotation<type>::value
|| is_allowed_unsafe_message_type<type>::value) {
// skip element
} else {
if (auto err = dref().apply(deconst(x))) {
result = std::move(err);
return false;
}
}
return true;
};
if ((f(std::forward<Ts>(xs)) && ...))
return none;
return result;
}
#else // defined(__cpp_fold_expressions) && defined(__cpp_if_constexpr)
template <class F, class... Ts>
error operator()(meta::save_callback_t<F> x, Ts&&... xs) {
// TODO: use `if constexpr` when switching to C++17.
......@@ -525,6 +562,8 @@ public:
return dref()(std::forward<Ts>(xs)...);
}
#endif // defined(__cpp_fold_expressions) && defined(__cpp_if_constexpr)
protected:
virtual error apply_impl(int8_t&) = 0;
......
......@@ -18,6 +18,8 @@
#pragma once
#include <type_traits>
#include "caf/meta/annotation.hpp"
namespace caf {
......@@ -36,6 +38,12 @@ struct load_callback_t : annotation {
F fun;
};
template <class T>
struct is_load_callback : std::false_type {};
template <class F>
struct is_load_callback<load_callback_t<F>> : std::true_type {};
/// Returns an annotation that allows inspectors to call
/// user-defined code after performing load operations.
template <class F>
......
......@@ -18,6 +18,8 @@
#pragma once
#include <type_traits>
#include "caf/meta/annotation.hpp"
namespace caf {
......@@ -36,6 +38,12 @@ struct save_callback_t : annotation {
F fun;
};
template <class T>
struct is_save_callback : std::false_type {};
template <class F>
struct is_save_callback<save_callback_t<F>> : std::true_type {};
/// Returns an annotation that allows inspectors to call
/// user-defined code after performing save operations.
template <class F>
......
......@@ -23,7 +23,6 @@
#include "caf/optional.hpp"
using namespace std;
using namespace caf;
namespace {
......
This diff is collapsed.
......@@ -33,6 +33,14 @@
#include "caf/all.hpp"
#include "caf/io/all.hpp"
#include "caf/deep_to_string.hpp"
#include "caf/io/network/interfaces.hpp"
#include "caf/io/network/test_multiplexer.hpp"
using namespace caf;
using namespace caf::io;
namespace {
using caf::make_message_id;
......@@ -72,14 +80,6 @@ constexpr auto basp_atom = caf::atom("BASP");
constexpr auto spawn_serv_atom = caf::atom("SpawnServ");
constexpr auto config_serv_atom = caf::atom("ConfigServ");
} // namespace
using namespace std;
using namespace caf;
using namespace caf::io;
namespace {
constexpr uint32_t num_remote_nodes = 2;
using buffer = std::vector<char>;
......@@ -259,10 +259,9 @@ public:
return {hdr, std::move(payload)};
}
void connect_node(
node& n, optional<accept_handle> ax = none,
void connect_node(node& n, optional<accept_handle> ax = none,
actor_id published_actor_id = invalid_actor_id,
const set<string>& published_actor_ifs = std::set<std::string>{}) {
const std::set<std::string>& published_actor_ifs = {}) {
auto src = ax ? *ax : ahdl_;
CAF_MESSAGE("connect remote node "
<< n.name << ", connection ID = " << n.connection.id()
......@@ -412,8 +411,8 @@ private:
accept_handle ahdl_;
network::test_multiplexer* mpx_;
node_id this_node_;
unique_ptr<scoped_actor> self_;
array<node, num_remote_nodes> nodes_;
std::unique_ptr<scoped_actor> self_;
std::array<node, num_remote_nodes> nodes_;
/*
array<node_id, num_remote_nodes> remote_node_;
array<connection_handle, num_remote_nodes> remote_hdl_;
......@@ -493,7 +492,7 @@ CAF_TEST(non_empty_server_handshake) {
buffer expected_payload;
binary_serializer bd{nullptr, expected_payload};
bd(instance().this_node(), defaults::middleman::app_identifiers, self()->id(),
set<string>{"caf::replies_to<@u16>::with<@u16>"});
std::set<std::string>{"caf::replies_to<@u16>::with<@u16>"});
CAF_CHECK_EQUAL(hexstr(payload), hexstr(expected_payload));
}
......@@ -633,7 +632,7 @@ CAF_TEST(remote_actor_and_send) {
{basp::message_type::direct_message, 0, 0, 0,
jupiter().dummy_actor->id(), self()->id()},
std::vector<strong_actor_ptr>{}, make_message("hi there!"));
self()->receive([&](const string& str) {
self()->receive([&](const std::string& str) {
CAF_CHECK_EQUAL(to_string(self()->current_sender()), to_string(result));
CAF_CHECK_EQUAL(self()->current_sender(), result.address());
CAF_CHECK_EQUAL(str, "hi there!");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment