Commit 196b8080 authored by neverlord's avatar neverlord

behavior typedef

parent f4912f83
...@@ -78,6 +78,7 @@ nobase_library_include_HEADERS = \ ...@@ -78,6 +78,7 @@ nobase_library_include_HEADERS = \
cppa/anything.hpp \ cppa/anything.hpp \
cppa/atom.hpp \ cppa/atom.hpp \
cppa/attachable.hpp \ cppa/attachable.hpp \
cppa/behavior.hpp \
cppa/binary_deserializer.hpp \ cppa/binary_deserializer.hpp \
cppa/binary_serializer.hpp \ cppa/binary_serializer.hpp \
cppa/channel.hpp \ cppa/channel.hpp \
......
...@@ -242,3 +242,4 @@ cppa/event_based_actor_mixin.hpp ...@@ -242,3 +242,4 @@ cppa/event_based_actor_mixin.hpp
cppa/fsm_actor.hpp cppa/fsm_actor.hpp
cppa/self.hpp cppa/self.hpp
src/self.cpp src/self.cpp
cppa/behavior.hpp
...@@ -27,7 +27,7 @@ class actor : public channel ...@@ -27,7 +27,7 @@ class actor : public channel
{ {
bool m_is_proxy; bool m_is_proxy;
std::uint32_t m_id; actor_id m_id;
process_information_ptr m_parent_process; process_information_ptr m_parent_process;
protected: protected:
...@@ -163,14 +163,14 @@ class actor : public channel ...@@ -163,14 +163,14 @@ class actor : public channel
* the process it's executed in. * the process it's executed in.
* @returns The unique identifier of this actor. * @returns The unique identifier of this actor.
*/ */
inline std::uint32_t id() const; inline actor_id id() const;
/** /**
* @brief Get the actor that has the unique identifier @p actor_id. * @brief Get the actor that has the unique identifier @p needle.
* @returns A pointer to the requestet actor or @c nullptr if no * @returns A pointer to the requestet actor or @c nullptr if no
* running actor with the ID @p actor_id was found in this process. * running actor with the ID @p actor_id was found in this process.
*/ */
static intrusive_ptr<actor> by_id(std::uint32_t actor_id); static intrusive_ptr<actor> by_id(actor_id needle);
inline bool is_proxy() const; inline bool is_proxy() const;
......
#ifndef BEHAVIOR_HPP
#define BEHAVIOR_HPP
#include "cppa/util/either.hpp"
#include "cppa/invoke_rules.hpp"
namespace cppa {
typedef util::either<invoke_rules, timed_invoke_rules> behavior;
} // namespace cppa
#endif // BEHAVIOR_HPP
#ifndef EVENT_BASED_ACTOR_HPP #ifndef EVENT_BASED_ACTOR_HPP
#define EVENT_BASED_ACTOR_HPP #define EVENT_BASED_ACTOR_HPP
#include "cppa/behavior.hpp"
#include "cppa/event_based_actor_mixin.hpp" #include "cppa/event_based_actor_mixin.hpp"
namespace cppa { namespace cppa {
...@@ -13,8 +14,10 @@ class event_based_actor : public event_based_actor_mixin<event_based_actor> ...@@ -13,8 +14,10 @@ class event_based_actor : public event_based_actor_mixin<event_based_actor>
typedef abstract_event_based_actor::stack_element stack_element; typedef abstract_event_based_actor::stack_element stack_element;
void clear(); void clear();
void do_become(invoke_rules* behavior, bool has_ownership); // has_ownership == false
void do_become(timed_invoke_rules* behavior, bool has_ownership); void do_become(behavior* bhvr);
void do_become(invoke_rules* bhvr, bool has_ownership);
void do_become(timed_invoke_rules* bhvr, bool has_ownership);
}; };
......
#ifndef EVENT_BASED_ACTOR_MIXIN_HPP #ifndef EVENT_BASED_ACTOR_MIXIN_HPP
#define EVENT_BASED_ACTOR_MIXIN_HPP #define EVENT_BASED_ACTOR_MIXIN_HPP
#include "cppa/behavior.hpp"
#include "cppa/abstract_event_based_actor.hpp" #include "cppa/abstract_event_based_actor.hpp"
#include <iostream>
using std::cout;
using std::endl;
namespace cppa { namespace cppa {
template<typename Derived> template<typename Derived>
...@@ -36,22 +33,27 @@ class event_based_actor_mixin : public abstract_event_based_actor ...@@ -36,22 +33,27 @@ class event_based_actor_mixin : public abstract_event_based_actor
protected: protected:
void become(invoke_rules* behavior) inline void become(behavior* bhvr)
{
d_this()->do_become(bhvr);
}
inline void become(invoke_rules* behavior)
{ {
d_this()->do_become(behavior, false); d_this()->do_become(behavior, false);
} }
void become(timed_invoke_rules* behavior) inline void become(timed_invoke_rules* behavior)
{ {
d_this()->do_become(behavior, false); d_this()->do_become(behavior, false);
} }
void become(invoke_rules&& behavior) inline void become(invoke_rules&& behavior)
{ {
d_this()->do_become(new invoke_rules(std::move(behavior)), true); d_this()->do_become(new invoke_rules(std::move(behavior)), true);
} }
void become(timed_invoke_rules&& behavior) inline void become(timed_invoke_rules&& behavior)
{ {
d_this()->do_become(new timed_invoke_rules(std::move(behavior)), true); d_this()->do_become(new timed_invoke_rules(std::move(behavior)), true);
} }
......
...@@ -7,20 +7,30 @@ void event_based_actor::clear() ...@@ -7,20 +7,30 @@ void event_based_actor::clear()
if (!m_loop_stack.empty()) m_loop_stack.pop(); if (!m_loop_stack.empty()) m_loop_stack.pop();
} }
void event_based_actor::do_become(invoke_rules* behavior, void event_based_actor::do_become(behavior* bhvr)
bool has_ownership) {
if (bhvr->is_left())
{
do_become(&(bhvr->left()), false);
}
else
{
do_become(&(bhvr->right()), false);
}
}
void event_based_actor::do_become(invoke_rules* bhvr, bool has_ownership)
{ {
clear(); clear();
reset_timeout(); reset_timeout();
m_loop_stack.push(stack_element(behavior, has_ownership)); m_loop_stack.push(stack_element(bhvr, has_ownership));
} }
void event_based_actor::do_become(timed_invoke_rules* behavior, void event_based_actor::do_become(timed_invoke_rules* bhvr, bool has_ownership)
bool has_ownership)
{ {
clear(); clear();
request_timeout(behavior->timeout()); request_timeout(bhvr->timeout());
m_loop_stack.push(stack_element(behavior, has_ownership)); m_loop_stack.push(stack_element(bhvr, has_ownership));
} }
} // namespace cppa } // namespace cppa
...@@ -28,7 +28,7 @@ using namespace cppa; ...@@ -28,7 +28,7 @@ using namespace cppa;
struct event_testee : public fsm_actor<event_testee> struct event_testee : public fsm_actor<event_testee>
{ {
invoke_rules wait4string = behavior wait4string =
( (
on<std::string>() >> [this]() on<std::string>() >> [this]()
{ {
...@@ -40,7 +40,7 @@ struct event_testee : public fsm_actor<event_testee> ...@@ -40,7 +40,7 @@ struct event_testee : public fsm_actor<event_testee>
} }
); );
invoke_rules wait4float = behavior wait4float =
( (
on<float>() >> [=]() on<float>() >> [=]()
{ {
...@@ -52,7 +52,7 @@ struct event_testee : public fsm_actor<event_testee> ...@@ -52,7 +52,7 @@ struct event_testee : public fsm_actor<event_testee>
} }
); );
invoke_rules init_state = behavior init_state =
( (
on<int>() >> [=]() on<int>() >> [=]()
{ {
...@@ -73,9 +73,9 @@ class event_testee : public fsm_actor<event_testee> ...@@ -73,9 +73,9 @@ class event_testee : public fsm_actor<event_testee>
friend class fsm_actor<event_testee>; friend class fsm_actor<event_testee>;
invoke_rules wait4string; behavior wait4string;
invoke_rules wait4float; behavior wait4float;
invoke_rules init_state; behavior init_state;
public: public:
...@@ -125,7 +125,12 @@ class event_testee : public fsm_actor<event_testee> ...@@ -125,7 +125,12 @@ class event_testee : public fsm_actor<event_testee>
on<atom("GetState")>() >> [=]() on<atom("GetState")>() >> [=]()
{ {
reply("init_state"); reply("init_state");
},
after(std::chrono::seconds(5)) >> [=]()
{
quit(exit_reason::user_defined);
} }
); );
} }
...@@ -307,14 +312,14 @@ size_t test__spawn() ...@@ -307,14 +312,14 @@ size_t test__spawn()
CPPA_CHECK_EQUAL(behavior_test<testee_actor>(), "init_state"); CPPA_CHECK_EQUAL(behavior_test<testee_actor>(), "init_state");
CPPA_CHECK_EQUAL(behavior_test<event_testee>(), "init_state"); CPPA_CHECK_EQUAL(behavior_test<event_testee>(), "init_state");
// create one million actors linked to one single actor // create 20,000 actors linked to one single actor
// and kill them all through killing the link // and kill them all through killing the link
auto my_link = spawn(new event_testee); // auto my_link = spawn(new event_testee);
for (int i = 0; i < 20000; ++i) // for (int i = 0; i < 20000; ++i)
{ // {
link(my_link, spawn(new event_testee)); // link(my_link, spawn(new event_testee));
} // }
send(my_link, atom(":Exit"), exit_reason::user_defined); // send(my_link, atom(":Exit"), exit_reason::user_defined);
return CPPA_TEST_RESULT; return CPPA_TEST_RESULT;
......
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