mirror of
https://github.com/matrix-construct/construct
synced 2024-11-17 23:40:57 +01:00
ircd::buffer: Additional assertions; simplify unique_buffer operations; minor cleanup.
This commit is contained in:
parent
96ff7022d0
commit
b45306a012
2 changed files with 49 additions and 37 deletions
|
@ -20,21 +20,21 @@ struct ircd::buffer::buffer
|
||||||
using iterator = it;
|
using iterator = it;
|
||||||
using value_type = typename std::remove_pointer<iterator>::type;
|
using value_type = typename std::remove_pointer<iterator>::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() const { return std::get<0>(*this); }
|
||||||
auto &begin() { return std::get<0>(*this); }
|
auto &begin() { return std::get<0>(*this); }
|
||||||
auto &end() const { return std::get<1>(*this); }
|
auto &end() const { return std::get<1>(*this); }
|
||||||
auto &end() { 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) const;
|
||||||
auto &operator[](const size_t &i);
|
auto &operator[](const size_t &i);
|
||||||
|
|
||||||
// For boost::spirit conceptual compliance.
|
explicit operator const it &() const;
|
||||||
auto empty() const { return ircd::buffer::empty(*this); }
|
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 it &stop);
|
||||||
buffer(const it &start, const size_t &size);
|
buffer(const it &start, const size_t &size);
|
||||||
|
@ -59,18 +59,10 @@ ircd::buffer::buffer<it>::buffer(const it &start,
|
||||||
{}
|
{}
|
||||||
|
|
||||||
template<class it>
|
template<class it>
|
||||||
auto &
|
ircd::buffer::buffer<it>::operator string_view()
|
||||||
ircd::buffer::buffer<it>::operator[](const size_t &i)
|
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
return *(begin() + i);
|
return { reinterpret_cast<const char *>(data(*this)), size(*this) };
|
||||||
}
|
|
||||||
|
|
||||||
template<class it>
|
|
||||||
auto &
|
|
||||||
ircd::buffer::buffer<it>::operator[](const size_t &i)
|
|
||||||
{
|
|
||||||
return *(begin() + i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class it>
|
template<class it>
|
||||||
|
@ -88,10 +80,36 @@ const
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class it>
|
template<class it>
|
||||||
ircd::buffer::buffer<it>::operator string_view()
|
auto &
|
||||||
|
ircd::buffer::buffer<it>::operator[](const size_t &i)
|
||||||
|
{
|
||||||
|
assert(begin() + i < end());
|
||||||
|
return *(begin() + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class it>
|
||||||
|
auto &
|
||||||
|
ircd::buffer::buffer<it>::operator[](const size_t &i)
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
return { reinterpret_cast<const char *>(data(*this)), size(*this) };
|
assert(begin() + i < end());
|
||||||
|
return *(begin() + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class it>
|
||||||
|
bool
|
||||||
|
ircd::buffer::buffer<it>::empty()
|
||||||
|
const
|
||||||
|
{
|
||||||
|
return null() || std::distance(begin(), end()) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class it>
|
||||||
|
bool
|
||||||
|
ircd::buffer::buffer<it>::null()
|
||||||
|
const
|
||||||
|
{
|
||||||
|
return !begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class it>
|
template<class it>
|
||||||
|
|
|
@ -26,7 +26,7 @@ struct ircd::buffer::unique_buffer
|
||||||
|
|
||||||
unique_buffer(const size_t &size, const size_t &align = 0);
|
unique_buffer(const size_t &size, const size_t &align = 0);
|
||||||
explicit unique_buffer(const buffer &);
|
explicit unique_buffer(const buffer &);
|
||||||
unique_buffer();
|
unique_buffer() = default;
|
||||||
unique_buffer(unique_buffer &&) noexcept;
|
unique_buffer(unique_buffer &&) noexcept;
|
||||||
unique_buffer(const unique_buffer &) = delete;
|
unique_buffer(const unique_buffer &) = delete;
|
||||||
unique_buffer &operator=(unique_buffer &&) noexcept;
|
unique_buffer &operator=(unique_buffer &&) noexcept;
|
||||||
|
@ -34,14 +34,6 @@ struct ircd::buffer::unique_buffer
|
||||||
~unique_buffer() noexcept;
|
~unique_buffer() noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class buffer>
|
|
||||||
ircd::buffer::unique_buffer<buffer>::unique_buffer()
|
|
||||||
:buffer
|
|
||||||
{
|
|
||||||
nullptr, nullptr
|
|
||||||
}
|
|
||||||
{}
|
|
||||||
|
|
||||||
template<class buffer>
|
template<class buffer>
|
||||||
ircd::buffer::unique_buffer<buffer>::unique_buffer(const buffer &src)
|
ircd::buffer::unique_buffer<buffer>::unique_buffer(const buffer &src)
|
||||||
:unique_buffer
|
:unique_buffer
|
||||||
|
@ -49,9 +41,11 @@ ircd::buffer::unique_buffer<buffer>::unique_buffer(const buffer &src)
|
||||||
size(src)
|
size(src)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
assert(this->begin() != nullptr);
|
||||||
|
assert(size(src) == size(*this));
|
||||||
const mutable_buffer dst
|
const mutable_buffer dst
|
||||||
{
|
{
|
||||||
const_cast<char *>(data(*this)), size(*this)
|
const_cast<char *>(this->begin()), size(src)
|
||||||
};
|
};
|
||||||
|
|
||||||
copy(dst, src);
|
copy(dst, src);
|
||||||
|
@ -72,10 +66,10 @@ ircd::buffer::unique_buffer<buffer>::unique_buffer(unique_buffer &&other)
|
||||||
noexcept
|
noexcept
|
||||||
:buffer
|
:buffer
|
||||||
{
|
{
|
||||||
std::move(static_cast<buffer &>(other))
|
other.release()
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
get<0>(other) = nullptr;
|
assert(std::get<0>(other) == nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class buffer>
|
template<class buffer>
|
||||||
|
@ -85,8 +79,8 @@ noexcept
|
||||||
{
|
{
|
||||||
this->~unique_buffer();
|
this->~unique_buffer();
|
||||||
|
|
||||||
static_cast<buffer &>(*this) = std::move(static_cast<buffer &>(other));
|
static_cast<buffer &>(*this) = other.release();
|
||||||
get<0>(other) = nullptr;
|
assert(std::get<0>(other) == nullptr);
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -95,15 +89,15 @@ template<class buffer>
|
||||||
ircd::buffer::unique_buffer<buffer>::~unique_buffer()
|
ircd::buffer::unique_buffer<buffer>::~unique_buffer()
|
||||||
noexcept
|
noexcept
|
||||||
{
|
{
|
||||||
std::free(const_cast<char *>(data(*this)));
|
std::free(const_cast<char *>(this->begin()));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class buffer>
|
template<class buffer>
|
||||||
buffer
|
buffer
|
||||||
ircd::buffer::unique_buffer<buffer>::release()
|
ircd::buffer::unique_buffer<buffer>::release()
|
||||||
{
|
{
|
||||||
const buffer ret{static_cast<buffer>(*this)};
|
const buffer ret{*this};
|
||||||
static_cast<buffer &>(*this) = buffer{};
|
this->begin() = nullptr;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,6 +130,6 @@ ircd::buffer::aligned_alloc(const size_t &align,
|
||||||
|
|
||||||
return std::unique_ptr<char, decltype(&std::free)>
|
return std::unique_ptr<char, decltype(&std::free)>
|
||||||
{
|
{
|
||||||
reinterpret_cast<char *>(ret), std::free
|
reinterpret_cast<char *>(ret), &std::free
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue