aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-01-13 18:25:45 +0800
committercrupest <crupest@outlook.com>2022-01-13 18:25:45 +0800
commitd4d9c6810954010e5de23aba19244f59f2139e41 (patch)
treed2e3277cc7e1f352f1aa5f865b7c7450b9fe4247
parent276ae73fa444c16f34a379ae9d8f58c883056b4a (diff)
downloadcru-d4d9c6810954010e5de23aba19244f59f2139e41.tar.gz
cru-d4d9c6810954010e5de23aba19244f59f2139e41.tar.bz2
cru-d4d9c6810954010e5de23aba19244f59f2139e41.zip
...
-rw-r--r--include/cru/common/io/MemoryStream.hpp41
-rw-r--r--include/cru/common/io/Stream.hpp36
-rw-r--r--src/common/CMakeLists.txt5
-rw-r--r--src/common/io/MemoryStream.cpp54
-rw-r--r--src/common/io/Stream.cpp21
5 files changed, 157 insertions, 0 deletions
diff --git a/include/cru/common/io/MemoryStream.hpp b/include/cru/common/io/MemoryStream.hpp
new file mode 100644
index 00000000..96eea226
--- /dev/null
+++ b/include/cru/common/io/MemoryStream.hpp
@@ -0,0 +1,41 @@
+#pragma once
+
+#include "Stream.hpp"
+
+namespace cru::io {
+class CRU_BASE_API MemoryStream : public Stream {
+ public:
+ MemoryStream() = default;
+ MemoryStream(std::byte* buffer, Index size, bool read_only = false,
+ bool auto_release = false)
+ : buffer_(buffer),
+ size_(size),
+ read_only_(read_only),
+ auto_release_(auto_release) {}
+
+ CRU_DELETE_COPY(MemoryStream)
+ CRU_DELETE_MOVE(MemoryStream)
+
+ ~MemoryStream() override;
+
+ public:
+ bool CanSeek() override;
+ Index Tell() override;
+ void Seek(Index offset, SeekOrigin origin = SeekOrigin::Current) override;
+
+ bool CanRead() override;
+ Index Read(std::byte* buffer, Index offset, Index size) override;
+
+ bool CanWrite() override;
+ Index Write(const std::byte* buffer, Index offset, Index size) override;
+
+ void Flush() override;
+
+ private:
+ std::byte* buffer_ = nullptr;
+ Index size_ = 0;
+ Index position_ = 0;
+ bool auto_release_ = false;
+ bool read_only_ = false;
+};
+} // namespace cru::io
diff --git a/include/cru/common/io/Stream.hpp b/include/cru/common/io/Stream.hpp
new file mode 100644
index 00000000..e13e5388
--- /dev/null
+++ b/include/cru/common/io/Stream.hpp
@@ -0,0 +1,36 @@
+#pragma once
+
+#include "../Base.hpp"
+
+#include <cstddef>
+
+namespace cru::io {
+class CRU_BASE_API Stream : public Object {
+ public:
+ enum class SeekOrigin { Current, Begin, End };
+
+ Stream() = default;
+
+ CRU_DELETE_COPY(Stream)
+ CRU_DELETE_MOVE(Stream)
+
+ ~Stream() override = default;
+
+ public:
+ virtual bool CanSeek() = 0;
+ virtual Index Tell() = 0;
+ virtual void Seek(Index offset, SeekOrigin origin = SeekOrigin::Current) = 0;
+ virtual void Rewind();
+ virtual Index GetSize();
+
+ virtual bool CanRead() = 0;
+ virtual Index Read(std::byte* buffer, Index offset, Index size) = 0;
+ virtual Index Read(std::byte* buffer, Index size);
+
+ virtual bool CanWrite() = 0;
+ virtual Index Write(const std::byte* buffer, Index offset, Index size) = 0;
+ virtual Index Write(const std::byte* buffer, Index size);
+
+ virtual void Flush() = 0;
+};
+} // namespace cru::io
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 5cc60690..b91b380c 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -4,6 +4,8 @@ add_library(cru_base SHARED
PropertyTree.cpp
String.cpp
StringUtil.cpp
+ io/Stream.cpp
+ io/MemoryStream.cpp
)
target_sources(cru_base PUBLIC
${CRU_BASE_INCLUDE_DIR}/Base.hpp
@@ -12,10 +14,13 @@ target_sources(cru_base PUBLIC
${CRU_BASE_INCLUDE_DIR}/Exception.hpp
${CRU_BASE_INCLUDE_DIR}/Logger.hpp
${CRU_BASE_INCLUDE_DIR}/PreConfig.hpp
+ ${CRU_BASE_INCLUDE_DIR}/PropertyTree.hpp
${CRU_BASE_INCLUDE_DIR}/Range.hpp
${CRU_BASE_INCLUDE_DIR}/SelfResolvable.hpp
${CRU_BASE_INCLUDE_DIR}/String.hpp
${CRU_BASE_INCLUDE_DIR}/StringUtil.hpp
+ ${CRU_BASE_INCLUDE_DIR}/io/Stream.hpp
+ ${CRU_BASE_INCLUDE_DIR}/io/MemoryStream.hpp
)
target_include_directories(cru_base PUBLIC ${CRU_INCLUDE_DIR})
target_compile_definitions(cru_base PUBLIC $<$<CONFIG:Debug>:CRU_DEBUG>)
diff --git a/src/common/io/MemoryStream.cpp b/src/common/io/MemoryStream.cpp
new file mode 100644
index 00000000..e3c9c605
--- /dev/null
+++ b/src/common/io/MemoryStream.cpp
@@ -0,0 +1,54 @@
+#include "cru/common/io/MemoryStream.hpp"
+
+namespace cru::io {
+bool MemoryStream::CanSeek() { return true; }
+
+Index MemoryStream::Tell() { return position_; }
+
+void MemoryStream::Seek(Index offset, SeekOrigin origin) {
+ switch (origin) {
+ case SeekOrigin::Current:
+ position_ += offset;
+ break;
+ case SeekOrigin::Begin:
+ position_ = offset;
+ break;
+ case SeekOrigin::End:
+ position_ = size_ + offset;
+ break;
+ }
+}
+
+bool MemoryStream::CanRead() { return true; }
+
+Index MemoryStream::Read(std::byte *buffer, Index offset, Index size) {
+ if (position_ + size > size_) {
+ size = size_ - position_;
+ }
+ if (size <= 0) {
+ return 0;
+ }
+ std::memcpy(buffer + offset, buffer_ + position_, size);
+ position_ += size;
+ return size;
+}
+
+bool MemoryStream::CanWrite() { return !read_only_; }
+
+Index MemoryStream::Write(const std::byte *buffer, Index offset, Index size) {
+ if (read_only_) {
+ return 0;
+ }
+ if (position_ + size > size_) {
+ size = size_ - position_;
+ }
+ if (size <= 0) {
+ return 0;
+ }
+ std::memcpy(buffer_ + position_, buffer + offset, size);
+ position_ += size;
+ return size;
+}
+
+void MemoryStream::Flush() {}
+} // namespace cru::io
diff --git a/src/common/io/Stream.cpp b/src/common/io/Stream.cpp
new file mode 100644
index 00000000..6addfdc0
--- /dev/null
+++ b/src/common/io/Stream.cpp
@@ -0,0 +1,21 @@
+#include "cru/common/io/Stream.hpp"
+
+namespace cru::io {
+void Stream::Rewind() { Seek(0); }
+
+Index Stream::GetSize() {
+ Index current_position = Tell();
+ Seek(0, SeekOrigin::End);
+ Index size = Tell();
+ Seek(current_position);
+ return size;
+}
+
+Index Stream::Read(std::byte* buffer, Index size) {
+ return Read(buffer, 0, size);
+}
+
+Index Stream::Write(const std::byte* buffer, Index size) {
+ return Write(buffer, 0, size);
+}
+} // namespace cru::io