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