refactor file2 and add span with ownership

This commit is contained in:
2024-05-26 21:32:44 +02:00
parent 2420af464f
commit f2f001b190
7 changed files with 72 additions and 14 deletions

View File

@@ -5,6 +5,29 @@
#include <vector>
#include <variant>
// TODO: move to/next-to span
// a span that can also represent its ownership
template<typename T>
struct SpanWithOwnership : public Span<T> {
// TODO: allow for different owners
std::vector<T> _data_owner;
// self interactions
SpanWithOwnership(void) = delete;
constexpr SpanWithOwnership(const SpanWithOwnership&) = delete;
constexpr SpanWithOwnership(SpanWithOwnership&&) = default;
// non-owning
constexpr SpanWithOwnership(const Span<T>& span) : Span<T>(span) {}
constexpr SpanWithOwnership(const Span<T>&& span) : Span<T>(span) {}
// owning
constexpr SpanWithOwnership(std::vector<T>&& data) : Span<T>(data), _data_owner(std::move(data)) {}
bool isOwning(void) const { return !_data_owner.empty(); }
};
using ByteSpanWithOwnership = SpanWithOwnership<uint8_t>;
struct File2I {
// read only
uint64_t _file_size {0};
@@ -20,6 +43,7 @@ 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;
[[nodiscard]] 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;
[[nodiscard]] virtual ByteSpanWithOwnership read(uint64_t size, int64_t pos = -1) = 0;
};