Commit 97dffef0 authored by Dominik Charousset's avatar Dominik Charousset

Allow non-const default handlers

parent e8234af5
...@@ -20,6 +20,8 @@ is based on [Keep a Changelog](https://keepachangelog.com). ...@@ -20,6 +20,8 @@ is based on [Keep a Changelog](https://keepachangelog.com).
properly back off, causing high CPU load due to spinning and in some scenarios properly back off, causing high CPU load due to spinning and in some scenarios
never recovering. This issue has been resolved by properly handling never recovering. This issue has been resolved by properly handling
`SSL_ERROR_WANT_READ` on the transport (#1060). `SSL_ERROR_WANT_READ` on the transport (#1060).
- Scheduled actors now accept default handlers for down messages etc. with
non-const apply operator such as lambda expressions declared as `mutable`.
### Removed ### Removed
......
...@@ -294,10 +294,12 @@ public: ...@@ -294,10 +294,12 @@ public:
/// Sets a custom handler for unexpected messages. /// Sets a custom handler for unexpected messages.
template <class F> template <class F>
typename std::enable_if<std::is_convertible< std::enable_if_t<std::is_invocable_r_v<skippable_result, F, message&>>
F, std::function<skippable_result(message&)>>::value>::type
set_default_handler(F fun) { set_default_handler(F fun) {
default_handler_ = [=](scheduled_actor*, message& xs) { return fun(xs); }; using std::move;
default_handler_ = [fn{move(fun)}](scheduled_actor*, message& xs) mutable {
return fn(xs);
};
} }
/// Sets a custom handler for error messages. /// Sets a custom handler for error messages.
...@@ -309,9 +311,11 @@ public: ...@@ -309,9 +311,11 @@ public:
} }
/// Sets a custom handler for error messages. /// Sets a custom handler for error messages.
template <class T> template <class F>
auto set_error_handler(T fun) -> decltype(fun(std::declval<error&>())) { std::enable_if_t<std::is_invocable_v<F, error&>> set_error_handler(F fun) {
set_error_handler([fun](scheduled_actor*, error& x) { fun(x); }); error_handler_ = [fn{std::move(fun)}](scheduled_actor*, error& x) mutable {
fn(x);
};
} }
/// Sets a custom handler for down messages. /// Sets a custom handler for down messages.
...@@ -323,9 +327,12 @@ public: ...@@ -323,9 +327,12 @@ public:
} }
/// Sets a custom handler for down messages. /// Sets a custom handler for down messages.
template <class T> template <class F>
auto set_down_handler(T fun) -> decltype(fun(std::declval<down_msg&>())) { std::enable_if_t<std::is_invocable_v<F, down_msg&>> set_down_handler(F fun) {
set_down_handler([fun](scheduled_actor*, down_msg& x) { fun(x); }); using std::move;
down_handler_ = [fn{move(fun)}](scheduled_actor*, down_msg& x) mutable {
fn(x);
};
} }
/// Sets a custom handler for node down messages. /// Sets a custom handler for node down messages.
...@@ -337,11 +344,13 @@ public: ...@@ -337,11 +344,13 @@ public:
} }
/// Sets a custom handler for down messages. /// Sets a custom handler for down messages.
template <class T> template <class F>
auto set_node_down_handler(T fun) std::enable_if_t<std::is_invocable_v<F, node_down_msg&>>
-> decltype(fun(std::declval<node_down_msg&>())) { set_node_down_handler(F fun) {
set_node_down_handler( node_down_handler_ = [fn{std::move(fun)}](scheduled_actor*,
[fun](scheduled_actor*, node_down_msg& x) { fun(x); }); node_down_msg& x) mutable {
fn(x);
};
} }
/// Sets a custom handler for error messages. /// Sets a custom handler for error messages.
...@@ -353,9 +362,12 @@ public: ...@@ -353,9 +362,12 @@ public:
} }
/// Sets a custom handler for exit messages. /// Sets a custom handler for exit messages.
template <class T> template <class F>
auto set_exit_handler(T fun) -> decltype(fun(std::declval<exit_msg&>())) { std::enable_if_t<std::is_invocable_v<F, exit_msg&>> set_exit_handler(F fun) {
set_exit_handler([fun](scheduled_actor*, exit_msg& x) { fun(x); }); using std::move;
exit_handler_ = [fn{move(fun)}](scheduled_actor*, exit_msg& x) mutable {
fn(x);
};
} }
#ifdef CAF_ENABLE_EXCEPTIONS #ifdef CAF_ENABLE_EXCEPTIONS
...@@ -371,11 +383,12 @@ public: ...@@ -371,11 +383,12 @@ public:
/// Sets a custom exception handler for this actor. If multiple handlers are /// Sets a custom exception handler for this actor. If multiple handlers are
/// defined, only the functor that was added *last* is being executed. /// defined, only the functor that was added *last* is being executed.
template <class F> template <class F>
typename std::enable_if<std::is_convertible< std::enable_if_t<std::is_invocable_r_v<error, F, std::exception_ptr&>>
F, std::function<error(std::exception_ptr&)>>::value>::type set_exception_handler(F fun) {
set_exception_handler(F f) { exception_handler_ = [fn{std::move(fun)}](scheduled_actor*,
set_exception_handler( std::exception_ptr& x) mutable {
[f](scheduled_actor*, std::exception_ptr& x) { return f(x); }); return fn(x);
};
} }
#endif // CAF_ENABLE_EXCEPTIONS #endif // CAF_ENABLE_EXCEPTIONS
......
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