Permalink
Browse files

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`
  • Loading branch information...
1 parent 09a6675 commit 6d12f13a2fa500eb6a152244e1fb1cb9d45d67a0 @Neverlord Neverlord committed Feb 4, 2013
Showing with 35 additions and 12 deletions.
  1. +16 −3 cppa/sb_actor.hpp
  2. +19 −9 unit_testing/test__sync_send.cpp
View
19 cppa/sb_actor.hpp
@@ -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,16 +44,26 @@ 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:
/**
* @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)...) { }
};
View
28 unit_testing/test__sync_send.cpp
@@ -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();
+ }
+ );
};

0 comments on commit 6d12f13

Please sign in to comment.