forked from boostorg/beast
Boost prep
This commit is contained in:
1 parent
3777cb8b9c
commit
7139dd639a
375 files changed
+5253
-3884
No files matched your search
@@ -0,0 +1,495 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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_BUFFER_CAT_IPP
|
||||
#define BOOST_BEAST_IMPL_BUFFER_CAT_IPP
|
||||
|
||||
#include <boost/beast/core/detail/type_traits.hpp>
|
||||
#include <boost/asio/buffer.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <iterator>
|
||||
#include <new>
|
||||
#include <stdexcept>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
template<class... Bn>
|
||||
class buffer_cat_view<Bn...>::const_iterator
|
||||
{
|
||||
std::size_t n_;
|
||||
std::tuple<Bn...> const* bn_;
|
||||
std::array<char, detail::max_sizeof<
|
||||
typename Bn::const_iterator...>()> buf_;
|
||||
|
||||
friend class buffer_cat_view<Bn...>;
|
||||
|
||||
template<std::size_t I>
|
||||
using C = std::integral_constant<std::size_t, I>;
|
||||
|
||||
template<std::size_t I>
|
||||
using iter_t = typename std::tuple_element<
|
||||
I, std::tuple<Bn...>>::type::const_iterator;
|
||||
|
||||
template<std::size_t I>
|
||||
iter_t<I>&
|
||||
iter()
|
||||
{
|
||||
// type-pun
|
||||
return *reinterpret_cast<
|
||||
iter_t<I>*>(static_cast<void*>(buf_.data()));
|
||||
}
|
||||
|
||||
template<std::size_t I>
|
||||
iter_t<I> const&
|
||||
iter() const
|
||||
{
|
||||
// type-pun
|
||||
return *reinterpret_cast<
|
||||
iter_t<I> const*>(static_cast<
|
||||
void const*>(buf_.data()));
|
||||
}
|
||||
|
||||
public:
|
||||
using value_type = typename
|
||||
detail::common_buffers_type<Bn...>::type;
|
||||
using pointer = value_type const*;
|
||||
using reference = value_type;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using iterator_category =
|
||||
std::bidirectional_iterator_tag;
|
||||
|
||||
~const_iterator();
|
||||
const_iterator();
|
||||
const_iterator(const_iterator&& other);
|
||||
const_iterator(const_iterator const& other);
|
||||
const_iterator& operator=(const_iterator&& other);
|
||||
const_iterator& operator=(const_iterator const& other);
|
||||
|
||||
bool
|
||||
operator==(const_iterator const& other) const;
|
||||
|
||||
bool
|
||||
operator!=(const_iterator const& other) const
|
||||
{
|
||||
return ! (*this == other);
|
||||
}
|
||||
|
||||
reference
|
||||
operator*() const;
|
||||
|
||||
pointer
|
||||
operator->() const = delete;
|
||||
|
||||
const_iterator&
|
||||
operator++();
|
||||
|
||||
const_iterator
|
||||
operator++(int);
|
||||
|
||||
const_iterator&
|
||||
operator--();
|
||||
|
||||
const_iterator
|
||||
operator--(int);
|
||||
|
||||
private:
|
||||
const_iterator(
|
||||
std::tuple<Bn...> const& bn, bool at_end);
|
||||
|
||||
void
|
||||
construct(C<sizeof...(Bn)> const&)
|
||||
{
|
||||
auto constexpr I = sizeof...(Bn);
|
||||
n_ = I;
|
||||
}
|
||||
|
||||
template<std::size_t I>
|
||||
void
|
||||
construct(C<I> const&)
|
||||
{
|
||||
if(boost::asio::buffer_size(
|
||||
std::get<I>(*bn_)) != 0)
|
||||
{
|
||||
n_ = I;
|
||||
new(&buf_[0]) iter_t<I>{
|
||||
std::get<I>(*bn_).begin()};
|
||||
return;
|
||||
}
|
||||
construct(C<I+1>{});
|
||||
}
|
||||
|
||||
void
|
||||
rconstruct(C<0> const&)
|
||||
{
|
||||
auto constexpr I = 0;
|
||||
if(boost::asio::buffer_size(
|
||||
std::get<I>(*bn_)) != 0)
|
||||
{
|
||||
n_ = I;
|
||||
new(&buf_[0]) iter_t<I>{
|
||||
std::get<I>(*bn_).end()};
|
||||
return;
|
||||
}
|
||||
BOOST_THROW_EXCEPTION(std::logic_error{
|
||||
"invalid iterator"});
|
||||
}
|
||||
|
||||
template<std::size_t I>
|
||||
void
|
||||
rconstruct(C<I> const&)
|
||||
{
|
||||
if(boost::asio::buffer_size(
|
||||
std::get<I>(*bn_)) != 0)
|
||||
{
|
||||
n_ = I;
|
||||
new(&buf_[0]) iter_t<I>{
|
||||
std::get<I>(*bn_).end()};
|
||||
return;
|
||||
}
|
||||
rconstruct(C<I-1>{});
|
||||
}
|
||||
|
||||
void
|
||||
destroy(C<sizeof...(Bn)> const&)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
template<std::size_t I>
|
||||
void
|
||||
destroy(C<I> const&)
|
||||
{
|
||||
if(n_ == I)
|
||||
{
|
||||
using Iter = iter_t<I>;
|
||||
iter<I>().~Iter();
|
||||
return;
|
||||
}
|
||||
destroy(C<I+1>{});
|
||||
}
|
||||
|
||||
void
|
||||
move(const_iterator&&,
|
||||
C<sizeof...(Bn)> const&)
|
||||
{
|
||||
}
|
||||
|
||||
template<std::size_t I>
|
||||
void
|
||||
move(const_iterator&& other,
|
||||
C<I> const&)
|
||||
{
|
||||
if(n_ == I)
|
||||
{
|
||||
new(&buf_[0]) iter_t<I>{
|
||||
std::move(other.iter<I>())};
|
||||
return;
|
||||
}
|
||||
move(std::move(other), C<I+1>{});
|
||||
}
|
||||
|
||||
void
|
||||
copy(const_iterator const&,
|
||||
C<sizeof...(Bn)> const&)
|
||||
{
|
||||
}
|
||||
|
||||
template<std::size_t I>
|
||||
void
|
||||
copy(const_iterator const& other,
|
||||
C<I> const&)
|
||||
{
|
||||
if(n_ == I)
|
||||
{
|
||||
new(&buf_[0]) iter_t<I>{
|
||||
other.iter<I>()};
|
||||
return;
|
||||
}
|
||||
copy(other, C<I+1>{});
|
||||
}
|
||||
|
||||
bool
|
||||
equal(const_iterator const&,
|
||||
C<sizeof...(Bn)> const&) const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
template<std::size_t I>
|
||||
bool
|
||||
equal(const_iterator const& other,
|
||||
C<I> const&) const
|
||||
{
|
||||
if(n_ == I)
|
||||
return iter<I>() == other.iter<I>();
|
||||
return equal(other, C<I+1>{});
|
||||
}
|
||||
|
||||
[[noreturn]]
|
||||
reference
|
||||
dereference(C<sizeof...(Bn)> const&) const
|
||||
{
|
||||
BOOST_THROW_EXCEPTION(std::logic_error{
|
||||
"invalid iterator"});
|
||||
}
|
||||
|
||||
template<std::size_t I>
|
||||
reference
|
||||
dereference(C<I> const&) const
|
||||
{
|
||||
if(n_ == I)
|
||||
return *iter<I>();
|
||||
return dereference(C<I+1>{});
|
||||
}
|
||||
|
||||
[[noreturn]]
|
||||
void
|
||||
increment(C<sizeof...(Bn)> const&)
|
||||
{
|
||||
BOOST_THROW_EXCEPTION(std::logic_error{
|
||||
"invalid iterator"});
|
||||
}
|
||||
|
||||
template<std::size_t I>
|
||||
void
|
||||
increment(C<I> const&)
|
||||
{
|
||||
if(n_ == I)
|
||||
{
|
||||
if(++iter<I>() !=
|
||||
std::get<I>(*bn_).end())
|
||||
return;
|
||||
using Iter = iter_t<I>;
|
||||
iter<I>().~Iter();
|
||||
return construct(C<I+1>{});
|
||||
}
|
||||
increment(C<I+1>{});
|
||||
}
|
||||
|
||||
void
|
||||
decrement(C<sizeof...(Bn)> const&)
|
||||
{
|
||||
auto constexpr I = sizeof...(Bn);
|
||||
if(n_ == I)
|
||||
rconstruct(C<I-1>{});
|
||||
decrement(C<I-1>{});
|
||||
}
|
||||
|
||||
template<std::size_t I>
|
||||
void
|
||||
decrement(C<I> const&)
|
||||
{
|
||||
if(n_ == I)
|
||||
{
|
||||
if(iter<I>() != std::get<I>(*bn_).begin())
|
||||
{
|
||||
--iter<I>();
|
||||
return;
|
||||
}
|
||||
--n_;
|
||||
using Iter = iter_t<I>;
|
||||
iter<I>().~Iter();
|
||||
rconstruct(C<I-1>{});
|
||||
}
|
||||
decrement(C<I-1>{});
|
||||
}
|
||||
|
||||
void
|
||||
decrement(C<0> const&)
|
||||
{
|
||||
auto constexpr I = 0;
|
||||
if(iter<I>() != std::get<I>(*bn_).begin())
|
||||
{
|
||||
--iter<I>();
|
||||
return;
|
||||
}
|
||||
BOOST_THROW_EXCEPTION(std::logic_error{
|
||||
"invalid iterator"});
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<class... Bn>
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::~const_iterator()
|
||||
{
|
||||
destroy(C<0>{});
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::const_iterator()
|
||||
: n_(sizeof...(Bn))
|
||||
, bn_(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::const_iterator(
|
||||
std::tuple<Bn...> const& bn, bool at_end)
|
||||
: bn_(&bn)
|
||||
{
|
||||
if(at_end)
|
||||
n_ = sizeof...(Bn);
|
||||
else
|
||||
construct(C<0>{});
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::const_iterator(const_iterator&& other)
|
||||
: n_(other.n_)
|
||||
, bn_(other.bn_)
|
||||
{
|
||||
move(std::move(other), C<0>{});
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::const_iterator(const_iterator const& other)
|
||||
: n_(other.n_)
|
||||
, bn_(other.bn_)
|
||||
{
|
||||
copy(other, C<0>{});
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
auto
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::operator=(const_iterator&& other) ->
|
||||
const_iterator&
|
||||
{
|
||||
if(&other == this)
|
||||
return *this;
|
||||
destroy(C<0>{});
|
||||
n_ = other.n_;
|
||||
bn_ = other.bn_;
|
||||
// VFALCO What about exceptions?
|
||||
move(std::move(other), C<0>{});
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
auto
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::operator=(const_iterator const& other) ->
|
||||
const_iterator&
|
||||
{
|
||||
if(&other == this)
|
||||
return *this;
|
||||
destroy(C<0>{});
|
||||
n_ = other.n_;
|
||||
bn_ = other.bn_;
|
||||
// VFALCO What about exceptions?
|
||||
copy(other, C<0>{});
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
bool
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::operator==(const_iterator const& other) const
|
||||
{
|
||||
if(bn_ != other.bn_)
|
||||
return false;
|
||||
if(n_ != other.n_)
|
||||
return false;
|
||||
return equal(other, C<0>{});
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
auto
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::operator*() const ->
|
||||
reference
|
||||
{
|
||||
return dereference(C<0>{});
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
auto
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::operator++() ->
|
||||
const_iterator&
|
||||
{
|
||||
increment(C<0>{});
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
auto
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::operator++(int) ->
|
||||
const_iterator
|
||||
{
|
||||
auto temp = *this;
|
||||
++(*this);
|
||||
return temp;
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
auto
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::operator--() ->
|
||||
const_iterator&
|
||||
{
|
||||
decrement(C<sizeof...(Bn)>{});
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
auto
|
||||
buffer_cat_view<Bn...>::
|
||||
const_iterator::operator--(int) ->
|
||||
const_iterator
|
||||
{
|
||||
auto temp = *this;
|
||||
--(*this);
|
||||
return temp;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<class... Bn>
|
||||
buffer_cat_view<Bn...>::
|
||||
buffer_cat_view(Bn const&... bn)
|
||||
: bn_(bn...)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
template<class... Bn>
|
||||
inline
|
||||
auto
|
||||
buffer_cat_view<Bn...>::begin() const ->
|
||||
const_iterator
|
||||
{
|
||||
return const_iterator{bn_, false};
|
||||
}
|
||||
|
||||
template<class... Bn>
|
||||
inline
|
||||
auto
|
||||
buffer_cat_view<Bn...>::end() const ->
|
||||
const_iterator
|
||||
{
|
||||
return const_iterator{bn_, true};
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,254 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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_BUFFER_PREFIX_IPP
|
||||
#define BOOST_BEAST_IMPL_BUFFER_PREFIX_IPP
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
namespace detail {
|
||||
|
||||
inline
|
||||
boost::asio::const_buffer
|
||||
buffer_prefix(std::size_t size,
|
||||
boost::asio::const_buffer buffer)
|
||||
{
|
||||
using boost::asio::buffer_cast;
|
||||
using boost::asio::buffer_size;
|
||||
return {buffer_cast<void const*>(buffer),
|
||||
(std::min)(size, buffer_size(buffer))};
|
||||
}
|
||||
|
||||
inline
|
||||
boost::asio::mutable_buffer
|
||||
buffer_prefix(std::size_t size,
|
||||
boost::asio::mutable_buffer buffer)
|
||||
{
|
||||
using boost::asio::buffer_cast;
|
||||
using boost::asio::buffer_size;
|
||||
return {buffer_cast<void*>(buffer),
|
||||
(std::min)(size, buffer_size(buffer))};
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
template<class BufferSequence>
|
||||
class buffer_prefix_view<BufferSequence>::const_iterator
|
||||
{
|
||||
friend class buffer_prefix_view<BufferSequence>;
|
||||
|
||||
buffer_prefix_view const* b_ = nullptr;
|
||||
std::size_t remain_;
|
||||
iter_type it_;
|
||||
|
||||
public:
|
||||
using value_type = typename std::conditional<
|
||||
std::is_convertible<typename
|
||||
std::iterator_traits<iter_type>::value_type,
|
||||
boost::asio::mutable_buffer>::value,
|
||||
boost::asio::mutable_buffer,
|
||||
boost::asio::const_buffer>::type;
|
||||
using pointer = value_type const*;
|
||||
using reference = value_type;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using iterator_category =
|
||||
std::bidirectional_iterator_tag;
|
||||
|
||||
const_iterator() = default;
|
||||
const_iterator(const_iterator&& other) = default;
|
||||
const_iterator(const_iterator const& other) = default;
|
||||
const_iterator& operator=(const_iterator&& other) = default;
|
||||
const_iterator& operator=(const_iterator const& other) = default;
|
||||
|
||||
bool
|
||||
operator==(const_iterator const& other) const
|
||||
{
|
||||
return b_ == other.b_ && it_ == other.it_;
|
||||
}
|
||||
|
||||
bool
|
||||
operator!=(const_iterator const& other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
reference
|
||||
operator*() const
|
||||
{
|
||||
return detail::buffer_prefix(remain_, *it_);
|
||||
}
|
||||
|
||||
pointer
|
||||
operator->() const = delete;
|
||||
|
||||
const_iterator&
|
||||
operator++()
|
||||
{
|
||||
remain_ -= boost::asio::buffer_size(*it_++);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
operator++(int)
|
||||
{
|
||||
auto temp = *this;
|
||||
remain_ -= boost::asio::buffer_size(*it_++);
|
||||
return temp;
|
||||
}
|
||||
|
||||
const_iterator&
|
||||
operator--()
|
||||
{
|
||||
remain_ += boost::asio::buffer_size(*--it_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
operator--(int)
|
||||
{
|
||||
auto temp = *this;
|
||||
remain_ += boost::asio::buffer_size(*--it_);
|
||||
return temp;
|
||||
}
|
||||
|
||||
private:
|
||||
const_iterator(buffer_prefix_view const& b,
|
||||
std::true_type)
|
||||
: b_(&b)
|
||||
, remain_(0)
|
||||
, it_(b_->end_)
|
||||
{
|
||||
}
|
||||
|
||||
const_iterator(buffer_prefix_view const& b,
|
||||
std::false_type)
|
||||
: b_(&b)
|
||||
, remain_(b_->size_)
|
||||
, it_(b_->bs_.begin())
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template<class BufferSequence>
|
||||
void
|
||||
buffer_prefix_view<BufferSequence>::
|
||||
setup(std::size_t size)
|
||||
{
|
||||
size_ = 0;
|
||||
end_ = bs_.begin();
|
||||
auto const last = bs_.end();
|
||||
while(end_ != last)
|
||||
{
|
||||
auto const len =
|
||||
boost::asio::buffer_size(*end_++);
|
||||
if(len >= size)
|
||||
{
|
||||
size_ += size;
|
||||
break;
|
||||
}
|
||||
size -= len;
|
||||
size_ += len;
|
||||
}
|
||||
}
|
||||
|
||||
template<class BufferSequence>
|
||||
buffer_prefix_view<BufferSequence>::
|
||||
buffer_prefix_view(buffer_prefix_view&& other)
|
||||
: buffer_prefix_view(std::move(other),
|
||||
std::distance<iter_type>(
|
||||
other.bs_.begin(), other.end_))
|
||||
{
|
||||
}
|
||||
|
||||
template<class BufferSequence>
|
||||
buffer_prefix_view<BufferSequence>::
|
||||
buffer_prefix_view(buffer_prefix_view const& other)
|
||||
: buffer_prefix_view(other,
|
||||
std::distance<iter_type>(
|
||||
other.bs_.begin(), other.end_))
|
||||
{
|
||||
}
|
||||
|
||||
template<class BufferSequence>
|
||||
auto
|
||||
buffer_prefix_view<BufferSequence>::
|
||||
operator=(buffer_prefix_view&& other) ->
|
||||
buffer_prefix_view&
|
||||
{
|
||||
auto const dist = std::distance<iter_type>(
|
||||
other.bs_.begin(), other.end_);
|
||||
bs_ = std::move(other.bs_);
|
||||
size_ = other.size_;
|
||||
end_ = std::next(bs_.begin(), dist);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class BufferSequence>
|
||||
auto
|
||||
buffer_prefix_view<BufferSequence>::
|
||||
operator=(buffer_prefix_view const& other) ->
|
||||
buffer_prefix_view&
|
||||
{
|
||||
auto const dist = std::distance<iter_type>(
|
||||
other.bs_.begin(), other.end_);
|
||||
bs_ = other.bs_;
|
||||
size_ = other.size_;
|
||||
end_ = std::next(bs_.begin(), dist);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class BufferSequence>
|
||||
buffer_prefix_view<BufferSequence>::
|
||||
buffer_prefix_view(std::size_t size,
|
||||
BufferSequence const& bs)
|
||||
: bs_(bs)
|
||||
{
|
||||
setup(size);
|
||||
}
|
||||
|
||||
template<class BufferSequence>
|
||||
template<class... Args>
|
||||
buffer_prefix_view<BufferSequence>::
|
||||
buffer_prefix_view(std::size_t size,
|
||||
boost::in_place_init_t, Args&&... args)
|
||||
: bs_(std::forward<Args>(args)...)
|
||||
{
|
||||
setup(size);
|
||||
}
|
||||
|
||||
template<class BufferSequence>
|
||||
inline
|
||||
auto
|
||||
buffer_prefix_view<BufferSequence>::begin() const ->
|
||||
const_iterator
|
||||
{
|
||||
return const_iterator{*this, std::false_type{}};
|
||||
}
|
||||
|
||||
template<class BufferSequence>
|
||||
inline
|
||||
auto
|
||||
buffer_prefix_view<BufferSequence>::end() const ->
|
||||
const_iterator
|
||||
{
|
||||
return const_iterator{*this, std::true_type{}};
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,245 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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_BUFFERED_READ_STREAM_IPP
|
||||
#define BOOST_BEAST_IMPL_BUFFERED_READ_STREAM_IPP
|
||||
|
||||
#include <boost/beast/core/bind_handler.hpp>
|
||||
#include <boost/beast/core/error.hpp>
|
||||
#include <boost/beast/core/handler_ptr.hpp>
|
||||
#include <boost/beast/core/type_traits.hpp>
|
||||
#include <boost/beast/core/detail/config.hpp>
|
||||
#include <boost/asio/handler_alloc_hook.hpp>
|
||||
#include <boost/asio/handler_continuation_hook.hpp>
|
||||
#include <boost/asio/handler_invoke_hook.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
template<class Stream, class DynamicBuffer>
|
||||
template<class MutableBufferSequence, class Handler>
|
||||
class buffered_read_stream<
|
||||
Stream, DynamicBuffer>::read_some_op
|
||||
{
|
||||
int step_ = 0;
|
||||
buffered_read_stream& s_;
|
||||
MutableBufferSequence b_;
|
||||
Handler h_;
|
||||
|
||||
public:
|
||||
read_some_op(read_some_op&&) = default;
|
||||
read_some_op(read_some_op const&) = default;
|
||||
|
||||
template<class DeducedHandler, class... Args>
|
||||
read_some_op(DeducedHandler&& h,
|
||||
buffered_read_stream& s,
|
||||
MutableBufferSequence const& b)
|
||||
: s_(s)
|
||||
, b_(b)
|
||||
, h_(std::forward<DeducedHandler>(h))
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
operator()(error_code const& ec,
|
||||
std::size_t bytes_transferred);
|
||||
|
||||
friend
|
||||
void* asio_handler_allocate(
|
||||
std::size_t size, read_some_op* op)
|
||||
{
|
||||
using boost::asio::asio_handler_allocate;
|
||||
return asio_handler_allocate(
|
||||
size, std::addressof(op->h_));
|
||||
}
|
||||
|
||||
friend
|
||||
void asio_handler_deallocate(
|
||||
void* p, std::size_t size, read_some_op* op)
|
||||
{
|
||||
using boost::asio::asio_handler_deallocate;
|
||||
asio_handler_deallocate(
|
||||
p, size, std::addressof(op->h_));
|
||||
}
|
||||
|
||||
friend
|
||||
bool asio_handler_is_continuation(read_some_op* op)
|
||||
{
|
||||
using boost::asio::asio_handler_is_continuation;
|
||||
return asio_handler_is_continuation(
|
||||
std::addressof(op->h_));
|
||||
}
|
||||
|
||||
template<class Function>
|
||||
friend
|
||||
void asio_handler_invoke(Function&& f, read_some_op* op)
|
||||
{
|
||||
using boost::asio::asio_handler_invoke;
|
||||
asio_handler_invoke(f, std::addressof(op->h_));
|
||||
}
|
||||
};
|
||||
|
||||
template<class Stream, class DynamicBuffer>
|
||||
template<class MutableBufferSequence, class Handler>
|
||||
void
|
||||
buffered_read_stream<Stream, DynamicBuffer>::
|
||||
read_some_op<MutableBufferSequence, Handler>::operator()(
|
||||
error_code const& ec, std::size_t bytes_transferred)
|
||||
{
|
||||
switch(step_)
|
||||
{
|
||||
case 0:
|
||||
if(s_.sb_.size() == 0)
|
||||
{
|
||||
if(s_.capacity_ == 0)
|
||||
{
|
||||
// read (unbuffered)
|
||||
step_ = 1;
|
||||
return s_.next_layer_.async_read_some(
|
||||
b_, std::move(*this));
|
||||
}
|
||||
|
||||
// read
|
||||
step_ = 2;
|
||||
return s_.next_layer_.async_read_some(
|
||||
s_.sb_.prepare(s_.capacity_),
|
||||
std::move(*this));
|
||||
|
||||
}
|
||||
step_ = 3;
|
||||
s_.get_io_service().post(
|
||||
bind_handler(std::move(*this), ec, 0));
|
||||
return;
|
||||
|
||||
case 1:
|
||||
// upcall
|
||||
break;
|
||||
|
||||
case 2:
|
||||
s_.sb_.commit(bytes_transferred);
|
||||
BOOST_BEAST_FALLTHROUGH;
|
||||
|
||||
case 3:
|
||||
bytes_transferred =
|
||||
boost::asio::buffer_copy(b_, s_.sb_.data());
|
||||
s_.sb_.consume(bytes_transferred);
|
||||
break;
|
||||
}
|
||||
h_(ec, bytes_transferred);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<class Stream, class DynamicBuffer>
|
||||
template<class... Args>
|
||||
buffered_read_stream<Stream, DynamicBuffer>::
|
||||
buffered_read_stream(Args&&... args)
|
||||
: next_layer_(std::forward<Args>(args)...)
|
||||
{
|
||||
}
|
||||
|
||||
template<class Stream, class DynamicBuffer>
|
||||
template<class ConstBufferSequence, class WriteHandler>
|
||||
auto
|
||||
buffered_read_stream<Stream, DynamicBuffer>::
|
||||
async_write_some(ConstBufferSequence const& buffers,
|
||||
WriteHandler&& handler) ->
|
||||
async_return_type<WriteHandler, void(error_code)>
|
||||
{
|
||||
static_assert(is_async_write_stream<next_layer_type>::value,
|
||||
"AsyncWriteStream requirements not met");
|
||||
static_assert(is_const_buffer_sequence<
|
||||
ConstBufferSequence>::value,
|
||||
"ConstBufferSequence requirements not met");
|
||||
static_assert(is_completion_handler<WriteHandler,
|
||||
void(error_code, std::size_t)>::value,
|
||||
"WriteHandler requirements not met");
|
||||
return next_layer_.async_write_some(buffers,
|
||||
std::forward<WriteHandler>(handler));
|
||||
}
|
||||
|
||||
template<class Stream, class DynamicBuffer>
|
||||
template<class MutableBufferSequence>
|
||||
std::size_t
|
||||
buffered_read_stream<Stream, DynamicBuffer>::
|
||||
read_some(
|
||||
MutableBufferSequence const& buffers)
|
||||
{
|
||||
static_assert(is_sync_read_stream<next_layer_type>::value,
|
||||
"SyncReadStream requirements not met");
|
||||
static_assert(is_mutable_buffer_sequence<
|
||||
MutableBufferSequence>::value,
|
||||
"MutableBufferSequence requirements not met");
|
||||
error_code ec;
|
||||
auto n = read_some(buffers, ec);
|
||||
if(ec)
|
||||
BOOST_THROW_EXCEPTION(system_error{ec});
|
||||
return n;
|
||||
}
|
||||
|
||||
template<class Stream, class DynamicBuffer>
|
||||
template<class MutableBufferSequence>
|
||||
std::size_t
|
||||
buffered_read_stream<Stream, DynamicBuffer>::
|
||||
read_some(MutableBufferSequence const& buffers,
|
||||
error_code& ec)
|
||||
{
|
||||
static_assert(is_sync_read_stream<next_layer_type>::value,
|
||||
"SyncReadStream requirements not met");
|
||||
static_assert(is_mutable_buffer_sequence<
|
||||
MutableBufferSequence>::value,
|
||||
"MutableBufferSequence requirements not met");
|
||||
using boost::asio::buffer_size;
|
||||
using boost::asio::buffer_copy;
|
||||
if(sb_.size() == 0)
|
||||
{
|
||||
if(capacity_ == 0)
|
||||
return next_layer_.read_some(buffers, ec);
|
||||
sb_.commit(next_layer_.read_some(
|
||||
sb_.prepare(capacity_), ec));
|
||||
if(ec)
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
auto bytes_transferred =
|
||||
buffer_copy(buffers, sb_.data());
|
||||
sb_.consume(bytes_transferred);
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
template<class Stream, class DynamicBuffer>
|
||||
template<class MutableBufferSequence, class ReadHandler>
|
||||
auto
|
||||
buffered_read_stream<Stream, DynamicBuffer>::
|
||||
async_read_some(MutableBufferSequence const& buffers,
|
||||
ReadHandler&& handler) ->
|
||||
async_return_type<ReadHandler, void(error_code)>
|
||||
{
|
||||
static_assert(is_async_read_stream<next_layer_type>::value,
|
||||
"Stream requirements not met");
|
||||
static_assert(is_mutable_buffer_sequence<
|
||||
MutableBufferSequence>::value,
|
||||
"MutableBufferSequence requirements not met");
|
||||
async_completion<ReadHandler,
|
||||
void(error_code, std::size_t)> init{handler};
|
||||
read_some_op<MutableBufferSequence, handler_type<
|
||||
ReadHandler, void(error_code, std::size_t)>>{
|
||||
init.completion_handler, *this, buffers}(
|
||||
error_code{}, 0);
|
||||
return init.result.get();
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,509 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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_BUFFERS_ADAPTER_IPP
|
||||
#define BOOST_BEAST_IMPL_BUFFERS_ADAPTER_IPP
|
||||
|
||||
#include <boost/beast/core/detail/type_traits.hpp>
|
||||
#include <boost/asio/buffer.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
class buffers_adapter<MutableBufferSequence>::
|
||||
const_buffers_type
|
||||
{
|
||||
buffers_adapter const* ba_;
|
||||
|
||||
public:
|
||||
using value_type = boost::asio::const_buffer;
|
||||
|
||||
class const_iterator;
|
||||
|
||||
const_buffers_type() = delete;
|
||||
const_buffers_type(
|
||||
const_buffers_type const&) = default;
|
||||
const_buffers_type& operator=(
|
||||
const_buffers_type const&) = default;
|
||||
|
||||
const_iterator
|
||||
begin() const;
|
||||
|
||||
const_iterator
|
||||
end() const;
|
||||
|
||||
private:
|
||||
friend class buffers_adapter;
|
||||
|
||||
const_buffers_type(buffers_adapter const& ba)
|
||||
: ba_(&ba)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
class buffers_adapter<MutableBufferSequence>::
|
||||
const_buffers_type::const_iterator
|
||||
{
|
||||
iter_type it_;
|
||||
buffers_adapter const* ba_ = nullptr;
|
||||
|
||||
public:
|
||||
using value_type = boost::asio::const_buffer;
|
||||
using pointer = value_type const*;
|
||||
using reference = value_type;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using iterator_category =
|
||||
std::bidirectional_iterator_tag;
|
||||
|
||||
const_iterator() = default;
|
||||
const_iterator(const_iterator&& other) = default;
|
||||
const_iterator(const_iterator const& other) = default;
|
||||
const_iterator& operator=(const_iterator&& other) = default;
|
||||
const_iterator& operator=(const_iterator const& other) = default;
|
||||
|
||||
bool
|
||||
operator==(const_iterator const& other) const
|
||||
{
|
||||
return ba_ == other.ba_ &&
|
||||
it_ == other.it_;
|
||||
}
|
||||
|
||||
bool
|
||||
operator!=(const_iterator const& other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
reference
|
||||
operator*() const
|
||||
{
|
||||
using boost::asio::buffer_cast;
|
||||
using boost::asio::buffer_size;
|
||||
return value_type{buffer_cast<void const*>(*it_),
|
||||
(ba_->out_ == ba_->bs_.end() ||
|
||||
it_ != ba_->out_) ? buffer_size(*it_) : ba_->out_pos_} +
|
||||
(it_ == ba_->begin_ ? ba_->in_pos_ : 0);
|
||||
}
|
||||
|
||||
pointer
|
||||
operator->() const = delete;
|
||||
|
||||
const_iterator&
|
||||
operator++()
|
||||
{
|
||||
++it_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
operator++(int)
|
||||
{
|
||||
auto temp = *this;
|
||||
++(*this);
|
||||
return temp;
|
||||
}
|
||||
|
||||
const_iterator&
|
||||
operator--()
|
||||
{
|
||||
--it_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
operator--(int)
|
||||
{
|
||||
auto temp = *this;
|
||||
--(*this);
|
||||
return temp;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class const_buffers_type;
|
||||
|
||||
const_iterator(buffers_adapter const& ba,
|
||||
iter_type iter)
|
||||
: it_(iter)
|
||||
, ba_(&ba)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
inline
|
||||
auto
|
||||
buffers_adapter<MutableBufferSequence>::const_buffers_type::begin() const ->
|
||||
const_iterator
|
||||
{
|
||||
return const_iterator{*ba_, ba_->begin_};
|
||||
}
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
inline
|
||||
auto
|
||||
buffers_adapter<MutableBufferSequence>::const_buffers_type::end() const ->
|
||||
const_iterator
|
||||
{
|
||||
return const_iterator{*ba_, ba_->out_ ==
|
||||
ba_->end_ ? ba_->end_ : std::next(ba_->out_)};
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
class buffers_adapter<MutableBufferSequence>::
|
||||
mutable_buffers_type
|
||||
{
|
||||
buffers_adapter const* ba_;
|
||||
|
||||
public:
|
||||
using value_type = boost::asio::mutable_buffer;
|
||||
|
||||
class const_iterator;
|
||||
|
||||
mutable_buffers_type() = delete;
|
||||
mutable_buffers_type(
|
||||
mutable_buffers_type const&) = default;
|
||||
mutable_buffers_type& operator=(
|
||||
mutable_buffers_type const&) = default;
|
||||
|
||||
const_iterator
|
||||
begin() const;
|
||||
|
||||
const_iterator
|
||||
end() const;
|
||||
|
||||
private:
|
||||
friend class buffers_adapter;
|
||||
|
||||
mutable_buffers_type(
|
||||
buffers_adapter const& ba)
|
||||
: ba_(&ba)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
class buffers_adapter<MutableBufferSequence>::
|
||||
mutable_buffers_type::const_iterator
|
||||
{
|
||||
iter_type it_;
|
||||
buffers_adapter const* ba_ = nullptr;
|
||||
|
||||
public:
|
||||
using value_type = boost::asio::mutable_buffer;
|
||||
using pointer = value_type const*;
|
||||
using reference = value_type;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using iterator_category =
|
||||
std::bidirectional_iterator_tag;
|
||||
|
||||
const_iterator() = default;
|
||||
const_iterator(const_iterator&& other) = default;
|
||||
const_iterator(const_iterator const& other) = default;
|
||||
const_iterator& operator=(const_iterator&& other) = default;
|
||||
const_iterator& operator=(const_iterator const& other) = default;
|
||||
|
||||
bool
|
||||
operator==(const_iterator const& other) const
|
||||
{
|
||||
return ba_ == other.ba_ &&
|
||||
it_ == other.it_;
|
||||
}
|
||||
|
||||
bool
|
||||
operator!=(const_iterator const& other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
reference
|
||||
operator*() const
|
||||
{
|
||||
using boost::asio::buffer_cast;
|
||||
using boost::asio::buffer_size;
|
||||
return value_type{buffer_cast<void*>(*it_),
|
||||
it_ == std::prev(ba_->end_) ?
|
||||
ba_->out_end_ : buffer_size(*it_)} +
|
||||
(it_ == ba_->out_ ? ba_->out_pos_ : 0);
|
||||
}
|
||||
|
||||
pointer
|
||||
operator->() const = delete;
|
||||
|
||||
const_iterator&
|
||||
operator++()
|
||||
{
|
||||
++it_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
operator++(int)
|
||||
{
|
||||
auto temp = *this;
|
||||
++(*this);
|
||||
return temp;
|
||||
}
|
||||
|
||||
const_iterator&
|
||||
operator--()
|
||||
{
|
||||
--it_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
operator--(int)
|
||||
{
|
||||
auto temp = *this;
|
||||
--(*this);
|
||||
return temp;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class mutable_buffers_type;
|
||||
|
||||
const_iterator(buffers_adapter const& ba,
|
||||
iter_type iter)
|
||||
: it_(iter)
|
||||
, ba_(&ba)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
inline
|
||||
auto
|
||||
buffers_adapter<MutableBufferSequence>::mutable_buffers_type::begin() const ->
|
||||
const_iterator
|
||||
{
|
||||
return const_iterator{*ba_, ba_->out_};
|
||||
}
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
inline
|
||||
auto
|
||||
buffers_adapter<MutableBufferSequence>::mutable_buffers_type::end() const ->
|
||||
const_iterator
|
||||
{
|
||||
return const_iterator{*ba_, ba_->end_};
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
buffers_adapter<MutableBufferSequence>::buffers_adapter(
|
||||
buffers_adapter&& other)
|
||||
: buffers_adapter(std::move(other),
|
||||
std::distance<iter_type>(other.bs_.begin(), other.begin_),
|
||||
std::distance<iter_type>(other.bs_.begin(), other.out_),
|
||||
std::distance<iter_type>(other.bs_.begin(), other.end_))
|
||||
{
|
||||
}
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
buffers_adapter<MutableBufferSequence>::buffers_adapter(
|
||||
buffers_adapter const& other)
|
||||
: buffers_adapter(other,
|
||||
std::distance<iter_type>(other.bs_.begin(), other.begin_),
|
||||
std::distance<iter_type>(other.bs_.begin(), other.out_),
|
||||
std::distance<iter_type>(other.bs_.begin(), other.end_))
|
||||
{
|
||||
}
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
auto
|
||||
buffers_adapter<MutableBufferSequence>::operator=(
|
||||
buffers_adapter&& other) -> buffers_adapter&
|
||||
{
|
||||
auto const nbegin = std::distance<iter_type>(
|
||||
other.bs_.begin(), other.begin_);
|
||||
auto const nout = std::distance<iter_type>(
|
||||
other.bs_.begin(), other.out_);
|
||||
auto const nend = std::distance<iter_type>(
|
||||
other.bs_.begin(), other.end_);
|
||||
bs_ = std::move(other.bs_);
|
||||
begin_ = std::next(bs_.begin(), nbegin);
|
||||
out_ = std::next(bs_.begin(), nout);
|
||||
end_ = std::next(bs_.begin(), nend);
|
||||
max_size_ = other.max_size_;
|
||||
in_pos_ = other.in_pos_;
|
||||
in_size_ = other.in_size_;
|
||||
out_pos_ = other.out_pos_;
|
||||
out_end_ = other.out_end_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
auto
|
||||
buffers_adapter<MutableBufferSequence>::operator=(
|
||||
buffers_adapter const& other) -> buffers_adapter&
|
||||
{
|
||||
auto const nbegin = std::distance<iter_type>(
|
||||
other.bs_.begin(), other.begin_);
|
||||
auto const nout = std::distance<iter_type>(
|
||||
other.bs_.begin(), other.out_);
|
||||
auto const nend = std::distance<iter_type>(
|
||||
other.bs_.begin(), other.end_);
|
||||
bs_ = other.bs_;
|
||||
begin_ = std::next(bs_.begin(), nbegin);
|
||||
out_ = std::next(bs_.begin(), nout);
|
||||
end_ = std::next(bs_.begin(), nend);
|
||||
max_size_ = other.max_size_;
|
||||
in_pos_ = other.in_pos_;
|
||||
in_size_ = other.in_size_;
|
||||
out_pos_ = other.out_pos_;
|
||||
out_end_ = other.out_end_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
buffers_adapter<MutableBufferSequence>::buffers_adapter(
|
||||
MutableBufferSequence const& bs)
|
||||
: bs_(bs)
|
||||
, begin_(bs_.begin())
|
||||
, out_(bs_.begin())
|
||||
, end_(bs_.begin())
|
||||
, max_size_(boost::asio::buffer_size(bs_))
|
||||
{
|
||||
}
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
auto
|
||||
buffers_adapter<MutableBufferSequence>::prepare(std::size_t n) ->
|
||||
mutable_buffers_type
|
||||
{
|
||||
using boost::asio::buffer_size;
|
||||
end_ = out_;
|
||||
if(end_ != bs_.end())
|
||||
{
|
||||
auto size = buffer_size(*end_) - out_pos_;
|
||||
if(n > size)
|
||||
{
|
||||
n -= size;
|
||||
while(++end_ != bs_.end())
|
||||
{
|
||||
size = buffer_size(*end_);
|
||||
if(n < size)
|
||||
{
|
||||
out_end_ = n;
|
||||
n = 0;
|
||||
++end_;
|
||||
break;
|
||||
}
|
||||
n -= size;
|
||||
out_end_ = size;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
++end_;
|
||||
out_end_ = out_pos_ + n;
|
||||
n = 0;
|
||||
}
|
||||
}
|
||||
if(n > 0)
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"buffer overflow"});
|
||||
return mutable_buffers_type{*this};
|
||||
}
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
void
|
||||
buffers_adapter<MutableBufferSequence>::commit(std::size_t n)
|
||||
{
|
||||
using boost::asio::buffer_size;
|
||||
if(out_ == end_)
|
||||
return;
|
||||
auto const last = std::prev(end_);
|
||||
while(out_ != last)
|
||||
{
|
||||
auto const avail =
|
||||
buffer_size(*out_) - out_pos_;
|
||||
if(n < avail)
|
||||
{
|
||||
out_pos_ += n;
|
||||
in_size_ += n;
|
||||
max_size_ -= n;
|
||||
return;
|
||||
}
|
||||
++out_;
|
||||
n -= avail;
|
||||
out_pos_ = 0;
|
||||
in_size_ += avail;
|
||||
max_size_ -= avail;
|
||||
}
|
||||
|
||||
n = (std::min)(n, out_end_ - out_pos_);
|
||||
out_pos_ += n;
|
||||
in_size_ += n;
|
||||
max_size_ -= n;
|
||||
if(out_pos_ == buffer_size(*out_))
|
||||
{
|
||||
++out_;
|
||||
out_pos_ = 0;
|
||||
out_end_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
inline
|
||||
auto
|
||||
buffers_adapter<MutableBufferSequence>::data() const ->
|
||||
const_buffers_type
|
||||
{
|
||||
return const_buffers_type{*this};
|
||||
}
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
void
|
||||
buffers_adapter<MutableBufferSequence>::consume(std::size_t n)
|
||||
{
|
||||
using boost::asio::buffer_size;
|
||||
while(begin_ != out_)
|
||||
{
|
||||
auto const avail =
|
||||
buffer_size(*begin_) - in_pos_;
|
||||
if(n < avail)
|
||||
{
|
||||
in_size_ -= n;
|
||||
in_pos_ += n;
|
||||
return;
|
||||
}
|
||||
n -= avail;
|
||||
in_size_ -= avail;
|
||||
in_pos_ = 0;
|
||||
++begin_;
|
||||
}
|
||||
auto const avail = out_pos_ - in_pos_;
|
||||
if(n < avail)
|
||||
{
|
||||
in_size_ -= n;
|
||||
in_pos_ += n;
|
||||
}
|
||||
else
|
||||
{
|
||||
in_size_ -= avail;
|
||||
in_pos_ = out_pos_;
|
||||
}
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,239 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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_CONSUMING_BUFFERS_IPP
|
||||
#define BOOST_BEAST_IMPL_CONSUMING_BUFFERS_IPP
|
||||
|
||||
#include <boost/beast/core/type_traits.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
template<class Buffers>
|
||||
class consuming_buffers<Buffers>::const_iterator
|
||||
{
|
||||
friend class consuming_buffers<Buffers>;
|
||||
|
||||
using iter_type =
|
||||
typename Buffers::const_iterator;
|
||||
|
||||
iter_type it_;
|
||||
consuming_buffers const* b_ = nullptr;
|
||||
|
||||
public:
|
||||
using value_type = typename std::conditional<
|
||||
std::is_convertible<typename
|
||||
std::iterator_traits<iter_type>::value_type,
|
||||
boost::asio::mutable_buffer>::value,
|
||||
boost::asio::mutable_buffer,
|
||||
boost::asio::const_buffer>::type;
|
||||
using pointer = value_type const*;
|
||||
using reference = value_type;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using iterator_category =
|
||||
std::bidirectional_iterator_tag;
|
||||
|
||||
const_iterator() = default;
|
||||
const_iterator(const_iterator&& other) = default;
|
||||
const_iterator(const_iterator const& other) = default;
|
||||
const_iterator& operator=(const_iterator&& other) = default;
|
||||
const_iterator& operator=(const_iterator const& other) = default;
|
||||
|
||||
bool
|
||||
operator==(const_iterator const& other) const
|
||||
{
|
||||
return b_ == other.b_ && it_ == other.it_;
|
||||
}
|
||||
|
||||
bool
|
||||
operator!=(const_iterator const& other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
reference
|
||||
operator*() const
|
||||
{
|
||||
return it_ == b_->begin_
|
||||
? value_type{*it_} + b_->skip_
|
||||
: *it_;
|
||||
}
|
||||
|
||||
pointer
|
||||
operator->() const = delete;
|
||||
|
||||
const_iterator&
|
||||
operator++()
|
||||
{
|
||||
++it_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
operator++(int)
|
||||
{
|
||||
auto temp = *this;
|
||||
++(*this);
|
||||
return temp;
|
||||
}
|
||||
|
||||
const_iterator&
|
||||
operator--()
|
||||
{
|
||||
--it_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
operator--(int)
|
||||
{
|
||||
auto temp = *this;
|
||||
--(*this);
|
||||
return temp;
|
||||
}
|
||||
|
||||
private:
|
||||
const_iterator(consuming_buffers const& b,
|
||||
iter_type it)
|
||||
: it_(it)
|
||||
, b_(&b)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<class Buffers>
|
||||
consuming_buffers<Buffers>::
|
||||
consuming_buffers()
|
||||
: begin_(bs_.begin())
|
||||
{
|
||||
}
|
||||
|
||||
template<class Buffers>
|
||||
consuming_buffers<Buffers>::
|
||||
consuming_buffers(consuming_buffers&& other)
|
||||
: consuming_buffers(std::move(other),
|
||||
std::distance<iter_type>(
|
||||
other.bs_.begin(), other.begin_))
|
||||
{
|
||||
}
|
||||
|
||||
template<class Buffers>
|
||||
consuming_buffers<Buffers>::
|
||||
consuming_buffers(consuming_buffers const& other)
|
||||
: consuming_buffers(other,
|
||||
std::distance<iter_type>(
|
||||
other.bs_.begin(), other.begin_))
|
||||
{
|
||||
}
|
||||
|
||||
template<class Buffers>
|
||||
consuming_buffers<Buffers>::
|
||||
consuming_buffers(Buffers const& bs)
|
||||
: bs_(bs)
|
||||
, begin_(bs_.begin())
|
||||
{
|
||||
static_assert(
|
||||
is_const_buffer_sequence<Buffers>::value||
|
||||
is_mutable_buffer_sequence<Buffers>::value,
|
||||
"BufferSequence requirements not met");
|
||||
}
|
||||
|
||||
template<class Buffers>
|
||||
template<class... Args>
|
||||
consuming_buffers<Buffers>::
|
||||
consuming_buffers(boost::in_place_init_t, Args&&... args)
|
||||
: bs_(std::forward<Args>(args)...)
|
||||
, begin_(bs_.begin())
|
||||
{
|
||||
static_assert(sizeof...(Args) > 0,
|
||||
"Missing constructor arguments");
|
||||
static_assert(
|
||||
std::is_constructible<Buffers, Args...>::value,
|
||||
"Buffers not constructible from arguments");
|
||||
}
|
||||
|
||||
template<class Buffers>
|
||||
auto
|
||||
consuming_buffers<Buffers>::
|
||||
operator=(consuming_buffers&& other) ->
|
||||
consuming_buffers&
|
||||
{
|
||||
auto const dist = std::distance<iter_type>(
|
||||
other.bs_.begin(), other.begin_);
|
||||
bs_ = std::move(other.bs_);
|
||||
begin_ = std::next(bs_.begin(), dist);
|
||||
skip_ = other.skip_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Buffers>
|
||||
auto
|
||||
consuming_buffers<Buffers>::
|
||||
operator=(consuming_buffers const& other) ->
|
||||
consuming_buffers&
|
||||
{
|
||||
auto const dist = std::distance<iter_type>(
|
||||
other.bs_.begin(), other.begin_);
|
||||
bs_ = other.bs_;
|
||||
begin_ = std::next(bs_.begin(), dist);
|
||||
skip_ = other.skip_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Buffers>
|
||||
inline
|
||||
auto
|
||||
consuming_buffers<Buffers>::
|
||||
begin() const ->
|
||||
const_iterator
|
||||
{
|
||||
return const_iterator{*this, begin_};
|
||||
}
|
||||
|
||||
template<class Buffers>
|
||||
inline
|
||||
auto
|
||||
consuming_buffers<Buffers>::
|
||||
end() const ->
|
||||
const_iterator
|
||||
{
|
||||
return const_iterator{*this, bs_.end()};
|
||||
}
|
||||
|
||||
template<class Buffers>
|
||||
void
|
||||
consuming_buffers<Buffers>::
|
||||
consume(std::size_t amount)
|
||||
{
|
||||
using boost::asio::buffer_size;
|
||||
for(;amount > 0 && begin_ != bs_.end(); ++begin_)
|
||||
{
|
||||
auto const len =
|
||||
buffer_size(*begin_) - skip_;
|
||||
if(amount < len)
|
||||
{
|
||||
skip_ += amount;
|
||||
break;
|
||||
}
|
||||
amount -= len;
|
||||
skip_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,335 @@
|
||||
//
|
||||
// Copyright (c) 2015-2016 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_CORE_IMPL_FILE_POSIX_IPP
|
||||
#define BOOST_BEAST_CORE_IMPL_FILE_POSIX_IPP
|
||||
|
||||
#include <limits>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/uio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
namespace detail {
|
||||
|
||||
inline
|
||||
int
|
||||
file_posix_close(int fd)
|
||||
{
|
||||
for(;;)
|
||||
{
|
||||
if(! ::close(fd))
|
||||
break;
|
||||
int const ev = errno;
|
||||
if(errno != EINTR)
|
||||
return ev;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
inline
|
||||
file_posix::
|
||||
~file_posix()
|
||||
{
|
||||
if(fd_ != -1)
|
||||
detail::file_posix_close(fd_);
|
||||
}
|
||||
|
||||
inline
|
||||
file_posix::
|
||||
file_posix(file_posix&& other)
|
||||
: fd_(other.fd_)
|
||||
{
|
||||
other.fd_ = -1;
|
||||
}
|
||||
|
||||
inline
|
||||
file_posix&
|
||||
file_posix::
|
||||
operator=(file_posix&& other)
|
||||
{
|
||||
if(&other == this)
|
||||
return *this;
|
||||
if(fd_ != -1)
|
||||
detail::file_posix_close(fd_);
|
||||
fd_ = other.fd_;
|
||||
other.fd_ = -1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
file_posix::
|
||||
native_handle(native_handle_type fd)
|
||||
{
|
||||
if(fd_ != -1)
|
||||
detail::file_posix_close(fd_);
|
||||
fd_ = fd;
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
file_posix::
|
||||
close(error_code& ec)
|
||||
{
|
||||
if(fd_ != -1)
|
||||
{
|
||||
auto const ev =
|
||||
detail::file_posix_close(fd_);
|
||||
if(ev)
|
||||
ec.assign(ev, generic_category());
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
fd_ = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
file_posix::
|
||||
open(char const* path, file_mode mode, error_code& ec)
|
||||
{
|
||||
if(fd_ != -1)
|
||||
{
|
||||
auto const ev =
|
||||
detail::file_posix_close(fd_);
|
||||
if(ev)
|
||||
ec.assign(ev, generic_category());
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
fd_ = -1;
|
||||
}
|
||||
int f = 0;
|
||||
#ifndef __APPLE__
|
||||
int advise = 0;
|
||||
#endif
|
||||
switch(mode)
|
||||
{
|
||||
default:
|
||||
case file_mode::read:
|
||||
f = O_RDONLY;
|
||||
#ifndef __APPLE__
|
||||
advise = POSIX_FADV_RANDOM;
|
||||
#endif
|
||||
break;
|
||||
case file_mode::scan:
|
||||
f = O_RDONLY;
|
||||
#ifndef __APPLE__
|
||||
advise = POSIX_FADV_SEQUENTIAL;
|
||||
#endif
|
||||
break;
|
||||
|
||||
case file_mode::write:
|
||||
f = O_RDWR | O_CREAT | O_TRUNC;
|
||||
#ifndef __APPLE__
|
||||
advise = POSIX_FADV_RANDOM;
|
||||
#endif
|
||||
break;
|
||||
|
||||
case file_mode::write_new:
|
||||
f = O_RDWR | O_CREAT | O_EXCL;
|
||||
#ifndef __APPLE__
|
||||
advise = POSIX_FADV_RANDOM;
|
||||
#endif
|
||||
break;
|
||||
|
||||
case file_mode::write_existing:
|
||||
f = O_RDWR | O_EXCL;
|
||||
#ifndef __APPLE__
|
||||
advise = POSIX_FADV_RANDOM;
|
||||
#endif
|
||||
break;
|
||||
|
||||
case file_mode::append:
|
||||
f = O_RDWR | O_CREAT | O_TRUNC;
|
||||
#ifndef __APPLE__
|
||||
advise = POSIX_FADV_SEQUENTIAL;
|
||||
#endif
|
||||
break;
|
||||
|
||||
case file_mode::append_new:
|
||||
f = O_RDWR | O_CREAT | O_EXCL;
|
||||
#ifndef __APPLE__
|
||||
advise = POSIX_FADV_SEQUENTIAL;
|
||||
#endif
|
||||
break;
|
||||
|
||||
case file_mode::append_existing:
|
||||
f = O_RDWR | O_EXCL;
|
||||
#ifndef __APPLE__
|
||||
advise = POSIX_FADV_SEQUENTIAL;
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
for(;;)
|
||||
{
|
||||
fd_ = ::open(path, f, 0644);
|
||||
if(fd_ != -1)
|
||||
break;
|
||||
auto const ev = errno;
|
||||
if(ev != EINTR)
|
||||
{
|
||||
ec.assign(ev, generic_category());
|
||||
return;
|
||||
}
|
||||
}
|
||||
#ifndef __APPLE__
|
||||
if(::posix_fadvise(fd_, 0, 0, advise))
|
||||
{
|
||||
auto const ev = errno;
|
||||
detail::file_posix_close(fd_);
|
||||
fd_ = -1;
|
||||
ec.assign(ev, generic_category());
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
inline
|
||||
std::uint64_t
|
||||
file_posix::
|
||||
size(error_code& ec) const
|
||||
{
|
||||
if(fd_ == -1)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return 0;
|
||||
}
|
||||
struct stat st;
|
||||
if(::fstat(fd_, &st) != 0)
|
||||
{
|
||||
ec.assign(errno, generic_category());
|
||||
return 0;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
return st.st_size;
|
||||
}
|
||||
|
||||
inline
|
||||
std::uint64_t
|
||||
file_posix::
|
||||
pos(error_code& ec) const
|
||||
{
|
||||
if(fd_ == -1)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return 0;
|
||||
}
|
||||
auto const result = ::lseek(fd_, 0, SEEK_CUR);
|
||||
if(result == (off_t)-1)
|
||||
{
|
||||
ec.assign(errno, generic_category());
|
||||
return 0;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
return result;
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
file_posix::
|
||||
seek(std::uint64_t offset, error_code& ec)
|
||||
{
|
||||
if(fd_ == -1)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return;
|
||||
}
|
||||
auto const result = ::lseek(fd_, offset, SEEK_SET);
|
||||
if(result == static_cast<off_t>(-1))
|
||||
{
|
||||
ec.assign(errno, generic_category());
|
||||
return;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
inline
|
||||
std::size_t
|
||||
file_posix::
|
||||
read(void* buffer, std::size_t n, error_code& ec) const
|
||||
{
|
||||
if(fd_ == -1)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return 0;
|
||||
}
|
||||
std::size_t nread = 0;
|
||||
while(n > 0)
|
||||
{
|
||||
auto const amount = static_cast<ssize_t>((std::min)(
|
||||
n, static_cast<std::size_t>(SSIZE_MAX)));
|
||||
auto const result = ::read(fd_, buffer, amount);
|
||||
if(result == -1)
|
||||
{
|
||||
auto const ev = errno;
|
||||
if(ev == EINTR)
|
||||
continue;
|
||||
ec.assign(ev, generic_category());
|
||||
return nread;
|
||||
}
|
||||
if(result == 0)
|
||||
{
|
||||
// short read
|
||||
return nread;
|
||||
}
|
||||
n -= result;
|
||||
nread += result;
|
||||
buffer = reinterpret_cast<char*>(buffer) + result;
|
||||
}
|
||||
return nread;
|
||||
}
|
||||
|
||||
inline
|
||||
std::size_t
|
||||
file_posix::
|
||||
write(void const* buffer, std::size_t n, error_code& ec)
|
||||
{
|
||||
if(fd_ == -1)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return 0;
|
||||
}
|
||||
std::size_t nwritten = 0;
|
||||
while(n > 0)
|
||||
{
|
||||
auto const amount = static_cast<ssize_t>((std::min)(
|
||||
n, static_cast<std::size_t>(SSIZE_MAX)));
|
||||
auto const result = ::write(fd_, buffer, amount);
|
||||
if(result == -1)
|
||||
{
|
||||
auto const ev = errno;
|
||||
if(ev == EINTR)
|
||||
continue;
|
||||
ec.assign(ev, generic_category());
|
||||
return nwritten;
|
||||
}
|
||||
n -= result;
|
||||
nwritten += result;
|
||||
buffer = reinterpret_cast<char const*>(buffer) + result;
|
||||
}
|
||||
return nwritten;
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,229 @@
|
||||
//
|
||||
// Copyright (c) 2015-2016 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_CORE_IMPL_FILE_STDIO_IPP
|
||||
#define BOOST_BEAST_CORE_IMPL_FILE_STDIO_IPP
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
inline
|
||||
file_stdio::
|
||||
~file_stdio()
|
||||
{
|
||||
if(f_)
|
||||
fclose(f_);
|
||||
}
|
||||
|
||||
inline
|
||||
file_stdio::
|
||||
file_stdio(file_stdio&& other)
|
||||
: f_(other.f_)
|
||||
{
|
||||
other.f_ = nullptr;
|
||||
}
|
||||
|
||||
inline
|
||||
file_stdio&
|
||||
file_stdio::
|
||||
operator=(file_stdio&& other)
|
||||
{
|
||||
if(&other == this)
|
||||
return *this;
|
||||
if(f_)
|
||||
fclose(f_);
|
||||
f_ = other.f_;
|
||||
other.f_ = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
file_stdio::
|
||||
native_handle(FILE* f)
|
||||
{
|
||||
if(f_)
|
||||
fclose(f_);
|
||||
f_ = f;
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
file_stdio::
|
||||
close(error_code& ec)
|
||||
{
|
||||
if(f_)
|
||||
{
|
||||
int failed = fclose(f_);
|
||||
f_ = nullptr;
|
||||
if(failed)
|
||||
{
|
||||
ec.assign(errno, generic_category());
|
||||
return;
|
||||
}
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
file_stdio::
|
||||
open(char const* path, file_mode mode, error_code& ec)
|
||||
{
|
||||
if(f_)
|
||||
{
|
||||
fclose(f_);
|
||||
f_ = nullptr;
|
||||
}
|
||||
char const* s;
|
||||
switch(mode)
|
||||
{
|
||||
default:
|
||||
case file_mode::read: s = "rb"; break;
|
||||
case file_mode::scan: s = "rb"; break;
|
||||
case file_mode::write: s = "wb"; break;
|
||||
case file_mode::write_new: s = "wbx"; break;
|
||||
case file_mode::write_existing: s = "wb"; break;
|
||||
case file_mode::append: s = "ab"; break;
|
||||
case file_mode::append_new: s = "abx"; break;
|
||||
case file_mode::append_existing: s = "ab"; break;
|
||||
}
|
||||
f_ = std::fopen(path, s);
|
||||
if(! f_)
|
||||
{
|
||||
ec.assign(errno, generic_category());
|
||||
return;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
inline
|
||||
std::uint64_t
|
||||
file_stdio::
|
||||
size(error_code& ec) const
|
||||
{
|
||||
if(! f_)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return 0;
|
||||
}
|
||||
long pos = std::ftell(f_);
|
||||
if(pos == -1L)
|
||||
{
|
||||
ec.assign(errno, generic_category());
|
||||
return 0;
|
||||
}
|
||||
int result = std::fseek(f_, 0, SEEK_END);
|
||||
if(result != 0)
|
||||
{
|
||||
ec.assign(errno, generic_category());
|
||||
return 0;
|
||||
}
|
||||
long size = std::ftell(f_);
|
||||
if(size == -1L)
|
||||
{
|
||||
ec.assign(errno, generic_category());
|
||||
std::fseek(f_, pos, SEEK_SET);
|
||||
return 0;
|
||||
}
|
||||
result = std::fseek(f_, pos, SEEK_SET);
|
||||
if(result != 0)
|
||||
ec.assign(errno, generic_category());
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
return size;
|
||||
}
|
||||
|
||||
inline
|
||||
std::uint64_t
|
||||
file_stdio::
|
||||
pos(error_code& ec) const
|
||||
{
|
||||
if(! f_)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return 0;
|
||||
}
|
||||
long pos = std::ftell(f_);
|
||||
if(pos == -1L)
|
||||
{
|
||||
ec.assign(errno, generic_category());
|
||||
return 0;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
return pos;
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
file_stdio::
|
||||
seek(std::uint64_t offset, error_code& ec)
|
||||
{
|
||||
if(! f_)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return;
|
||||
}
|
||||
if(offset > (std::numeric_limits<long>::max)())
|
||||
{
|
||||
ec = make_error_code(errc::invalid_seek);
|
||||
return;
|
||||
}
|
||||
int result = std::fseek(f_,
|
||||
static_cast<long>(offset), SEEK_SET);
|
||||
if(result != 0)
|
||||
ec.assign(errno, generic_category());
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
inline
|
||||
std::size_t
|
||||
file_stdio::
|
||||
read(void* buffer, std::size_t n, error_code& ec) const
|
||||
{
|
||||
if(! f_)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return 0;
|
||||
}
|
||||
auto nread = std::fread(buffer, 1, n, f_);
|
||||
if(std::ferror(f_))
|
||||
{
|
||||
ec.assign(errno, generic_category());
|
||||
return 0;
|
||||
}
|
||||
return nread;
|
||||
}
|
||||
|
||||
inline
|
||||
std::size_t
|
||||
file_stdio::
|
||||
write(void const* buffer, std::size_t n, error_code& ec)
|
||||
{
|
||||
if(! f_)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return 0;
|
||||
}
|
||||
auto nwritten = std::fwrite(buffer, 1, n, f_);
|
||||
if(std::ferror(f_))
|
||||
{
|
||||
ec.assign(errno, generic_category());
|
||||
return 0;
|
||||
}
|
||||
return nwritten;
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,360 @@
|
||||
//
|
||||
// Copyright (c) 2015-2016 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_CORE_IMPL_FILE_WIN32_IPP
|
||||
#define BOOST_BEAST_CORE_IMPL_FILE_WIN32_IPP
|
||||
|
||||
#include <boost/detail/winapi/access_rights.hpp>
|
||||
#include <boost/detail/winapi/error_codes.hpp>
|
||||
#include <boost/detail/winapi/file_management.hpp>
|
||||
#include <boost/detail/winapi/get_last_error.hpp>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
namespace detail {
|
||||
|
||||
// VFALCO Can't seem to get boost/detail/winapi to work with
|
||||
// this so use the non-Ex version for now.
|
||||
inline
|
||||
boost::detail::winapi::BOOL_
|
||||
set_file_pointer_ex(
|
||||
boost::detail::winapi::HANDLE_ hFile,
|
||||
boost::detail::winapi::LARGE_INTEGER_ lpDistanceToMove,
|
||||
boost::detail::winapi::PLARGE_INTEGER_ lpNewFilePointer,
|
||||
boost::detail::winapi::DWORD_ dwMoveMethod)
|
||||
{
|
||||
auto dwHighPart = lpDistanceToMove.u.HighPart;
|
||||
auto dwLowPart = boost::detail::winapi::SetFilePointer(
|
||||
hFile,
|
||||
lpDistanceToMove.u.LowPart,
|
||||
&dwHighPart,
|
||||
dwMoveMethod);
|
||||
if(dwLowPart == boost::detail::winapi::INVALID_SET_FILE_POINTER_)
|
||||
return 0;
|
||||
if(lpNewFilePointer)
|
||||
{
|
||||
lpNewFilePointer->u.LowPart = dwLowPart;
|
||||
lpNewFilePointer->u.HighPart = dwHighPart;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
inline
|
||||
file_win32::
|
||||
~file_win32()
|
||||
{
|
||||
if(h_ != boost::detail::winapi::INVALID_HANDLE_VALUE_)
|
||||
boost::detail::winapi::CloseHandle(h_);
|
||||
}
|
||||
|
||||
inline
|
||||
file_win32::
|
||||
file_win32(file_win32&& other)
|
||||
: h_(other.h_)
|
||||
{
|
||||
other.h_ = boost::detail::winapi::INVALID_HANDLE_VALUE_;
|
||||
}
|
||||
|
||||
inline
|
||||
file_win32&
|
||||
file_win32::
|
||||
operator=(file_win32&& other)
|
||||
{
|
||||
if(&other == this)
|
||||
return *this;
|
||||
if(h_)
|
||||
boost::detail::winapi::CloseHandle(h_);
|
||||
h_ = other.h_;
|
||||
other.h_ = boost::detail::winapi::INVALID_HANDLE_VALUE_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
file_win32::
|
||||
native_handle(native_handle_type h)
|
||||
{
|
||||
if(h_ != boost::detail::winapi::INVALID_HANDLE_VALUE_)
|
||||
boost::detail::winapi::CloseHandle(h_);
|
||||
h_ = h;
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
file_win32::
|
||||
close(error_code& ec)
|
||||
{
|
||||
if(h_ != boost::detail::winapi::INVALID_HANDLE_VALUE_)
|
||||
{
|
||||
if(! boost::detail::winapi::CloseHandle(h_))
|
||||
ec.assign(boost::detail::winapi::GetLastError(),
|
||||
system_category());
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
h_ = boost::detail::winapi::INVALID_HANDLE_VALUE_;
|
||||
}
|
||||
else
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
file_win32::
|
||||
open(char const* path, file_mode mode, error_code& ec)
|
||||
{
|
||||
if(h_ != boost::detail::winapi::INVALID_HANDLE_VALUE_)
|
||||
{
|
||||
boost::detail::winapi::CloseHandle(h_);
|
||||
h_ = boost::detail::winapi::INVALID_HANDLE_VALUE_;
|
||||
}
|
||||
boost::detail::winapi::DWORD_ dw1 = 0;
|
||||
boost::detail::winapi::DWORD_ dw2 = 0;
|
||||
boost::detail::winapi::DWORD_ dw3 = 0;
|
||||
/*
|
||||
| When the file...
|
||||
This argument: | Exists Does not exist
|
||||
-------------------------+------------------------------------------------------
|
||||
CREATE_ALWAYS | Truncates Creates
|
||||
CREATE_NEW +-----------+ Fails Creates
|
||||
OPEN_ALWAYS ===| does this |===> Opens Creates
|
||||
OPEN_EXISTING +-----------+ Opens Fails
|
||||
TRUNCATE_EXISTING | Truncates Fails
|
||||
*/
|
||||
switch(mode)
|
||||
{
|
||||
default:
|
||||
case file_mode::read:
|
||||
dw1 = boost::detail::winapi::GENERIC_READ_;
|
||||
dw2 = boost::detail::winapi::OPEN_EXISTING_;
|
||||
dw3 = 0x10000000; // FILE_FLAG_RANDOM_ACCESS
|
||||
break;
|
||||
|
||||
case file_mode::scan:
|
||||
dw1 = boost::detail::winapi::GENERIC_READ_;
|
||||
dw2 = boost::detail::winapi::OPEN_EXISTING_;
|
||||
dw3 = 0x08000000; // FILE_FLAG_SEQUENTIAL_SCAN
|
||||
break;
|
||||
|
||||
case file_mode::write:
|
||||
dw1 = boost::detail::winapi::GENERIC_READ_ |
|
||||
boost::detail::winapi::GENERIC_WRITE_;
|
||||
dw2 = boost::detail::winapi::CREATE_ALWAYS_;
|
||||
dw3 = 0x10000000; // FILE_FLAG_RANDOM_ACCESS
|
||||
break;
|
||||
|
||||
case file_mode::write_new:
|
||||
dw1 = boost::detail::winapi::GENERIC_READ_ |
|
||||
boost::detail::winapi::GENERIC_WRITE_;
|
||||
dw2 = boost::detail::winapi::CREATE_NEW_;
|
||||
dw3 = 0x10000000; // FILE_FLAG_RANDOM_ACCESS
|
||||
break;
|
||||
|
||||
case file_mode::write_existing:
|
||||
dw1 = boost::detail::winapi::GENERIC_READ_ |
|
||||
boost::detail::winapi::GENERIC_WRITE_;
|
||||
dw2 = boost::detail::winapi::OPEN_EXISTING_;
|
||||
dw3 = 0x10000000; // FILE_FLAG_RANDOM_ACCESS
|
||||
break;
|
||||
|
||||
case file_mode::append:
|
||||
dw1 = boost::detail::winapi::GENERIC_READ_ |
|
||||
boost::detail::winapi::GENERIC_WRITE_;
|
||||
dw2 = boost::detail::winapi::CREATE_ALWAYS_;
|
||||
dw3 = 0x08000000; // FILE_FLAG_SEQUENTIAL_SCAN
|
||||
break;
|
||||
|
||||
case file_mode::append_new:
|
||||
dw1 = boost::detail::winapi::GENERIC_READ_ |
|
||||
boost::detail::winapi::GENERIC_WRITE_;
|
||||
dw2 = boost::detail::winapi::CREATE_NEW_;
|
||||
dw3 = 0x08000000; // FILE_FLAG_SEQUENTIAL_SCAN
|
||||
break;
|
||||
|
||||
case file_mode::append_existing:
|
||||
dw1 = boost::detail::winapi::GENERIC_READ_ |
|
||||
boost::detail::winapi::GENERIC_WRITE_;
|
||||
dw2 = boost::detail::winapi::OPEN_EXISTING_;
|
||||
dw3 = 0x08000000; // FILE_FLAG_SEQUENTIAL_SCAN
|
||||
break;
|
||||
}
|
||||
h_ = ::CreateFileA(
|
||||
path,
|
||||
dw1,
|
||||
0,
|
||||
NULL,
|
||||
dw2,
|
||||
dw3,
|
||||
NULL);
|
||||
if(h_ == boost::detail::winapi::INVALID_HANDLE_VALUE_)
|
||||
ec.assign(boost::detail::winapi::GetLastError(),
|
||||
system_category());
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
inline
|
||||
std::uint64_t
|
||||
file_win32::
|
||||
size(error_code& ec) const
|
||||
{
|
||||
if(h_ == boost::detail::winapi::INVALID_HANDLE_VALUE_)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return 0;
|
||||
}
|
||||
boost::detail::winapi::LARGE_INTEGER_ fileSize;
|
||||
if(! boost::detail::winapi::GetFileSizeEx(h_, &fileSize))
|
||||
{
|
||||
ec.assign(boost::detail::winapi::GetLastError(),
|
||||
system_category());
|
||||
return 0;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
return fileSize.QuadPart;
|
||||
}
|
||||
|
||||
inline
|
||||
std::uint64_t
|
||||
file_win32::
|
||||
pos(error_code& ec)
|
||||
{
|
||||
if(h_ == boost::detail::winapi::INVALID_HANDLE_VALUE_)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return 0;
|
||||
}
|
||||
boost::detail::winapi::LARGE_INTEGER_ in;
|
||||
boost::detail::winapi::LARGE_INTEGER_ out;
|
||||
in.QuadPart = 0;
|
||||
if(! detail::set_file_pointer_ex(h_, in, &out,
|
||||
boost::detail::winapi::FILE_CURRENT_))
|
||||
{
|
||||
ec.assign(boost::detail::winapi::GetLastError(),
|
||||
system_category());
|
||||
return 0;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
return out.QuadPart;
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
file_win32::
|
||||
seek(std::uint64_t offset, error_code& ec)
|
||||
{
|
||||
if(h_ == boost::detail::winapi::INVALID_HANDLE_VALUE_)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return;
|
||||
}
|
||||
boost::detail::winapi::LARGE_INTEGER_ in;
|
||||
in.QuadPart = offset;
|
||||
if(! detail::set_file_pointer_ex(h_, in, 0,
|
||||
boost::detail::winapi::FILE_BEGIN_))
|
||||
{
|
||||
ec.assign(boost::detail::winapi::GetLastError(),
|
||||
system_category());
|
||||
return;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
inline
|
||||
std::size_t
|
||||
file_win32::
|
||||
read(void* buffer, std::size_t n, error_code& ec)
|
||||
{
|
||||
if(h_ == boost::detail::winapi::INVALID_HANDLE_VALUE_)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return 0;
|
||||
}
|
||||
std::size_t nread = 0;
|
||||
while(n > 0)
|
||||
{
|
||||
boost::detail::winapi::DWORD_ amount;
|
||||
if(n > (std::numeric_limits<
|
||||
boost::detail::winapi::DWORD_>::max)())
|
||||
amount = (std::numeric_limits<
|
||||
boost::detail::winapi::DWORD_>::max)();
|
||||
else
|
||||
amount = static_cast<
|
||||
boost::detail::winapi::DWORD_>(n);
|
||||
boost::detail::winapi::DWORD_ bytesRead;
|
||||
if(! ::ReadFile(h_, buffer, amount, &bytesRead, 0))
|
||||
{
|
||||
auto const dwError = ::GetLastError();
|
||||
if(dwError != boost::detail::winapi::ERROR_HANDLE_EOF_)
|
||||
ec.assign(::GetLastError(), system_category());
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
return nread;
|
||||
}
|
||||
if(bytesRead == 0)
|
||||
return nread;
|
||||
n -= bytesRead;
|
||||
nread += bytesRead;
|
||||
buffer = reinterpret_cast<char*>(buffer) + bytesRead;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
return nread;
|
||||
}
|
||||
|
||||
inline
|
||||
std::size_t
|
||||
file_win32::
|
||||
write(void const* buffer, std::size_t n, error_code& ec)
|
||||
{
|
||||
if(h_ == boost::detail::winapi::INVALID_HANDLE_VALUE_)
|
||||
{
|
||||
ec.assign(errc::invalid_argument, generic_category());
|
||||
return 0;
|
||||
}
|
||||
std::size_t nwritten = 0;
|
||||
while(n > 0)
|
||||
{
|
||||
boost::detail::winapi::DWORD_ amount;
|
||||
if(n > (std::numeric_limits<
|
||||
boost::detail::winapi::DWORD_>::max)())
|
||||
amount = (std::numeric_limits<
|
||||
boost::detail::winapi::DWORD_>::max)();
|
||||
else
|
||||
amount = static_cast<
|
||||
boost::detail::winapi::DWORD_>(n);
|
||||
boost::detail::winapi::DWORD_ bytesWritten;
|
||||
if(! ::WriteFile(h_, buffer, amount, &bytesWritten, 0))
|
||||
{
|
||||
auto const dwError = ::GetLastError();
|
||||
if(dwError != boost::detail::winapi::ERROR_HANDLE_EOF_)
|
||||
ec.assign(::GetLastError(), system_category());
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
return nwritten;
|
||||
}
|
||||
if(bytesWritten == 0)
|
||||
return nwritten;
|
||||
n -= bytesWritten;
|
||||
nwritten += bytesWritten;
|
||||
buffer = reinterpret_cast<char const*>(buffer) + bytesWritten;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
return nwritten;
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,475 @@
|
||||
//
|
||||
// Copyright (c) 2013-2016 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_FLAT_BUFFER_HPP
|
||||
#define BOOST_BEAST_IMPL_FLAT_BUFFER_HPP
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
/* Memory is laid out thusly:
|
||||
|
||||
begin_ ..|.. in_ ..|.. out_ ..|.. last_ ..|.. end_
|
||||
*/
|
||||
|
||||
template<class Allocator>
|
||||
basic_flat_buffer<Allocator>::
|
||||
~basic_flat_buffer()
|
||||
{
|
||||
if(begin_)
|
||||
alloc_traits::deallocate(
|
||||
this->member(), begin_, dist(begin_, end_));
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
basic_flat_buffer<Allocator>::
|
||||
basic_flat_buffer()
|
||||
: begin_(nullptr)
|
||||
, in_(nullptr)
|
||||
, out_(nullptr)
|
||||
, last_(nullptr)
|
||||
, end_(nullptr)
|
||||
, max_((std::numeric_limits<std::size_t>::max)())
|
||||
{
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
basic_flat_buffer<Allocator>::
|
||||
basic_flat_buffer(std::size_t limit)
|
||||
: begin_(nullptr)
|
||||
, in_(nullptr)
|
||||
, out_(nullptr)
|
||||
, last_(nullptr)
|
||||
, end_(nullptr)
|
||||
, max_(limit)
|
||||
{
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
basic_flat_buffer<Allocator>::
|
||||
basic_flat_buffer(Allocator const& alloc)
|
||||
: detail::empty_base_optimization<allocator_type>(alloc)
|
||||
, begin_(nullptr)
|
||||
, in_(nullptr)
|
||||
, out_(nullptr)
|
||||
, last_(nullptr)
|
||||
, end_(nullptr)
|
||||
, max_((std::numeric_limits<std::size_t>::max)())
|
||||
{
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
basic_flat_buffer<Allocator>::
|
||||
basic_flat_buffer(std::size_t limit, Allocator const& alloc)
|
||||
: detail::empty_base_optimization<allocator_type>(alloc)
|
||||
, begin_(nullptr)
|
||||
, in_(nullptr)
|
||||
, out_(nullptr)
|
||||
, last_(nullptr)
|
||||
, end_(nullptr)
|
||||
, max_(limit)
|
||||
{
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
basic_flat_buffer<Allocator>::
|
||||
basic_flat_buffer(basic_flat_buffer&& other)
|
||||
: detail::empty_base_optimization<allocator_type>(
|
||||
std::move(other.member()))
|
||||
, begin_(other.begin_)
|
||||
, in_(other.in_)
|
||||
, out_(other.out_)
|
||||
, last_(out_)
|
||||
, end_(other.end_)
|
||||
, max_(other.max_)
|
||||
{
|
||||
other.begin_ = nullptr;
|
||||
other.in_ = nullptr;
|
||||
other.out_ = nullptr;
|
||||
other.last_ = nullptr;
|
||||
other.end_ = nullptr;
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
basic_flat_buffer<Allocator>::
|
||||
basic_flat_buffer(basic_flat_buffer&& other,
|
||||
Allocator const& alloc)
|
||||
: detail::empty_base_optimization<allocator_type>(alloc)
|
||||
{
|
||||
if(this->member() != other.member())
|
||||
{
|
||||
begin_ = nullptr;
|
||||
in_ = nullptr;
|
||||
out_ = nullptr;
|
||||
last_ = nullptr;
|
||||
end_ = nullptr;
|
||||
max_ = other.max_;
|
||||
copy_from(other);
|
||||
other.reset();
|
||||
}
|
||||
else
|
||||
{
|
||||
begin_ = other.begin_;
|
||||
in_ = other.in_;
|
||||
out_ = other.out_;
|
||||
last_ = out_;
|
||||
end_ = other.end_;
|
||||
max_ = other.max_;
|
||||
other.begin_ = nullptr;
|
||||
other.in_ = nullptr;
|
||||
other.out_ = nullptr;
|
||||
other.last_ = nullptr;
|
||||
other.end_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
basic_flat_buffer<Allocator>::
|
||||
basic_flat_buffer(basic_flat_buffer const& other)
|
||||
: detail::empty_base_optimization<allocator_type>(
|
||||
alloc_traits::select_on_container_copy_construction(
|
||||
other.member()))
|
||||
, begin_(nullptr)
|
||||
, in_(nullptr)
|
||||
, out_(nullptr)
|
||||
, last_(nullptr)
|
||||
, end_(nullptr)
|
||||
, max_(other.max_)
|
||||
{
|
||||
copy_from(other);
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
basic_flat_buffer<Allocator>::
|
||||
basic_flat_buffer(basic_flat_buffer const& other,
|
||||
Allocator const& alloc)
|
||||
: detail::empty_base_optimization<allocator_type>(alloc)
|
||||
, begin_(nullptr)
|
||||
, in_(nullptr)
|
||||
, out_(nullptr)
|
||||
, last_(nullptr)
|
||||
, end_(nullptr)
|
||||
, max_(other.max_)
|
||||
{
|
||||
copy_from(other);
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
template<class OtherAlloc>
|
||||
basic_flat_buffer<Allocator>::
|
||||
basic_flat_buffer(
|
||||
basic_flat_buffer<OtherAlloc> const& other)
|
||||
: begin_(nullptr)
|
||||
, in_(nullptr)
|
||||
, out_(nullptr)
|
||||
, last_(nullptr)
|
||||
, end_(nullptr)
|
||||
, max_(other.max_)
|
||||
{
|
||||
copy_from(other);
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
template<class OtherAlloc>
|
||||
basic_flat_buffer<Allocator>::
|
||||
basic_flat_buffer(basic_flat_buffer<OtherAlloc> const& other,
|
||||
Allocator const& alloc)
|
||||
: detail::empty_base_optimization<allocator_type>(alloc)
|
||||
, begin_(nullptr)
|
||||
, in_(nullptr)
|
||||
, out_(nullptr)
|
||||
, last_(nullptr)
|
||||
, end_(nullptr)
|
||||
, max_(other.max_)
|
||||
{
|
||||
copy_from(other);
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
auto
|
||||
basic_flat_buffer<Allocator>::
|
||||
operator=(basic_flat_buffer&& other) ->
|
||||
basic_flat_buffer&
|
||||
{
|
||||
if(this != &other)
|
||||
move_assign(other,
|
||||
typename alloc_traits::propagate_on_container_move_assignment{});
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
auto
|
||||
basic_flat_buffer<Allocator>::
|
||||
operator=(basic_flat_buffer const& other) ->
|
||||
basic_flat_buffer&
|
||||
{
|
||||
if(this != &other)
|
||||
copy_assign(other,
|
||||
typename alloc_traits::propagate_on_container_copy_assignment{});
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
template<class OtherAlloc>
|
||||
auto
|
||||
basic_flat_buffer<Allocator>::
|
||||
operator=(basic_flat_buffer<OtherAlloc> const& other) ->
|
||||
basic_flat_buffer&
|
||||
{
|
||||
reset();
|
||||
max_ = other.max_;
|
||||
copy_from(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<class Allocator>
|
||||
auto
|
||||
basic_flat_buffer<Allocator>::
|
||||
prepare(std::size_t n) ->
|
||||
mutable_buffers_type
|
||||
{
|
||||
if(n <= dist(out_, end_))
|
||||
{
|
||||
// existing capacity is sufficient
|
||||
last_ = out_ + n;
|
||||
return{out_, n};
|
||||
}
|
||||
auto const len = size();
|
||||
if(n <= capacity() - len)
|
||||
{
|
||||
// after a memmove,
|
||||
// existing capacity is sufficient
|
||||
if(len > 0)
|
||||
std::memmove(begin_, in_, len);
|
||||
in_ = begin_;
|
||||
out_ = in_ + len;
|
||||
last_ = out_ + n;
|
||||
return {out_, n};
|
||||
}
|
||||
// enforce maximum capacity
|
||||
if(n > max_ - len)
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"basic_flat_buffer overflow"});
|
||||
// allocate a new buffer
|
||||
auto const new_size = std::min<std::size_t>(
|
||||
max_,
|
||||
std::max<std::size_t>(2 * len, len + n));
|
||||
auto const p = alloc_traits::allocate(
|
||||
this->member(), new_size);
|
||||
if(begin_)
|
||||
{
|
||||
BOOST_ASSERT(p);
|
||||
BOOST_ASSERT(in_);
|
||||
std::memcpy(p, in_, len);
|
||||
alloc_traits::deallocate(
|
||||
this->member(), begin_, capacity());
|
||||
}
|
||||
begin_ = p;
|
||||
in_ = begin_;
|
||||
out_ = in_ + len;
|
||||
last_ = out_ + n;
|
||||
end_ = begin_ + new_size;
|
||||
return {out_, n};
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
void
|
||||
basic_flat_buffer<Allocator>::
|
||||
consume(std::size_t n)
|
||||
{
|
||||
if(n >= dist(in_, out_))
|
||||
{
|
||||
in_ = begin_;
|
||||
out_ = begin_;
|
||||
return;
|
||||
}
|
||||
in_ += n;
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
void
|
||||
basic_flat_buffer<Allocator>::
|
||||
shrink_to_fit()
|
||||
{
|
||||
auto const len = size();
|
||||
if(len == capacity())
|
||||
return;
|
||||
char* p;
|
||||
if(len > 0)
|
||||
{
|
||||
BOOST_ASSERT(begin_);
|
||||
BOOST_ASSERT(in_);
|
||||
p = alloc_traits::allocate(
|
||||
this->member(), len);
|
||||
std::memcpy(p, in_, len);
|
||||
}
|
||||
else
|
||||
{
|
||||
p = nullptr;
|
||||
}
|
||||
alloc_traits::deallocate(
|
||||
this->member(), begin_, dist(begin_, end_));
|
||||
begin_ = p;
|
||||
in_ = begin_;
|
||||
out_ = begin_ + len;
|
||||
last_ = out_;
|
||||
end_ = out_;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<class Allocator>
|
||||
inline
|
||||
void
|
||||
basic_flat_buffer<Allocator>::
|
||||
reset()
|
||||
{
|
||||
consume(size());
|
||||
shrink_to_fit();
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
template<class DynamicBuffer>
|
||||
inline
|
||||
void
|
||||
basic_flat_buffer<Allocator>::
|
||||
copy_from(DynamicBuffer const& buffer)
|
||||
{
|
||||
if(buffer.size() == 0)
|
||||
return;
|
||||
using boost::asio::buffer_copy;
|
||||
commit(buffer_copy(
|
||||
prepare(buffer.size()), buffer.data()));
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
inline
|
||||
void
|
||||
basic_flat_buffer<Allocator>::
|
||||
move_assign(basic_flat_buffer& other, std::true_type)
|
||||
{
|
||||
reset();
|
||||
this->member() = std::move(other.member());
|
||||
begin_ = other.begin_;
|
||||
in_ = other.in_;
|
||||
out_ = other.out_;
|
||||
last_ = out_;
|
||||
end_ = other.end_;
|
||||
max_ = other.max_;
|
||||
other.begin_ = nullptr;
|
||||
other.in_ = nullptr;
|
||||
other.out_ = nullptr;
|
||||
other.last_ = nullptr;
|
||||
other.end_ = nullptr;
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
inline
|
||||
void
|
||||
basic_flat_buffer<Allocator>::
|
||||
move_assign(basic_flat_buffer& other, std::false_type)
|
||||
{
|
||||
reset();
|
||||
if(this->member() != other.member())
|
||||
{
|
||||
copy_from(other);
|
||||
other.reset();
|
||||
}
|
||||
else
|
||||
{
|
||||
move_assign(other, std::true_type{});
|
||||
}
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
inline
|
||||
void
|
||||
basic_flat_buffer<Allocator>::
|
||||
copy_assign(basic_flat_buffer const& other, std::true_type)
|
||||
{
|
||||
reset();
|
||||
max_ = other.max_;
|
||||
this->member() = other.member();
|
||||
copy_from(other);
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
inline
|
||||
void
|
||||
basic_flat_buffer<Allocator>::
|
||||
copy_assign(basic_flat_buffer const& other, std::false_type)
|
||||
{
|
||||
reset();
|
||||
max_ = other.max_;
|
||||
copy_from(other);
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
inline
|
||||
void
|
||||
basic_flat_buffer<Allocator>::
|
||||
swap(basic_flat_buffer& other)
|
||||
{
|
||||
swap(other, typename
|
||||
alloc_traits::propagate_on_container_swap{});
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
inline
|
||||
void
|
||||
basic_flat_buffer<Allocator>::
|
||||
swap(basic_flat_buffer& other, std::true_type)
|
||||
{
|
||||
using std::swap;
|
||||
swap(this->member(), other.member());
|
||||
swap(max_, other.max_);
|
||||
swap(begin_, other.begin_);
|
||||
swap(in_, other.in_);
|
||||
swap(out_, other.out_);
|
||||
last_ = this->out_;
|
||||
other.last_ = other.out_;
|
||||
swap(end_, other.end_);
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
inline
|
||||
void
|
||||
basic_flat_buffer<Allocator>::
|
||||
swap(basic_flat_buffer& other, std::false_type)
|
||||
{
|
||||
BOOST_ASSERT(this->member() == other.member());
|
||||
using std::swap;
|
||||
swap(max_, other.max_);
|
||||
swap(begin_, other.begin_);
|
||||
swap(in_, other.in_);
|
||||
swap(out_, other.out_);
|
||||
last_ = this->out_;
|
||||
other.last_ = other.out_;
|
||||
swap(end_, other.end_);
|
||||
}
|
||||
|
||||
template<class Allocator>
|
||||
void
|
||||
swap(
|
||||
basic_flat_buffer<Allocator>& lhs,
|
||||
basic_flat_buffer<Allocator>& rhs)
|
||||
{
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,142 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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_FLAT_STATIC_BUFFER_IPP
|
||||
#define BOOST_BEAST_IMPL_FLAT_STATIC_BUFFER_IPP
|
||||
|
||||
#include <boost/beast/core/detail/type_traits.hpp>
|
||||
#include <boost/asio/buffer.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
/* Memory is laid out thusly:
|
||||
|
||||
begin_ ..|.. in_ ..|.. out_ ..|.. last_ ..|.. end_
|
||||
*/
|
||||
|
||||
inline
|
||||
auto
|
||||
flat_static_buffer_base::
|
||||
data() const ->
|
||||
const_buffers_type
|
||||
{
|
||||
return {in_, dist(in_, out_)};
|
||||
}
|
||||
|
||||
inline
|
||||
auto
|
||||
flat_static_buffer_base::
|
||||
mutable_data() ->
|
||||
mutable_data_type
|
||||
{
|
||||
return {in_, dist(in_, out_)};
|
||||
}
|
||||
|
||||
inline
|
||||
auto
|
||||
flat_static_buffer_base::
|
||||
prepare(std::size_t n) ->
|
||||
mutable_buffers_type
|
||||
{
|
||||
return prepare_impl(n);
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
flat_static_buffer_base::
|
||||
reset(void* p, std::size_t n)
|
||||
{
|
||||
reset_impl(p, n);
|
||||
}
|
||||
|
||||
template<class>
|
||||
void
|
||||
flat_static_buffer_base::
|
||||
reset_impl(void* p, std::size_t n)
|
||||
{
|
||||
begin_ =
|
||||
reinterpret_cast<char*>(p);
|
||||
in_ = begin_;
|
||||
out_ = begin_;
|
||||
last_ = begin_;
|
||||
end_ = begin_ + n;
|
||||
}
|
||||
|
||||
template<class>
|
||||
auto
|
||||
flat_static_buffer_base::
|
||||
prepare_impl(std::size_t n) ->
|
||||
mutable_buffers_type
|
||||
{
|
||||
if(n <= dist(out_, end_))
|
||||
{
|
||||
last_ = out_ + n;
|
||||
return {out_, n};
|
||||
}
|
||||
auto const len = size();
|
||||
if(n > capacity() - len)
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"buffer overflow"});
|
||||
if(len > 0)
|
||||
std::memmove(begin_, in_, len);
|
||||
in_ = begin_;
|
||||
out_ = in_ + len;
|
||||
last_ = out_ + n;
|
||||
return {out_, n};
|
||||
}
|
||||
|
||||
template<class>
|
||||
void
|
||||
flat_static_buffer_base::
|
||||
consume_impl(std::size_t n)
|
||||
{
|
||||
if(n >= size())
|
||||
{
|
||||
in_ = begin_;
|
||||
out_ = in_;
|
||||
return;
|
||||
}
|
||||
in_ += n;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<std::size_t N>
|
||||
flat_static_buffer<N>::
|
||||
flat_static_buffer(flat_static_buffer const& other)
|
||||
: flat_static_buffer_base(buf_, N)
|
||||
{
|
||||
using boost::asio::buffer_copy;
|
||||
this->commit(buffer_copy(
|
||||
this->prepare(other.size()), other.data()));
|
||||
}
|
||||
|
||||
template<std::size_t N>
|
||||
auto
|
||||
flat_static_buffer<N>::
|
||||
operator=(flat_static_buffer const& other) ->
|
||||
flat_static_buffer<N>&
|
||||
{
|
||||
using boost::asio::buffer_copy;
|
||||
this->consume(this->size());
|
||||
this->commit(buffer_copy(
|
||||
this->prepare(other.size()), other.data()));
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,137 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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 <boost/asio/handler_alloc_hook.hpp>
|
||||
#include <boost/asio/handler_continuation_hook.hpp>
|
||||
#include <boost/asio/handler_invoke_hook.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
template<class T, class Handler>
|
||||
template<class DeducedHandler, class... Args>
|
||||
inline
|
||||
handler_ptr<T, Handler>::P::
|
||||
P(DeducedHandler&& h, Args&&... args)
|
||||
: n(1)
|
||||
, handler(std::forward<DeducedHandler>(h))
|
||||
{
|
||||
using boost::asio::asio_handler_allocate;
|
||||
t = reinterpret_cast<T*>(
|
||||
asio_handler_allocate(
|
||||
sizeof(T), std::addressof(handler)));
|
||||
try
|
||||
{
|
||||
t = new(t) T{handler,
|
||||
std::forward<Args>(args)...};
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
using boost::asio::asio_handler_deallocate;
|
||||
asio_handler_deallocate(
|
||||
t, sizeof(T), std::addressof(handler));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
template<class T, class Handler>
|
||||
handler_ptr<T, Handler>::
|
||||
~handler_ptr()
|
||||
{
|
||||
if(! p_)
|
||||
return;
|
||||
if(--p_->n)
|
||||
return;
|
||||
if(p_->t)
|
||||
{
|
||||
p_->t->~T();
|
||||
using boost::asio::asio_handler_deallocate;
|
||||
asio_handler_deallocate(
|
||||
p_->t, sizeof(T), std::addressof(p_->handler));
|
||||
}
|
||||
delete p_;
|
||||
}
|
||||
|
||||
template<class T, class Handler>
|
||||
handler_ptr<T, Handler>::
|
||||
handler_ptr(handler_ptr&& other)
|
||||
: p_(other.p_)
|
||||
{
|
||||
other.p_ = nullptr;
|
||||
}
|
||||
|
||||
template<class T, class Handler>
|
||||
handler_ptr<T, Handler>::
|
||||
handler_ptr(handler_ptr const& other)
|
||||
: p_(other.p_)
|
||||
{
|
||||
if(p_)
|
||||
++p_->n;
|
||||
}
|
||||
|
||||
template<class T, class Handler>
|
||||
template<class... Args>
|
||||
handler_ptr<T, Handler>::
|
||||
handler_ptr(Handler&& handler, Args&&... args)
|
||||
: p_(new P{std::move(handler),
|
||||
std::forward<Args>(args)...})
|
||||
{
|
||||
BOOST_STATIC_ASSERT(! std::is_array<T>::value);
|
||||
}
|
||||
|
||||
template<class T, class Handler>
|
||||
template<class... Args>
|
||||
handler_ptr<T, Handler>::
|
||||
handler_ptr(Handler const& handler, Args&&... args)
|
||||
: p_(new P{handler, std::forward<Args>(args)...})
|
||||
{
|
||||
BOOST_STATIC_ASSERT(! std::is_array<T>::value);
|
||||
}
|
||||
|
||||
template<class T, class Handler>
|
||||
auto
|
||||
handler_ptr<T, Handler>::
|
||||
release_handler() ->
|
||||
handler_type
|
||||
{
|
||||
BOOST_ASSERT(p_);
|
||||
BOOST_ASSERT(p_->t);
|
||||
p_->t->~T();
|
||||
using boost::asio::asio_handler_deallocate;
|
||||
asio_handler_deallocate(
|
||||
p_->t, sizeof(T), std::addressof(p_->handler));
|
||||
p_->t = nullptr;
|
||||
return std::move(p_->handler);
|
||||
}
|
||||
|
||||
template<class T, class Handler>
|
||||
template<class... Args>
|
||||
void
|
||||
handler_ptr<T, Handler>::
|
||||
invoke(Args&&... args)
|
||||
{
|
||||
BOOST_ASSERT(p_);
|
||||
BOOST_ASSERT(p_->t);
|
||||
p_->t->~T();
|
||||
using boost::asio::asio_handler_deallocate;
|
||||
asio_handler_deallocate(
|
||||
p_->t, sizeof(T), std::addressof(p_->handler));
|
||||
p_->t = nullptr;
|
||||
p_->handler(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,79 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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_READ_SIZE_IPP
|
||||
#define BOOST_BEAST_IMPL_READ_SIZE_IPP
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class T, class = void>
|
||||
struct has_read_size_helper : std::false_type {};
|
||||
|
||||
template<class T>
|
||||
struct has_read_size_helper<T, decltype(
|
||||
read_size_helper(std::declval<T&>(), 512),
|
||||
(void)0)> : std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
template<class DynamicBuffer>
|
||||
std::size_t
|
||||
read_size(DynamicBuffer& buffer,
|
||||
std::size_t max_size, std::true_type)
|
||||
{
|
||||
return read_size_helper(buffer, max_size);
|
||||
}
|
||||
|
||||
template<class DynamicBuffer>
|
||||
std::size_t
|
||||
read_size(DynamicBuffer& buffer,
|
||||
std::size_t max_size, std::false_type)
|
||||
{
|
||||
static_assert(is_dynamic_buffer<DynamicBuffer>::value,
|
||||
"DynamicBuffer requirements not met");
|
||||
BOOST_ASSERT(max_size >= 1);
|
||||
auto const size = buffer.size();
|
||||
auto const limit = buffer.max_size() - size;
|
||||
BOOST_ASSERT(size <= buffer.max_size());
|
||||
return std::min<std::size_t>(
|
||||
std::max<std::size_t>(512, buffer.capacity() - size),
|
||||
std::min<std::size_t>(max_size, limit));
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
template<class DynamicBuffer>
|
||||
inline
|
||||
std::size_t
|
||||
read_size(
|
||||
DynamicBuffer& buffer, std::size_t max_size)
|
||||
{
|
||||
return detail::read_size(buffer, max_size,
|
||||
detail::has_read_size_helper<DynamicBuffer>{});
|
||||
}
|
||||
|
||||
template<class DynamicBuffer>
|
||||
std::size_t
|
||||
read_size_or_throw(
|
||||
DynamicBuffer& buffer, std::size_t max_size)
|
||||
{
|
||||
auto const n = read_size(buffer, max_size);
|
||||
if(n == 0)
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"buffer overflow"});
|
||||
return n;
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,321 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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_STATIC_BUFFER_IPP
|
||||
#define BOOST_BEAST_IMPL_STATIC_BUFFER_IPP
|
||||
|
||||
#include <boost/beast/core/detail/type_traits.hpp>
|
||||
#include <boost/asio/buffer.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
template<bool isInput, class T>
|
||||
class static_buffer_base::buffers_type
|
||||
{
|
||||
public:
|
||||
using value_type = T;
|
||||
class const_iterator
|
||||
{
|
||||
friend class buffers_type;
|
||||
|
||||
static_buffer_base const* b_ = nullptr;
|
||||
int seg_ = 0;
|
||||
|
||||
const_iterator(bool at_end,
|
||||
static_buffer_base const& b)
|
||||
: b_(&b)
|
||||
{
|
||||
if(! at_end)
|
||||
{
|
||||
seg_ = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
set_end(std::integral_constant<bool, isInput>{});
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
set_end(std::true_type)
|
||||
{
|
||||
if(b_->in_off_ + b_->in_size_ <= b_->capacity_)
|
||||
seg_ = 1;
|
||||
else
|
||||
seg_ = 2;
|
||||
}
|
||||
|
||||
void
|
||||
set_end(std::false_type)
|
||||
{
|
||||
if(((b_->in_off_ + b_->in_size_) % b_->capacity_)
|
||||
+ b_->out_size_ <= b_->capacity_)
|
||||
seg_ = 1;
|
||||
else
|
||||
seg_ = 2;
|
||||
}
|
||||
|
||||
T
|
||||
dereference(std::true_type) const
|
||||
{
|
||||
switch(seg_)
|
||||
{
|
||||
case 0:
|
||||
return {
|
||||
b_->begin_ + b_->in_off_,
|
||||
(std::min)(
|
||||
b_->in_size_,
|
||||
b_->capacity_ - b_->in_off_)};
|
||||
default:
|
||||
case 1:
|
||||
return {
|
||||
b_->begin_,
|
||||
b_->in_size_ - (
|
||||
b_->capacity_ - b_->in_off_)};
|
||||
}
|
||||
}
|
||||
|
||||
T
|
||||
dereference(std::false_type) const
|
||||
{
|
||||
switch(seg_)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
auto const out_off =
|
||||
(b_->in_off_ + b_->in_size_)
|
||||
% b_->capacity_;
|
||||
return {
|
||||
b_->begin_ + out_off,
|
||||
(std::min)(
|
||||
b_->out_size_,
|
||||
b_->capacity_ - out_off)};
|
||||
}
|
||||
default:
|
||||
case 1:
|
||||
{
|
||||
auto const out_off =
|
||||
(b_->in_off_ + b_->in_size_)
|
||||
% b_->capacity_;
|
||||
return {
|
||||
b_->begin_,
|
||||
b_->out_size_ - (
|
||||
b_->capacity_ - out_off)};
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
using value_type = T;
|
||||
using pointer = value_type const*;
|
||||
using reference = value_type const&;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using iterator_category =
|
||||
std::bidirectional_iterator_tag;
|
||||
|
||||
const_iterator() = default;
|
||||
const_iterator(const_iterator const& other) = default;
|
||||
const_iterator& operator=(const_iterator const& other) = default;
|
||||
|
||||
bool
|
||||
operator==(const_iterator const& other) const
|
||||
{
|
||||
return b_ == other.b_ && seg_ == other.seg_;
|
||||
}
|
||||
|
||||
bool
|
||||
operator!=(const_iterator const& other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
value_type
|
||||
operator*() const
|
||||
{
|
||||
return dereference(
|
||||
std::integral_constant<bool, isInput>{});
|
||||
}
|
||||
|
||||
pointer
|
||||
operator->() = delete;
|
||||
|
||||
const_iterator&
|
||||
operator++()
|
||||
{
|
||||
++seg_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
operator++(int)
|
||||
{
|
||||
auto temp = *this;
|
||||
++(*this);
|
||||
return temp;
|
||||
}
|
||||
|
||||
const_iterator&
|
||||
operator--()
|
||||
{
|
||||
--seg_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
operator--(int)
|
||||
{
|
||||
auto temp = *this;
|
||||
--(*this);
|
||||
return temp;
|
||||
}
|
||||
};
|
||||
|
||||
buffers_type() = delete;
|
||||
buffers_type(buffers_type const&) = default;
|
||||
buffers_type& operator=(buffers_type const&) = delete;
|
||||
|
||||
const_iterator
|
||||
begin() const
|
||||
{
|
||||
return const_iterator{false, b_};
|
||||
}
|
||||
|
||||
const_iterator
|
||||
end() const
|
||||
{
|
||||
return const_iterator{true, b_};
|
||||
}
|
||||
|
||||
private:
|
||||
friend class static_buffer_base;
|
||||
|
||||
static_buffer_base const& b_;
|
||||
|
||||
explicit
|
||||
buffers_type(static_buffer_base const& b)
|
||||
: b_(b)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
inline
|
||||
static_buffer_base::
|
||||
static_buffer_base(void* p, std::size_t size)
|
||||
: begin_(reinterpret_cast<char*>(p))
|
||||
, capacity_(size)
|
||||
{
|
||||
}
|
||||
|
||||
inline
|
||||
auto
|
||||
static_buffer_base::
|
||||
data() const ->
|
||||
const_buffers_type
|
||||
{
|
||||
return const_buffers_type{*this};
|
||||
}
|
||||
|
||||
inline
|
||||
auto
|
||||
static_buffer_base::
|
||||
mutable_data() ->
|
||||
mutable_data_type
|
||||
{
|
||||
return mutable_data_type{*this};
|
||||
}
|
||||
|
||||
inline
|
||||
auto
|
||||
static_buffer_base::
|
||||
prepare(std::size_t size) ->
|
||||
mutable_buffers_type
|
||||
{
|
||||
if(size > capacity_ - in_size_)
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"buffer overflow"});
|
||||
out_size_ = size;
|
||||
return mutable_buffers_type{*this};
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
static_buffer_base::
|
||||
commit(std::size_t size)
|
||||
{
|
||||
in_size_ += (std::min)(size, out_size_);
|
||||
out_size_ = 0;
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
static_buffer_base::
|
||||
consume(std::size_t size)
|
||||
{
|
||||
if(size < in_size_)
|
||||
{
|
||||
in_off_ = (in_off_ + size) % capacity_;
|
||||
in_size_ -= size;
|
||||
}
|
||||
else
|
||||
{
|
||||
// rewind the offset, so the next call to prepare
|
||||
// can have a longer continguous segment. this helps
|
||||
// algorithms optimized for larger buffesr.
|
||||
in_off_ = 0;
|
||||
in_size_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
static_buffer_base::
|
||||
reset(void* p, std::size_t size)
|
||||
{
|
||||
begin_ = reinterpret_cast<char*>(p);
|
||||
capacity_ = size;
|
||||
in_off_ = 0;
|
||||
in_size_ = 0;
|
||||
out_size_ = 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<std::size_t N>
|
||||
static_buffer<N>::
|
||||
static_buffer(static_buffer const& other)
|
||||
: static_buffer_base(buf_, N)
|
||||
{
|
||||
using boost::asio::buffer_copy;
|
||||
this->commit(buffer_copy(
|
||||
this->prepare(other.size()), other.data()));
|
||||
}
|
||||
|
||||
template<std::size_t N>
|
||||
auto
|
||||
static_buffer<N>::
|
||||
operator=(static_buffer const& other) ->
|
||||
static_buffer<N>&
|
||||
{
|
||||
using boost::asio::buffer_copy;
|
||||
this->consume(this->size());
|
||||
this->commit(buffer_copy(
|
||||
this->prepare(other.size()), other.data()));
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,618 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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_STATIC_STRING_IPP
|
||||
#define BOOST_BEAST_IMPL_STATIC_STRING_IPP
|
||||
|
||||
#include <boost/beast/core/detail/static_string.hpp>
|
||||
#include <boost/beast/core/detail/type_traits.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
//
|
||||
// (constructor)
|
||||
//
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
static_string<N, CharT, Traits>::
|
||||
static_string()
|
||||
{
|
||||
n_ = 0;
|
||||
term();
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
static_string<N, CharT, Traits>::
|
||||
static_string(size_type count, CharT ch)
|
||||
{
|
||||
assign(count, ch);
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<std::size_t M>
|
||||
static_string<N, CharT, Traits>::
|
||||
static_string(static_string<M, CharT, Traits> const& other,
|
||||
size_type pos)
|
||||
{
|
||||
assign(other, pos);
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<std::size_t M>
|
||||
static_string<N, CharT, Traits>::
|
||||
static_string(static_string<M, CharT, Traits> const& other,
|
||||
size_type pos, size_type count)
|
||||
{
|
||||
assign(other, pos, count);
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
static_string<N, CharT, Traits>::
|
||||
static_string(CharT const* s, size_type count)
|
||||
{
|
||||
assign(s, count);
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
static_string<N, CharT, Traits>::
|
||||
static_string(CharT const* s)
|
||||
{
|
||||
assign(s);
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<class InputIt>
|
||||
static_string<N, CharT, Traits>::
|
||||
static_string(InputIt first, InputIt last)
|
||||
{
|
||||
assign(first, last);
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
static_string<N, CharT, Traits>::
|
||||
static_string(static_string const& s)
|
||||
{
|
||||
assign(s);
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<std::size_t M>
|
||||
static_string<N, CharT, Traits>::
|
||||
static_string(static_string<M, CharT, Traits> const& s)
|
||||
{
|
||||
assign(s);
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
static_string<N, CharT, Traits>::
|
||||
static_string(std::initializer_list<CharT> init)
|
||||
{
|
||||
assign(init.begin(), init.end());
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
static_string<N, CharT, Traits>::
|
||||
static_string(string_view_type sv)
|
||||
{
|
||||
assign(sv);
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<class T, class>
|
||||
static_string<N, CharT, Traits>::
|
||||
static_string(T const& t, size_type pos, size_type n)
|
||||
{
|
||||
assign(t, pos, n);
|
||||
}
|
||||
|
||||
//
|
||||
// (assignment)
|
||||
//
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
assign(size_type count, CharT ch) ->
|
||||
static_string&
|
||||
{
|
||||
if(count > max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"count > max_size()"});
|
||||
n_ = count;
|
||||
Traits::assign(&s_[0], n_, ch);
|
||||
term();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
assign(static_string const& str) ->
|
||||
static_string&
|
||||
{
|
||||
n_ = str.n_;
|
||||
Traits::copy(&s_[0], &str.s_[0], n_ + 1);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<std::size_t M>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
assign(static_string<M, CharT, Traits> const& str,
|
||||
size_type pos, size_type count) ->
|
||||
static_string&
|
||||
{
|
||||
auto const ss = str.substr(pos, count);
|
||||
return assign(ss.data(), ss.size());
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
assign(CharT const* s, size_type count) ->
|
||||
static_string&
|
||||
{
|
||||
if(count > max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"count > max_size()"});
|
||||
n_ = count;
|
||||
Traits::copy(&s_[0], s, n_);
|
||||
term();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<class InputIt>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
assign(InputIt first, InputIt last) ->
|
||||
static_string&
|
||||
{
|
||||
std::size_t const n = std::distance(first, last);
|
||||
if(n > max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"n > max_size()"});
|
||||
n_ = n;
|
||||
for(auto it = &s_[0]; first != last; ++it, ++first)
|
||||
Traits::assign(*it, *first);
|
||||
term();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<class T>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
assign(T const& t, size_type pos, size_type count) ->
|
||||
typename std::enable_if<std::is_convertible<T,
|
||||
string_view_type>::value, static_string&>::type
|
||||
{
|
||||
auto const sv = string_view_type(t).substr(pos, count);
|
||||
if(sv.size() > max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"sv.size() > max_size()"});
|
||||
n_ = sv.size();
|
||||
Traits::copy(&s_[0], &sv[0], n_);
|
||||
term();
|
||||
return *this;
|
||||
}
|
||||
|
||||
//
|
||||
// Element access
|
||||
//
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
at(size_type pos) ->
|
||||
reference
|
||||
{
|
||||
if(pos >= size())
|
||||
BOOST_THROW_EXCEPTION(std::out_of_range{
|
||||
"pos >= size()"});
|
||||
return s_[pos];
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
at(size_type pos) const ->
|
||||
const_reference
|
||||
{
|
||||
if(pos >= size())
|
||||
BOOST_THROW_EXCEPTION(std::out_of_range{
|
||||
"pos >= size()"});
|
||||
return s_[pos];
|
||||
}
|
||||
|
||||
//
|
||||
// Capacity
|
||||
//
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
void
|
||||
static_string<N, CharT, Traits>::
|
||||
reserve(std::size_t n)
|
||||
{
|
||||
if(n > max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"n > max_size()"});
|
||||
}
|
||||
|
||||
//
|
||||
// Operations
|
||||
//
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
void
|
||||
static_string<N, CharT, Traits>::
|
||||
clear()
|
||||
{
|
||||
n_ = 0;
|
||||
term();
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
insert(size_type index, size_type count, CharT ch) ->
|
||||
static_string&
|
||||
{
|
||||
if(index > size())
|
||||
BOOST_THROW_EXCEPTION(std::out_of_range{
|
||||
"index > size()"});
|
||||
insert(begin() + index, count, ch);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
insert(size_type index, CharT const* s, size_type count) ->
|
||||
static_string&
|
||||
{
|
||||
if(index > size())
|
||||
BOOST_THROW_EXCEPTION(std::out_of_range{
|
||||
"index > size()"});
|
||||
if(size() + count > max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"size() + count > max_size()"});
|
||||
Traits::move(
|
||||
&s_[index + count], &s_[index], size() - index);
|
||||
n_ += count;
|
||||
Traits::copy(&s_[index], s, count);
|
||||
term();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<std::size_t M>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
insert(size_type index,
|
||||
static_string<M, CharT, Traits> const& str,
|
||||
size_type index_str, size_type count) ->
|
||||
static_string&
|
||||
{
|
||||
auto const ss = str.substr(index_str, count);
|
||||
return insert(index, ss.data(), ss.size());
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
insert(const_iterator pos, size_type count, CharT ch) ->
|
||||
iterator
|
||||
{
|
||||
if(size() + count > max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"size() + count() > max_size()"});
|
||||
auto const index = pos - &s_[0];
|
||||
Traits::move(
|
||||
&s_[index + count], &s_[index], size() - index);
|
||||
n_ += count;
|
||||
Traits::assign(&s_[index], count, ch);
|
||||
term();
|
||||
return &s_[index];
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<class InputIt>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
insert(const_iterator pos, InputIt first, InputIt last) ->
|
||||
typename std::enable_if<
|
||||
detail::is_input_iterator<InputIt>::value,
|
||||
iterator>::type
|
||||
{
|
||||
std::size_t const count = std::distance(first, last);
|
||||
if(size() + count > max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"size() + count > max_size()"});
|
||||
std::size_t const index = pos - begin();
|
||||
Traits::move(
|
||||
&s_[index + count], &s_[index], size() - index);
|
||||
n_ += count;
|
||||
for(auto it = begin() + index;
|
||||
first != last; ++it, ++first)
|
||||
Traits::assign(*it, *first);
|
||||
term();
|
||||
return begin() + index;
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<class T>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
insert(size_type index, const T& t,
|
||||
size_type index_str, size_type count) ->
|
||||
typename std::enable_if<std::is_convertible<
|
||||
T const&, string_view_type>::value &&
|
||||
! std::is_convertible<T const&, CharT const*>::value,
|
||||
static_string&>::type
|
||||
{
|
||||
auto const str =
|
||||
string_view_type(t).substr(index_str, count);
|
||||
return insert(index, str.data(), str.size());
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
erase(size_type index, size_type count) ->
|
||||
static_string&
|
||||
{
|
||||
if(index > size())
|
||||
BOOST_THROW_EXCEPTION(std::out_of_range{
|
||||
"index > size()"});
|
||||
auto const n = (std::min)(count, size() - index);
|
||||
Traits::move(
|
||||
&s_[index], &s_[index + n], size() - (index + n) + 1);
|
||||
n_ -= n;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
erase(const_iterator pos) ->
|
||||
iterator
|
||||
{
|
||||
erase(pos - begin(), 1);
|
||||
return begin() + (pos - begin());
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
erase(const_iterator first, const_iterator last) ->
|
||||
iterator
|
||||
{
|
||||
erase(first - begin(),
|
||||
std::distance(first, last));
|
||||
return begin() + (first - begin());
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
void
|
||||
static_string<N, CharT, Traits>::
|
||||
push_back(CharT ch)
|
||||
{
|
||||
if(size() >= max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"size() >= max_size()"});
|
||||
Traits::assign(s_[n_++], ch);
|
||||
term();
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<std::size_t M>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
append(static_string<M, CharT, Traits> const& str,
|
||||
size_type pos, size_type count) ->
|
||||
static_string&
|
||||
{
|
||||
// Valid range is [0, size)
|
||||
if(pos >= str.size())
|
||||
BOOST_THROW_EXCEPTION(std::out_of_range{
|
||||
"pos > str.size()"});
|
||||
string_view_type const ss{&str.s_[pos],
|
||||
(std::min)(count, str.size() - pos)};
|
||||
insert(size(), ss.data(), ss.size());
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
substr(size_type pos, size_type count) const ->
|
||||
string_view_type
|
||||
{
|
||||
if(pos > size())
|
||||
BOOST_THROW_EXCEPTION(std::out_of_range{
|
||||
"pos > size()"});
|
||||
return{&s_[pos], (std::min)(count, size() - pos)};
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
copy(CharT* dest, size_type count, size_type pos) const ->
|
||||
size_type
|
||||
{
|
||||
auto const str = substr(pos, count);
|
||||
Traits::copy(dest, str.data(), str.size());
|
||||
return str.size();
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
void
|
||||
static_string<N, CharT, Traits>::
|
||||
resize(std::size_t n)
|
||||
{
|
||||
if(n > max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"n > max_size()"});
|
||||
n_ = n;
|
||||
term();
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
void
|
||||
static_string<N, CharT, Traits>::
|
||||
resize(std::size_t n, CharT c)
|
||||
{
|
||||
if(n > max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"n > max_size()"});
|
||||
if(n > n_)
|
||||
Traits::assign(&s_[n_], n - n_, c);
|
||||
n_ = n;
|
||||
term();
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
void
|
||||
static_string<N, CharT, Traits>::
|
||||
swap(static_string& str)
|
||||
{
|
||||
static_string tmp(str);
|
||||
str.n_ = n_;
|
||||
Traits::copy(&str.s_[0], &s_[0], n_ + 1);
|
||||
n_ = tmp.n_;
|
||||
Traits::copy(&s_[0], &tmp.s_[0], n_ + 1);
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
template<std::size_t M>
|
||||
void
|
||||
static_string<N, CharT, Traits>::
|
||||
swap(static_string<M, CharT, Traits>& str)
|
||||
{
|
||||
if(size() > str.max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"size() > str.max_size()"});
|
||||
if(str.size() > max_size())
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"str.size() > max_size()"});
|
||||
static_string tmp(str);
|
||||
str.n_ = n_;
|
||||
Traits::copy(&str.s_[0], &s_[0], n_ + 1);
|
||||
n_ = tmp.n_;
|
||||
Traits::copy(&s_[0], &tmp.s_[0], n_ + 1);
|
||||
}
|
||||
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
assign_char(CharT ch, std::true_type) ->
|
||||
static_string&
|
||||
{
|
||||
n_ = 1;
|
||||
Traits::assign(s_[0], ch);
|
||||
term();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t N, class CharT, class Traits>
|
||||
auto
|
||||
static_string<N, CharT, Traits>::
|
||||
assign_char(CharT, std::false_type) ->
|
||||
static_string&
|
||||
{
|
||||
BOOST_THROW_EXCEPTION(std::length_error{
|
||||
"max_size() == 0"});
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class Integer>
|
||||
static_string<max_digits(sizeof(Integer))>
|
||||
to_static_string(Integer x, std::true_type)
|
||||
{
|
||||
if(x == 0)
|
||||
return {'0'};
|
||||
static_string<detail::max_digits(
|
||||
sizeof(Integer))> s;
|
||||
if(x < 0)
|
||||
{
|
||||
x = -x;
|
||||
char buf[max_digits(sizeof(x))];
|
||||
char* p = buf;
|
||||
for(;x > 0; x /= 10)
|
||||
*p++ = "0123456789"[x % 10];
|
||||
s.resize(1 + p - buf);
|
||||
s[0] = '-';
|
||||
auto d = &s[1];
|
||||
while(p > buf)
|
||||
*d++ = *--p;
|
||||
}
|
||||
else
|
||||
{
|
||||
char buf[max_digits(sizeof(x))];
|
||||
char* p = buf;
|
||||
for(;x > 0; x /= 10)
|
||||
*p++ = "0123456789"[x % 10];
|
||||
s.resize(p - buf);
|
||||
auto d = &s[0];
|
||||
while(p > buf)
|
||||
*d++ = *--p;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
template<class Integer>
|
||||
static_string<max_digits(sizeof(Integer))>
|
||||
to_static_string(Integer x, std::false_type)
|
||||
{
|
||||
if(x == 0)
|
||||
return {'0'};
|
||||
char buf[max_digits(sizeof(x))];
|
||||
char* p = buf;
|
||||
for(;x > 0; x /= 10)
|
||||
*p++ = "0123456789"[x % 10];
|
||||
static_string<detail::max_digits(
|
||||
sizeof(Integer))> s;
|
||||
s.resize(p - buf);
|
||||
auto d = &s[0];
|
||||
while(p > buf)
|
||||
*d++ = *--p;
|
||||
return s;
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
template<class Integer>
|
||||
static_string<detail::max_digits(sizeof(Integer))>
|
||||
to_static_string(Integer x)
|
||||
{
|
||||
using CharT = char;
|
||||
using Traits = std::char_traits<CharT>;
|
||||
BOOST_STATIC_ASSERT(std::is_integral<Integer>::value);
|
||||
char buf[detail::max_digits(sizeof(Integer))];
|
||||
auto last = buf + sizeof(buf);
|
||||
auto it = detail::raw_to_string<
|
||||
CharT, Integer, Traits>(last, sizeof(buf), x);
|
||||
static_string<detail::max_digits(sizeof(Integer))> s;
|
||||
s.resize(static_cast<std::size_t>(last - it));
|
||||
auto p = s.data();
|
||||
while(it < last)
|
||||
Traits::assign(*p++, *it++);
|
||||
return s;
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,108 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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_STRING_PARAM_IPP
|
||||
#define BOOST_BEAST_IMPL_STRING_PARAM_IPP
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<
|
||||
std::is_integral<T>::value>::type
|
||||
string_param::
|
||||
print(T const& t)
|
||||
{
|
||||
auto const last = buf_ + sizeof(buf_);
|
||||
auto const it = detail::raw_to_string<
|
||||
char, T, std::char_traits<char>>(
|
||||
last, sizeof(buf_), t);
|
||||
sv_ = {it, static_cast<std::size_t>(
|
||||
last - it)};
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<
|
||||
! std::is_integral<T>::value &&
|
||||
! std::is_convertible<T, string_view>::value
|
||||
>::type
|
||||
string_param::
|
||||
print(T const& t)
|
||||
{
|
||||
os_.emplace(buf_, sizeof(buf_));
|
||||
*os_ << t;
|
||||
os_->flush();
|
||||
sv_ = os_->str();
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
string_param::
|
||||
print(string_view sv)
|
||||
{
|
||||
sv_ = sv;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<
|
||||
std::is_integral<T>::value>::type
|
||||
string_param::
|
||||
print_1(T const& t)
|
||||
{
|
||||
char buf[detail::max_digits(sizeof(T))];
|
||||
auto const last = buf + sizeof(buf);
|
||||
auto const it = detail::raw_to_string<
|
||||
char, T, std::char_traits<char>>(
|
||||
last, sizeof(buf), t);
|
||||
*os_ << string_view{it,
|
||||
static_cast<std::size_t>(last - it)};
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename std::enable_if<
|
||||
! std::is_integral<T>::value>::type
|
||||
string_param::
|
||||
print_1(T const& t)
|
||||
{
|
||||
*os_ << t;
|
||||
}
|
||||
|
||||
template<class T0, class... TN>
|
||||
void
|
||||
string_param::
|
||||
print_n(T0 const& t0, TN const&... tn)
|
||||
{
|
||||
print_1(t0);
|
||||
print_n(tn...);
|
||||
}
|
||||
|
||||
template<class T0, class T1, class... TN>
|
||||
void
|
||||
string_param::
|
||||
print(T0 const& t0, T1 const& t1, TN const&... tn)
|
||||
{
|
||||
os_.emplace(buf_, sizeof(buf_));
|
||||
print_1(t0);
|
||||
print_1(t1);
|
||||
print_n(tn...);
|
||||
os_->flush();
|
||||
sv_ = os_->str();
|
||||
}
|
||||
|
||||
template<class... Args>
|
||||
string_param::
|
||||
string_param(Args const&... args)
|
||||
{
|
||||
print(args...);
|
||||
}
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user