BAN: Update {Byte}Span API with better constness

const BAN::Span<int> is now allowed to modify its underlying data, but
the container itself is const.

BAN::Span<const int> can be used for spans over constant data.
This commit is contained in:
Bananymous 2024-10-10 21:53:23 +03:00
parent a68f411024
commit 2da6776451
2 changed files with 103 additions and 168 deletions

View File

@ -21,75 +21,56 @@ namespace BAN
, m_size(size) , m_size(size)
{ } { }
ByteSpanGeneral(ByteSpanGeneral& other) template<bool SRC_CONST>
ByteSpanGeneral(const ByteSpanGeneral<SRC_CONST>& other) requires(CONST || !SRC_CONST)
: m_data(other.data()) : m_data(other.data())
, m_size(other.size()) , m_size(other.size())
{ } { }
ByteSpanGeneral(ByteSpanGeneral&& other) template<bool SRC_CONST>
ByteSpanGeneral(ByteSpanGeneral<SRC_CONST>&& other) requires(CONST || !SRC_CONST)
: m_data(other.data()) : m_data(other.data())
, m_size(other.size()) , m_size(other.size())
{ {
other.m_data = nullptr; other.clear();
other.m_size = 0;
} }
template<bool C2>
ByteSpanGeneral(const ByteSpanGeneral<C2>& other) requires(CONST)
: m_data(other.data())
, m_size(other.size())
{ }
template<bool C2>
ByteSpanGeneral(ByteSpanGeneral<C2>&& other) requires(CONST)
: m_data(other.data())
, m_size(other.size())
{
other.m_data = nullptr;
other.m_size = 0;
}
ByteSpanGeneral(Span<uint8_t> other)
: m_data(other.data())
, m_size(other.size())
{ }
ByteSpanGeneral(const Span<const uint8_t>& other) requires(CONST)
: m_data(other.data())
, m_size(other.size())
{ }
ByteSpanGeneral& operator=(ByteSpanGeneral other) template<typename T>
ByteSpanGeneral(const Span<T>& other) requires(is_same_v<T, uint8_t> || (is_same_v<T, const uint8_t> && CONST))
: m_data(other.data())
, m_size(other.size())
{ }
template<typename T>
ByteSpanGeneral(Span<T>&& other) requires(is_same_v<T, uint8_t> || (is_same_v<T, const uint8_t> && CONST))
: m_data(other.data())
, m_size(other.size())
{
other.clear();
}
template<bool SRC_CONST>
ByteSpanGeneral& operator=(const ByteSpanGeneral<SRC_CONST>& other) requires(CONST || !SRC_CONST)
{ {
m_data = other.data(); m_data = other.data();
m_size = other.size(); m_size = other.size();
return *this; return *this;
} }
template<bool C2> template<bool SRC_CONST>
ByteSpanGeneral& operator=(const ByteSpanGeneral<C2>& other) requires(CONST) ByteSpanGeneral& operator=(ByteSpanGeneral<SRC_CONST>&& other) requires(CONST || !SRC_CONST)
{
m_data = other.data();
m_size = other.size();
return *this;
}
ByteSpanGeneral& operator=(Span<uint8_t> other)
{
m_data = other.data();
m_size = other.size();
return *this;
}
ByteSpanGeneral& operator=(const Span<const uint8_t>& other) requires(CONST)
{ {
m_data = other.data(); m_data = other.data();
m_size = other.size(); m_size = other.size();
other.clear();
return *this; return *this;
} }
template<typename S> template<typename S>
requires(CONST || !is_const_v<S>) static ByteSpanGeneral from(S& value) requires(CONST || !is_const_v<S>)
static ByteSpanGeneral from(S& value)
{ {
return ByteSpanGeneral(reinterpret_cast<value_type*>(&value), sizeof(S)); return ByteSpanGeneral(reinterpret_cast<value_type*>(&value), sizeof(S));
} }
template<typename S> template<typename S>
requires(!CONST && !is_const_v<S>) S& as() const requires(!CONST || is_const_v<S>)
S& as()
{ {
ASSERT(m_data); ASSERT(m_data);
ASSERT(m_size >= sizeof(S)); ASSERT(m_size >= sizeof(S));
@ -97,30 +78,13 @@ namespace BAN
} }
template<typename S> template<typename S>
requires(is_const_v<S>) Span<S> as_span() const requires(!CONST || is_const_v<S>)
S& as() const
{
ASSERT(m_data);
ASSERT(m_size >= sizeof(S));
return *reinterpret_cast<S*>(m_data);
}
template<typename S>
requires(!CONST && !is_const_v<S>)
Span<S> as_span()
{ {
ASSERT(m_data); ASSERT(m_data);
return Span<S>(reinterpret_cast<S*>(m_data), m_size / sizeof(S)); return Span<S>(reinterpret_cast<S*>(m_data), m_size / sizeof(S));
} }
template<typename S> ByteSpanGeneral slice(size_type offset, size_type length = size_type(-1)) const
const Span<S> as_span() const
{
ASSERT(m_data);
return Span<S>(reinterpret_cast<S*>(m_data), m_size / sizeof(S));
}
ByteSpanGeneral slice(size_type offset, size_type length = size_type(-1))
{ {
ASSERT(m_data); ASSERT(m_data);
ASSERT(m_size >= offset); ASSERT(m_size >= offset);
@ -130,22 +94,23 @@ namespace BAN
return ByteSpanGeneral(m_data + offset, length); return ByteSpanGeneral(m_data + offset, length);
} }
value_type& operator[](size_type offset) value_type& operator[](size_type offset) const
{
ASSERT(offset < m_size);
return m_data[offset];
}
const value_type& operator[](size_type offset) const
{ {
ASSERT(offset < m_size); ASSERT(offset < m_size);
return m_data[offset]; return m_data[offset];
} }
value_type* data() { return m_data; } value_type* data() const { return m_data; }
const value_type* data() const { return m_data; }
bool empty() const { return m_size == 0; }
size_type size() const { return m_size; } size_type size() const { return m_size; }
void clear()
{
m_data = nullptr;
m_size = 0;
}
private: private:
value_type* m_data { nullptr }; value_type* m_data { nullptr };
size_type m_size { 0 }; size_type m_size { 0 };

