saved_handler.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_CORE_SAVED_HANDLER_HPP
  10. #define BOOST_BEAST_CORE_SAVED_HANDLER_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/asio/cancellation_type.hpp>
  13. namespace boost {
  14. namespace beast {
  15. /** An invocable, nullary function object which holds a completion handler.
  16. This container can hold a type-erased instance of any completion
  17. handler, or it can be empty. When the container holds a value,
  18. the implementation maintains an instance of `net::executor_work_guard`
  19. for the handler's associated executor. Memory is dynamically allocated
  20. to store the completion handler, and the allocator may optionally
  21. be specified. Otherwise, the implementation uses the handler's
  22. associated allocator.
  23. */
  24. class saved_handler
  25. {
  26. class base;
  27. template<class, class>
  28. class impl;
  29. base* p_ = nullptr;
  30. public:
  31. /// Default Constructor
  32. saved_handler() = default;
  33. /// Copy Constructor (deleted)
  34. saved_handler(saved_handler const&) = delete;
  35. /// Copy Assignment (deleted)
  36. saved_handler& operator=(saved_handler const&) = delete;
  37. /// Destructor
  38. BOOST_BEAST_DECL
  39. ~saved_handler();
  40. /// Move Constructor
  41. BOOST_BEAST_DECL
  42. saved_handler(saved_handler&& other) noexcept;
  43. /// Move Assignment
  44. BOOST_BEAST_DECL
  45. saved_handler&
  46. operator=(saved_handler&& other) noexcept;
  47. /// Returns `true` if `*this` contains a completion handler.
  48. bool
  49. has_value() const noexcept
  50. {
  51. return p_ != nullptr;
  52. }
  53. /** Store a completion handler in the container.
  54. Requires `this->has_value() == false`.
  55. @param handler The completion handler to store.
  56. The implementation takes ownership of the handler by performing a decay-copy.
  57. @param alloc The allocator to use.
  58. @param cancel_type The type of cancellation allowed to complete this op.
  59. */
  60. template<class Handler, class Allocator>
  61. void
  62. emplace(Handler&& handler, Allocator const& alloc,
  63. net::cancellation_type cancel_type = net::cancellation_type::terminal);
  64. /** Store a completion handler in the container.
  65. Requires `this->has_value() == false`. The
  66. implementation will use the handler's associated
  67. allocator to obtian storage.
  68. @param handler The completion handler to store.
  69. The implementation takes ownership of the handler by performing a decay-copy.
  70. @param cancel_type The type of cancellation allowed to complete this op.
  71. */
  72. template<class Handler>
  73. void
  74. emplace(Handler&& handler,
  75. net::cancellation_type cancel_type = net::cancellation_type::terminal);
  76. /** Discard the saved handler, if one exists.
  77. If `*this` contains an object, it is destroyed.
  78. @returns `true` if an object was destroyed.
  79. */
  80. BOOST_BEAST_DECL
  81. bool
  82. reset() noexcept;
  83. /** Unconditionally invoke the stored completion handler.
  84. Requires `this->has_value() == true`. Any dynamic memory
  85. used is deallocated before the stored completion handler
  86. is invoked. The executor work guard is also reset before
  87. the invocation.
  88. */
  89. BOOST_BEAST_DECL
  90. void
  91. invoke();
  92. /** Conditionally invoke the stored completion handler.
  93. Invokes the stored completion handler if
  94. `this->has_value() == true`, otherwise does nothing. Any
  95. dynamic memory used is deallocated before the stored completion
  96. handler is invoked. The executor work guard is also reset before
  97. the invocation.
  98. @return `true` if the invocation took place.
  99. */
  100. BOOST_BEAST_DECL
  101. bool
  102. maybe_invoke();
  103. };
  104. } // beast
  105. } // boost
  106. #include <boost/beast/core/impl/saved_handler.hpp>
  107. #ifdef BOOST_BEAST_HEADER_ONLY
  108. #include <boost/beast/core/impl/saved_handler.ipp>
  109. #endif
  110. #endif