Skip to content

Commit

Permalink
Fixed typed calculator example
Browse files Browse the repository at this point in the history
  • Loading branch information
Neverlord committed May 11, 2018
1 parent 8f2cd32 commit 2e704f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
11 changes: 6 additions & 5 deletions examples/CMakeLists.txt
Expand Up @@ -21,14 +21,15 @@ add(. aout)
add(. hello_world)

# 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 cell)
add(message_passing dancing_kirby)
add(message_passing delegating)
add(message_passing divider)
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'
add(dynamic_behavior skip_messages)
Expand Down
16 changes: 9 additions & 7 deletions examples/message_passing/typed_calculator.cpp
Expand Up @@ -32,11 +32,12 @@ calculator_type::behavior_type typed_calculator_fun(calculator_type::pointer) {
}

class typed_calculator_class : public calculator_type::base {
protected:
public:
typed_calculator_class(actor_config& cfg) : calculator_type::base(cfg) {
// nop
}

protected:
behavior_type make_behavior() override {
return {
[](plus_atom, int x, int y) {
Expand All @@ -52,10 +53,10 @@ class typed_calculator_class : public calculator_type::base {
void tester(event_based_actor* self, const calculator_type& testee) {
self->link_to(testee);
// 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) {
// 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) {
// both tests succeeded
if (r1 == 3 && r2 == 1) {
Expand All @@ -74,13 +75,14 @@ void tester(event_based_actor* self, const calculator_type& testee) {
);
}

} // namespace <anonymous>

int main() {
actor_system system;
void caf_main(actor_system& system) {
// test function-based impl
system.spawn(tester, system.spawn(typed_calculator_fun));
system.await_all_actors_done();
// test class-based impl
system.spawn(tester, system.spawn<typed_calculator_class>());
}

} // namespace <anonymous>

CAF_MAIN()

0 comments on commit 2e704f7

Please sign in to comment.