aboutsummaryrefslogtreecommitdiff
path: root/include/cru/base/io/Stream.h
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2026-03-07 20:42:37 +0800
committerYuqian Yang <crupest@crupest.life>2026-03-07 20:42:37 +0800
commit38756822825e20eca3b9e01b735946175223d692 (patch)
treefc2a495bfc0e082d5ed9a1642278ae6467fe2742 /include/cru/base/io/Stream.h
parent924f4b472712d0cfc55b81dcb3eaed3f8a478288 (diff)
downloadcru-38756822825e20eca3b9e01b735946175223d692.tar.gz
cru-38756822825e20eca3b9e01b735946175223d692.tar.bz2
cru-38756822825e20eca3b9e01b735946175223d692.zip
Refactor stream.
Diffstat (limited to 'include/cru/base/io/Stream.h')
-rw-r--r--include/cru/base/io/Stream.h76
1 files changed, 35 insertions, 41 deletions
diff --git a/include/cru/base/io/Stream.h b/include/cru/base/io/Stream.h
index 8c0d3669..f497d725 100644
--- a/include/cru/base/io/Stream.h
+++ b/include/cru/base/io/Stream.h
@@ -1,46 +1,51 @@
#pragma once
#include "../Base.h"
-#include "../Buffer.h"
+#include "../Guard.h" // IWYU pragma: keep
+#include <atomic>
#include <cstddef>
namespace cru::io {
-class CRU_BASE_API StreamOperationNotSupportedException : public Exception {
- public:
- explicit StreamOperationNotSupportedException(std::string operation);
+class Stream;
+class CRU_BASE_API StreamException : public Exception {
public:
- std::string GetOperation() const { return operation_; }
+ explicit StreamException(Stream* stream, std::string message = "",
+ std::shared_ptr<std::exception> inner = nullptr);
- public:
- static void CheckSeek(bool seekable);
- static void CheckRead(bool readable);
- static void CheckWrite(bool writable);
+ Stream* GetStream() const { return stream_; }
private:
- std::string operation_;
+ Stream* stream_;
};
-class CRU_BASE_API StreamClosedException : public Exception {
+class CRU_BASE_API StreamOperationNotSupportedException
+ : public StreamException {
public:
- StreamClosedException();
+ explicit StreamOperationNotSupportedException(Stream* stream,
+ std::string operation);
- CRU_DEFAULT_DESTRUCTOR(StreamClosedException)
+ public:
+ std::string GetOperation() const { return operation_; }
+
+ public:
+ static void CheckSeek(Stream* stream, bool seekable);
+ static void CheckRead(Stream* stream, bool readable);
+ static void CheckWrite(Stream* stream, bool writable);
- static void Check(bool closed);
+ private:
+ std::string operation_;
};
-#define CRU_STREAM_IMPLEMENT_CLOSE_BY_DO_CLOSE \
- void Close() override { DoClose(); }
+class CRU_BASE_API StreamClosedException : public StreamException {
+ using StreamException::StreamException;
+};
-#define CRU_STREAM_BEGIN_CLOSE \
- if (GetClosed()) return; \
- CloseGuard close_guard(this);
+class CRU_BASE_API StreamIOException : public StreamException {
+ using StreamException::StreamException;
+};
-/**
- * All stream is thread-unsafe by default unless being documented.
- */
class CRU_BASE_API Stream : public Object {
protected:
struct SupportedOperations {
@@ -49,12 +54,6 @@ class CRU_BASE_API Stream : public Object {
std::optional<bool> can_write;
};
- struct CloseGuard {
- explicit CloseGuard(Stream* stream) : stream(stream) {}
- ~CloseGuard() { stream->SetClosed(true); }
- Stream* stream;
- };
-
protected:
explicit Stream(SupportedOperations supported_operations = {});
Stream(std::optional<bool> can_seek, std::optional<bool> can_read,
@@ -62,8 +61,7 @@ class CRU_BASE_API Stream : public Object {
public:
enum class SeekOrigin { Current, Begin, End };
-
- ~Stream() override = default;
+ constexpr static Index kEOF = -1;
public:
bool CanSeek();
@@ -85,11 +83,11 @@ class CRU_BASE_API Stream : public Object {
Index Write(const char* buffer, Index size);
void Flush();
- virtual void Close() = 0;
- virtual Buffer ReadToEnd(Index grow_size = 256);
+ bool IsClosed();
+ bool Close();
- // Utf8 encoding.
+ virtual std::vector<std::byte> ReadToEnd(Index grow_size = 256);
std::string ReadToEndAsUtf8String();
protected:
@@ -103,17 +101,13 @@ class CRU_BASE_API Stream : public Object {
virtual Index DoRead(std::byte* buffer, Index offset, Index size);
virtual Index DoWrite(const std::byte* buffer, Index offset, Index size);
virtual void DoFlush();
+ virtual void DoClose();
- void SetSupportedOperations(SupportedOperations supported_operations) {
- supported_operations_ = std::move(supported_operations);
- }
-
- bool GetClosed() { return closed_; }
- void SetClosed(bool closed) { closed_ = closed; }
- void CheckClosed() { StreamClosedException::Check(closed_); }
+ void SetSupportedOperations(SupportedOperations supported_operations);
+ void CheckClosed();
private:
SupportedOperations supported_operations_;
- bool closed_;
+ std::atomic_bool closed_;
};
} // namespace cru::io