View File

@ -14,114 +14,75 @@ namespace BAN
public: public:
using value_type = T; using value_type = T;
using size_type = size_t; using size_type = size_t;
using iterator = IteratorSimple<T, Span>; using iterator = IteratorSimple<value_type, Span>;
using const_iterator = ConstIteratorSimple<T, Span>; using const_iterator = ConstIteratorSimple<value_type, Span>;
private:
template<typename S>
static inline constexpr bool can_init_from_v = is_same_v<value_type, const S> || is_same_v<value_type, S>;
public: public:
Span() = default; Span() = default;
Span(T*, size_type); Span(value_type* data, size_type size)
Span(Span<T>&); : m_data(data)
, m_size(size)
{ }
template<typename S> template<typename S>
requires(is_same_v<T, const S>) Span(const Span<S>& other) requires can_init_from_v<S>
Span(const Span<S>&); : m_data(other.m_data)
, m_size(other.m_size)
{ }
template<typename S>
Span(Span<S>&& other) requires can_init_from_v<S>
: m_data(other.m_data)
, m_size(other.m_size)
{
other.clear();
}
template<typename S>
Span& operator=(const Span<S>& other) requires can_init_from_v<S>
{
m_data = other.m_data;
m_size = other.m_size;
return *this;
}
template<typename S>
Span& operator=(Span<S>&& other) requires can_init_from_v<S>
{
m_data = other.m_data;
m_size = other.m_size;
return *this;
}
iterator begin() { return iterator(m_data); } iterator begin() { return iterator(m_data); }
iterator end() { return iterator(m_data + m_size); } iterator end() { return iterator(m_data + m_size); }
const_iterator begin() const { return const_iterator(m_data); } const_iterator begin() const { return const_iterator(m_data); }
const_iterator end() const { return const_iterator(m_data + m_size); } const_iterator end() const { return const_iterator(m_data + m_size); }
T& operator[](size_type); value_type& operator[](size_type index) const
const T& operator[](size_type) const;
T* data();
const T* data() const;
bool empty() const;
size_type size() const;
void clear();
Span slice(size_type, size_type = ~size_type(0));
Span<const T> as_const() const { return Span<const T>(m_data, m_size); }
private:
T* m_data = nullptr;
size_type m_size = 0;
};
template<typename T>
Span<T>::Span(T* data, size_type size)
: m_data(data)
, m_size(size)
{ {
}
template<typename T>
Span<T>::Span(Span& other)
: m_data(other.data())
, m_size(other.size())
{
}
template<typename T>
template<typename S>
requires(is_same_v<T, const S>)
Span<T>::Span(const Span<S>& other)
: m_data(other.data())
, m_size(other.size())
{
}
template<typename T>
T& Span<T>::operator[](size_type index)
{
ASSERT(m_data);
ASSERT(index < m_size); ASSERT(index < m_size);
return m_data[index]; return m_data[index];
} }
template<typename T> value_type* data() const
const T& Span<T>::operator[](size_type index) const
{ {
ASSERT(m_data); ASSERT(m_data);
ASSERT(index < m_size);
return m_data[index];
}
template<typename T>
T* Span<T>::data()
{
return m_data; return m_data;
} }
template<typename T> bool empty() const { return m_size == 0; }
const T* Span<T>::data() const size_type size() const { return m_size; }
{
return m_data;
}
template<typename T> void clear()
bool Span<T>::empty() const
{
return m_size == 0;
}
template<typename T>
typename Span<T>::size_type Span<T>::size() const
{
return m_size;
}
template<typename T>
void Span<T>::clear()
{ {
m_data = nullptr; m_data = nullptr;
m_size = 0; m_size = 0;
} }
template<typename T> Span slice(size_type start, size_type length = ~size_type(0)) const
Span<T> Span<T>::slice(size_type start, size_type length)
{ {
ASSERT(m_data); ASSERT(m_data);
ASSERT(start <= m_size); ASSERT(start <= m_size);
@ -131,4 +92,13 @@ namespace BAN
return Span(m_data + start, length); return Span(m_data + start, length);
} }
Span<const value_type> as_const() const { return *this; }
private:
value_type* m_data = nullptr;
size_type m_size = 0;
friend class Span<const value_type>;
};
} }