Commit 037450b4 authored by Dominik Charousset's avatar Dominik Charousset

Add example for response promises

parent fc3ae7ce
......@@ -30,6 +30,7 @@ add(. hello_world)
add(message_passing cell)
add(message_passing divider)
add(message_passing request)
add(message_passing promises)
add(message_passing calculator)
add(message_passing delegating)
add(message_passing fixed_stack)
......
/******************************************************************************\
* Illustrates response promises. *
\******************************************************************************/
// This file is partially included in the manual, do not modify
// without updating the references in the *.tex files!
// Manual references: lines 18-43 (MessagePassing.tex)
#include <iostream>
#include "caf/all.hpp"
using std::cout;
using std::endl;
using namespace caf;
using add_atom = atom_constant<atom("add")>;
using adder = typed_actor<replies_to<add_atom, int, int>::with<int>>;
// function-based, statically typed, event-based API
adder::behavior_type worker() {
return {
[](add_atom, int a, int b) {
return a + b;
}
};
}
// function-based, statically typed, event-based API
adder::behavior_type calculator_master(adder::pointer self) {
auto w = self->spawn(worker);
return {
[=](add_atom x, int y, int z) -> result<int> {
auto rp = self->make_response_promise<int>();
self->request(w, infinite, x, y, z).then([=](int result) mutable {
rp.deliver(result);
});
return rp;
}
};
}
int main(int argc, char** argv) {
actor_system system{argc, argv};
auto f = make_function_view(system.spawn(calculator_master));
cout << "12 + 13 = " << f(add_atom::value, 12, 13) << endl;
}
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