Commit 370707c7 authored by Dominik Charousset's avatar Dominik Charousset

Use atom constants in typed calculator example

parent 366c771a
...@@ -12,24 +12,21 @@ using namespace caf; ...@@ -12,24 +12,21 @@ using namespace caf;
namespace { namespace {
struct shutdown_request { }; using plus_atom = atom_constant<atom("plus")>;
struct plus_request { int a; int b; }; using minus_atom = atom_constant<atom("minus")>;
struct minus_request { int a; int b; }; using result_atom = atom_constant<atom("result")>;
using calculator_type = typed_actor<replies_to<plus_request>::with<int>, using calculator_type =
replies_to<minus_request>::with<int>, typed_actor<replies_to<plus_atom, int, int>::with<result_atom, int>,
replies_to<shutdown_request>::with<void>>; replies_to<minus_atom, int, int>::with<result_atom, int>>;
calculator_type::behavior_type typed_calculator(calculator_type::pointer self) { calculator_type::behavior_type typed_calculator(calculator_type::pointer) {
return { return {
[](const plus_request& pr) { [](plus_atom, int x, int y) {
return pr.a + pr.b; return std::make_tuple(result_atom::value, x + y);
}, },
[](const minus_request& pr) { [](minus_atom, int x, int y) {
return pr.a - pr.b; return std::make_tuple(result_atom::value, x - y);
},
[=](const shutdown_request&) {
self->quit();
} }
}; };
} }
...@@ -38,14 +35,11 @@ class typed_calculator_class : public calculator_type::base { ...@@ -38,14 +35,11 @@ class typed_calculator_class : public calculator_type::base {
protected: protected:
behavior_type make_behavior() override { behavior_type make_behavior() override {
return { return {
[](const plus_request& pr) { [](plus_atom, int x, int y) {
return pr.a + pr.b; return std::make_tuple(result_atom::value, x + y);
},
[](const minus_request& pr) {
return pr.a - pr.b;
}, },
[=](const shutdown_request&) { [](minus_atom, int x, int y) {
quit(); return std::make_tuple(result_atom::value, x - y);
} }
}; };
} }
...@@ -59,17 +53,17 @@ void tester(event_based_actor* self, const calculator_type& testee) { ...@@ -59,17 +53,17 @@ void tester(event_based_actor* self, const calculator_type& testee) {
self->quit(exit_reason::user_shutdown); self->quit(exit_reason::user_shutdown);
}); });
// first test: 2 + 1 = 3 // first test: 2 + 1 = 3
self->sync_send(testee, plus_request{2, 1}).then( self->sync_send(testee, plus_atom::value, 2, 1).then(
[=](int r1) { [=](result_atom, int r1) {
// second test: 2 - 1 = 1 // second test: 2 - 1 = 1
self->sync_send(testee, minus_request{2, 1}).then( self->sync_send(testee, minus_atom::value, 2, 1).then(
[=](int r2) { [=](result_atom, int r2) {
// both tests succeeded // both tests succeeded
if (r1 == 3 && r2 == 1) { if (r1 == 3 && r2 == 1) {
aout(self) << "AUT (actor under test) seems to be ok" aout(self) << "AUT (actor under test) seems to be ok"
<< endl; << endl;
} }
self->send(testee, shutdown_request{}); self->send_exit(testee, exit_reason::user_shutdown);
} }
); );
} }
...@@ -79,11 +73,6 @@ void tester(event_based_actor* self, const calculator_type& testee) { ...@@ -79,11 +73,6 @@ void tester(event_based_actor* self, const calculator_type& testee) {
} // namespace <anonymous> } // namespace <anonymous>
int main() { int main() {
// announce custom message types
announce<shutdown_request>("shutdown_request");
announce<plus_request>("plus_request", &plus_request::a, &plus_request::b);
announce<minus_request>("minus_request", &minus_request::a,
&minus_request::b);
// test function-based impl // test function-based impl
spawn(tester, spawn_typed(typed_calculator)); spawn(tester, spawn_typed(typed_calculator));
await_all_actors_done(); await_all_actors_done();
...@@ -92,5 +81,4 @@ int main() { ...@@ -92,5 +81,4 @@ int main() {
await_all_actors_done(); await_all_actors_done();
// done // done
shutdown(); shutdown();
return 0;
} }
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