Files
boost_beast/include/beast/websocket/detail/pausation.hpp

163 lines
3.2 KiB
C++
Raw Normal View History

2017-07-20 08:01:46 -07:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-07-20 08:01:46 -07:00
//
// 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)
//
#ifndef BEAST_WEBSOCKET_DETAIL_PAUSATION_HPP
#define BEAST_WEBSOCKET_DETAIL_PAUSATION_HPP
2017-07-20 08:01:46 -07:00
#include <beast/core/handler_ptr.hpp>
2016-09-25 12:17:32 -04:00
#include <boost/assert.hpp>
2017-07-20 08:01:46 -07:00
#include <array>
#include <memory>
#include <new>
#include <utility>
namespace beast {
namespace websocket {
namespace detail {
// A container that holds a suspended, asynchronous composed
// operation. The contained object may be invoked later to
// resume the operation, or the container may be destroyed.
2017-07-20 08:01:46 -07:00
//
class pausation
2017-07-20 08:01:46 -07:00
{
struct base
{
base() = default;
base(base &&) = default;
virtual ~base() = default;
virtual void move(void* p) = 0;
virtual void operator()() = 0;
};
template<class F>
struct holder : base
{
F f;
holder(holder&&) = default;
template<class U>
explicit
holder(U&& u)
: f(std::forward<U>(u))
{
}
void
move(void* p) override
{
::new(p) holder(std::move(*this));
}
void
operator()() override
{
F f_(std::move(f));
this->~holder();
// invocation of f_() can
// assign a new object to *this.
2017-07-20 08:01:46 -07:00
f_();
}
};
struct exemplar
{
struct H
{
void operator()();
};
struct T
{
using handler_type = H;
};
handler_ptr<T, H> hp;
void operator()();
2017-07-20 08:01:46 -07:00
};
using buf_type = char[sizeof(holder<exemplar>)];
base* base_ = nullptr;
alignas(holder<exemplar>) buf_type buf_;
public:
~pausation()
2017-07-20 08:01:46 -07:00
{
if(base_)
base_->~base();
2017-07-20 08:01:46 -07:00
}
pausation() = default;
2017-07-20 08:01:46 -07:00
pausation(pausation&& other)
2017-07-20 08:01:46 -07:00
{
if(other.base_)
{
// type-pun
2017-07-20 08:01:46 -07:00
base_ = reinterpret_cast<base*>(&buf_[0]);
other.base_->move(buf_);
other.base_ = nullptr;
}
}
pausation&
operator=(pausation&& other)
2017-07-20 08:01:46 -07:00
{
// Engaged pausations must be invoked before
2017-07-20 08:01:46 -07:00
// assignment otherwise the io_service
// completion invariants are broken.
2016-09-25 12:17:32 -04:00
BOOST_ASSERT(! base_);
2017-07-20 08:01:46 -07:00
if(other.base_)
{
// type-pun
2017-07-20 08:01:46 -07:00
base_ = reinterpret_cast<base*>(&buf_[0]);
other.base_->move(buf_);
other.base_ = nullptr;
}
return *this;
}
template<class F>
void
emplace(F&& f);
bool
2017-07-20 08:01:46 -07:00
maybe_invoke()
{
if(base_)
{
auto const basep = base_;
base_ = nullptr;
(*basep)();
return true;
2017-07-20 08:01:46 -07:00
}
return false;
2017-07-20 08:01:46 -07:00
}
};
template<class F>
void
pausation::emplace(F&& f)
2017-07-20 08:01:46 -07:00
{
static_assert(sizeof(buf_type) >= sizeof(holder<F>),
"buffer too small");
2016-09-25 12:17:32 -04:00
BOOST_ASSERT(! base_);
2017-07-20 08:01:46 -07:00
::new(buf_) holder<F>(std::forward<F>(f));
// type-pun
2017-07-20 08:01:46 -07:00
base_ = reinterpret_cast<base*>(&buf_[0]);
}
} // detail
} // websocket
} // beast
#endif