Commit 38490411 authored by neverlord's avatar neverlord

fixed math_actor_example makefile

parent 4f73fcf5
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
namespace cppa { namespace detail { namespace cppa { namespace detail {
// note: implemented in post_office.cpp
class network_manager class network_manager
{ {
......
...@@ -159,6 +159,8 @@ class invoke_rules : public invoke_rules_base ...@@ -159,6 +159,8 @@ class invoke_rules : public invoke_rules_base
timed_invoke_rules operator,(timed_invoke_rules&& other); timed_invoke_rules operator,(timed_invoke_rules&& other);
invoke_rules& operator=(invoke_rules&& other);
private: private:
invoke_rules(invokable_list&& ll); invoke_rules(invokable_list&& ll);
......
...@@ -5,13 +5,13 @@ ...@@ -5,13 +5,13 @@
namespace cppa { namespace cppa {
/** /*
* @brief Dequeues the next message from the mailbox. * @brief Dequeues the next message from the mailbox.
*/ */
inline const any_tuple& receive() //inline const any_tuple& receive()
{ //{
return self()->mailbox().dequeue(); // return self()->mailbox().dequeue();
} //}
/** /**
* @brief Dequeues the next message from the mailbox that's matched * @brief Dequeues the next message from the mailbox that's matched
...@@ -54,25 +54,25 @@ void receive(invoke_rules& rules, Head&& head, Tail&&... tail) ...@@ -54,25 +54,25 @@ void receive(invoke_rules& rules, Head&& head, Tail&&... tail)
std::forward<Tail>(tail)...); std::forward<Tail>(tail)...);
} }
/** /*
* @brief Tries to dequeue the next message from the mailbox. * @brief Tries to dequeue the next message from the mailbox.
* @returns @p true if a messages was dequeued; * @returns @p true if a messages was dequeued;
* @p false if the mailbox is empty * @p false if the mailbox is empty
*/ */
inline bool try_receive(any_tuple& msg) //inline bool try_receive(any_tuple& msg)
{ //{
return self()->mailbox().try_dequeue(msg); // return self()->mailbox().try_dequeue(msg);
} //}
/** /*
* @brief Tries to dequeue the next message from the mailbox. * @brief Tries to dequeue the next message from the mailbox.
* @returns @p true if a messages was dequeued; * @returns @p true if a messages was dequeued;
* @p false if the mailbox is empty * @p false if the mailbox is empty
*/ */
inline bool try_receive(invoke_rules& rules) //inline bool try_receive(invoke_rules& rules)
{ //{
return self()->mailbox().try_dequeue(rules); // return self()->mailbox().try_dequeue(rules);
} //}
} // namespace cppa } // namespace cppa
......
...@@ -72,7 +72,7 @@ serializer& operator<<(serializer& s, const T& what) ...@@ -72,7 +72,7 @@ serializer& operator<<(serializer& s, const T& what)
if (mtype == nullptr) if (mtype == nullptr)
{ {
throw std::logic_error( "no uniform type info found for " throw std::logic_error( "no uniform type info found for "
+ cppa::detail::to_uniform_name(typeid(T))); + cppa::detail::to_uniform_name(typeid(T)));
} }
mtype->serialize(&what, &s); mtype->serialize(&what, &s);
return s; return s;
......
...@@ -9,7 +9,7 @@ noinst_PROGRAMS = announce_example_1 \ ...@@ -9,7 +9,7 @@ noinst_PROGRAMS = announce_example_1 \
announce_example_4 \ announce_example_4 \
announce_example_5 \ announce_example_5 \
hello_world_example \ hello_world_example \
math_example math_actor_example
announce_example_1_SOURCES = announce_example_1.cpp announce_example_1_SOURCES = announce_example_1.cpp
announce_example_2_SOURCES = announce_example_2.cpp announce_example_2_SOURCES = announce_example_2.cpp
...@@ -17,7 +17,7 @@ announce_example_3_SOURCES = announce_example_3.cpp ...@@ -17,7 +17,7 @@ announce_example_3_SOURCES = announce_example_3.cpp
announce_example_4_SOURCES = announce_example_4.cpp announce_example_4_SOURCES = announce_example_4.cpp
announce_example_5_SOURCES = announce_example_5.cpp announce_example_5_SOURCES = announce_example_5.cpp
hello_world_example_SOURCES = hello_world_example.cpp hello_world_example_SOURCES = hello_world_example.cpp
math_example_SOURCES = math_example.cpp math_actor_example_SOURCES = math_actor_example.cpp
EXAMPLES_LIBS = $(BOOST_LDFLAGS) $(BOOST_THREAD_LIB) -L../.libs/ -lcppa EXAMPLES_LIBS = $(BOOST_LDFLAGS) $(BOOST_THREAD_LIB) -L../.libs/ -lcppa
...@@ -27,5 +27,5 @@ announce_example_3_LDADD = $(EXAMPLES_LIBS) ...@@ -27,5 +27,5 @@ announce_example_3_LDADD = $(EXAMPLES_LIBS)
announce_example_4_LDADD = $(EXAMPLES_LIBS) announce_example_4_LDADD = $(EXAMPLES_LIBS)
announce_example_5_LDADD = $(EXAMPLES_LIBS) announce_example_5_LDADD = $(EXAMPLES_LIBS)
hello_world_example_LDADD = $(EXAMPLES_LIBS) hello_world_example_LDADD = $(EXAMPLES_LIBS)
math_example_LDADD = $(EXAMPLES_LIBS) math_actor_example_LDADD = $(EXAMPLES_LIBS)
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
#include <iostream> #include <iostream>
#include "cppa/cppa.hpp" #include "cppa/cppa.hpp"
using std::cout;
using std::endl;
using namespace cppa; using namespace cppa;
void math_actor() void math_actor()
......
...@@ -137,4 +137,11 @@ invoke_rules invoke_rules::operator,(invoke_rules&& other) ...@@ -137,4 +137,11 @@ invoke_rules invoke_rules::operator,(invoke_rules&& other)
return std::move(m_list); return std::move(m_list);
} }
invoke_rules& invoke_rules::operator=(invoke_rules&& other)
{
m_list = std::move(other.m_list);
other.m_list.clear();
return *this;
}
} // namespace cppa } // namespace cppa
...@@ -56,7 +56,7 @@ size_t test__atom() ...@@ -56,7 +56,7 @@ size_t test__atom()
} }
); );
CPPA_CHECK(matched_pattern[0] && matched_pattern[1] && matched_pattern[2]); CPPA_CHECK(matched_pattern[0] && matched_pattern[1] && matched_pattern[2]);
any_tuple msg = receive(); any_tuple msg = self()->mailbox().dequeue();
CPPA_CHECK(try_receive(msg) == false); CPPA_CHECK(self()->mailbox().try_dequeue(msg) == false);
return CPPA_TEST_RESULT; return CPPA_TEST_RESULT;
} }
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
using std::cout; using std::cout;
using std::endl; using std::endl;
using std::string;
using namespace cppa; using namespace cppa;
size_t test__local_group() size_t test__local_group()
...@@ -56,6 +57,6 @@ size_t test__local_group() ...@@ -56,6 +57,6 @@ size_t test__local_group()
.until([&result]() { return result == 10; }); .until([&result]() { return result == 10; });
await_all_others_done(); await_all_others_done();
any_tuple tmp; any_tuple tmp;
CPPA_CHECK_EQUAL(try_receive(tmp), false); CPPA_CHECK_EQUAL(self()->mailbox().try_dequeue(tmp), false);
return CPPA_TEST_RESULT; return CPPA_TEST_RESULT;
} }
#include <stack>
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <functional> #include <functional>
...@@ -18,6 +19,130 @@ using std::endl; ...@@ -18,6 +19,130 @@ using std::endl;
using namespace cppa; using namespace cppa;
namespace { const any_tuple* s_lm = nullptr; }
class event_actor
{
std::stack<invoke_rules> m_behavior;
invoke_rules m_next_behavior;
invoke_rules* m_behavior_ptr;
public:
event_actor(invoke_rules&& behavior)
{
m_behavior.push(std::move(behavior));
m_behavior_ptr = &(m_behavior.top());
}
void become(invoke_rules&& behavior)
{
m_behavior.push(std::move(behavior));
m_behavior_ptr = &(m_behavior.top());
}
void set_next_behavior(invoke_rules&& behavior)
{
m_next_behavior = std::move(behavior);
m_behavior_ptr = &m_next_behavior;
}
void unbecome()
{
if (!m_behavior.empty())
{
if (m_behavior_ptr == &(m_behavior.top()))
{
m_behavior.pop();
m_behavior_ptr = m_behavior.empty() ? nullptr
: &(m_behavior.top());
}
else
{
m_behavior.pop();
}
}
}
void operator()(const any_tuple& msg)
{
if (m_behavior_ptr != nullptr)
{
s_lm = &msg;
auto ptr = m_behavior_ptr;
(*ptr)(msg);
// execute m_next_behavior at most once
if (ptr == m_behavior_ptr && ptr == &m_next_behavior)
{
m_behavior_ptr = m_behavior.empty() ? nullptr
: &m_behavior.top();
}
}
}
};
namespace { event_actor* s_event_actor_self = nullptr; }
void set_next_behavior(invoke_rules&& behavior)
{
s_event_actor_self->set_next_behavior(std::move(behavior));
}
void become(invoke_rules&& behavior)
{
s_event_actor_self->become(std::move(behavior));
}
void unbecome()
{
s_event_actor_self->unbecome();
}
event_actor* event_testee()
{
return new event_actor
{(
on<int>() >> [](int i)
{
// do NOT call receive() here!
// this would hijack the worker thread
set_next_behavior
((
on<int>() >> [=](int i2)
{
cout << "event testee: (" << i << ", " << i2 << ")" << endl;
},
on<float>() >> [=](float f)
{
cout << "event testee: (" << i << ", " << f << ")" << endl;
become
((
on<float>() >> []()
{
unbecome();
},
others() >> []()
{
cout << "event testee[line " << __LINE__ << "]: "
<< to_string(*s_lm)
<< endl;
}
));
}
));
},
others() >> []()
{
cout << "event testee[line " << __LINE__ << "]: "
<< to_string(*s_lm)
<< endl;
}
)};
}
void testee1() void testee1()
{ {
receive_loop receive_loop
...@@ -69,6 +194,18 @@ size_t test__spawn() ...@@ -69,6 +194,18 @@ size_t test__spawn()
spawn(testee1); spawn(testee1);
await_all_others_done(); await_all_others_done();
auto et = event_testee();
s_event_actor_self = et;
(*et)(make_tuple(42));
(*et)(make_tuple(24));
(*et)(make_tuple(42));
(*et)(make_tuple(.24f));
(*et)(make_tuple("hello event actor"));
(*et)(make_tuple(42));
(*et)(make_tuple(.24f));
(*et)(make_tuple("hello event actor"));
delete et;
return CPPA_TEST_RESULT; return CPPA_TEST_RESULT;
...@@ -128,7 +265,7 @@ size_t test__spawn() ...@@ -128,7 +265,7 @@ size_t test__spawn()
CPPA_CHECK_EQUAL(flags, 0x07); CPPA_CHECK_EQUAL(flags, 0x07);
// mailbox has to be empty // mailbox has to be empty
any_tuple msg; any_tuple msg;
while (try_receive(msg)) while (self()->mailbox().try_dequeue(msg))
{ {
report_unexpected(); report_unexpected();
} }
......
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