From 34037d538e02a3c0616f648b972f8b1d948d52ed Mon Sep 17 00:00:00 2001 From: Damian Jarek Date: Fri, 2 Nov 2018 14:15:25 +0100 Subject: [PATCH] Simplify handler_ptr: Replace aligned_union in handler_ptr with the built-in one to reduce the number of template instantiations and remove the reinterpret_casts that were required. Signed-off-by: Damian Jarek --- CHANGELOG.md | 1 + include/boost/beast/core/handler_ptr.hpp | 17 ++++----- include/boost/beast/core/impl/handler_ptr.ipp | 35 ++++++++++--------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ce331d4..55c54134 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Version 196: * Tidy up calls to placement new * Remove unused type_traits +* Simplify handler_ptr -------------------------------------------------------------------------------- diff --git a/include/boost/beast/core/handler_ptr.hpp b/include/boost/beast/core/handler_ptr.hpp index d25e07df..e4117cc8 100644 --- a/include/boost/beast/core/handler_ptr.hpp +++ b/include/boost/beast/core/handler_ptr.hpp @@ -12,7 +12,6 @@ #include #include -#include #include #include #include @@ -50,10 +49,11 @@ namespace beast { template class handler_ptr { - using handler_storage_t = typename detail::aligned_union<1, Handler>::type; - T* t_ = nullptr; - handler_storage_t h_; + union + { + Handler h_; + }; void clear(); @@ -113,20 +113,21 @@ public: the owned object's constructor. */ template - explicit handler_ptr(DeducedHandler&& handler, Args&&... args); + explicit + handler_ptr(DeducedHandler&& handler, Args&&... args); /// Return a reference to the handler handler_type const& handler() const { - return *reinterpret_cast(&h_); + return h_; } /// Return a reference to the handler handler_type& handler() { - return *reinterpret_cast(&h_); + return h_; } /// Return `true` if `*this` owns an object @@ -167,7 +168,7 @@ public: return t_; } - /** Return ownership of the handler + /** Returns ownership of the handler Before this function returns, the owned object is destroyed, satisfying the deallocation-before-invocation diff --git a/include/boost/beast/core/impl/handler_ptr.ipp b/include/boost/beast/core/impl/handler_ptr.ipp index 70fbd6d8..1c64da61 100644 --- a/include/boost/beast/core/impl/handler_ptr.ipp +++ b/include/boost/beast/core/impl/handler_ptr.ipp @@ -41,7 +41,7 @@ handler_ptr:: if(t_) { clear(); - handler().~Handler(); + h_.~Handler(); } } @@ -52,8 +52,9 @@ handler_ptr(handler_ptr&& other) { if(other.t_) { - new(&h_) Handler(std::move(other.handler())); - other.handler().~Handler(); + ::new(static_cast(std::addressof(h_))) + Handler(std::move(other.h_)); + other.h_.~Handler(); other.t_ = nullptr; } } @@ -64,25 +65,27 @@ handler_ptr:: handler_ptr(DeducedHandler&& h, Args&&... args) { BOOST_STATIC_ASSERT(! std::is_array::value); - typename beast::detail::allocator_traits< + using A = typename beast::detail::allocator_traits< net::associated_allocator_t< - Handler>>::template rebind_alloc alloc{ - net::get_associated_allocator(h)}; - using A = decltype(alloc); + Handler>>::template rebind_alloc; + A alloc{net::get_associated_allocator(h)}; + using traits = + typename beast::detail::allocator_traits; bool destroy = false; auto deleter = [&alloc, &destroy](T* p) { if(destroy) - beast::detail::allocator_traits::destroy(alloc, p); - beast::detail::allocator_traits::deallocate(alloc, p, 1); + traits::destroy(alloc, p); + traits::deallocate(alloc, p, 1); }; std::unique_ptr t{ - beast::detail::allocator_traits::allocate(alloc, 1), deleter}; - beast::detail::allocator_traits::construct(alloc, t.get(), + traits::allocate(alloc, 1), deleter}; + traits::construct(alloc, t.get(), static_cast(h), std::forward(args)...); destroy = true; - new(&h_) Handler(std::forward(h)); + ::new(static_cast(std::addressof(h_))) + Handler(std::forward(h)); t_ = t.release(); } @@ -100,8 +103,8 @@ release_handler() -> }; std::unique_ptr< Handler, decltype(deleter)> destroyer{ - &handler(), deleter}; - return std::move(handler()); + std::addressof(h_), deleter}; + return std::move(h_); } template @@ -119,8 +122,8 @@ invoke(Args&&... args) }; std::unique_ptr< Handler, decltype(deleter)> destroyer{ - &handler(), deleter}; - handler()(std::forward(args)...); + std::addressof(h_), deleter}; + h_(std::forward(args)...); } } // beast