handler_ptr.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // Copyright (c) 2016-2019 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. #ifndef BOOST_BEAST_HANDLER_PTR_HPP
  10. #define BOOST_BEAST_HANDLER_PTR_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/detail/allocator.hpp>
  13. #include <boost/assert.hpp>
  14. #include <boost/config/pragma_message.hpp>
  15. #include <type_traits>
  16. #include <utility>
  17. #ifndef BOOST_BEAST_DOXYGEN
  18. BOOST_PRAGMA_MESSAGE("<boost/beast/core/handler_ptr.hpp> is DEPRECATED and will be removed in a future release.")
  19. namespace boost {
  20. namespace beast {
  21. /** A smart pointer container with associated completion handler.
  22. This is a smart pointer that retains unique ownership of an
  23. object through a pointer. Memory is managed using the allocator
  24. associated with a completion handler stored in the object. The
  25. managed object is destroyed and its memory deallocated when one
  26. of the following occurs:
  27. @li The function @ref invoke is called.
  28. @li The function @ref release_handler is called.
  29. @li The container is destroyed.
  30. Objects of this type are used in the implementation of composed
  31. operations with states that are expensive or impossible to move.
  32. This container manages that non-trivial state on behalf of the
  33. composed operation.
  34. @par Thread Safety
  35. @e Distinct @e objects: Safe.@n
  36. @e Shared @e objects: Unsafe.
  37. @tparam T The type of the owned object. Must be noexcept destructible.
  38. @tparam Handler The type of the completion handler.
  39. */
  40. template<class T, class Handler>
  41. class handler_ptr
  42. {
  43. #ifndef BOOST_BEAST_ALLOW_DEPRECATED
  44. static_assert(sizeof(T) == 0,
  45. BOOST_BEAST_DEPRECATION_STRING);
  46. #endif
  47. T* t_ = nullptr;
  48. union
  49. {
  50. Handler h_;
  51. };
  52. void clear();
  53. public:
  54. /// The type of element stored
  55. using element_type = T;
  56. /// The type of handler stored
  57. using handler_type = Handler;
  58. /// Default constructor (deleted).
  59. handler_ptr() = delete;
  60. /// Copy assignment (deleted).
  61. handler_ptr& operator=(handler_ptr const&) = delete;
  62. /// Move assignment (deleted).
  63. handler_ptr& operator=(handler_ptr &&) = delete;
  64. /** Destructor
  65. If `*this` owns an object the object is destroyed and
  66. the memory deallocated using the allocator associated
  67. with the handler.
  68. */
  69. ~handler_ptr();
  70. /** Move constructor.
  71. When this call returns, the moved-from container
  72. will have no owned object.
  73. */
  74. handler_ptr(handler_ptr&& other);
  75. /// Copy constructor (deleted).
  76. handler_ptr(handler_ptr const& other) = delete;
  77. /** Constructor
  78. This creates a new container with an owned object of
  79. type `T`. The allocator associated with the handler will
  80. be used to allocate memory for the owned object. The
  81. constructor for the owned object will be called with the
  82. following equivalent signature:
  83. @code
  84. T::T(Handler const&, Args&&...)
  85. @endcode
  86. @esafe
  87. Strong guarantee.
  88. @param handler The handler to associate with the owned object.
  89. The implementation takes ownership of the handler by performing a decay-copy.
  90. @param args Optional arguments forwarded to
  91. the owned object's constructor.
  92. */
  93. template<class DeducedHandler, class... Args>
  94. explicit
  95. handler_ptr(DeducedHandler&& handler, Args&&... args);
  96. /// Return a reference to the handler
  97. handler_type const&
  98. handler() const noexcept
  99. {
  100. return h_;
  101. }
  102. /// Return a reference to the handler
  103. handler_type&
  104. handler() noexcept
  105. {
  106. return h_;
  107. }
  108. /// Return `true` if `*this` owns an object
  109. bool
  110. has_value() const noexcept
  111. {
  112. return t_ != nullptr;
  113. }
  114. /** Return a pointer to the owned object.
  115. @par Preconditions:
  116. `has_value() == true`
  117. */
  118. T*
  119. get() const
  120. {
  121. BOOST_ASSERT(t_);
  122. return t_;
  123. }
  124. /** Return a reference to the owned object.
  125. @par Preconditions:
  126. `has_value() == true`
  127. */
  128. T&
  129. operator*() const
  130. {
  131. BOOST_ASSERT(t_);
  132. return *t_;
  133. }
  134. /// Return a pointer to the owned object.
  135. T*
  136. operator->() const
  137. {
  138. BOOST_ASSERT(t_);
  139. return t_;
  140. }
  141. /** Returns ownership of the handler
  142. Before this function returns, the owned object is
  143. destroyed, satisfying the deallocation-before-invocation
  144. Asio guarantee.
  145. @return The released handler.
  146. @par Preconditions:
  147. `has_value() == true`
  148. @par Postconditions:
  149. `has_value() == false`
  150. */
  151. handler_type
  152. release_handler();
  153. /** Invoke the handler in the owned object.
  154. This function invokes the handler in the owned object
  155. with a forwarded argument list. Before the invocation,
  156. the owned object is destroyed, satisfying the
  157. deallocation-before-invocation Asio guarantee.
  158. @par Preconditions:
  159. `has_value() == true`
  160. @par Postconditions:
  161. `has_value() == false`
  162. @note Care must be taken when the arguments are themselves
  163. stored in the owned object. Such arguments must first be
  164. moved to the stack or elsewhere, and then passed, or else
  165. undefined behavior will result.
  166. */
  167. template<class... Args>
  168. void
  169. invoke(Args&&... args);
  170. };
  171. } // beast
  172. } // boost
  173. #include <boost/beast/core/impl/handler_ptr.hpp>
  174. #endif
  175. #endif