Commit f765d3dd authored by Dominik Charousset's avatar Dominik Charousset

moved behavior_impl and timeout_definition to their own headers

parent d33d5b2c
......@@ -263,3 +263,5 @@ cppa/factory.hpp
src/factory.cpp
cppa/message_id.hpp
cppa/cppa_fwd.hpp
cppa/timeout_definition.hpp
cppa/detail/behavior_impl.hpp
......@@ -36,6 +36,7 @@
#include "cppa/match_expr.hpp"
#include "cppa/partial_function.hpp"
#include "cppa/timeout_definition.hpp"
#include "cppa/util/tbind.hpp"
#include "cppa/util/rm_ref.hpp"
......@@ -49,42 +50,41 @@ namespace cppa {
/**
* @brief Describes the behavior of an actor.
*/
class behavior : public partial_function {
class behavior {
typedef partial_function super;
public:
typedef intrusive_ptr<detail::behavior_impl> impl_ptr;
behavior() = default;
behavior(behavior&&) = default;
behavior(const behavior&) = default;
behavior& operator=(behavior&&) = default;
behavior& operator=(const behavior&) = default;
inline behavior(partial_function fun) : super(std::move(fun)) { }
inline behavior(partial_function fun) : m_impl(std::move(fun.m_impl)) { }
inline behavior(partial_function::impl_ptr ptr) : super(std::move(ptr)) { }
inline behavior(partial_function::impl_ptr ptr) : m_impl(std::move(ptr)) { }
template<typename F>
behavior(const timeout_definition<F>& arg)
: super(new default_behavior_impl<dummy_match_expr, F>(dummy_match_expr{},
: m_impl(detail::new_default_behavior_impl(detail::dummy_match_expr{},
arg.timeout,
arg.handler)) {
}
arg.handler) ) { }
template<typename F>
behavior(util::duration d, F f)
: super(new default_behavior_impl<dummy_match_expr, F>(dummy_match_expr{},
d, f)) {
}
: m_impl(detail::new_default_behavior_impl(detail::dummy_match_expr{}, d, f)) { }
template<typename... Cases>
behavior(const match_expr<Cases...>& arg0)
: super(arg0) { }
: m_impl(detail::new_default_behavior_impl(arg0, util::duration(), []() { })) { }
template<typename... Cases, typename Arg1, typename... Args>
behavior(const match_expr<Cases...>& arg0, const Arg1& arg1, const Args&... args)
: super(match_expr_concat(arg0, arg1, args...)) { }
: m_impl(match_expr_concat(arg0, arg1, args...)) { }
inline void handle_timeout() {
m_impl->handle_timeout();
......@@ -95,9 +95,18 @@ class behavior : public partial_function {
}
inline bool undefined() const {
return m_impl == nullptr && m_impl->timeout().valid();
return m_impl == nullptr || m_impl->timeout().valid() == false;
}
template<typename T>
inline bool operator()(T&& arg) {
return (m_impl) && m_impl->invoke(std::forward<T>(arg));
}
private:
impl_ptr m_impl;
};
template<typename Arg0, typename... Args>
......
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011, 2012 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License *
* or (at your option) any later version. *
* *
* libcppa is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#ifndef BEHAVIOR_IMPL_HPP
#define BEHAVIOR_IMPL_HPP
#include "cppa/ref_counted.hpp"
#include "cppa/util/duration.hpp"
namespace cppa { namespace detail {
class behavior_impl : public ref_counted {
public:
behavior_impl() = default;
inline behavior_impl(util::duration tout) : m_timeout(tout) { }
virtual bool invoke(any_tuple&) = 0;
virtual bool invoke(const any_tuple&) = 0;
inline bool invoke(any_tuple&& arg) {
any_tuple tmp(std::move(arg));
return invoke(tmp);
}
virtual bool defined_at(const any_tuple&) = 0;
virtual void handle_timeout();
inline const util::duration& timeout() const {
return m_timeout;
}
private:
util::duration m_timeout;
};
struct dummy_match_expr {
inline bool invoke(const any_tuple&) { return false; }
inline bool can_invoke(const any_tuple&) { return false; }
};
template<class MatchExpr, typename F>
class default_behavior_impl : public behavior_impl {
typedef behavior_impl super;
public:
template<typename Expr>
default_behavior_impl(Expr&& expr, const timeout_definition<F>& d)
: super(d.timeout), m_expr(std::forward<Expr>(expr)), m_fun(d.handler) { }
template<typename Expr>
default_behavior_impl(Expr&& expr, util::duration tout, F f)
: super(tout), m_expr(std::forward<Expr>(expr)), m_fun(f) { }
bool invoke(any_tuple& tup) {
return m_expr.invoke(tup);
}
bool invoke(const any_tuple& tup) {
return m_expr.invoke(tup);
}
bool defined_at(const any_tuple& tup) {
return m_expr.can_invoke(tup);
}
void handle_timeout() { m_fun(); }
private:
MatchExpr m_expr;
F m_fun;
};
template<class MatchExpr, typename F>
default_behavior_impl<MatchExpr, F>* new_default_behavior_impl(const MatchExpr& mexpr, util::duration d, F f) {
return new default_behavior_impl<MatchExpr, F>(mexpr, d, f);
}
} } // namespace cppa::detail
#endif // BEHAVIOR_IMPL_HPP
......@@ -118,6 +118,11 @@ class do_receive_helper {
static_assert(std::is_same<bool, decltype(stmt())>::value,
"functor or function does not return a boolean");
local_actor* sptr = self;
do {
sptr->dequeue(m_bhvr);
}
while (stmt() == false);
/*
if (m_bhvr.timeout().valid()) {
do {
sptr->dequeue(m_bhvr);
......@@ -131,6 +136,7 @@ class do_receive_helper {
}
while (stmt() == false);
}
*/
}
};
......
......@@ -131,20 +131,24 @@ class receive_policy {
return false;
}
template<class Client>
void receive(Client* client, partial_function& fun) {
template<class Client, class FunOrBehavior>
inline void receive_wo_timeout(Client *client, FunOrBehavior& fun) {
if (invoke_from_cache(client, fun) == false) {
while (invoke(client, client->receive_node(), fun) == false) { }
}
}
template<class Client>
void receive(Client* client, partial_function& fun) {
receive_wo_timeout(client, fun);
}
template<class Client>
void receive(Client* client, behavior& bhvr) {
partial_function& fun = bhvr;
if (bhvr.timeout().valid() == false) {
receive(client, fun);
receive_wo_timeout(client, bhvr);
}
else if (invoke_from_cache(client, fun) == false) {
else if (invoke_from_cache(client, bhvr) == false) {
if (bhvr.timeout().is_zero()) {
pointer e = nullptr;
while ((e = client->try_receive_node()) != nullptr) {
......
......@@ -715,7 +715,7 @@ class match_expr {
return m_cases;
}
struct pfun_impl : behavior_impl {
struct pfun_impl : detail::behavior_impl {
match_expr pfun;
template<typename Arg>
pfun_impl(const Arg& from) : pfun(from) { }
......@@ -879,7 +879,7 @@ inline match_expr<Lhs..., Rhs...> operator,(const match_expr<Lhs...>& lhs,
template<bool HasTimeout>
struct match_expr_concat_impl {
template<typename Arg0, typename... Args>
static behavior_impl* _(const Arg0& arg0, const Args&... args) {
static detail::behavior_impl* _(const Arg0& arg0, const Args&... args) {
typename detail::tdata_from_type_list<
typename util::tl_map<
typename util::tl_concat<
......@@ -898,7 +898,8 @@ struct match_expr_concat_impl {
>::type
combined_type;
auto lvoid = []() { };
typedef default_behavior_impl<combined_type, decltype(lvoid)> impl_type;
typedef detail::default_behavior_impl<combined_type, decltype(lvoid)>
impl_type;
detail::collect_tdata(all_cases, arg0.cases(), args.cases()...);
return new impl_type(all_cases, util::duration{}, lvoid);
}
......@@ -908,14 +909,14 @@ template<>
struct match_expr_concat_impl<true> {
template<class TData, class Token, typename F>
static behavior_impl* __(const TData& data, Token, const timeout_definition<F>& arg0) {
static detail::behavior_impl* __(const TData& data, Token, const timeout_definition<F>& arg0) {
typedef typename match_expr_from_type_list<Token>::type combined_type;
typedef default_behavior_impl<combined_type, F> impl_type;
typedef detail::default_behavior_impl<combined_type, F> impl_type;
return new impl_type(data, arg0);
}
template<class TData, class Token, typename... Cases, typename... Args>
static behavior_impl* __(const TData& data, Token, const match_expr<Cases...>& arg0, const Args&... args) {
static detail::behavior_impl* __(const TData& data, Token, const match_expr<Cases...>& arg0, const Args&... args) {
typedef typename util::tl_concat<
Token,
util::type_list<Cases...>
......@@ -934,13 +935,13 @@ struct match_expr_concat_impl<true> {
}
template<typename F>
static behavior_impl* _(const timeout_definition<F>& arg0) {
typedef default_behavior_impl<dummy_match_expr, F> impl_type;
return new impl_type(dummy_match_expr{}, arg0);
static detail::behavior_impl* _(const timeout_definition<F>& arg0) {
typedef detail::default_behavior_impl<detail::dummy_match_expr, F> impl_type;
return new impl_type(detail::dummy_match_expr{}, arg0);
}
template<typename... Cases, typename... Args>
static behavior_impl* _(const match_expr<Cases...>& arg0, const Args&... args) {
static detail::behavior_impl* _(const match_expr<Cases...>& arg0, const Args&... args) {
util::type_list<Cases...> token;
typename detail::tdata_from_type_list<
typename util::tl_map<
......@@ -956,7 +957,7 @@ struct match_expr_concat_impl<true> {
};
template<typename Arg0, typename... Args>
intrusive_ptr<behavior_impl> match_expr_concat(const Arg0& arg0,
intrusive_ptr<detail::behavior_impl> match_expr_concat(const Arg0& arg0,
const Args&... args) {
constexpr bool has_timeout = util::disjunction<
is_timeout_definition<Arg0>,
......
......@@ -39,83 +39,15 @@
#include "cppa/any_tuple.hpp"
#include "cppa/ref_counted.hpp"
#include "cppa/intrusive_ptr.hpp"
#include "cppa/util/duration.hpp"
namespace cppa {
class behavior_impl : public ref_counted {
public:
behavior_impl() = default;
behavior_impl(util::duration tout);
virtual bool invoke(any_tuple&) = 0;
virtual bool invoke(const any_tuple&) = 0;
virtual bool defined_at(const any_tuple&) = 0;
virtual void handle_timeout();
inline const util::duration& timeout() const {
return m_timeout;
}
private:
util::duration m_timeout;
};
template<typename F>
struct timeout_definition {
util::duration timeout;
F handler;
};
template<typename T>
struct is_timeout_definition : std::false_type { };
template<typename F>
struct is_timeout_definition<timeout_definition<F> > : std::true_type { };
struct dummy_match_expr {
inline bool invoke(const any_tuple&) { return false; }
inline bool can_invoke(const any_tuple&) { return false; }
};
template<class MatchExpr, typename F>
class default_behavior_impl : public behavior_impl {
#include "cppa/timeout_definition.hpp"
typedef behavior_impl super;
public:
template<typename Expr>
default_behavior_impl(Expr&& expr, const timeout_definition<F>& d)
: super(d.timeout), m_expr(std::forward<Expr>(expr)), m_fun(d.handler) { }
template<typename Expr>
default_behavior_impl(Expr&& expr, util::duration tout, F f)
: super(tout), m_expr(std::forward<Expr>(expr)), m_fun(f) { }
bool invoke(any_tuple& tup) {
return m_expr.invoke(tup);
}
bool invoke(const any_tuple& tup) {
return m_expr.invoke(tup);
}
bool defined_at(const any_tuple& tup) {
return m_expr.can_invoke(tup);
}
void handle_timeout() { m_fun(); }
#include "cppa/util/duration.hpp"
private:
#include "cppa/detail/behavior_impl.hpp"
MatchExpr m_expr;
F m_fun;
namespace cppa {
};
class behavior;
/**
* @brief A partial function implementation
......@@ -123,9 +55,11 @@ class default_behavior_impl : public behavior_impl {
*/
class partial_function {
friend class behavior;
public:
typedef intrusive_ptr<behavior_impl> impl_ptr;
typedef intrusive_ptr<detail::behavior_impl> impl_ptr;
partial_function() = default;
partial_function(partial_function&&) = default;
......@@ -143,22 +77,14 @@ class partial_function {
return (m_impl) && m_impl->defined_at(value);
}
inline bool operator()(any_tuple& value) {
return (m_impl) && m_impl->invoke(value);
template<typename T>
inline bool operator()(T&& arg) {
return (m_impl) && m_impl->invoke(std::forward<T>(arg));
}
inline bool operator()(const any_tuple& value) {
return (m_impl) && m_impl->invoke(value);
}
inline bool operator()(any_tuple&& value) {
any_tuple cpy{std::move(value)};
return (*this)(cpy);
}
protected:
private:
intrusive_ptr<behavior_impl> m_impl;
impl_ptr m_impl;
};
......
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011, 2012 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License *
* or (at your option) any later version. *
* *
* libcppa is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#ifndef TIMEOUT_DEFINITION_HPP
#define TIMEOUT_DEFINITION_HPP
#include "cppa/util/duration.hpp"
namespace cppa {
template<typename F>
struct timeout_definition {
util::duration timeout;
F handler;
};
template<typename T>
struct is_timeout_definition : std::false_type { };
template<typename F>
struct is_timeout_definition<timeout_definition<F> > : std::true_type { };
} // namespace cppa
#endif // TIMEOUT_DEFINITION_HPP
......@@ -38,8 +38,6 @@ namespace cppa {
partial_function::partial_function(impl_ptr ptr) : m_impl(std::move(ptr)) { }
behavior_impl::behavior_impl(util::duration tout) : m_timeout(tout) { }
void behavior_impl::handle_timeout() { }
void detail::behavior_impl::handle_timeout() { }
} // namespace cppa
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