Commit cc944f64 authored by Dominik Charousset's avatar Dominik Charousset

Refine API of stateful actors

parent 8d96abe9
......@@ -28,10 +28,15 @@
namespace caf {
namespace experimental {
/// An event-based actor with managed state. The state is constructed
/// before `make_behavior` will get called and destroyed after the
/// actor called `quit`. This state management brakes cycles and
/// allows actors to automatically release ressources as soon
/// as possible.
template <class State, class Base = event_based_actor>
class stateful_actor : public Base {
public:
stateful_actor() {
stateful_actor() : state(state_) {
// nop
}
......@@ -39,19 +44,23 @@ public:
// nop
}
void initialize() override {
cr_state(this);
Base::initialize();
}
/// Destroys the state of this actor (no further overriding allowed).
void on_exit() override final {
state_.~State();
}
State& state() {
return state_;
/// A reference to the actor's state.
State& state;
/// @cond PRIVATE
void initialize() override {
cr_state(this);
Base::initialize();
}
/// @endcond
private:
template <class T>
typename std::enable_if<std::is_constructible<State, T>::value>::type
......
......@@ -43,10 +43,10 @@ struct counter {
behavior adder(stateful_actor<counter>* self) {
return {
[=](add_atom, int x) {
self->state().value += x;
self->state.value += x;
},
[=](get_atom) {
return self->state().value;
return self->state.value;
}
};
}
......@@ -62,10 +62,10 @@ typed_adder_actor::behavior_type
typed_adder(typed_adder_actor::stateful_pointer<counter> self) {
return {
[=](add_atom, int x) {
self->state().value += x;
self->state.value += x;
},
[=](get_atom) {
return self->state().value;
return self->state.value;
}
};
}
......
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