0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-30 04:38:52 +02:00

ircd::buffer: Additional assertions; simplify unique_buffer operations; minor cleanup.

This commit is contained in:
Jason Volk 2019-04-10 13:38:47 -07:00
parent 96ff7022d0
commit b45306a012
2 changed files with 49 additions and 37 deletions

View file

@ -20,21 +20,21 @@ struct ircd::buffer::buffer
using iterator = it;
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() { 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<it>::buffer(const it &start,
{}
template<class it>
auto &
ircd::buffer::buffer<it>::operator[](const size_t &i)
ircd::buffer::buffer<it>::operator string_view()
const
{
return *(begin() + i);
}
template<class it>
auto &
ircd::buffer::buffer<it>::operator[](const size_t &i)
{
return *(begin() + i);
return { reinterpret_cast<const char *>(data(*this)), size(*this) };
}
template<class it>
@ -88,10 +80,36 @@ const
}
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
{
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>

View file

@ -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<class buffer>
ircd::buffer::unique_buffer<buffer>::unique_buffer()
:buffer
{
nullptr, nullptr
}
{}
template<class buffer>
ircd::buffer::unique_buffer<buffer>::unique_buffer(const buffer &src)
:unique_buffer
@ -49,9 +41,11 @@ ircd::buffer::unique_buffer<buffer>::unique_buffer(const buffer &src)
size(src)
}
{
assert(this->begin() != nullptr);
assert(size(src) == size(*this));
const mutable_buffer dst
{
const_cast<char *>(data(*this)), size(*this)
const_cast<char *>(this->begin()), size(src)
};
copy(dst, src);
@ -72,10 +66,10 @@ ircd::buffer::unique_buffer<buffer>::unique_buffer(unique_buffer &&other)
noexcept
:buffer
{
std::move(static_cast<buffer &>(other))
other.release()
}
{
get<0>(other) = nullptr;
assert(std::get<0>(other) == nullptr);
}
template<class buffer>
@ -85,8 +79,8 @@ noexcept
{
this->~unique_buffer();
static_cast<buffer &>(*this) = std::move(static_cast<buffer &>(other));
get<0>(other) = nullptr;
static_cast<buffer &>(*this) = other.release();
assert(std::get<0>(other) == nullptr);
return *this;
}
@ -95,15 +89,15 @@ template<class buffer>
ircd::buffer::unique_buffer<buffer>::~unique_buffer()
noexcept
{
std::free(const_cast<char *>(data(*this)));
std::free(const_cast<char *>(this->begin()));
}
template<class buffer>
buffer
ircd::buffer::unique_buffer<buffer>::release()
{
const buffer ret{static_cast<buffer>(*this)};
static_cast<buffer &>(*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<char, decltype(&std::free)>
{
reinterpret_cast<char *>(ret), std::free
reinterpret_cast<char *>(ret), &std::free
};
}