From 51f5045c06ecf8b8ff988d3447f500f6af001d26 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 5 Oct 2017 18:32:48 -0700 Subject: [PATCH] ircd::buffer: Add fixed_buffer with construction closure; minor cleanup. --- include/ircd/buffer.h | 69 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/include/ircd/buffer.h b/include/ircd/buffer.h index 6601f7a27..fcc2a4614 100644 --- a/include/ircd/buffer.h +++ b/include/ircd/buffer.h @@ -55,8 +55,14 @@ namespace ircd::buffer struct mutable_buffer; struct const_raw_buffer; struct mutable_raw_buffer; + template struct fixed_buffer; template struct unique_buffer; + template using fixed_const_buffer = fixed_buffer; + template using fixed_mutable_buffer = fixed_buffer; + template using fixed_const_raw_buffer = fixed_buffer; + template using fixed_mutable_raw_buffer = fixed_buffer; + template class I> using const_buffers = I; template class I> using mutable_buffers = I; template class I> using const_raw_buffers = I; @@ -100,8 +106,13 @@ namespace ircd using buffer::mutable_buffer; using buffer::const_raw_buffer; using buffer::mutable_raw_buffer; + using buffer::fixed_buffer; using buffer::unique_buffer; using buffer::null_buffer; + using buffer::fixed_const_buffer; + using buffer::fixed_mutable_buffer; + using buffer::fixed_const_raw_buffer; + using buffer::fixed_mutable_raw_buffer; using buffer::const_buffers; using buffer::mutable_buffers; @@ -175,7 +186,9 @@ struct ircd::buffer::mutable_buffer_base } using buffer::buffer; - mutable_buffer_base(): buffer{} {} + + mutable_buffer_base() + :buffer{} {} template mutable_buffer_base(value_type (&buf)[SIZE]) @@ -205,6 +218,11 @@ struct ircd::buffer::mutable_buffer operator boost::asio::mutable_buffer() const; using mutable_buffer_base::mutable_buffer_base; + + mutable_buffer(const std::function &closure) + { + closure(*this); + } }; /// A writable buffer of unsigned signed char data. Convention is for this @@ -224,6 +242,11 @@ struct ircd::buffer::mutable_raw_buffer mutable_raw_buffer(const mutable_buffer &b) :mutable_buffer_base{reinterpret_cast(data(b)), size(b)} {} + + mutable_raw_buffer(const std::function &closure) + { + closure(*this); + } }; namespace ircd::buffer @@ -240,7 +263,10 @@ struct ircd::buffer::const_buffer_base using mutable_value_type = typename std::remove_const::type; using buffer::buffer; - const_buffer_base(): buffer{} {} + + const_buffer_base() + :buffer{} + {} template const_buffer_base(const value_type (&buf)[SIZE]) @@ -297,6 +323,45 @@ struct ircd::buffer::const_raw_buffer {} }; +/// fixed_buffer wraps an std::array with construction and conversions apropos +/// the ircd::buffer suite +/// +template +struct ircd::buffer::fixed_buffer +:std::array::type, SIZE> +{ + using mutable_type = typename std::remove_const::type; + using const_type = typename std::add_const::type; + using array_type = std::array; + + operator buffer() const + { + return { std::begin(*this), std::end(*this) }; + } + + operator buffer() + { + return { std::begin(*this), std::end(*this) }; + } + + using array_type::array_type; + fixed_buffer(const nullptr_t &) + :array_type{{0}} + {} + + fixed_buffer(const std::function &closure) + { + closure(mutable_raw_buffer{std::begin(*this), std::end(*this)}); + } + + fixed_buffer(buffer b) + :array_type{std::begin(b), std::end(b)} + {} + + fixed_buffer() = default; +}; + /// Like unique_ptr, this template holds ownership of an allocated buffer /// ///