Commit ae7a2017 authored by Dominik Charousset's avatar Dominik Charousset

Coding style nitpicks

parent d95f40d6
......@@ -69,6 +69,11 @@ struct atom_constant {
template <atom_value V>
const atom_constant<V> atom_constant<V>::value = atom_constant<V>{};
/**
* Generic 'ADD' atom for request operations.
*/
using add_atom = atom_constant<atom("ADD")>;
/**
* Generic 'GET' atom for request operations.
*/
......@@ -117,6 +122,11 @@ using leave_atom = atom_constant<atom("LEAVE")>;
*/
using forward_atom = atom_constant<atom("FORWARD")>;
/**
* Generic 'FLUSH' atom, e.g., used by `aout`.
*/
using flush_atom = atom_constant<atom("FLUSH")>;
} // namespace caf
#endif // CAF_ATOM_HPP
......@@ -132,8 +132,8 @@ class blocking_actor
* ~~~
* int i = 0;
* receive_for(i, 10) (
* on(atom("get")) >> [&]() -> message {
* return {"result", i};
* [&](get_atom) {
* return i;
* }
* );
* ~~~
......
......@@ -158,7 +158,7 @@ void printer_loop(blocking_actor* self) {
};
bool running = true;
self->receive_while([&] { return running; })(
on(atom("add"), arg_match) >> [&](std::string& str) {
[&](add_atom, std::string& str) {
auto s = self->current_sender();
if (str.empty() || s == invalid_actor_addr) {
return;
......@@ -173,7 +173,7 @@ void printer_loop(blocking_actor* self) {
}
flush_if_needed(i->second);
},
on(atom("flush")) >> [&] {
[&](flush_atom) {
flush_output(self->current_sender());
},
[&](const down_msg& dm) {
......
......@@ -34,12 +34,12 @@ actor_ostream::actor_ostream(actor self) : m_self(std::move(self)) {
}
actor_ostream& actor_ostream::write(std::string arg) {
send_as(m_self, m_printer, atom("add"), std::move(arg));
send_as(m_self, m_printer, add_atom::value, std::move(arg));
return *this;
}
actor_ostream& actor_ostream::flush() {
send_as(m_self, m_printer, atom("flush"));
send_as(m_self, m_printer, flush_atom::value);
return *this;
}
......
......@@ -31,7 +31,12 @@ namespace {
constexpr auto s_foo = atom("FooBar");
using a_atom = atom_constant<atom("a")>;
using b_atom = atom_constant<atom("b")>;
using c_atom = atom_constant<atom("c")>;
using abc_atom = atom_constant<atom("abc")>;
using def_atom = atom_constant<atom("def")>;
using foo_atom = atom_constant<atom("foo")>;
struct fixture {
~fixture() {
......@@ -69,31 +74,31 @@ struct send_to_self {
CAF_TEST(receive_atoms) {
scoped_actor self;
send_to_self f{self.get()};
f(atom("foo"), static_cast<uint32_t>(42));
f(atom("abc"), atom("def"), "cstring");
f(foo_atom::value, static_cast<uint32_t>(42));
f(abc_atom::value, def_atom::value, "cstring");
f(1.f);
f(atom("a"), atom("b"), atom("c"), 23.f);
f(a_atom::value, b_atom::value, c_atom::value, 23.f);
bool matched_pattern[3] = {false, false, false};
int i = 0;
CAF_MESSAGE("start receive loop");
for (i = 0; i < 3; ++i) {
self->receive(
on(atom("foo"), arg_match) >> [&](uint32_t value) {
[&](foo_atom, uint32_t value) {
matched_pattern[0] = true;
CAF_CHECK_EQUAL(value, 42);
},
on(atom("abc"), atom("def"), arg_match) >> [&](const std::string& str) {
[&](abc_atom, def_atom, const std::string& str) {
matched_pattern[1] = true;
CAF_CHECK_EQUAL(str, "cstring");
},
on(atom("a"), atom("b"), atom("c"), arg_match) >> [&](float value) {
[&](a_atom, b_atom, c_atom, float value) {
matched_pattern[2] = true;
CAF_CHECK_EQUAL(value, 23.f);
});
}
CAF_CHECK(matched_pattern[0] && matched_pattern[1] && matched_pattern[2]);
self->receive(
// "erase" message { atom("b"), atom("a"), atom("c"), 23.f }
// "erase" message { 'b', 'a, 'c', 23.f }
others >> [] {
CAF_MESSAGE("drain mailbox");
},
......@@ -104,10 +109,10 @@ CAF_TEST(receive_atoms) {
atom_value x = atom("abc");
atom_value y = abc_atom::value;
CAF_CHECK_EQUAL(x, y);
auto msg = make_message(abc_atom());
auto msg = make_message(atom("abc"));
self->send(self, msg);
self->receive(
on(atom("abc")) >> [] {
[](abc_atom) {
CAF_MESSAGE("received 'abc'");
},
others >> [] {
......
......@@ -24,17 +24,24 @@
using namespace caf;
namespace {
using die_atom = atom_constant<atom("die")>;
using done_atom = atom_constant<atom("done")>;
} // namespace <anonymous>
CAF_TEST(constructor_attach) {
class testee : public event_based_actor {
public:
testee(actor buddy) : m_buddy(buddy) {
attach_functor([=](uint32_t reason) {
send(m_buddy, atom("done"), reason);
send(m_buddy, done_atom::value, reason);
});
}
behavior make_behavior() {
return {
on(atom("die")) >> [=] {
[=](die_atom) {
quit(exit_reason::user_shutdown);
}
};
......@@ -55,7 +62,7 @@ CAF_TEST(constructor_attach) {
quit(msg.reason);
}
},
on(atom("done"), arg_match) >> [=](uint32_t reason) {
[=](done_atom, uint32_t reason) {
CAF_CHECK_EQUAL(reason, exit_reason::user_shutdown);
if (++m_downs == 2) {
quit(reason);
......@@ -70,7 +77,7 @@ CAF_TEST(constructor_attach) {
int m_downs;
actor m_testee;
};
anon_send(spawn<spawner>(), atom("die"));
anon_send(spawn<spawner>(), die_atom::value);
await_all_actors_done();
shutdown();
}
......@@ -26,6 +26,19 @@ using std::cout;
using std::endl;
using namespace caf;
namespace {
using hi_atom = atom_constant<atom("hi")>;
using ho_atom = atom_constant<atom("ho")>;
using sub0_atom = atom_constant<atom("sub0")>;
using sub1_atom = atom_constant<atom("sub1")>;
using sub2_atom = atom_constant<atom("sub2")>;
using sub3_atom = atom_constant<atom("sub3")>;
using sub4_atom = atom_constant<atom("sub4")>;
} // namespace <anonymous>
CAF_TEST(test_serial_reply) {
auto mirror_behavior = [=](event_based_actor* self) {
self->become(others >> [=]() -> message {
......@@ -42,44 +55,43 @@ CAF_TEST(test_serial_reply) {
auto c3 = self->spawn<linked>(mirror_behavior);
auto c4 = self->spawn<linked>(mirror_behavior);
self->become (
on(atom("hi there")) >> [=]() -> continue_helper {
CAF_MESSAGE("received 'hi there'");
return self->sync_send(c0, atom("sub0")).then(
on(atom("sub0")) >> [=]() -> continue_helper {
CAF_MESSAGE("received 'sub0'");
return self->sync_send(c1, atom("sub1")).then(
on(atom("sub1")) >> [=]() -> continue_helper {
CAF_MESSAGE("received 'sub1'");
return self->sync_send(c2, atom("sub2")).then(
on(atom("sub2")) >> [=]() -> continue_helper {
CAF_MESSAGE("received 'sub2'");
return self->sync_send(c3, atom("sub3")).then(
on(atom("sub3")) >> [=]() -> continue_helper {
CAF_MESSAGE("received 'sub3'");
return self->sync_send(c4, atom("sub4")).then(
on(atom("sub4")) >> [=]() -> atom_value {
CAF_MESSAGE("received 'sub4'");
return atom("hiho");
}
);
[=](hi_atom) -> continue_helper {
CAF_MESSAGE("received 'hi there'");
return self->sync_send(c0, sub0_atom::value).then(
[=](sub0_atom) -> continue_helper {
CAF_MESSAGE("received 'sub0'");
return self->sync_send(c1, sub1_atom::value).then(
[=](sub1_atom) -> continue_helper {
CAF_MESSAGE("received 'sub1'");
return self->sync_send(c2, sub2_atom::value).then(
[=](sub2_atom) -> continue_helper {
CAF_MESSAGE("received 'sub2'");
return self->sync_send(c3, sub3_atom::value).then(
[=](sub3_atom) -> continue_helper {
CAF_MESSAGE("received 'sub3'");
return self->sync_send(c4, sub4_atom::value).then(
[=](sub4_atom) -> atom_value {
CAF_MESSAGE("received 'sub4'");
return ho_atom::value;
}
);
}
);
}
);
}
);
}
);
}
);
}
);
}
);
}
);
});
{ // lifetime scope of self
scoped_actor self;
CAF_MESSAGE("ID of main: " << self->id());
self->sync_send(master, atom("hi there")).await(
on(atom("hiho")) >> [] {
CAF_MESSAGE("received `hiho` atom");
self->sync_send(master, hi_atom::value).await(
[](ho_atom) {
CAF_MESSAGE("received 'ho'");
},
others >> [&] {
CAF_TEST_ERROR("Unexpected message: "
......
......@@ -36,6 +36,13 @@ namespace {
std::atomic<long> s_max_actor_instances;
std::atomic<long> s_actor_instances;
using a_atom = atom_constant<atom("a")>;
using b_atom = atom_constant<atom("b")>;
using c_atom = atom_constant<atom("c")>;
using abc_atom = atom_constant<atom("abc")>;
using name_atom = atom_constant<atom("name")>;
void inc_actor_instances() {
long v1 = ++s_actor_instances;
long v2 = s_max_actor_instances.load();
......@@ -107,7 +114,7 @@ actor spawn_event_testee2(actor parent) {
after(chrono::milliseconds(1)) >> [=] {
CAF_MESSAGE(CAF_ARG(remaining));
if (remaining == 1) {
send(parent, atom("t2done"));
send(parent, ok_atom::value);
quit();
}
else become(wait4timeout(remaining - 1));
......@@ -121,47 +128,41 @@ actor spawn_event_testee2(actor parent) {
return spawn<impl>(parent);
}
struct chopstick : public sb_actor<chopstick> {
class chopstick : public event_based_actor {
public:
behavior make_behavior() override {
return available;
}
behavior taken_by(actor whom) {
return {
on<atom("take")>() >> [=] {
return atom("busy");
[=](get_atom) {
return error_atom::value;
},
on(atom("put"), whom) >> [=] {
on(put_atom::value, whom) >> [=]() {
become(available);
},
on(atom("break")) >> [=] {
quit();
}
};
}
behavior available;
chopstick() {
inc_actor_instances();
available.assign(
[=](get_atom, actor whom) -> atom_value {
become(taken_by(whom));
return ok_atom::value;
}
);
}
behavior& init_state = available;
~chopstick() {
dec_actor_instances();
}
chopstick();
~chopstick();
private:
behavior available;
};
chopstick::chopstick() {
inc_actor_instances();
available.assign(
on(atom("take"), arg_match) >> [=](actor whom) -> atom_value {
become(taken_by(whom));
return atom("taken");
},
on(atom("break")) >> [=] {
quit();
}
);
}
chopstick::~chopstick() {
dec_actor_instances();
}
class testee_actor : public blocking_actor {
public:
testee_actor();
......@@ -292,18 +293,18 @@ behavior simple_mirror::make_behavior() {
}
behavior high_priority_testee(event_based_actor* self) {
self->send(self, atom("b"));
self->send(message_priority::high, self, atom("a"));
self->send(self, b_atom::value);
self->send(message_priority::high, self, a_atom::value);
// 'a' must be self->received before 'b'
return {
on(atom("b")) >> [=] {
[=](b_atom) {
CAF_TEST_ERROR("received 'b' before 'a'");
self->quit();
},
on(atom("a")) >> [=] {
[=](a_atom) {
CAF_MESSAGE("received \"a\" atom");
self->become (
on(atom("b")) >> [=] {
[=](b_atom) {
CAF_MESSAGE("received \"b\" atom, about to quit");
self->quit();
},
......@@ -329,7 +330,7 @@ struct high_priority_testee_class : event_based_actor {
struct master : event_based_actor {
behavior make_behavior() override {
return (
on(atom("done")) >> [=] {
[=](ok_atom) {
CAF_MESSAGE("master: received done");
quit(exit_reason::user_shutdown);
}
......@@ -376,11 +377,11 @@ counting_actor::~counting_actor() {
behavior counting_actor::make_behavior() {
for (int i = 0; i < 100; ++i) {
send(this, atom("dummy"));
send(this, ok_atom::value);
}
CAF_CHECK_EQUAL(mailbox().count(), 100);
for (int i = 0; i < 100; ++i) {
send(this, atom("dummy"));
send(this, ok_atom::value);
}
CAF_CHECK_EQUAL(mailbox().count(), 200);
return {};
......@@ -417,7 +418,7 @@ CAF_TEST(detached_actors_and_schedulued_actors) {
auto m = spawn<master, detached>();
spawn<slave>(m);
spawn<slave>(m);
self->send(m, atom("done"));
self->send(m, ok_atom::value);
}
CAF_TEST(self_receive_with_zero_timeout) {
......@@ -552,8 +553,8 @@ CAF_TEST(spawn_event_testee2) {
scoped_actor self;
spawn_event_testee2(self);
self->receive(
on(atom("t2done")) >> [] {
CAF_MESSAGE("Received \"t2done\"");
[](ok_atom) {
CAF_MESSAGE("Received 'ok'");
}
);
}
......@@ -561,11 +562,11 @@ CAF_TEST(spawn_event_testee2) {
CAF_TEST(chopsticks) {
scoped_actor self;
auto cstk = spawn<chopstick>();
self->send(cstk, atom("take"), self);
self->send(cstk, get_atom::value, self);
self->receive(
on(atom("taken")) >> [&] {
self->send(cstk, atom("put"), self);
self->send(cstk, atom("break"));
[&](ok_atom) {
self->send(cstk, put_atom::value, self);
self->send_exit(cstk, exit_reason::kill);
},
others >> [&] {
CAF_TEST_ERROR("Unexpected message: " <<
......@@ -649,7 +650,7 @@ CAF_TEST(inflater) {
[=](int n, const string& str) {
send(m_buddy, n * 2, str + " from " + m_name);
},
on(atom("done")) >> [=] {
[=](ok_atom) {
quit();
}
};
......@@ -671,9 +672,9 @@ CAF_TEST(inflater) {
}
);
// kill joe and bob
auto poison_pill = make_message(atom("done"));
anon_send(joe, poison_pill);
anon_send(bob, poison_pill);
auto ok_message = make_message(ok_atom::value);
anon_send(joe, ok_message);
anon_send(bob, ok_message);
}
CAF_TEST(kr34t0r) {
......@@ -709,29 +710,29 @@ CAF_TEST(kr34t0r) {
};
scoped_actor self;
auto joe_the_second = spawn<kr34t0r>("Joe", invalid_actor);
self->send(joe_the_second, atom("done"));
self->send(joe_the_second, ok_atom::value);
}
CAF_TEST(function_spawn) {
scoped_actor self;
auto f = [](const string& name) -> behavior {
return (
on(atom("get_name")) >> [name] {
return make_message(atom("name"), name);
[name](get_atom) {
return std::make_tuple(name_atom::value, name);
}
);
};
auto a1 = spawn(f, "alice");
auto a2 = spawn(f, "bob");
self->send(a1, atom("get_name"));
self->send(a1, get_atom::value);
self->receive (
on(atom("name"), arg_match) >> [&](const string& name) {
[&](name_atom, const string& name) {
CAF_CHECK_EQUAL(name, "alice");
}
);
self->send(a2, atom("get_name"));
self->send(a2, get_atom::value);
self->receive (
on(atom("name"), arg_match) >> [&](const string& name) {
[&](name_atom, const string& name) {
CAF_CHECK_EQUAL(name, "bob");
}
);
......@@ -739,14 +740,12 @@ CAF_TEST(function_spawn) {
self->send_exit(a2, exit_reason::user_shutdown);
}
using abc_atom = atom_constant<atom("abc")>;
using typed_testee = typed_actor<replies_to<abc_atom>::with<std::string>>;
typed_testee::behavior_type testee() {
return {
[](abc_atom) {
CAF_MESSAGE("received abc_atom");
CAF_MESSAGE("received 'abc'");
return "abc";
}
};
......@@ -755,7 +754,7 @@ typed_testee::behavior_type testee() {
CAF_TEST(typed_await) {
scoped_actor self;
auto x = spawn_typed(testee);
self->sync_send(x, abc_atom()).await(
self->sync_send(x, abc_atom::value).await(
[](const std::string& str) {
CAF_CHECK_EQUAL(str, "abc");
}
......@@ -769,14 +768,15 @@ CAF_TEST(constructor_attach) {
public:
testee(actor buddy) : m_buddy(buddy) {
attach_functor([=](uint32_t reason) {
send(buddy, atom("done"), reason);
send(buddy, ok_atom::value, reason);
});
}
behavior make_behavior() {
return {
on(atom("die")) >> [=] {
quit(exit_reason::user_shutdown);
others >> [=] {
CAF_TEST_ERROR("Unexpected message: "
<< to_string(current_message()));
}
};
}
......@@ -794,6 +794,7 @@ CAF_TEST(constructor_attach) {
// nop
}
behavior make_behavior() {
trap_exit(true);
m_testee = spawn<testee, monitored>(this);
return {
[=](const down_msg& msg) {
......@@ -802,7 +803,7 @@ CAF_TEST(constructor_attach) {
quit(msg.reason);
}
},
on(atom("done"), arg_match) >> [=](uint32_t reason) {
[=](ok_atom, uint32_t reason) {
CAF_CHECK_EQUAL(reason, exit_reason::user_shutdown);
if (++m_downs == 2) {
quit(reason);
......@@ -822,7 +823,7 @@ CAF_TEST(constructor_attach) {
int m_downs;
actor m_testee;
};
anon_send(spawn<spawner>(), atom("die"));
anon_send_exit(spawn<spawner>(), exit_reason::user_shutdown);
}
namespace {
......@@ -893,8 +894,9 @@ CAF_TEST(kill_the_immortal) {
auto wannabe_immortal = spawn([](event_based_actor* self) -> behavior {
self->trap_exit(true);
return {
others >> [] {
CAF_TEST_ERROR("Unexpected message");
others >> [=] {
CAF_TEST_ERROR("Unexpected message: "
<< to_string(self->current_message()));
}
};
});
......
......@@ -21,6 +21,8 @@
#ifndef CAF_IO_NETWORK_ASIO_MULTIPLEXER_HPP
#define CAF_IO_NETWORK_ASIO_MULTIPLEXER_HPP
#include "caf/config.hpp"
CAF_PUSH_WARNINGS
#include "boost/asio.hpp"
CAF_POP_WARNINGS
......@@ -100,7 +102,9 @@ class asio_multiplexer : public multiplexer {
void run() override;
private:
inline boost::asio::io_service& backend() { return m_backend; }
inline boost::asio::io_service& backend() {
return m_backend;
}
io_backend m_backend;
std::mutex m_mtx_sockets;
......
......@@ -25,6 +25,7 @@
#include <functional>
#include "caf/extend.hpp"
#include "caf/make_counted.hpp"
#include "caf/memory_managed.hpp"
#include "caf/io/fwd.hpp"
......@@ -34,8 +35,6 @@
#include "caf/io/network/protocol.hpp"
#include "caf/io/network/native_socket.hpp"
#include "caf/detail/memory.hpp"
#include "caf/detail/disposer.hpp"
#include "caf/detail/memory_cache_flag_type.hpp"
namespace boost {
......@@ -176,8 +175,7 @@ class multiplexer {
f();
}
};
dispatch_runnable(runnable_ptr{detail::memory::create<impl>(std::move(fun)),
false});
dispatch_runnable(make_counted<impl>(std::move(fun)));
}
/**
......
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