Commit c6c4aa65 authored by Dominik Charousset's avatar Dominik Charousset

Remove virtual `sync_handler` from local_actor

By re-organizing `continue_helper`, we no longer need a virtual member function
only for this purpose. Relates #180.
parent 682d1216
......@@ -184,7 +184,7 @@ class blocking_actor
return {make_dequeue_callback(), behavior{std::forward<Ts>(args)...}};
}
optional<behavior&> sync_handler(message_id msg_id) override {
optional<behavior&> sync_handler(message_id msg_id) {
auto i = m_sync_handler.find(msg_id);
if (i != m_sync_handler.end()) return i->second;
return none;
......
......@@ -38,8 +38,9 @@ class local_actor;
class continue_helper {
public:
using message_id_wrapper_tag = int;
using getter = std::function<optional<behavior&> (message_id msg_id)>;
continue_helper(message_id mid, local_actor* self);
continue_helper(message_id mid, getter get_sync_handler);
/**
* Adds the continuation `fun` to the synchronous message handler
......@@ -64,7 +65,8 @@ class continue_helper {
private:
message_id m_mid;
local_actor* m_self;
getter m_getter;
//local_actor* m_self;
};
} // namespace caf
......
......@@ -60,13 +60,8 @@ class sync_handle_helper;
* @extends abstract_actor
*/
class local_actor : public extend<abstract_actor>::with<mixin::memory_cached> {
using super = combined_type;
public:
using del = detail::disposer;
using mailbox_type = detail::single_reader_queue<mailbox_element, del>;
~local_actor();
......@@ -236,8 +231,7 @@ class local_actor : public extend<abstract_actor>::with<mixin::memory_cached> {
* Sends a message to `whom` that is delayed by `rel_time`.
*/
template <class... Ts>
void delayed_send(const channel& whom, const duration& rtime,
Ts&&... args) {
void delayed_send(const channel& whom, const duration& rtime, Ts&&... args) {
delayed_send_tuple(message_priority::normal, whom, rtime,
make_message(std::forward<Ts>(args)...));
}
......@@ -437,7 +431,9 @@ class local_actor : public extend<abstract_actor>::with<mixin::memory_cached> {
this->m_current_node = ptr;
}
inline mailbox_element* current_node() { return this->m_current_node; }
inline mailbox_element* current_node() {
return this->m_current_node;
}
inline message_id new_request_id() {
auto result = ++m_last_request_id;
......@@ -446,18 +442,20 @@ class local_actor : public extend<abstract_actor>::with<mixin::memory_cached> {
}
inline void handle_sync_timeout() {
if (m_sync_timeout_handler)
if (m_sync_timeout_handler) {
m_sync_timeout_handler();
else
} else {
quit(exit_reason::unhandled_sync_timeout);
}
}
inline void handle_sync_failure() {
if (m_sync_failure_handler)
if (m_sync_failure_handler) {
m_sync_failure_handler();
else
} else {
quit(exit_reason::unhandled_sync_failure);
}
}
// returns the response ID
message_id timed_sync_send_tuple_impl(message_priority mp,
......@@ -466,7 +464,8 @@ class local_actor : public extend<abstract_actor>::with<mixin::memory_cached> {
message&& what);
// returns the response ID
message_id sync_send_tuple_impl(message_priority mp, const actor& whom,
message_id sync_send_tuple_impl(message_priority mp,
const actor& whom,
message&& what);
// returns the response ID
......@@ -474,8 +473,7 @@ class local_actor : public extend<abstract_actor>::with<mixin::memory_cached> {
message_id sync_send_tuple_impl(message_priority mp,
const typed_actor<Rs...>& whom,
message&& msg) {
return sync_send_tuple_impl(mp, actor{whom.m_ptr.get()},
std::move(msg));
return sync_send_tuple_impl(mp, actor{whom.m_ptr.get()}, std::move(msg));
}
// returns 0 if last_dequeued() is an asynchronous or sync request message,
......@@ -513,12 +511,11 @@ class local_actor : public extend<abstract_actor>::with<mixin::memory_cached> {
void cleanup(uint32_t reason) override;
mailbox_element* dummy_node() { return &m_dummy_node; }
virtual optional<behavior&> sync_handler(message_id msg_id) = 0;
inline mailbox_element* dummy_node() {
return &m_dummy_node;
}
protected:
template <class... Ts>
inline mailbox_element* new_mailbox_element(Ts&&... args) {
return mailbox_element::create(std::forward<Ts>(args)...);
......@@ -546,10 +543,9 @@ class local_actor : public extend<abstract_actor>::with<mixin::memory_cached> {
/** @endcond */
private:
using super = combined_type;
std::function<void()> m_sync_failure_handler;
std::function<void()> m_sync_timeout_handler;
};
/**
......
......@@ -92,7 +92,7 @@ class behavior_stack_based_impl : public single_timeout<Base, Subtype> {
return m_bhvr_stack.back();
}
optional<behavior&> sync_handler(message_id msg_id) override {
optional<behavior&> sync_handler(message_id msg_id) {
return m_bhvr_stack.sync_handler(msg_id);
}
......
......@@ -60,18 +60,19 @@ class response_handle;
******************************************************************************/
template <class Self>
class response_handle<Self, message, nonblocking_response_handle_tag> {
public:
response_handle() = delete;
response_handle(const response_handle&) = default;
response_handle& operator=(const response_handle&) = default;
inline continue_helper then(behavior bhvr) const {
response_handle(message_id mid, Self* self) : m_mid(mid), m_self(self) {
// nop
}
continue_helper then(behavior bhvr) const {
m_self->bhvr_stack().push_back(std::move(bhvr), m_mid);
return {m_mid, m_self};
auto ptr = m_self;
return {m_mid, [ptr](message_id mid) { return ptr->sync_handler(mid); }};
}
template <class... Cs, class... Ts>
......@@ -81,21 +82,17 @@ class response_handle<Self, message, nonblocking_response_handle_tag> {
}
template <class... Fs>
typename std::enable_if<detail::all_callable<Fs...>::value,
continue_helper>::type
typename std::enable_if<
detail::all_callable<Fs...>::value,
continue_helper
>::type
then(Fs... fs) const {
return then(detail::fs2bhvr(m_self, fs...));
}
response_handle(const message_id& mid, Self* self)
: m_mid(mid), m_self(self) {}
private:
message_id m_mid;
Self* m_self;
};
/******************************************************************************
......@@ -104,35 +101,34 @@ class response_handle<Self, message, nonblocking_response_handle_tag> {
template <class Self, class... Ts>
class response_handle<Self, detail::type_list<Ts...>,
nonblocking_response_handle_tag> {
public:
response_handle() = delete;
response_handle(const response_handle&) = default;
response_handle& operator=(const response_handle&) = default;
template <class F, typename Enable = typename std::enable_if<
detail::is_callable<F>::value &&
!is_match_expr<F>::value>::type>
typed_continue_helper<typename detail::lifted_result_type<
typename detail::get_callable_trait<F>::result_type>::type>
response_handle(message_id mid, Self* self) : m_mid(mid), m_self(self) {
// nop
}
template <class F,
class Enable = typename std::enable_if<
detail::is_callable<F>::value
&& !is_match_expr<F>::value
>::type>
typed_continue_helper<
typename detail::lifted_result_type<
typename detail::get_callable_trait<F>::result_type
>::type>
then(F fun) {
detail::assert_types<detail::type_list<Ts...>, F>();
m_self->bhvr_stack().push_back(behavior{on_arg_match >> fun}, m_mid);
return {m_mid, m_self};
auto ptr = m_self;
return {m_mid, [ptr](message_id mid) { return ptr->sync_handler(mid); }};
}
response_handle(const message_id& mid, Self* self)
: m_mid(mid), m_self(self) {}
private:
message_id m_mid;
Self* m_self;
};
/******************************************************************************
......@@ -140,18 +136,20 @@ class response_handle<Self, detail::type_list<Ts...>,
******************************************************************************/
template <class Self>
class response_handle<Self, message, blocking_response_handle_tag> {
public:
response_handle() = delete;
response_handle(const response_handle&) = default;
response_handle& operator=(const response_handle&) = default;
void await(behavior& pfun) { m_self->dequeue_response(pfun, m_mid); }
response_handle(message_id mid, Self* self) : m_mid(mid), m_self(self) {
// nop
}
void await(behavior& pfun) {
m_self->dequeue_response(pfun, m_mid);
}
inline void await(behavior&& pfun) {
void await(behavior&& pfun) {
behavior tmp{std::move(pfun)};
await(tmp);
}
......@@ -168,15 +166,9 @@ class response_handle<Self, message, blocking_response_handle_tag> {
await(detail::fs2bhvr(m_self, fs...));
}
response_handle(const message_id& mid, Self* self)
: m_mid(mid), m_self(self) {}
private:
message_id m_mid;
Self* m_self;
};
/******************************************************************************
......@@ -185,17 +177,17 @@ class response_handle<Self, message, blocking_response_handle_tag> {
template <class Self, class... Ts>
class response_handle<Self, detail::type_list<Ts...>,
blocking_response_handle_tag> {
public:
using result_types = detail::type_list<Ts...>;
response_handle() = delete;
response_handle(const response_handle&) = default;
response_handle& operator=(const response_handle&) = default;
response_handle(message_id mid, Self* self) : m_mid(mid), m_self(self) {
// nop
}
template <class F>
void await(F fun) {
using arg_types =
......@@ -217,15 +209,9 @@ class response_handle<Self, detail::type_list<Ts...>,
m_self->dequeue_response(tmp, m_mid);
}
response_handle(const message_id& mid, Self* self)
: m_mid(mid), m_self(self) {}
private:
message_id m_mid;
Self* m_self;
};
} // namespace caf
......
......@@ -30,13 +30,18 @@ namespace caf {
template <class OutputList>
class typed_continue_helper {
public:
using message_id_wrapper_tag = int;
using getter = std::function<optional<behavior&> (message_id msg_id)>;
typed_continue_helper(message_id mid, local_actor* self)
: m_ch(mid, self) {}
typed_continue_helper(message_id mid, getter get_sync_handler)
: m_ch(mid, std::move(get_sync_handler)) {
// nop
}
typed_continue_helper(continue_helper ch) : m_ch(std::move(ch)) {
// nop
}
template <class F>
typed_continue_helper<typename detail::get_callable_trait<F>::result_type>
......@@ -46,14 +51,12 @@ class typed_continue_helper {
return {m_ch};
}
inline message_id get_message_id() const { return m_ch.get_message_id(); }
typed_continue_helper(continue_helper ch) : m_ch(std::move(ch)) {}
message_id get_message_id() const {
return m_ch.get_message_id();
}
private:
continue_helper m_ch;
};
} // namespace caf
......
......@@ -22,11 +22,11 @@
namespace caf {
continue_helper::continue_helper(message_id mid, local_actor* self)
: m_mid(mid), m_self(self) {}
continue_helper::continue_helper(message_id mid, getter g)
: m_mid(mid), m_getter(std::move(g)) {}
continue_helper& continue_helper::continue_with(behavior::continuation_fun f) {
auto ref_opt = m_self->sync_handler(m_mid);
auto ref_opt = m_getter(m_mid); //m_self->sync_handler(m_mid);
if (ref_opt) {
behavior cpy = *ref_opt;
*ref_opt = cpy.add_continuation(std::move(f));
......
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