diff --git a/CHANGELOG.md b/CHANGELOG.md index 018deee4..8d8ecd6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +Version 171: + +* Add handler_ptr::has_value + +-------------------------------------------------------------------------------- + Version 170: * Add flat_stream to experimental diff --git a/include/boost/beast/core/handler_ptr.hpp b/include/boost/beast/core/handler_ptr.hpp index 559c0c15..64de1b7d 100644 --- a/include/boost/beast/core/handler_ptr.hpp +++ b/include/boost/beast/core/handler_ptr.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -117,32 +118,48 @@ public: template explicit handler_ptr(DeducedHandler&& handler, Args&&... args); - /// Returns a const reference to the handler + /// Return a reference to the handler handler_type const& handler() const { return *reinterpret_cast(&h_); } - /// Returns a reference to the handler + /// Return a reference to the handler handler_type& handler() { return *reinterpret_cast(&h_); } - /** Returns a pointer to the owned object. + /// Return `true` if `*this` owns an object + bool + has_value() const noexcept + { + return t_ != nullptr; + } + + /** Return a pointer to the owned object. + + @par Preconditions: + `has_value() == true` */ T* get() const { + BOOST_ASSERT(t_); return t_; } - /// Return a reference to the owned object. + /** Return a reference to the owned object. + + @par Preconditions: + `has_value() == true` + */ T& operator*() const { + BOOST_ASSERT(t_); return *t_; } @@ -153,15 +170,19 @@ public: return t_; } - /** Release ownership of the handler - - Requires: `*this` owns an object + /** Return ownership of the handler Before this function returns, the owned object is destroyed, satisfying the deallocation-before-invocation Asio guarantee. @return The released handler. + + @par Preconditions: + `has_value() == true` + + @par Postconditions: + `has_value() == false` */ handler_type release_handler(); @@ -173,6 +194,12 @@ public: the owned object is destroyed, satisfying the deallocation-before-invocation Asio guarantee. + @par Preconditions: + `has_value() == true` + + @par Postconditions: + `has_value() == false` + @note Care must be taken when the arguments are themselves stored in the owned object. Such arguments must first be moved to the stack or elsewhere, and then passed, or else diff --git a/test/beast/core/handler_ptr.cpp b/test/beast/core/handler_ptr.cpp index 41fe444e..4830c8bf 100644 --- a/test/beast/core/handler_ptr.cpp +++ b/test/beast/core/handler_ptr.cpp @@ -102,7 +102,9 @@ public: { handler_ptr p{handler{}}; bool b = false; + BEAST_EXPECT(p.has_value()); p.invoke(std::ref(b)); + BEAST_EXPECT(! p.has_value()); BEAST_EXPECT(b); }