saved_handler.hpp 3.7 KB

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