ゴミ箱
bind_handler.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9 
10 #ifndef BOOST_BEAST_DETAIL_BIND_HANDLER_HPP
11 #define BOOST_BEAST_DETAIL_BIND_HANDLER_HPP
12 
14 #include <boost/asio/handler_alloc_hook.hpp>
15 #include <boost/asio/handler_continuation_hook.hpp>
16 #include <boost/asio/handler_invoke_hook.hpp>
17 #include <boost/core/ignore_unused.hpp>
18 #include <functional>
19 #include <utility>
20 
21 namespace boost {
22 namespace beast {
23 namespace detail {
24 
25 /* Nullary handler that calls Handler with bound arguments.
26 
27  The bound handler provides the same io_service execution
28  guarantees as the original handler.
29 */
30 template<class Handler, class... Args>
32 {
33 private:
34  using args_type = std::tuple<
35  typename std::decay<Args>::type...>;
36 
37  Handler h_;
38  args_type args_;
39 
40  template<class Arg, class Vals>
41  static
42  typename std::enable_if<
43  std::is_placeholder<typename
44  std::decay<Arg>::type>::value == 0,
45  Arg&&>::type
46  extract(Arg&& arg, Vals& vals)
47  {
48  boost::ignore_unused(vals);
49  return arg;
50  }
51 
52  template<class Arg, class Vals>
53  static
54  typename std::enable_if<
55  std::is_placeholder<typename
56  std::decay<Arg>::type>::value != 0,
57  typename std::tuple_element<
58  std::is_placeholder<
59  typename std::decay<Arg>::type>::value - 1,
60  Vals>::type&&>::type
61  extract(Arg&&, Vals&& vals)
62  {
63  return std::get<std::is_placeholder<
64  typename std::decay<Arg>::type>::value - 1>(
65  std::forward<Vals>(vals));
66  }
67 
68  template<
69  class ArgsTuple,
70  std::size_t... S>
71  static
72  void
73  invoke(
74  Handler& h,
75  ArgsTuple& args,
76  std::tuple<>&&,
78  {
79  boost::ignore_unused(args);
80  h(std::get<S>(args)...);
81  }
82 
83  template<
84  class ArgsTuple,
85  class ValsTuple,
86  std::size_t... S>
87  static
88  void
89  invoke(
90  Handler& h,
91  ArgsTuple& args,
92  ValsTuple&& vals,
94  {
95  boost::ignore_unused(args);
96  boost::ignore_unused(vals);
97  h(extract(std::get<S>(args),
98  std::forward<ValsTuple>(vals))...);
99  }
100 
101 public:
102  using result_type = void;
103 
104  bound_handler(bound_handler&&) = default;
105  bound_handler(bound_handler const&) = default;
106 
107  template<class DeducedHandler>
108  explicit
110  DeducedHandler&& handler, Args&&... args)
111  : h_(std::forward<DeducedHandler>(handler))
112  , args_(std::forward<Args>(args)...)
113  {
114  }
115 
116  template<class... Values>
117  void
118  operator()(Values&&... values)
119  {
120  invoke(h_, args_,
121  std::forward_as_tuple(
122  std::forward<Values>(values)...),
124  }
125 
126  template<class... Values>
127  void
128  operator()(Values&&... values) const
129  {
130  invoke(h_, args_,
131  std::forward_as_tuple(
132  std::forward<Values>(values)...),
134  }
135 
136  friend
137  void*
139  std::size_t size, bound_handler* h)
140  {
141  using boost::asio::asio_handler_allocate;
142  return asio_handler_allocate(
143  size, std::addressof(h->h_));
144  }
145 
146  friend
147  void
149  void* p, std::size_t size, bound_handler* h)
150  {
151  using boost::asio::asio_handler_deallocate;
153  p, size, std::addressof(h->h_));
154  }
155 
156  friend
157  bool
159  {
160  using boost::asio::asio_handler_is_continuation;
161  return asio_handler_is_continuation(std::addressof(h->h_));
162  }
163 
164  template<class F>
165  friend
166  void
168  {
169  using boost::asio::asio_handler_invoke;
171  f, std::addressof(h->h_));
172  }
173 };
174 
175 } // detail
176 } // beast
177 } // boost
178 
179 namespace std {
180 template<class Handler, class... Args>
181 void
183  Handler, Args...>, ...) = delete;
184 } // std
185 
186 #endif
Definition: async_result.hpp:20
STL namespace.
make_index_sequence< sizeof_workaround< Args... >::size > index_sequence_for
Definition: integer_sequence.hpp:135
friend bool asio_handler_is_continuation(bound_handler *h)
Definition: bind_handler.hpp:158
bound_handler(DeducedHandler &&handler, Args &&...args)
Definition: bind_handler.hpp:109
Definition: integer_sequence.hpp:23
void result_type
Definition: bind_handler.hpp:102
friend void * asio_handler_allocate(std::size_t size, bound_handler *h)
Definition: bind_handler.hpp:138
Definition: bind_handler.hpp:31
bound_handler(bound_handler &&)=default
friend void asio_handler_invoke(F &&f, bound_handler *h)
Definition: bind_handler.hpp:167
void operator()(Values &&...values)
Definition: bind_handler.hpp:118
void operator()(Values &&...values) const
Definition: bind_handler.hpp:128
friend void asio_handler_deallocate(void *p, std::size_t size, bound_handler *h)
Definition: bind_handler.hpp:148