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