diff --git a/include/ircd/buffer/buffer_base.h b/include/ircd/buffer/buffer_base.h index 0160c738d..2bb8f1e64 100644 --- a/include/ircd/buffer/buffer_base.h +++ b/include/ircd/buffer/buffer_base.h @@ -20,21 +20,21 @@ struct ircd::buffer::buffer using iterator = it; using value_type = typename std::remove_pointer::type; - explicit operator const it &() const; - operator string_view() const; - explicit operator std::string_view() const; - explicit operator std::string() const; - auto &begin() const { return std::get<0>(*this); } auto &begin() { return std::get<0>(*this); } auto &end() const { return std::get<1>(*this); } auto &end() { return std::get<1>(*this); } + bool null() const; + bool empty() const; // For boost::spirit conceptual compliance. + auto &operator[](const size_t &i) const; auto &operator[](const size_t &i); - // For boost::spirit conceptual compliance. - auto empty() const { return ircd::buffer::empty(*this); } + explicit operator const it &() const; + explicit operator std::string_view() const; + explicit operator std::string() const; + operator string_view() const; buffer(const it &start, const it &stop); buffer(const it &start, const size_t &size); @@ -59,18 +59,10 @@ ircd::buffer::buffer::buffer(const it &start, {} template -auto & -ircd::buffer::buffer::operator[](const size_t &i) +ircd::buffer::buffer::operator string_view() const { - return *(begin() + i); -} - -template -auto & -ircd::buffer::buffer::operator[](const size_t &i) -{ - return *(begin() + i); + return { reinterpret_cast(data(*this)), size(*this) }; } template @@ -88,10 +80,36 @@ const } template -ircd::buffer::buffer::operator string_view() +auto & +ircd::buffer::buffer::operator[](const size_t &i) +{ + assert(begin() + i < end()); + return *(begin() + i); +} + +template +auto & +ircd::buffer::buffer::operator[](const size_t &i) const { - return { reinterpret_cast(data(*this)), size(*this) }; + assert(begin() + i < end()); + return *(begin() + i); +} + +template +bool +ircd::buffer::buffer::empty() +const +{ + return null() || std::distance(begin(), end()) == 0; +} + +template +bool +ircd::buffer::buffer::null() +const +{ + return !begin(); } template diff --git a/include/ircd/buffer/unique_buffer.h b/include/ircd/buffer/unique_buffer.h index 94e3d59e0..877c14054 100644 --- a/include/ircd/buffer/unique_buffer.h +++ b/include/ircd/buffer/unique_buffer.h @@ -26,7 +26,7 @@ struct ircd::buffer::unique_buffer unique_buffer(const size_t &size, const size_t &align = 0); explicit unique_buffer(const buffer &); - unique_buffer(); + unique_buffer() = default; unique_buffer(unique_buffer &&) noexcept; unique_buffer(const unique_buffer &) = delete; unique_buffer &operator=(unique_buffer &&) noexcept; @@ -34,14 +34,6 @@ struct ircd::buffer::unique_buffer ~unique_buffer() noexcept; }; -template -ircd::buffer::unique_buffer::unique_buffer() -:buffer -{ - nullptr, nullptr -} -{} - template ircd::buffer::unique_buffer::unique_buffer(const buffer &src) :unique_buffer @@ -49,9 +41,11 @@ ircd::buffer::unique_buffer::unique_buffer(const buffer &src) size(src) } { + assert(this->begin() != nullptr); + assert(size(src) == size(*this)); const mutable_buffer dst { - const_cast(data(*this)), size(*this) + const_cast(this->begin()), size(src) }; copy(dst, src); @@ -72,10 +66,10 @@ ircd::buffer::unique_buffer::unique_buffer(unique_buffer &&other) noexcept :buffer { - std::move(static_cast(other)) + other.release() } { - get<0>(other) = nullptr; + assert(std::get<0>(other) == nullptr); } template @@ -85,8 +79,8 @@ noexcept { this->~unique_buffer(); - static_cast(*this) = std::move(static_cast(other)); - get<0>(other) = nullptr; + static_cast(*this) = other.release(); + assert(std::get<0>(other) == nullptr); return *this; } @@ -95,15 +89,15 @@ template ircd::buffer::unique_buffer::~unique_buffer() noexcept { - std::free(const_cast(data(*this))); + std::free(const_cast(this->begin())); } template buffer ircd::buffer::unique_buffer::release() { - const buffer ret{static_cast(*this)}; - static_cast(*this) = buffer{}; + const buffer ret{*this}; + this->begin() = nullptr; return ret; } @@ -136,6 +130,6 @@ ircd::buffer::aligned_alloc(const size_t &align, return std::unique_ptr { - reinterpret_cast(ret), std::free + reinterpret_cast(ret), &std::free }; }