Add handler_ptr::has_value

This function returns `true` if the container holds a managed object.

Resolves: #1079

Signed-off-by: Damian Jarek <damian.jarek93@gmail.com>
This commit is contained in:
Damian Jarek
2018-05-18 21:28:44 +02:00
committed by Vinnie Falco
parent 344d957f42
commit 86555b90ae
3 changed files with 42 additions and 7 deletions

View File

@@ -1,3 +1,9 @@
Version 171:
* Add handler_ptr::has_value
--------------------------------------------------------------------------------
Version 170: Version 170:
* Add flat_stream to experimental * Add flat_stream to experimental

View File

@@ -13,6 +13,7 @@
#include <boost/beast/core/detail/allocator.hpp> #include <boost/beast/core/detail/allocator.hpp>
#include <boost/beast/core/detail/config.hpp> #include <boost/beast/core/detail/config.hpp>
#include <boost/beast/core/detail/type_traits.hpp> #include <boost/beast/core/detail/type_traits.hpp>
#include <boost/assert.hpp>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
@@ -117,32 +118,48 @@ public:
template<class DeducedHandler, class... Args> template<class DeducedHandler, class... Args>
explicit handler_ptr(DeducedHandler&& handler, Args&&... args); explicit handler_ptr(DeducedHandler&& handler, Args&&... args);
/// Returns a const reference to the handler /// Return a reference to the handler
handler_type const& handler_type const&
handler() const handler() const
{ {
return *reinterpret_cast<Handler const*>(&h_); return *reinterpret_cast<Handler const*>(&h_);
} }
/// Returns a reference to the handler /// Return a reference to the handler
handler_type& handler_type&
handler() handler()
{ {
return *reinterpret_cast<Handler*>(&h_); return *reinterpret_cast<Handler*>(&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* T*
get() const get() const
{ {
BOOST_ASSERT(t_);
return t_; return t_;
} }
/// Return a reference to the owned object. /** Return a reference to the owned object.
@par Preconditions:
`has_value() == true`
*/
T& T&
operator*() const operator*() const
{ {
BOOST_ASSERT(t_);
return *t_; return *t_;
} }
@@ -153,15 +170,19 @@ public:
return t_; return t_;
} }
/** Release ownership of the handler /** Return ownership of the handler
Requires: `*this` owns an object
Before this function returns, the owned object is Before this function returns, the owned object is
destroyed, satisfying the deallocation-before-invocation destroyed, satisfying the deallocation-before-invocation
Asio guarantee. Asio guarantee.
@return The released handler. @return The released handler.
@par Preconditions:
`has_value() == true`
@par Postconditions:
`has_value() == false`
*/ */
handler_type handler_type
release_handler(); release_handler();
@@ -173,6 +194,12 @@ public:
the owned object is destroyed, satisfying the the owned object is destroyed, satisfying the
deallocation-before-invocation Asio guarantee. 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 @note Care must be taken when the arguments are themselves
stored in the owned object. Such arguments must first be stored in the owned object. Such arguments must first be
moved to the stack or elsewhere, and then passed, or else moved to the stack or elsewhere, and then passed, or else

View File

@@ -102,7 +102,9 @@ public:
{ {
handler_ptr<T, handler> p{handler{}}; handler_ptr<T, handler> p{handler{}};
bool b = false; bool b = false;
BEAST_EXPECT(p.has_value());
p.invoke(std::ref(b)); p.invoke(std::ref(b));
BEAST_EXPECT(! p.has_value());
BEAST_EXPECT(b); BEAST_EXPECT(b);
} }