CppWAMP
C++11 client library for the WAMP protocol
|
Go to the documentation of this file.
7 #ifndef CPPWAMP_ANYHANDLER_HPP
8 #define CPPWAMP_ANYHANDLER_HPP
21 #include <type_traits>
22 #include <boost/asio/associated_allocator.hpp>
23 #include <boost/asio/associated_cancellation_slot.hpp>
24 #include <boost/asio/associated_executor.hpp>
25 #include <boost/asio/cancellation_signal.hpp>
26 #include <boost/asio/defer.hpp>
27 #include <boost/asio/dispatch.hpp>
28 #include <boost/asio/post.hpp>
30 #ifdef CPPWAMP_WITHOUT_BUNDLED_ASIO_ANY_COMPLETION_HANDLER
31 #include <boost/asio/any_completion_handler.hpp>
33 #include "bundled/boost_asio_any_completion_handler.hpp"
54 template <
typename TSignature>
66 template <
typename TSignature>
70 using Function = std::function<TSignature>;
73 static constexpr
bool fnConstructible() noexcept
75 using Decayed =
typename std::decay<F>::type;
76 return !std::is_same<Decayed, AnyReusableHandler>::value &&
77 !std::is_same<Decayed, std::nullptr_t>::value &&
78 std::is_constructible<Function, F>::value;
82 static constexpr
bool otherConstructible() noexcept
84 return std::is_constructible<Function, std::function<F>>::value;
114 template <typename S,
115 typename std::enable_if<otherConstructible<S>(),
int>::type = 0>
117 : executor_(rhs.executor_),
118 handler_(rhs.handler_)
126 template <
typename S,
127 typename std::enable_if<otherConstructible<S>(),
int>::type = 0>
129 : executor_(std::move(rhs.executor_)),
130 handler_(std::move(rhs.handler_))
139 template <
typename F,
140 typename std::enable_if<fnConstructible<F>(),
int>::type = 0>
142 : executor_(boost::asio::get_associated_executor(
144 cancelSlot_(boost::asio::get_associated_cancellation_slot(handler)),
145 handler_(std::forward<F>(handler))
170 swap(executor_, rhs.executor_);
172 handler_.swap(rhs.handler_);
176 explicit operator bool() const noexcept {
return bool(handler_);}
185 template <
typename... Ts>
187 -> decltype(std::declval<Function>()(std::forward<Ts>(args)...))
189 return handler_(std::forward<Ts>(args)...);
199 template <
typename S>
206 template <
typename S>
213 template <
typename S>
220 template <
typename S>
227 template <
typename S>
242 template <
typename S,
typename E>
243 struct associated_executor<
wamp::AnyReusableHandler<S>, E>
254 template <
typename S,
typename C>
255 struct associated_cancellation_slot<
wamp::AnyReusableHandler<S>, C>
266 template <
typename S,
typename A>
267 struct associated_allocator<
wamp::AnyReusableHandler<S>, A>
290 template <
typename F,
typename... Ts>
293 auto e = boost::asio::get_associated_executor(handler);
294 boost::asio::dispatch(e, std::bind(std::forward<F>(handler),
295 std::forward<Ts>(args)...));
302 template<
typename F,
typename E,
typename... Ts>
303 void dispatchVia(
const E& fallbackExec, F&& handler, Ts&&... args)
305 auto e = boost::asio::get_associated_executor(handler, fallbackExec);
306 boost::asio::dispatch(e, std::bind(std::forward<F>(handler),
307 std::forward<Ts>(args)...));
314 template<
typename F,
typename... Ts>
317 auto e = boost::asio::get_associated_executor(handler);
318 boost::asio::post(e, std::bind(std::forward<F>(handler),
319 std::forward<Ts>(args)...));
326 template<
typename F,
typename E,
typename... Ts>
327 void postVia(
const E& fallbackExec, F&& handler, Ts&&... args)
329 auto e = boost::asio::get_associated_executor(handler, fallbackExec);
330 boost::asio::post(e, std::bind(std::forward<F>(handler),
331 std::forward<Ts>(args)...));
338 template<
typename F,
typename... Ts>
341 auto e = boost::asio::get_associated_executor(handler);
342 boost::asio::defer(e, std::bind(std::forward<F>(handler),
343 std::forward<Ts>(args)...));
350 template<
typename F,
typename E,
typename... Ts>
351 void deferVia(
const E& fallbackExec, F&& handler, Ts&&... args)
353 auto e = boost::asio::get_associated_executor(handler, fallbackExec);
354 boost::asio::defer(e, std::bind(std::forward<F>(handler),
355 std::forward<Ts>(args)...));
360 #endif // CPPWAMP_ANYHANDLER_HPP
void deferVia(const E &fallbackExec, F &&handler, Ts &&... args)
Defers the given handler using the given fallback executor, passing the given arguments.
Definition: anyhandler.hpp:351
void dispatchVia(const E &fallbackExec, F &&handler, Ts &&... args)
Dispatches the given handler using the given fallback executor, passing the given arguments.
Definition: anyhandler.hpp:303
auto operator()(Ts &&... args) const -> decltype(std::declval< Function >()(std::forward< Ts >(args)...))
Invokes the handler with the given arguments.
Definition: anyhandler.hpp:186
void deferAny(F &&handler, Ts &&... args)
Defers the given handler using its associated executor, passing the given arguments.
Definition: anyhandler.hpp:339
const Executor & get_executor() const
Obtains the executor associated with this handler.
Definition: anyhandler.hpp:179
bool operator==(std::nullptr_t, const AnyReusableHandler< S > &f) noexcept
Returns true is the given handler is empty.
Definition: anyhandler.hpp:214
bool operator!=(std::nullptr_t, const AnyReusableHandler< S > &f) noexcept
Returns false is the given handler is empty.
Definition: anyhandler.hpp:228
Type-erases a multi-shot, copyable callback handler.
Definition: anyhandler.hpp:67
Executor executor_type
Asio-conformant type alias.
Definition: anyhandler.hpp:95
AnyReusableHandler(F &&handler)
Constructor taking a callable entity.
Definition: anyhandler.hpp:141
void dispatchAny(F &&handler, Ts &&... args)
Dispatches the given handler using its associated executor, passing the given arguments.
Definition: anyhandler.hpp:291
bool operator!=(const AnyReusableHandler< S > &f, std::nullptr_t) noexcept
Returns false is the given handler is empty.
Definition: anyhandler.hpp:221
void postAny(F &&handler, Ts &&... args)
Posts the given handler using its associated executor, passing the given arguments.
Definition: anyhandler.hpp:315
boost::asio::any_completion_executor AnyCompletionExecutor
Type-erases an executor that is to be used with type-erased handlers.
Definition: anyhandler.hpp:42
CancellationSlot cancellation_slot_type
Asio-conformant type alias.
Definition: anyhandler.hpp:98
Definition: anyhandler.hpp:36
AnyReusableHandler(std::nullptr_t) noexcept
Constructs an empty AnyReusableHandler.
Definition: anyhandler.hpp:149
void swap(AnyReusableHandler &rhs) noexcept
Swaps contents with another AnyReusableHandler.
Definition: anyhandler.hpp:166
boost::asio::cancellation_slot CancellationSlot
Cancellation slot type used by this handler.
Definition: anyhandler.hpp:92
bool operator==(const AnyReusableHandler< S > &f, std::nullptr_t) noexcept
Returns true is the given handler is empty.
Definition: anyhandler.hpp:207
AnyReusableHandler & operator=(std::nullptr_t) noexcept
Renders an AnyReusableHandler empty.
Definition: anyhandler.hpp:152
AnyReusableHandler(AnyReusableHandler< S > &&rhs) noexcept
Constructor moving another AnyReusableHandler with a different signature.
Definition: anyhandler.hpp:128
const CancellationSlot & get_cancellation_slot() const
Obtains the cancellation slot associated with this handler.
Definition: anyhandler.hpp:182
AnyCompletionExecutor Executor
Polymorphic wrapper around the executor associated with this handler.
Definition: anyhandler.hpp:89
AnyReusableHandler()=default
Default constructor.
void swap(AnyReusableHandler< S > &a, AnyReusableHandler< S > &b) noexcept
Non-member swap.
Definition: anyhandler.hpp:200
void postVia(const E &fallbackExec, F &&handler, Ts &&... args)
Posts the given handler using the given fallback executor, passing the given arguments.
Definition: anyhandler.hpp:327
boost::asio::any_completion_handler< TSignature > AnyCompletionHandler
Type-erases a one-shot (and possibly move-only) asynchronous completion handler.
Definition: anyhandler.hpp:55