00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef ASIO_DETAIL_WRAPPED_HANDLER_HPP
00012 #define ASIO_DETAIL_WRAPPED_HANDLER_HPP
00013
00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00015 # pragma once
00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
00017
00018 #include "asio/detail/push_options.hpp"
00019
00020 #include "asio/detail/bind_handler.hpp"
00021 #include "asio/detail/handler_alloc_helpers.hpp"
00022 #include "asio/detail/handler_invoke_helpers.hpp"
00023
00024 namespace asio {
00025 namespace detail {
00026
00027 template <typename Dispatcher, typename Handler>
00028 class wrapped_handler
00029 {
00030 public:
00031 typedef void result_type;
00032
00033 wrapped_handler(Dispatcher& dispatcher, Handler handler)
00034 : dispatcher_(dispatcher),
00035 handler_(handler)
00036 {
00037 }
00038
00039 void operator()()
00040 {
00041 dispatcher_.dispatch(handler_);
00042 }
00043
00044 void operator()() const
00045 {
00046 dispatcher_.dispatch(handler_);
00047 }
00048
00049 template <typename Arg1>
00050 void operator()(const Arg1& arg1)
00051 {
00052 dispatcher_.dispatch(detail::bind_handler(handler_, arg1));
00053 }
00054
00055 template <typename Arg1>
00056 void operator()(const Arg1& arg1) const
00057 {
00058 dispatcher_.dispatch(detail::bind_handler(handler_, arg1));
00059 }
00060
00061 template <typename Arg1, typename Arg2>
00062 void operator()(const Arg1& arg1, const Arg2& arg2)
00063 {
00064 dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2));
00065 }
00066
00067 template <typename Arg1, typename Arg2>
00068 void operator()(const Arg1& arg1, const Arg2& arg2) const
00069 {
00070 dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2));
00071 }
00072
00073 template <typename Arg1, typename Arg2, typename Arg3>
00074 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3)
00075 {
00076 dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3));
00077 }
00078
00079 template <typename Arg1, typename Arg2, typename Arg3>
00080 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) const
00081 {
00082 dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3));
00083 }
00084
00085 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
00086 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
00087 const Arg4& arg4)
00088 {
00089 dispatcher_.dispatch(
00090 detail::bind_handler(handler_, arg1, arg2, arg3, arg4));
00091 }
00092
00093 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
00094 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
00095 const Arg4& arg4) const
00096 {
00097 dispatcher_.dispatch(
00098 detail::bind_handler(handler_, arg1, arg2, arg3, arg4));
00099 }
00100
00101 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
00102 typename Arg5>
00103 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
00104 const Arg4& arg4, const Arg5& arg5)
00105 {
00106 dispatcher_.dispatch(
00107 detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5));
00108 }
00109
00110 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
00111 typename Arg5>
00112 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
00113 const Arg4& arg4, const Arg5& arg5) const
00114 {
00115 dispatcher_.dispatch(
00116 detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5));
00117 }
00118
00119
00120 Dispatcher& dispatcher_;
00121 Handler handler_;
00122 };
00123
00124 template <typename Dispatcher, typename Handler>
00125 inline void* asio_handler_allocate(std::size_t size,
00126 wrapped_handler<Dispatcher, Handler>* this_handler)
00127 {
00128 return asio_handler_alloc_helpers::allocate(
00129 size, &this_handler->handler_);
00130 }
00131
00132 template <typename Dispatcher, typename Handler>
00133 inline void asio_handler_deallocate(void* pointer, std::size_t size,
00134 wrapped_handler<Dispatcher, Handler>* this_handler)
00135 {
00136 asio_handler_alloc_helpers::deallocate(
00137 pointer, size, &this_handler->handler_);
00138 }
00139
00140 template <typename Handler, typename Context>
00141 class rewrapped_handler
00142 {
00143 public:
00144 explicit rewrapped_handler(const Handler& handler, const Context& context)
00145 : handler_(handler),
00146 context_(context)
00147 {
00148 }
00149
00150 void operator()()
00151 {
00152 handler_();
00153 }
00154
00155 void operator()() const
00156 {
00157 handler_();
00158 }
00159
00160
00161 Handler handler_;
00162 Context context_;
00163 };
00164
00165 template <typename Function, typename Dispatcher, typename Handler>
00166 inline void asio_handler_invoke(const Function& function,
00167 wrapped_handler<Dispatcher, Handler>* this_handler)
00168 {
00169 this_handler->dispatcher_.dispatch(
00170 rewrapped_handler<Function, Handler>(
00171 function, this_handler->handler_));
00172 }
00173
00174 template <typename Function, typename Dispatcher, typename Handler>
00175 inline void asio_handler_invoke(const Function& function,
00176 rewrapped_handler<Dispatcher, Handler>* this_handler)
00177 {
00178 asio_handler_invoke_helpers::invoke(
00179 function, &this_handler->context_);
00180 }
00181
00182 }
00183 }
00184
00185 #include "asio/detail/pop_options.hpp"
00186
00187 #endif // ASIO_DETAIL_WRAPPED_HANDLER_HPP