saved_handler.ipp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. p_->set_owner(this);
  26. }
  27. saved_handler&
  28. saved_handler::
  29. operator=(saved_handler&& other) noexcept
  30. {
  31. // Can't delete a handler before invoking
  32. BOOST_ASSERT(! has_value());
  33. p_ = boost::exchange(other.p_, nullptr);
  34. p_->set_owner(this);
  35. return *this;
  36. }
  37. bool
  38. saved_handler::
  39. reset() noexcept
  40. {
  41. if(! p_)
  42. return false;
  43. boost::exchange(p_, nullptr)->destroy();
  44. return true;
  45. }
  46. void
  47. saved_handler::
  48. invoke()
  49. {
  50. // Can't invoke without a value
  51. BOOST_ASSERT(has_value());
  52. boost::exchange(
  53. p_, nullptr)->invoke();
  54. }
  55. bool
  56. saved_handler::
  57. maybe_invoke()
  58. {
  59. if(! p_)
  60. return false;
  61. boost::exchange(
  62. p_, nullptr)->invoke();
  63. return true;
  64. }
  65. } // beast
  66. } // boost
  67. #endif