saved_handler.ipp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_IMPL_SAVED_HANDLER_IPP
  10. #define BOOST_BEAST_CORE_IMPL_SAVED_HANDLER_IPP
  11. #include <boost/beast/core/saved_handler.hpp>
  12. #include <boost/core/exchange.hpp>
  13. namespace boost {
  14. namespace beast {
  15. saved_handler::
  16. ~saved_handler()
  17. {
  18. if(p_)
  19. p_->destroy();
  20. }
  21. saved_handler::
  22. saved_handler(saved_handler&& other) noexcept
  23. : p_(boost::exchange(other.p_, nullptr))
  24. {
  25. }
  26. saved_handler&
  27. saved_handler::
  28. operator=(saved_handler&& other) noexcept
  29. {
  30. // Can't delete a handler before invoking
  31. BOOST_ASSERT(! has_value());
  32. p_ = boost::exchange(other.p_, nullptr);
  33. return *this;
  34. }
  35. bool
  36. saved_handler::
  37. reset() noexcept
  38. {
  39. if(! p_)
  40. return false;
  41. boost::exchange(p_, nullptr)->destroy();
  42. return true;
  43. }
  44. void
  45. saved_handler::
  46. invoke()
  47. {
  48. // Can't invoke without a value
  49. BOOST_ASSERT(has_value());
  50. boost::exchange(
  51. p_, nullptr)->invoke();
  52. }
  53. bool
  54. saved_handler::
  55. maybe_invoke()
  56. {
  57. if(! p_)
  58. return false;
  59. boost::exchange(
  60. p_, nullptr)->invoke();
  61. return true;
  62. }
  63. } // beast
  64. } // boost
  65. #endif