2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_HANDLER_PTR_HPP
11 #define BOOST_BEAST_HANDLER_PTR_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/detail/type_traits.hpp>
17 #include <type_traits>
23 /** A smart pointer container with associated completion handler.
25 This is a smart pointer that retains shared ownership of an
26 object through a pointer. Memory is managed using the allocation
27 and deallocation functions associated with a completion handler,
28 which is also stored in the object. The managed object is
29 destroyed and its memory deallocated when one of the following
32 @li The function @ref invoke is called.
34 @li The function @ref release_handler is called.
36 @li The last remaining container owning the object is destroyed.
38 Objects of this type are used in the implementation of
39 composed operations. Typically the composed operation's shared
40 state is managed by the @ref handler_ptr and an allocator
41 associated with the final handler is used to create the managed
44 @note The reference count is stored using a 16 bit unsigned
45 integer. Making more than 2^16 copies of one object results
46 in undefined behavior.
48 @tparam T The type of the owned object.
50 @tparam Handler The type of the completion handler.
52 template<class T, class Handler>
58 std::atomic<std::uint16_t> n;
60 // There's no way to put the handler anywhere else
61 // without exposing ourselves to race conditions
62 // and all sorts of ugliness.
64 // https://github.com/boostorg/beast/issues/215
67 template<class DeducedHandler, class... Args>
68 P(DeducedHandler&& handler, Args&&... args);
74 /// The type of element this object stores
75 using element_type = T;
77 /// The type of handler this object stores
78 using handler_type = Handler;
80 /// Copy assignment (disallowed).
81 handler_ptr& operator=(handler_ptr const&) = delete;
83 /** Destructs the owned object if no more @ref handler_ptr link to it.
85 If `*this` owns an object and it is the last @ref handler_ptr
86 owning it, the object is destroyed and the memory deallocated
87 using the associated deallocator.
93 When this call returns, the moved-from container
94 will have no owned object.
96 handler_ptr(handler_ptr&& other);
99 handler_ptr(handler_ptr const& other);
101 /** Construct a new @ref handler_ptr
103 This creates a new @ref handler_ptr with an owned object
104 of type `T`. The allocator associated with the handler will
105 be used to allocate memory for the owned object. The constructor
106 for the owned object will be called thusly:
109 T(handler, std::forward<Args>(args)...)
112 @param handler The handler to associate with the owned
113 object. The argument will be moved.
115 @param args Optional arguments forwarded to
116 the owned object's constructor.
118 template<class... Args>
119 handler_ptr(Handler&& handler, Args&&... args);
121 /** Construct a new @ref handler_ptr
123 This creates a new @ref handler_ptr with an owned object
124 of type `T`. The allocator associated with the handler will
125 be used to allocate memory for the owned object. The constructor
126 for the owned object will be called thusly:
129 T(handler, std::forward<Args>(args)...)
132 @param handler The handler to associate with the owned
133 object. The argument will be copied.
135 @param args Optional arguments forwarded to
136 the owned object's constructor.
138 template<class... Args>
139 handler_ptr(Handler const& handler, Args&&... args);
141 /// Returns a reference to the handler
148 /// Returns `true` if `*this` owns an object.
150 operator bool() const
155 /** Returns a pointer to the owned object.
157 If `*this` owns an object, a pointer to the
158 object is returned, else `nullptr` is returned.
163 return p_ ? p_->t : nullptr;
166 /// Return a reference to the owned object.
173 /// Return a pointer to the owned object.
180 /** Release ownership of the handler
182 If `*this` owns an object, it is first destroyed.
184 @return The released handler.
189 /** Invoke the handler in the owned object.
191 This function invokes the handler in the owned object
192 with a forwarded argument list. Before the invocation,
193 the owned object is destroyed, satisfying the
194 deallocation-before-invocation Asio guarantee. All
195 instances of @ref handler_ptr which refer to the
196 same owned object will be reset, including this instance.
198 @note Care must be taken when the arguments are themselves
199 stored in the owned object. Such arguments must first be
200 moved to the stack or elsewhere, and then passed, or else
201 undefined behavior will result.
203 template<class... Args>
205 invoke(Args&&... args);
211 #include <boost/beast/core/impl/handler_ptr.ipp>