Commit 6d12f13a authored by Dominik Charousset's avatar Dominik Charousset

allow to define base class of `sb_actor`

this patch enhances the class `sb_actor` and allows the user
to define a base class that must be derived from `event_based_actor`
parent 09a6675c
......@@ -31,6 +31,9 @@
#ifndef CPPA_FSM_ACTOR_HPP
#define CPPA_FSM_ACTOR_HPP
#include <utility>
#include <type_traits>
#include "cppa/event_based_actor.hpp"
namespace cppa {
......@@ -41,8 +44,11 @@ namespace cppa {
* to initialize the derived actor with its @p init_state member.
* @tparam Derived Direct subclass of @p sb_actor.
*/
template<class Derived>
class sb_actor : public event_based_actor {
template<class Derived, class Base = event_based_actor>
class sb_actor : public Base {
static_assert(std::is_base_of<event_based_actor,Base>::value,
"Base must be either event_based_actor or a derived type");
public:
......@@ -50,7 +56,14 @@ class sb_actor : public event_based_actor {
* @brief Overrides {@link event_based_actor::init()} and sets
* the initial actor behavior to <tt>Derived::init_state</tt>.
*/
void init() { become(static_cast<Derived*>(this)->init_state); }
virtual void init() {
this->become(static_cast<Derived*>(this)->init_state);
}
protected:
template<typename... Args>
sb_actor(Args&&... args) : Base(std::forward<Args>(args)...) { }
};
......
......@@ -44,6 +44,17 @@ struct A : popular_actor {
}
};
#ifdef __clang__
struct B : sb_actor<B,popular_actor> {
B(const actor_ptr& buddy) : sb_actor<B,popular_actor>(buddy) { }
behavior init_state = (
others() >> [=] {
forward_to(buddy());
quit();
}
);
};
#else
struct B : popular_actor {
B(const actor_ptr& buddy) : popular_actor(buddy) { }
void init() {
......@@ -55,16 +66,15 @@ struct B : popular_actor {
);
}
};
#endif
struct C : event_based_actor {
void init() {
become (
on(atom("gogo")) >> [=] {
reply(atom("gogogo"));
quit();
}
);
}
struct C : sb_actor<C> {
behavior init_state = (
on(atom("gogo")) >> [=] {
reply(atom("gogogo"));
self->quit();
}
);
};
......
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