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 @@ ...@@ -31,6 +31,9 @@
#ifndef CPPA_FSM_ACTOR_HPP #ifndef CPPA_FSM_ACTOR_HPP
#define CPPA_FSM_ACTOR_HPP #define CPPA_FSM_ACTOR_HPP
#include <utility>
#include <type_traits>
#include "cppa/event_based_actor.hpp" #include "cppa/event_based_actor.hpp"
namespace cppa { namespace cppa {
...@@ -41,8 +44,11 @@ namespace cppa { ...@@ -41,8 +44,11 @@ namespace cppa {
* to initialize the derived actor with its @p init_state member. * to initialize the derived actor with its @p init_state member.
* @tparam Derived Direct subclass of @p sb_actor. * @tparam Derived Direct subclass of @p sb_actor.
*/ */
template<class Derived> template<class Derived, class Base = event_based_actor>
class sb_actor : public 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: public:
...@@ -50,7 +56,14 @@ class sb_actor : public event_based_actor { ...@@ -50,7 +56,14 @@ class sb_actor : public event_based_actor {
* @brief Overrides {@link event_based_actor::init()} and sets * @brief Overrides {@link event_based_actor::init()} and sets
* the initial actor behavior to <tt>Derived::init_state</tt>. * 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 { ...@@ -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 { struct B : popular_actor {
B(const actor_ptr& buddy) : popular_actor(buddy) { } B(const actor_ptr& buddy) : popular_actor(buddy) { }
void init() { void init() {
...@@ -55,16 +66,15 @@ struct B : popular_actor { ...@@ -55,16 +66,15 @@ struct B : popular_actor {
); );
} }
}; };
#endif
struct C : event_based_actor { struct C : sb_actor<C> {
void init() { behavior init_state = (
become (
on(atom("gogo")) >> [=] { on(atom("gogo")) >> [=] {
reply(atom("gogogo")); reply(atom("gogogo"));
quit(); 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