1 //
2 // impl/compose.hpp
3 // ~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_IMPL_COMPOSE_HPP
12 #define ASIO_IMPL_COMPOSE_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 #include "asio/detail/handler_alloc_helpers.hpp"
20 #include "asio/detail/handler_cont_helpers.hpp"
21 #include "asio/detail/handler_invoke_helpers.hpp"
22 #include "asio/detail/type_traits.hpp"
23 #include "asio/detail/variadic_templates.hpp"
24 #include "asio/executor_work_guard.hpp"
25 #include "asio/is_executor.hpp"
26 #include "asio/system_executor.hpp"
27 
28 #include "asio/detail/push_options.hpp"
29 
30 namespace asio {
31 
32 namespace detail
33 {
34   template <typename>
35   struct composed_work;
36 
37   template <>
38   struct composed_work<void()>
39   {
composed_workasio::detail::composed_work40     composed_work() ASIO_NOEXCEPT
41       : head_(system_executor())
42     {
43     }
44 
resetasio::detail::composed_work45     void reset()
46     {
47       head_.reset();
48     }
49 
50     typedef system_executor head_type;
51     executor_work_guard<system_executor> head_;
52   };
53 
make_composed_work()54   inline composed_work<void()> make_composed_work()
55   {
56     return composed_work<void()>();
57   }
58 
59   template <typename Head>
60   struct composed_work<void(Head)>
61   {
composed_workasio::detail::composed_work62     explicit composed_work(const Head& ex) ASIO_NOEXCEPT
63       : head_(ex)
64     {
65     }
66 
resetasio::detail::composed_work67     void reset()
68     {
69       head_.reset();
70     }
71 
72     typedef Head head_type;
73     executor_work_guard<Head> head_;
74   };
75 
76   template <typename Head>
make_composed_work(const Head & head)77   inline composed_work<void(Head)> make_composed_work(const Head& head)
78   {
79     return composed_work<void(Head)>(head);
80   }
81 
82 #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
83 
84   template <typename Head, typename... Tail>
85   struct composed_work<void(Head, Tail...)>
86   {
composed_workasio::detail::composed_work87     explicit composed_work(const Head& head,
88         const Tail&... tail) ASIO_NOEXCEPT
89       : head_(head),
90         tail_(tail...)
91     {
92     }
93 
resetasio::detail::composed_work94     void reset()
95     {
96       head_.reset();
97       tail_.reset();
98     }
99 
100     typedef Head head_type;
101     executor_work_guard<Head> head_;
102     composed_work<void(Tail...)> tail_;
103   };
104 
105   template <typename Head, typename... Tail>
106   inline composed_work<void(Head, Tail...)>
make_composed_work(const Head & head,const Tail &...tail)107   make_composed_work(const Head& head, const Tail&... tail)
108   {
109     return composed_work<void(Head, Tail...)>(head, tail...);
110   }
111 
112 #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
113 
114 #define ASIO_PRIVATE_COMPOSED_WORK_DEF(n) \
115   template <typename Head, ASIO_VARIADIC_TPARAMS(n)> \
116   struct composed_work<void(Head, ASIO_VARIADIC_TARGS(n))> \
117   { \
118     explicit composed_work(const Head& head, \
119         ASIO_VARIADIC_CONSTREF_PARAMS(n)) ASIO_NOEXCEPT \
120       : head_(head), \
121         tail_(ASIO_VARIADIC_BYVAL_ARGS(n)) \
122     { \
123     } \
124   \
125     void reset() \
126     { \
127       head_.reset(); \
128       tail_.reset(); \
129     } \
130   \
131     typedef Head head_type; \
132     executor_work_guard<Head> head_; \
133     composed_work<void(ASIO_VARIADIC_TARGS(n))> tail_; \
134   }; \
135   \
136   template <typename Head, ASIO_VARIADIC_TPARAMS(n)> \
137   inline composed_work<void(Head, ASIO_VARIADIC_TARGS(n))> \
138   make_composed_work(const Head& head, ASIO_VARIADIC_CONSTREF_PARAMS(n)) \
139   { \
140     return composed_work< \
141       void(Head, ASIO_VARIADIC_TARGS(n))>( \
142         head, ASIO_VARIADIC_BYVAL_ARGS(n)); \
143   } \
144   /**/
145   ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_COMPOSED_WORK_DEF)
146 #undef ASIO_PRIVATE_COMPOSED_WORK_DEF
147 
148 #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
149 
150 #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
151   template <typename Impl, typename Work, typename Handler, typename Signature>
152   class composed_op;
153 
154   template <typename Impl, typename Work, typename Handler,
155       typename R, typename... Args>
156   class composed_op<Impl, Work, Handler, R(Args...)>
157 #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
158   template <typename Impl, typename Work, typename Handler, typename Signature>
159   class composed_op
160 #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
161   {
162   public:
composed_op(ASIO_MOVE_ARG (Impl)impl,ASIO_MOVE_ARG (Work)work,ASIO_MOVE_ARG (Handler)handler)163     composed_op(ASIO_MOVE_ARG(Impl) impl,
164         ASIO_MOVE_ARG(Work) work,
165         ASIO_MOVE_ARG(Handler) handler)
166       : impl_(ASIO_MOVE_CAST(Impl)(impl)),
167         work_(ASIO_MOVE_CAST(Work)(work)),
168         handler_(ASIO_MOVE_CAST(Handler)(handler)),
169         invocations_(0)
170     {
171     }
172 
173 #if defined(ASIO_HAS_MOVE)
composed_op(composed_op && other)174     composed_op(composed_op&& other)
175       : impl_(ASIO_MOVE_CAST(Impl)(other.impl_)),
176         work_(ASIO_MOVE_CAST(Work)(other.work_)),
177         handler_(ASIO_MOVE_CAST(Handler)(other.handler_)),
178         invocations_(other.invocations_)
179     {
180     }
181 #endif // defined(ASIO_HAS_MOVE)
182 
183     typedef typename associated_executor<Handler,
184         typename Work::head_type>::type executor_type;
185 
get_executor() const186     executor_type get_executor() const ASIO_NOEXCEPT
187     {
188       return (get_associated_executor)(handler_, work_.head_.get_executor());
189     }
190 
191     typedef typename associated_allocator<Handler,
192       std::allocator<void> >::type allocator_type;
193 
get_allocator() const194     allocator_type get_allocator() const ASIO_NOEXCEPT
195     {
196       return (get_associated_allocator)(handler_, std::allocator<void>());
197     }
198 
199 #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
200 
201     template<typename... T>
operator ()(ASIO_MOVE_ARG (T)...t)202     void operator()(ASIO_MOVE_ARG(T)... t)
203     {
204       if (invocations_ < ~unsigned(0))
205         ++invocations_;
206       impl_(*this, ASIO_MOVE_CAST(T)(t)...);
207     }
208 
complete(Args...args)209     void complete(Args... args)
210     {
211       this->work_.reset();
212       this->handler_(ASIO_MOVE_CAST(Args)(args)...);
213     }
214 
215 #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
216 
operator ()()217     void operator()()
218     {
219       if (invocations_ < ~unsigned(0))
220         ++invocations_;
221       impl_(*this);
222     }
223 
complete()224     void complete()
225     {
226       this->work_.reset();
227       this->handler_();
228     }
229 
230 #define ASIO_PRIVATE_COMPOSED_OP_DEF(n) \
231     template<ASIO_VARIADIC_TPARAMS(n)> \
232     void operator()(ASIO_VARIADIC_MOVE_PARAMS(n)) \
233     { \
234       if (invocations_ < ~unsigned(0)) \
235         ++invocations_; \
236       impl_(*this, ASIO_VARIADIC_MOVE_ARGS(n)); \
237     } \
238     \
239     template<ASIO_VARIADIC_TPARAMS(n)> \
240     void complete(ASIO_VARIADIC_MOVE_PARAMS(n)) \
241     { \
242       this->work_.reset(); \
243       this->handler_(ASIO_VARIADIC_MOVE_ARGS(n)); \
244     } \
245     /**/
246     ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_COMPOSED_OP_DEF)
247 #undef ASIO_PRIVATE_COMPOSED_OP_DEF
248 
249 #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
250 
251   //private:
252     Impl impl_;
253     Work work_;
254     Handler handler_;
255     unsigned invocations_;
256   };
257 
258   template <typename Impl, typename Work, typename Handler, typename Signature>
asio_handler_allocate(std::size_t size,composed_op<Impl,Work,Handler,Signature> * this_handler)259   inline void* asio_handler_allocate(std::size_t size,
260       composed_op<Impl, Work, Handler, Signature>* this_handler)
261   {
262     return asio_handler_alloc_helpers::allocate(
263         size, this_handler->handler_);
264   }
265 
266   template <typename Impl, typename Work, typename Handler, typename Signature>
asio_handler_deallocate(void * pointer,std::size_t size,composed_op<Impl,Work,Handler,Signature> * this_handler)267   inline void asio_handler_deallocate(void* pointer, std::size_t size,
268       composed_op<Impl, Work, Handler, Signature>* this_handler)
269   {
270     asio_handler_alloc_helpers::deallocate(
271         pointer, size, this_handler->handler_);
272   }
273 
274   template <typename Impl, typename Work, typename Handler, typename Signature>
asio_handler_is_continuation(composed_op<Impl,Work,Handler,Signature> * this_handler)275   inline bool asio_handler_is_continuation(
276       composed_op<Impl, Work, Handler, Signature>* this_handler)
277   {
278     return this_handler->invocations_ > 1 ? true
279       : asio_handler_cont_helpers::is_continuation(
280           this_handler->handler_);
281   }
282 
283   template <typename Function, typename Impl,
284       typename Work, typename Handler, typename Signature>
asio_handler_invoke(Function & function,composed_op<Impl,Work,Handler,Signature> * this_handler)285   inline void asio_handler_invoke(Function& function,
286       composed_op<Impl, Work, Handler, Signature>* this_handler)
287   {
288     asio_handler_invoke_helpers::invoke(
289         function, this_handler->handler_);
290   }
291 
292   template <typename Function, typename Impl,
293       typename Work, typename Handler, typename Signature>
asio_handler_invoke(const Function & function,composed_op<Impl,Work,Handler,Signature> * this_handler)294   inline void asio_handler_invoke(const Function& function,
295       composed_op<Impl, Work, Handler, Signature>* this_handler)
296   {
297     asio_handler_invoke_helpers::invoke(
298         function, this_handler->handler_);
299   }
300 
301   template <typename Signature>
302   struct initiate_composed_op
303   {
304     template <typename Handler, typename Impl, typename Work>
operator ()asio::detail::initiate_composed_op305     void operator()(ASIO_MOVE_ARG(Handler) handler,
306         ASIO_MOVE_ARG(Impl) impl,
307         ASIO_MOVE_ARG(Work) work) const
308     {
309       composed_op<typename decay<Impl>::type, typename decay<Work>::type,
310         typename decay<Handler>::type, Signature>(
311           ASIO_MOVE_CAST(Impl)(impl), ASIO_MOVE_CAST(Work)(work),
312           ASIO_MOVE_CAST(Handler)(handler))();
313     }
314   };
315 
316   template <typename IoObject>
317   inline typename IoObject::executor_type
get_composed_io_executor(IoObject & io_object)318   get_composed_io_executor(IoObject& io_object)
319   {
320     return io_object.get_executor();
321   }
322 
323   template <typename Executor>
get_composed_io_executor(const Executor & ex,typename enable_if<is_executor<Executor>::value>::type * =0)324   inline const Executor& get_composed_io_executor(const Executor& ex,
325       typename enable_if<is_executor<Executor>::value>::type* = 0)
326   {
327     return ex;
328   }
329 } // namespace detail
330 
331 #if !defined(GENERATING_DOCUMENTATION)
332 #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
333 
334 template <typename CompletionToken, typename Signature,
335     typename Implementation, typename... IoObjectsOrExecutors>
ASIO_INITFN_RESULT_TYPE(CompletionToken,Signature)336 ASIO_INITFN_RESULT_TYPE(CompletionToken, Signature)
337 async_compose(ASIO_MOVE_ARG(Implementation) implementation,
338     ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token,
339     ASIO_MOVE_ARG(IoObjectsOrExecutors)... io_objects_or_executors)
340 {
341   return async_initiate<CompletionToken, Signature>(
342       detail::initiate_composed_op<Signature>(), token,
343       ASIO_MOVE_CAST(Implementation)(implementation),
344       detail::make_composed_work(
345         detail::get_composed_io_executor(
346           ASIO_MOVE_CAST(IoObjectsOrExecutors)(
347             io_objects_or_executors))...));
348 }
349 
350 #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
351 
352 template <typename CompletionToken, typename Signature, typename Implementation>
ASIO_INITFN_RESULT_TYPE(CompletionToken,Signature)353 ASIO_INITFN_RESULT_TYPE(CompletionToken, Signature)
354 async_compose(ASIO_MOVE_ARG(Implementation) implementation,
355     ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token)
356 {
357   return async_initiate<CompletionToken, Signature>(
358       detail::initiate_composed_op<Signature>(), token,
359       ASIO_MOVE_CAST(Implementation)(implementation),
360       detail::make_composed_work());
361 }
362 
363 # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR(n) \
364   ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_##n
365 
366 # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_1 \
367   detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1))
368 # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_2 \
369   detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \
370   detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2))
371 # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_3 \
372   detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \
373   detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)), \
374   detail::get_composed_io_executor(ASIO_MOVE_CAST(T3)(x3))
375 # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_4 \
376   detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \
377   detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)), \
378   detail::get_composed_io_executor(ASIO_MOVE_CAST(T3)(x3)), \
379   detail::get_composed_io_executor(ASIO_MOVE_CAST(T4)(x4))
380 # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_5 \
381   detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \
382   detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)), \
383   detail::get_composed_io_executor(ASIO_MOVE_CAST(T3)(x3)), \
384   detail::get_composed_io_executor(ASIO_MOVE_CAST(T4)(x4)), \
385   detail::get_composed_io_executor(ASIO_MOVE_CAST(T5)(x5))
386 
387 #define ASIO_PRIVATE_ASYNC_COMPOSE_DEF(n) \
388   template <typename CompletionToken, typename Signature, \
389       typename Implementation, ASIO_VARIADIC_TPARAMS(n)> \
390   ASIO_INITFN_RESULT_TYPE(CompletionToken, Signature) \
391   async_compose(ASIO_MOVE_ARG(Implementation) implementation, \
392       ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token, \
393       ASIO_VARIADIC_MOVE_PARAMS(n)) \
394   { \
395     return async_initiate<CompletionToken, Signature>( \
396         detail::initiate_composed_op<Signature>(), token, \
397         ASIO_MOVE_CAST(Implementation)(implementation), \
398         detail::make_composed_work( \
399           ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR(n))); \
400   } \
401   /**/
402   ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ASYNC_COMPOSE_DEF)
403 #undef ASIO_PRIVATE_ASYNC_COMPOSE_DEF
404 
405 #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR
406 #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_1
407 #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_2
408 #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_3
409 #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_4
410 #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_5
411 
412 #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
413 #endif // !defined(GENERATING_DOCUMENTATION)
414 
415 } // namespace asio
416 
417 #include "asio/detail/pop_options.hpp"
418 
419 #endif // ASIO_IMPL_COMPOSE_HPP
420