2 // Copyright (c) 2016-2019 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/allocator.hpp>
15 #include <boost/assert.hpp>
16 #include <boost/config/pragma_message.hpp>
17 #include <type_traits>
20 #ifndef BOOST_BEAST_DOXYGEN
22 BOOST_PRAGMA_MESSAGE("<boost/beast/core/handler_ptr.hpp> is DEPRECATED and will be removed in a future release.")
27 /** A smart pointer container with associated completion handler.
29 This is a smart pointer that retains unique ownership of an
30 object through a pointer. Memory is managed using the allocator
31 associated with a completion handler stored in the object. The
32 managed object is destroyed and its memory deallocated when one
33 of the following occurs:
35 @li The function @ref invoke is called.
37 @li The function @ref release_handler is called.
39 @li The container is destroyed.
41 Objects of this type are used in the implementation of composed
42 operations with states that are expensive or impossible to move.
43 This container manages that non-trivial state on behalf of the
47 @e Distinct @e objects: Safe.@n
48 @e Shared @e objects: Unsafe.
50 @tparam T The type of the owned object. Must be noexcept destructible.
52 @tparam Handler The type of the completion handler.
54 template<class T, class Handler>
57 #ifndef BOOST_BEAST_ALLOW_DEPRECATED
58 static_assert(sizeof(T) == 0,
59 BOOST_BEAST_DEPRECATION_STRING);
71 /// The type of element stored
72 using element_type = T;
74 /// The type of handler stored
75 using handler_type = Handler;
77 /// Default constructor (deleted).
78 handler_ptr() = delete;
80 /// Copy assignment (deleted).
81 handler_ptr& operator=(handler_ptr const&) = delete;
83 /// Move assignment (deleted).
84 handler_ptr& operator=(handler_ptr &&) = delete;
88 If `*this` owns an object the object is destroyed and
89 the memory deallocated using the allocator associated
96 When this call returns, the moved-from container
97 will have no owned object.
99 handler_ptr(handler_ptr&& other);
101 /// Copy constructor (deleted).
102 handler_ptr(handler_ptr const& other) = delete;
106 This creates a new container with an owned object of
107 type `T`. The allocator associated with the handler will
108 be used to allocate memory for the owned object. The
109 constructor for the owned object will be called with the
110 following equivalent signature:
113 T::T(Handler const&, Args&&...)
119 @param handler The handler to associate with the owned object.
120 The implementation takes ownership of the handler by performing a decay-copy.
122 @param args Optional arguments forwarded to
123 the owned object's constructor.
125 template<class DeducedHandler, class... Args>
127 handler_ptr(DeducedHandler&& handler, Args&&... args);
129 /// Return a reference to the handler
131 handler() const noexcept
136 /// Return a reference to the handler
143 /// Return `true` if `*this` owns an object
145 has_value() const noexcept
147 return t_ != nullptr;
150 /** Return a pointer to the owned object.
153 `has_value() == true`
162 /** Return a reference to the owned object.
165 `has_value() == true`
174 /// Return a pointer to the owned object.
182 /** Returns ownership of the handler
184 Before this function returns, the owned object is
185 destroyed, satisfying the deallocation-before-invocation
188 @return The released handler.
191 `has_value() == true`
194 `has_value() == false`
199 /** Invoke the handler in the owned object.
201 This function invokes the handler in the owned object
202 with a forwarded argument list. Before the invocation,
203 the owned object is destroyed, satisfying the
204 deallocation-before-invocation Asio guarantee.
207 `has_value() == true`
210 `has_value() == false`
212 @note Care must be taken when the arguments are themselves
213 stored in the owned object. Such arguments must first be
214 moved to the stack or elsewhere, and then passed, or else
215 undefined behavior will result.
217 template<class... Args>
219 invoke(Args&&... args);
225 #include <boost/beast/core/impl/handler_ptr.hpp>