// // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // Official repository: https://github.com/boostorg/beast // #ifndef BOOST_BEAST_IMPL_HANDLER_PTR_HPP #define BOOST_BEAST_IMPL_HANDLER_PTR_HPP #include #include #include namespace boost { namespace beast { template void handler_ptr:: clear() { using A = typename detail::allocator_traits< net::associated_allocator_t< Handler>>::template rebind_alloc; using alloc_traits = beast::detail::allocator_traits; A alloc( net::get_associated_allocator(handler())); alloc_traits::destroy(alloc, t_); alloc_traits::deallocate(alloc, t_, 1); t_ = nullptr; } template handler_ptr:: ~handler_ptr() { if(t_) { clear(); h_.~Handler(); } } template handler_ptr:: handler_ptr(handler_ptr&& other) : t_(other.t_) { if(other.t_) { ::new(static_cast(std::addressof(h_))) Handler(std::move(other.h_)); other.h_.~Handler(); other.t_ = nullptr; } } template template handler_ptr:: handler_ptr(DeducedHandler&& h, Args&&... args) { BOOST_STATIC_ASSERT(! std::is_array::value); using A = typename detail::allocator_traits< net::associated_allocator_t< Handler>>::template rebind_alloc; using alloc_traits = beast::detail::allocator_traits; A alloc{net::get_associated_allocator(h)}; bool destroy = false; auto deleter = [&alloc, &destroy](T* p) { if(destroy) alloc_traits::destroy(alloc, p); alloc_traits::deallocate(alloc, p, 1); }; std::unique_ptr t{ alloc_traits::allocate(alloc, 1), deleter}; alloc_traits::construct(alloc, t.get(), static_cast(h), std::forward(args)...); destroy = true; ::new(static_cast(std::addressof(h_))) Handler(std::forward(h)); t_ = t.release(); } template auto handler_ptr:: release_handler() -> handler_type { BOOST_ASSERT(t_); clear(); auto deleter = [](Handler* h) { h->~Handler(); }; std::unique_ptr< Handler, decltype(deleter)> destroyer{ std::addressof(h_), deleter}; return std::move(h_); } template template void handler_ptr:: invoke(Args&&... args) { BOOST_ASSERT(t_); clear(); auto deleter = [](Handler* h) { boost::ignore_unused(h); // fix #1119 h->~Handler(); }; std::unique_ptr< Handler, decltype(deleter)> destroyer{ std::addressof(h_), deleter}; h_(std::forward(args)...); } } // beast } // boost #endif