Commit 2e704f7e authored by Dominik Charousset's avatar Dominik Charousset

Fixed typed calculator example

parent 8f2cd321
...@@ -21,14 +21,15 @@ add(. aout) ...@@ -21,14 +21,15 @@ add(. aout)
add(. hello_world) add(. hello_world)
# basic message passing primitives # basic message passing primitives
add(message_passing cell)
add(message_passing divider)
add(message_passing request)
add(message_passing promises)
add(message_passing calculator) add(message_passing calculator)
add(message_passing cell)
add(message_passing dancing_kirby)
add(message_passing delegating) add(message_passing delegating)
add(message_passing divider)
add(message_passing fixed_stack) add(message_passing fixed_stack)
add(message_passing dancing_kirby) add(message_passing promises)
add(message_passing request)
add(message_passing typed_calculator)
# dynamic behavior changes using 'become' # dynamic behavior changes using 'become'
add(dynamic_behavior skip_messages) add(dynamic_behavior skip_messages)
......
...@@ -32,11 +32,12 @@ calculator_type::behavior_type typed_calculator_fun(calculator_type::pointer) { ...@@ -32,11 +32,12 @@ calculator_type::behavior_type typed_calculator_fun(calculator_type::pointer) {
} }
class typed_calculator_class : public calculator_type::base { class typed_calculator_class : public calculator_type::base {
protected: public:
typed_calculator_class(actor_config& cfg) : calculator_type::base(cfg) { typed_calculator_class(actor_config& cfg) : calculator_type::base(cfg) {
// nop // nop
} }
protected:
behavior_type make_behavior() override { behavior_type make_behavior() override {
return { return {
[](plus_atom, int x, int y) { [](plus_atom, int x, int y) {
...@@ -52,10 +53,10 @@ protected: ...@@ -52,10 +53,10 @@ protected:
void tester(event_based_actor* self, const calculator_type& testee) { void tester(event_based_actor* self, const calculator_type& testee) {
self->link_to(testee); self->link_to(testee);
// first test: 2 + 1 = 3 // first test: 2 + 1 = 3
self->request(testee, plus_atom::value, 2, 1).then( self->request(testee, infinite, plus_atom::value, 2, 1).then(
[=](result_atom, int r1) { [=](result_atom, int r1) {
// second test: 2 - 1 = 1 // second test: 2 - 1 = 1
self->request(testee, minus_atom::value, 2, 1).then( self->request(testee, infinite, minus_atom::value, 2, 1).then(
[=](result_atom, int r2) { [=](result_atom, int r2) {
// both tests succeeded // both tests succeeded
if (r1 == 3 && r2 == 1) { if (r1 == 3 && r2 == 1) {
...@@ -74,13 +75,14 @@ void tester(event_based_actor* self, const calculator_type& testee) { ...@@ -74,13 +75,14 @@ void tester(event_based_actor* self, const calculator_type& testee) {
); );
} }
} // namespace <anonymous> void caf_main(actor_system& system) {
int main() {
actor_system system;
// test function-based impl // test function-based impl
system.spawn(tester, system.spawn(typed_calculator_fun)); system.spawn(tester, system.spawn(typed_calculator_fun));
system.await_all_actors_done(); system.await_all_actors_done();
// test class-based impl // test class-based impl
system.spawn(tester, system.spawn<typed_calculator_class>()); system.spawn(tester, system.spawn<typed_calculator_class>());
} }
} // namespace <anonymous>
CAF_MAIN()
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