mirror of
https://github.com/matrix-construct/construct
synced 2024-11-16 15:00:51 +01:00
ircd::buffer: Add the stream_buffer.
This commit is contained in:
parent
3d5eea0ce3
commit
7d863091bb
1 changed files with 47 additions and 1 deletions
|
@ -57,6 +57,7 @@ namespace ircd::buffer
|
||||||
struct mutable_raw_buffer;
|
struct mutable_raw_buffer;
|
||||||
template<class buffer, size_t SIZE> struct fixed_buffer;
|
template<class buffer, size_t SIZE> struct fixed_buffer;
|
||||||
template<class buffer, uint align = 16> struct unique_buffer;
|
template<class buffer, uint align = 16> struct unique_buffer;
|
||||||
|
struct stream_buffer;
|
||||||
|
|
||||||
template<size_t SIZE> using fixed_const_buffer = fixed_buffer<const_buffer, SIZE>;
|
template<size_t SIZE> using fixed_const_buffer = fixed_buffer<const_buffer, SIZE>;
|
||||||
template<size_t SIZE> using fixed_mutable_buffer = fixed_buffer<mutable_buffer, SIZE>;
|
template<size_t SIZE> using fixed_mutable_buffer = fixed_buffer<mutable_buffer, SIZE>;
|
||||||
|
@ -100,7 +101,7 @@ namespace ircd::buffer
|
||||||
template<template<class> class I, class T> size_t copy(const mutable_raw_buffer &, const I<T> &buffer);
|
template<template<class> class I, class T> size_t copy(const mutable_raw_buffer &, const I<T> &buffer);
|
||||||
template<template<class> class I, class T> size_t consume(I<T> &buffers, const size_t &bytes);
|
template<template<class> class I, class T> size_t consume(I<T> &buffers, const size_t &bytes);
|
||||||
|
|
||||||
// Convenience copy to stream
|
// Convenience copy to std stream
|
||||||
template<class it> std::ostream &operator<<(std::ostream &s, const buffer<it> &buffer);
|
template<class it> std::ostream &operator<<(std::ostream &s, const buffer<it> &buffer);
|
||||||
template<template<class> class I, class T> std::ostream &operator<<(std::ostream &s, const I<T> &buffers);
|
template<template<class> class I, class T> std::ostream &operator<<(std::ostream &s, const I<T> &buffers);
|
||||||
}
|
}
|
||||||
|
@ -115,6 +116,7 @@ namespace ircd
|
||||||
using buffer::fixed_buffer;
|
using buffer::fixed_buffer;
|
||||||
using buffer::unique_buffer;
|
using buffer::unique_buffer;
|
||||||
using buffer::null_buffer;
|
using buffer::null_buffer;
|
||||||
|
using buffer::stream_buffer;
|
||||||
using buffer::fixed_const_buffer;
|
using buffer::fixed_const_buffer;
|
||||||
using buffer::fixed_mutable_buffer;
|
using buffer::fixed_mutable_buffer;
|
||||||
using buffer::fixed_const_raw_buffer;
|
using buffer::fixed_const_raw_buffer;
|
||||||
|
@ -386,6 +388,50 @@ static_assert
|
||||||
"ircd::buffer::fixed_buffer must be standard layout"
|
"ircd::buffer::fixed_buffer must be standard layout"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
struct ircd::buffer::stream_buffer
|
||||||
|
:mutable_buffer
|
||||||
|
{
|
||||||
|
mutable_buffer base;
|
||||||
|
|
||||||
|
/// Bytes remaining for writes to the stream buffer (same as size(*this))
|
||||||
|
size_t remaining() const
|
||||||
|
{
|
||||||
|
assert(begin() <= base.end());
|
||||||
|
const size_t ret(std::distance(begin(), base.end()));
|
||||||
|
assert(ret == size(*this));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Bytes used by writes to the stream buffer
|
||||||
|
size_t consumed() const
|
||||||
|
{
|
||||||
|
assert(begin() >= base.begin());
|
||||||
|
assert(begin() <= base.end());
|
||||||
|
return std::distance(base.begin(), begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// View the completed portion of the stream
|
||||||
|
const_buffer completed() const
|
||||||
|
{
|
||||||
|
assert(base.begin() <= begin());
|
||||||
|
assert(base.begin() + consumed() <= base.end());
|
||||||
|
return { base.begin(), base.begin() + consumed() };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convenience closure presenting the writable window and advancing the
|
||||||
|
/// window with a consume() for the bytes written in the closure.
|
||||||
|
using closure = std::function<size_t (const mutable_buffer &)>;
|
||||||
|
void operator()(const closure &closure)
|
||||||
|
{
|
||||||
|
consume(*this, closure(*this));
|
||||||
|
}
|
||||||
|
|
||||||
|
stream_buffer(const mutable_buffer &base)
|
||||||
|
:mutable_buffer{base}
|
||||||
|
,base{base}
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
/// Like unique_ptr, this template holds ownership of an allocated buffer
|
/// Like unique_ptr, this template holds ownership of an allocated buffer
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
|
|
Loading…
Reference in a new issue