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 {
......
......@@ -21,17 +21,18 @@
// exclude this suite; seems to be too much to swallow for MSVC
#ifndef CAF_WINDOWS
#define CAF_SUITE typed_spawn
#include "caf/test/unit_test.hpp"
# define CAF_SUITE typed_spawn
#include "caf/string_algorithms.hpp"
# include "caf/test/unit_test.hpp"
#include "caf/all.hpp"
# include "caf/string_algorithms.hpp"
#define ERROR_HANDLER \
[&](error& err) { CAF_FAIL(system.render(err)); }
# include "caf/all.hpp"
# define ERROR_HANDLER [&](error& err) { CAF_FAIL(system.render(err)); }
using std::string;
using namespace std;
using namespace caf;
using passed_atom = caf::atom_constant<caf::atom("passed")>;
......@@ -39,7 +40,7 @@ using passed_atom = caf::atom_constant<caf::atom("passed")>;
namespace {
enum class mock_errc : uint8_t {
cannot_revert_empty = 1
cannot_revert_empty = 1,
};
error make_error(mock_errc x) {
......@@ -55,9 +56,6 @@ using dummy2 = dummy1::extend<reacts_to<ok_atom>>;
static_assert(std::is_convertible<dummy2, dummy1>::value,
"handle not assignable to narrower definition");
//static_assert(!std::is_convertible<dummy1, dummy2>::value,
// "handle is assignable to broader definition");
using dummy3 = typed_actor<reacts_to<float, int>>;
using dummy4 = typed_actor<replies_to<int>::with<double>>;
using dummy5 = dummy4::extend_with<dummy3>;
......@@ -68,12 +66,6 @@ static_assert(std::is_convertible<dummy5, dummy3>::value,
static_assert(std::is_convertible<dummy5, dummy4>::value,
"handle not assignable to narrower definition");
//static_assert(!std::is_convertible<dummy3, dummy5>::value,
// "handle is assignable to broader definition");
//static_assert(!std::is_convertible<dummy4, dummy5>::value,
// "handle is assignable to broader definition");
/******************************************************************************
* simple request/response test *
******************************************************************************/
......@@ -92,9 +84,7 @@ using server_type = typed_actor<replies_to<my_request>::with<bool>>;
server_type::behavior_type typed_server1() {
return {
[](const my_request& req) {
return req.a == req.b;
}
[](const my_request& req) { return req.a == req.b; },
};
}
......@@ -105,7 +95,7 @@ server_type::behavior_type typed_server2(server_type::pointer) {
class typed_server3 : public server_type::base {
public:
typed_server3(actor_config& cfg, const string& line, actor buddy)
: server_type::base(cfg) {
: server_type::base(cfg) {
anon_send(buddy, line);
}
......@@ -114,18 +104,15 @@ public:
}
};
void client(event_based_actor* self, const actor& parent, const server_type& serv) {
self->request(serv, infinite, my_request{0, 0}).then(
[=](bool val1) {
CAF_CHECK_EQUAL(val1, true);
self->request(serv, infinite, my_request{10, 20}).then(
[=](bool val2) {
CAF_CHECK_EQUAL(val2, false);
self->send(parent, passed_atom::value);
}
);
}
);
void client(event_based_actor* self, const actor& parent,
const server_type& serv) {
self->request(serv, infinite, my_request{0, 0}).then([=](bool val1) {
CAF_CHECK_EQUAL(val1, true);
self->request(serv, infinite, my_request{10, 20}).then([=](bool val2) {
CAF_CHECK_EQUAL(val2, false);
self->send(parent, passed_atom::value);
});
});
}
/******************************************************************************
......@@ -134,10 +121,9 @@ void client(event_based_actor* self, const actor& parent, const server_type& ser
struct get_state_msg {};
using event_testee_type = typed_actor<replies_to<get_state_msg>::with<string>,
replies_to<string>::with<void>,
replies_to<float>::with<void>,
replies_to<int>::with<int>>;
using event_testee_type = typed_actor<
replies_to<get_state_msg>::with<string>, replies_to<string>::with<void>,
replies_to<float>::with<void>, replies_to<int>::with<int>>;
class event_testee : public event_testee_type::base {
public:
......@@ -147,53 +133,31 @@ public:
behavior_type wait4string() {
return {
[](const get_state_msg&) {
return "wait4string";
},
[=](const string&) {
become(wait4int());
},
[](float) {
return skip();
},
[](int) {
return skip();
}
[=](const get_state_msg&) { return "wait4string"; },
[=](const string&) { become(wait4int()); },
[=](float) { return skip(); },
[=](int) { return skip(); },
};
}
behavior_type wait4int() {
return {
[](const get_state_msg&) {
return "wait4int";
},
[=](const get_state_msg&) { return "wait4int"; },
[=](int) -> int {
become(wait4float());
return 42;
},
[](float) {
return skip();
},
[](const string&) {
return skip();
}
[=](float) { return skip(); },
[=](const string&) { return skip(); },
};
}
behavior_type wait4float() {
return {
[](const get_state_msg&) {
return "wait4float";
},
[=](float) {
become(wait4string());
},
[](const string&) {
return skip();
},
[](int) {
return skip();
}
[=](const get_state_msg&) { return "wait4float"; },
[=](float) { become(wait4string()); },
[=](const string&) { return skip(); },
[=](int) { return skip(); },
};
}
......@@ -213,7 +177,7 @@ string_actor::behavior_type string_reverter() {
[](string& str) -> string {
std::reverse(str.begin(), str.end());
return std::move(str);
}
},
};
}
......@@ -225,12 +189,12 @@ string_actor::behavior_type string_delegator(string_actor::pointer self,
return {
[=](string& str) -> delegated<string> {
return self->delegate(next, std::move(str));
}
},
};
}
using maybe_string_actor = typed_actor<replies_to<string>
::with<ok_atom, string>>;
using maybe_string_actor = typed_actor<
replies_to<string>::with<ok_atom, string>>;
maybe_string_actor::behavior_type maybe_string_reverter() {
return {
......@@ -239,17 +203,18 @@ maybe_string_actor::behavior_type maybe_string_reverter() {
return mock_errc::cannot_revert_empty;
std::reverse(str.begin(), str.end());
return {ok_atom::value, std::move(str)};
}
},
};
}
maybe_string_actor::behavior_type
maybe_string_delegator(maybe_string_actor::pointer self, const maybe_string_actor& x) {
maybe_string_delegator(maybe_string_actor::pointer self,
const maybe_string_actor& x) {
self->link_to(x);
return {
[=](string& s) -> delegated<ok_atom, string> {
return self->delegate(x, std::move(s));
}
},
};
}
......@@ -263,7 +228,7 @@ using float_actor = typed_actor<reacts_to<float>>;
int_actor::behavior_type int_fun() {
return {
[](int i) { return i * i; }
[](int i) { return i * i; },
};
}
......@@ -272,7 +237,7 @@ behavior foo(event_based_actor* self) {
[=](int i, int_actor server) {
self->delegate(server, i);
self->quit();
}
},
};
}
......@@ -294,7 +259,7 @@ behavior foo2(event_based_actor* self) {
[=](int i, int_actor server) {
self->delegate(server, i);
self->quit();
}
},
};
}
......@@ -303,7 +268,7 @@ float_actor::behavior_type float_fun(float_actor::pointer self) {
[=](float a) {
CAF_CHECK_EQUAL(a, 1.0f);
self->quit(exit_reason::user_shutdown);
}
},
};
}
......@@ -311,9 +276,7 @@ int_actor::behavior_type foo3(int_actor::pointer self) {
auto b = self->spawn<linked>(float_fun);
self->send(b, 1.0f);
return {
[=](int) {
return 0;
}
[=](int) { return 0; },
};
}
......@@ -334,39 +297,20 @@ struct fixture {
void test_typed_spawn(server_type ts) {
self->send(ts, my_request{1, 2});
self->receive(
[](bool value) {
CAF_CHECK_EQUAL(value, false);
}
);
self->receive([](bool value) { CAF_CHECK_EQUAL(value, false); });
CAF_MESSAGE("async send + receive");
self->send(ts, my_request{42, 42});
self->receive(
[](bool value) {
CAF_CHECK_EQUAL(value, true);
}
);
self->receive([](bool value) { CAF_CHECK_EQUAL(value, true); });
CAF_MESSAGE("request + receive with result true");
self->request(ts, infinite, my_request{10, 20}).receive(
[](bool value) {
CAF_CHECK_EQUAL(value, false);
},
ERROR_HANDLER
);
self->request(ts, infinite, my_request{10, 20})
.receive([](bool value) { CAF_CHECK_EQUAL(value, false); },
ERROR_HANDLER);
CAF_MESSAGE("request + receive with result false");
self->request(ts, infinite, my_request{0, 0}).receive(
[](bool value) {
CAF_CHECK_EQUAL(value, true);
},
ERROR_HANDLER
);
self->request(ts, infinite, my_request{0, 0})
.receive([](bool value) { CAF_CHECK_EQUAL(value, true); }, ERROR_HANDLER);
CAF_CHECK_EQUAL(system.registry().running(), 2u);
auto c1 = self->spawn(client, self, ts);
self->receive(
[](passed_atom) {
CAF_MESSAGE("received `passed_atom`");
}
);
self->receive([](passed_atom) { CAF_MESSAGE("received `passed_atom`"); });
self->wait_for(c1);
CAF_CHECK_EQUAL(system.registry().running(), 2u);
}
......@@ -390,11 +334,7 @@ CAF_TEST(typed_spawns) {
self->await_all_other_actors_done();
CAF_MESSAGE("finished test series with `typed_server2`");
test_typed_spawn(self->spawn<typed_server3>("hi there", self));
self->receive(
[](const string& str) {
CAF_REQUIRE_EQUAL(str, "hi there");
}
);
self->receive([](const string& str) { CAF_REQUIRE_EQUAL(str, "hi there"); });
}
CAF_TEST(event_testee_series) {
......@@ -419,30 +359,22 @@ CAF_TEST(event_testee_series) {
// we expect three 42s
int i = 0;
self->receive_for(i, 3)([](int value) { CAF_CHECK_EQUAL(value, 42); });
self->receive(
[&](const string& str) {
result = str;
},
after(chrono::minutes(1)) >> [&] {
CAF_FAIL("event_testee does not reply");
}
);
self->receive([&](const string& str) { result = str; },
after(std::chrono::minutes(1)) >>
[&] { CAF_FAIL("event_testee does not reply"); });
CAF_CHECK_EQUAL(result, "wait4int");
}
CAF_TEST(string_delegator_chain) {
// run test series with string reverter
auto aut = self->spawn<monitored>(string_delegator,
system.spawn(string_reverter),
true);
set<string> iface{"caf::replies_to<@str>::with<@str>"};
system.spawn(string_reverter), true);
std::set<string> iface{"caf::replies_to<@str>::with<@str>"};
CAF_CHECK_EQUAL(aut->message_types(), iface);
self->request(aut, infinite, "Hello World!").receive(
[](const string& answer) {
CAF_CHECK_EQUAL(answer, "!dlroW olleH");
},
ERROR_HANDLER
);
self->request(aut, infinite, "Hello World!")
.receive([](const string&
answer) { CAF_CHECK_EQUAL(answer, "!dlroW olleH"); },
ERROR_HANDLER);
}
CAF_TEST(maybe_string_delegator_chain) {
......@@ -450,42 +382,31 @@ CAF_TEST(maybe_string_delegator_chain) {
auto aut = system.spawn(maybe_string_delegator,
system.spawn(maybe_string_reverter));
CAF_MESSAGE("send empty string, expect error");
self->request(aut, infinite, "").receive(
[](ok_atom, const string&) {
CAF_FAIL("unexpected string response");
},
[](const error& err) {
CAF_CHECK_EQUAL(err.category(), atom("mock"));
CAF_CHECK_EQUAL(err.code(),
static_cast<uint8_t>(mock_errc::cannot_revert_empty));
}
);
self->request(aut, infinite, "")
.receive([](ok_atom,
const string&) { CAF_FAIL("unexpected string response"); },
[](const error& err) {
CAF_CHECK_EQUAL(err.category(), atom("mock"));
CAF_CHECK_EQUAL(err.code(), static_cast<uint8_t>(
mock_errc::cannot_revert_empty));
});
CAF_MESSAGE("send abcd string, expect dcba");
self->request(aut, infinite, "abcd").receive(
[](ok_atom, const string& str) {
CAF_CHECK_EQUAL(str, "dcba");
},
ERROR_HANDLER
);
self->request(aut, infinite, "abcd")
.receive([](ok_atom, const string& str) { CAF_CHECK_EQUAL(str, "dcba"); },
ERROR_HANDLER);
}
CAF_TEST(sending_typed_actors) {
auto aut = system.spawn(int_fun);
self->send(self->spawn(foo), 10, aut);
self->receive(
[](int i) {
CAF_CHECK_EQUAL(i, 100);
}
);
self->receive([](int i) { CAF_CHECK_EQUAL(i, 100); });
self->spawn(foo3);
}
CAF_TEST(sending_typed_actors_and_down_msg) {
auto aut = system.spawn(int_fun2);
self->send(self->spawn(foo2), 10, aut);
self->receive([](int i) {
CAF_CHECK_EQUAL(i, 100);
});
self->receive([](int i) { CAF_CHECK_EQUAL(i, 100); });
}
CAF_TEST(check_signature) {
......@@ -494,20 +415,16 @@ CAF_TEST(check_signature) {
using bar_type = typed_actor<reacts_to<ok_atom>>;
auto foo_action = [](foo_type::pointer ptr) -> foo_type::behavior_type {
return {
[=] (put_atom) -> foo_result_type {
[=](put_atom) -> foo_result_type {
ptr->quit();
return {ok_atom::value};
}
},
};
};
auto bar_action = [=](bar_type::pointer ptr) -> bar_type::behavior_type {
auto foo = ptr->spawn<linked>(foo_action);
ptr->send(foo, put_atom::value);
return {
[=](ok_atom) {
ptr->quit();
}
};
return {[=](ok_atom) { ptr->quit(); }};
};
auto x = self->spawn(bar_action);
self->wait_for(x);
......
......@@ -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,
actor_id published_actor_id = invalid_actor_id,
const set<string>& published_actor_ifs = std::set<std::string>{}) {
void connect_node(node& n, optional<accept_handle> ax = none,
actor_id published_actor_id = invalid_actor_id,
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