File: promises.cpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (41 lines) | stat: -rw-r--r-- 940 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Illustrates how to use response promises.

#include <iostream>

#include "caf/all.hpp"

using std::cout;
using std::endl;

using namespace caf;

// --(rst-promises-begin)--
using adder
  = typed_actor<replies_to<add_atom, int32_t, int32_t>::with<int32_t>>;

adder::behavior_type worker() {
  return {
    [](add_atom, int32_t a, int32_t b) { return a + b; },
  };
}

adder::behavior_type calculator_master(adder::pointer self) {
  auto w = self->spawn(worker);
  return {
    [=](add_atom x, int32_t y, int32_t z) -> result<int32_t> {
      auto rp = self->make_response_promise<int32_t>();
      self->request(w, infinite, x, y, z).then([=](int32_t result) mutable {
        rp.deliver(result);
      });
      return rp;
    },
  };
}
// --(rst-promises-end)--

void caf_main(actor_system& system) {
  auto f = make_function_view(system.spawn(calculator_master));
  cout << "12 + 13 = " << f(add_atom_v, 12, 13) << endl;
}

CAF_MAIN()