ゴミ箱
handler_ptr.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_HANDLER_PTR_HPP
11 #define BOOST_BEAST_HANDLER_PTR_HPP
12 
13 #include <boost/beast/config.hpp>
15 #include <atomic>
16 #include <cstdint>
17 #include <type_traits>
18 #include <utility>
19 
20 namespace boost {
21 namespace beast {
22 
52 template<class T, class Handler>
54 {
55  struct P
56  {
57  T* t;
58  std::atomic<std::uint16_t> n;
59 
60  // There's no way to put the handler anywhere else
61  // without exposing ourselves to race conditions
62  // and all sorts of ugliness.
63  // See:
64  // https://github.com/boostorg/beast/issues/215
65  Handler handler;
66 
67  template<class DeducedHandler, class... Args>
68  P(DeducedHandler&& handler, Args&&... args);
69  };
70 
71  P* p_;
72 
73 public:
75  using element_type = T;
76 
78  using handler_type = Handler;
79 
81  handler_ptr& operator=(handler_ptr const&) = delete;
82 
89  ~handler_ptr();
90 
96  handler_ptr(handler_ptr&& other);
97 
99  handler_ptr(handler_ptr const& other);
100 
118  template<class... Args>
119  handler_ptr(Handler&& handler, Args&&... args);
120 
138  template<class... Args>
139  handler_ptr(Handler const& handler, Args&&... args);
140 
142  handler_type&
143  handler() const
144  {
145  return p_->handler;
146  }
147 
149  explicit
150  operator bool() const
151  {
152  return p_ && p_->t;
153  }
154 
160  T*
161  get() const
162  {
163  return p_ ? p_->t : nullptr;
164  }
165 
167  T&
168  operator*() const
169  {
170  return *p_->t;
171  }
172 
174  T*
175  operator->() const
176  {
177  return p_->t;
178  }
179 
187  release_handler();
188 
203  template<class... Args>
204  void
205  invoke(Args&&... args);
206 };
207 
208 } // beast
209 } // boost
210 
212 
213 #endif
Handler handler_type
The type of handler this object stores.
Definition: handler_ptr.hpp:78
Definition: async_result.hpp:20
Definition: handler_ptr.hpp:53
handler_type release_handler()
Definition: handler_ptr.ipp:105
handler_ptr(handler_ptr &&other)
Definition: handler_ptr.ipp:68
handler_ptr & operator=(handler_ptr const &)=delete
Copy assignment (disallowed).
T & operator*() const
Return a reference to the owned object.
Definition: handler_ptr.hpp:168
void invoke(Args &&...args)
Definition: handler_ptr.ipp:122
T * operator->() const
Return a pointer to the owned object.
Definition: handler_ptr.hpp:175
~handler_ptr()
Definition: handler_ptr.ipp:50
state element_type
The type of element this object stores.
Definition: handler_ptr.hpp:75
handler_type & handler() const
Returns a reference to the handler.
Definition: handler_ptr.hpp:143