|
|
@ -1,7 +1,8 @@
|
|
|
|
#pragma once
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A type that spans a sized region of memory
|
|
|
|
template<typename T>
|
|
|
|
template<typename T>
|
|
|
|
struct Slice {
|
|
|
|
struct Span {
|
|
|
|
virtual const void* area() const =0;
|
|
|
|
virtual const void* area() const =0;
|
|
|
|
virtual void* area() = 0;
|
|
|
|
virtual void* area() = 0;
|
|
|
|
virtual size_t size() const = 0;
|
|
|
|
virtual size_t size() const = 0;
|
|
|
@ -30,4 +31,39 @@ struct Slice {
|
|
|
|
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
template<typename U>
|
|
|
|
size_t size_as() const requires(sizeof(T) % sizeof(U) == 0) { return size_bytes() / sizeof(U); }
|
|
|
|
size_t size_as() const requires(sizeof(T) % sizeof(U) == 0) { return size_bytes() / sizeof(U); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Slice;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool bounds_ok(size_t start, size_t len)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return (start + len) < size();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool bounds_ok(size_t start)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return start < size();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline Slice slice(size_t start, size_t len) { if(bounds_ok(start,len)) return Slice(ptr()+start, len); else throw "Out of bounds slice"; }
|
|
|
|
|
|
|
|
inline Slice slice(size_t start) { return slice(start, size()-start); }
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A slice of memory with a backing pointer and size.
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
|
|
|
struct Span<T>::Slice : public Span<T> {
|
|
|
|
|
|
|
|
inline Slice(T* ptr, size_t sz) : _area((void*)ptr), _size(sz){}
|
|
|
|
|
|
|
|
inline Slice(const Span<T>& slice) : _area(slice.area()), _size(slice.size()){}
|
|
|
|
|
|
|
|
inline Slice(const Slice& copy) = default;
|
|
|
|
|
|
|
|
inline Slice(Slice&& copy) : _area(copy._area), _size(copy._size){ *const_cast<size_t*>(©._size) = 0; }
|
|
|
|
|
|
|
|
Slice() = delete;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline const void* area() const override { return _area; }
|
|
|
|
|
|
|
|
inline void* area() override { return _area; }
|
|
|
|
|
|
|
|
inline size_t size() const override { return _size; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void* const _area;
|
|
|
|
|
|
|
|
const size_t _size;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
|
|
|
typename Span<T>::Slice Slice;
|
|
|
|