A simple example for a delayed_send based application.
A simple example for a delayed_send based application.
#include <algorithm>
#include <chrono>
#include <iostream>
#include "caf/all.hpp"
using std::cout;
using std::endl;
using std::pair;
constexpr const char* figures[] = {
"<(^.^<)",
"<(^.^)>",
"(>^.^)>",
};
struct animation_step {
size_t figure_idx;
size_t offset;
};
constexpr animation_step animation_steps[] = {
{1, 7}, {0, 7}, {0, 6}, {0, 5}, {1, 5}, {2, 5}, {2, 6},
{2, 7}, {2, 8}, {2, 9}, {2, 10}, {1, 10}, {0, 10}, {0, 9},
{1, 9}, {2, 10}, {2, 11}, {2, 12}, {2, 13}, {1, 13}, {0, 13},
{0, 12}, {0, 11}, {0, 10}, {0, 9}, {0, 8}, {0, 7}, {1, 7},
};
constexpr size_t animation_width = 20;
void draw_kirby(const animation_step& animation) {
cout.width(animation_width);
cout << '\r';
std::fill_n(std::ostream_iterator<char>{cout}, animation.offset, ' ');
cout << figures[animation.figure_idx];
cout.fill(' ');
cout.flush();
}
auto i = std::begin(animation_steps);
auto e = std::end(animation_steps);
self->send(self, update_atom_v);
return {
[=](update_atom) mutable {
if (i == e) {
cout << endl;
return;
}
draw_kirby(*i);
++i;
self->delayed_send(self, std::chrono::milliseconds(150), update_atom_v);
},
};
}
system.
spawn(dancing_kirby);
}
CAF_MAIN()
Actor environment including scheduler, registry, and optional components such as a middleman.
Definition actor_system.hpp:117
infer_handle_from_class_t< C > spawn(Ts &&... xs)
Returns a new actor of type C using xs... as constructor arguments.
Definition actor_system.hpp:321
Describes the behavior of an actor, i.e., provides a message handler and an optional timeout.
Definition behavior.hpp:39
A cooperatively scheduled, event-based actor implementation.
Definition event_based_actor.hpp:53
void quit(error x=error{})
Finishes execution of this actor after any currently running message handler is done.
Root namespace of libcaf.
Definition abstract_actor.hpp:44