for span tweaking
This commit is contained in:
parent
b82f039aaf
commit
17b4cce69b
@ -20,6 +20,6 @@ struct File2I {
|
||||
|
||||
// pos -1 means stream, append to last written, or read position (independent, like FILE*s)
|
||||
virtual bool write(const ByteSpan data, int64_t pos = -1) = 0;
|
||||
virtual std::variant<ByteSpan, std::vector<uint8_t>> read(uint64_t size, int64_t pos = -1) = 0;
|
||||
[[nodiscard]] virtual std::variant<ByteSpan, std::vector<uint8_t>> read(uint64_t size, int64_t pos = -1) = 0;
|
||||
};
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <array>
|
||||
|
||||
// non owning view
|
||||
template<typename T>
|
||||
@ -10,9 +11,21 @@ struct Span final {
|
||||
const T* ptr {nullptr};
|
||||
uint64_t size {0};
|
||||
|
||||
constexpr Span(const Span&) = default;
|
||||
constexpr Span(Span&&) = default;
|
||||
constexpr Span(void) {}
|
||||
constexpr Span(const T* ptr_, uint64_t size_) : ptr(ptr_), size(size_) {}
|
||||
constexpr Span(T* ptr_, uint64_t size_) : ptr(ptr_), size(size_) {}
|
||||
constexpr explicit Span(const std::vector<T>& vec) : ptr(vec.data()), size(vec.size()) {}
|
||||
constexpr explicit Span(std::vector<T>& vec) : ptr(vec.data()), size(vec.size()) {}
|
||||
template<size_t N> constexpr explicit Span(const std::array<T, N>& arr) : ptr(arr.data()), size(arr.size()) {}
|
||||
template<size_t N> constexpr explicit Span(std::array<T, N>& arr) : ptr(arr.data()), size(arr.size()) {}
|
||||
|
||||
Span& operator=(const Span& other) {
|
||||
ptr = other.ptr;
|
||||
size = other.size;
|
||||
return *this;
|
||||
}
|
||||
|
||||
explicit operator std::vector<T>() const {
|
||||
return std::vector<T>{ptr, ptr+size};
|
||||
@ -26,6 +39,16 @@ struct Span final {
|
||||
return ptr[i];
|
||||
}
|
||||
|
||||
#if 0
|
||||
constexpr T& operator[](uint64_t i) {
|
||||
if (i > size) {
|
||||
throw std::out_of_range("accessed span out of range");
|
||||
}
|
||||
|
||||
return ptr[i];
|
||||
}
|
||||
#endif
|
||||
|
||||
constexpr const T* cbegin(void) const { return ptr; }
|
||||
constexpr const T* cend(void) const { return ptr+size; }
|
||||
constexpr const T* begin(void) const { return ptr; }
|
||||
@ -36,4 +59,5 @@ struct Span final {
|
||||
|
||||
// useful alias
|
||||
using ByteSpan = Span<uint8_t>;
|
||||
using ConstByteSpan = Span<const uint8_t>; // TODO: make use of?
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user