11#include "../microfmt.hpp"
12#include "../reloco.hpp"
13#include <boost/asio/buffer.hpp>
14#include <boost/asio/streambuf.hpp>
40 boost::asio::mutable_buffer buf RELOCO_LIFETIMEBOUND RELOCO_LIFETIME_CAPTURE_BY_THIS) noexcept
41 : data_(
static_cast<char *
>(buf.data())), capacity_(buf.size()) {}
51 [[nodiscard]]
sink as_sink() noexcept RELOCO_LIFETIMEBOUND {
return sink{
this, &write_impl}; }
56 [[nodiscard]]
size_t size() const noexcept {
return size_; }
66 [[nodiscard]] boost::asio::const_buffer
written_buffer() const noexcept RELOCO_LIFETIMEBOUND {
67 return boost::asio::const_buffer(data_, size_);
71 static void write_impl(
void *ctx, microfmt::string_view str)
noexcept {
74 if (self->size_ >= self->capacity_)
77 size_t to_copy = str.
size();
78 if (self->size_ + to_copy > self->capacity_) {
79 to_copy = self->capacity_ - self->size_;
82 RELOCO_BEGIN_UNSAFE_BUFFER_USAGE;
83 std::memcpy(self->data_ + self->size_, str.data(), to_copy);
84 RELOCO_END_UNSAFE_BUFFER_USAGE;
86 self->size_ += to_copy;
107 explicit asio_streambuf_sink(boost::asio::streambuf &buf RELOCO_LIFETIMEBOUND RELOCO_LIFETIME_CAPTURE_BY_THIS,
108 size_t max_chars = 1024) noexcept
109 : streambuf_(&buf), max_chars_(max_chars) {}
114 [[nodiscard]]
sink as_sink() noexcept RELOCO_LIFETIMEBOUND {
return sink{
this, &write_impl}; }
117 static void write_impl(
void *ctx, microfmt::string_view str)
noexcept {
120 if (self->written_ >= self->max_chars_)
123 size_t len = str.size();
124 if (self->written_ + len > self->max_chars_) {
125 len = self->max_chars_ - self->written_;
129 std::ostream os(self->streambuf_);
130 os.write(str.data(),
static_cast<std::streamsize
>(len));
131 self->written_ += len;
134 boost::asio::streambuf *streambuf_;
A sink adapter that writes formatted output directly into a boost::asio::mutable_buffer.
Definition asio_sink.hpp:30
asio_mutable_buffer_sink & operator=(const asio_mutable_buffer_sink &)=delete
asio_mutable_buffer_sink(asio_mutable_buffer_sink &&) noexcept=default
size_t size() const noexcept
Returns the total number of bytes written to the buffer so far.
Definition asio_sink.hpp:56
asio_mutable_buffer_sink(boost::asio::mutable_buffer buf RELOCO_LIFETIMEBOUND RELOCO_LIFETIME_CAPTURE_BY_THIS) noexcept
Constructs a sink over a Boost.Asio mutable buffer.
Definition asio_sink.hpp:39
boost::asio::const_buffer written_buffer() const noexcept RELOCO_LIFETIMEBOUND
Returns the written prefix of the wrapped buffer as a boost::asio::const_buffer, suitable for boost::...
Definition asio_sink.hpp:66
asio_mutable_buffer_sink(const asio_mutable_buffer_sink &)=delete
A sink adapter that writes formatted output into a boost::asio::streambuf dynamic buffer.
Definition asio_sink.hpp:98
asio_streambuf_sink(boost::asio::streambuf &buf RELOCO_LIFETIMEBOUND RELOCO_LIFETIME_CAPTURE_BY_THIS, size_t max_chars=1024) noexcept
Constructs a sink over an externally owned Boost.Asio streambuf.
Definition asio_sink.hpp:107
sink as_sink() noexcept RELOCO_LIFETIMEBOUND
Creates a type-erased sink adapter pointing to this instance.
Definition asio_sink.hpp:114
#define MICROFMT_API_CLASS
Definition compat.hpp:181
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46