diff options
Diffstat (limited to 'zencore/include')
| -rw-r--r-- | zencore/include/zencore/stream.h | 293 | ||||
| -rw-r--r-- | zencore/include/zencore/streamutil.h | 118 |
2 files changed, 27 insertions, 384 deletions
diff --git a/zencore/include/zencore/stream.h b/zencore/include/zencore/stream.h index a0e165bdc..9d1a7628c 100644 --- a/zencore/include/zencore/stream.h +++ b/zencore/include/zencore/stream.h @@ -5,51 +5,30 @@ #include "zencore.h" #include <zencore/memory.h> -#include <zencore/refcount.h> #include <zencore/thread.h> -#include <string_view> #include <vector> namespace zen { /** - * Basic byte stream interface - * - * This is intended as a minimal base class offering only the absolute minimum of functionality. - * - * IMPORTANT: To better support concurrency, this abstraction offers no "file pointer". Thus - * every read or write operation needs to specify the offset from which they wish to read. - * - * Most client code will likely want to use reader/writer classes like BinaryWriter/BinaryReader - * + * Binary stream writer */ -class OutStream : public RefCounted -{ -public: - virtual void Write(const void* Data, size_t ByteCount, uint64_t Offset) = 0; - virtual void Flush() = 0; -}; -class InStream : public RefCounted +class BinaryWriter { public: - virtual void Read(void* DataPtr, size_t ByteCount, uint64_t Offset) = 0; - virtual uint64_t Size() const = 0; - uint64_t GetSize() const { return Size(); } -}; + inline BinaryWriter() = default; + ~BinaryWriter() = default; -/** - * Stream which writes into a growing memory buffer - */ -class MemoryOutStream : public OutStream -{ -public: - MemoryOutStream() = default; - ~MemoryOutStream() = default; + inline void Write(const void* DataPtr, size_t ByteCount) + { + Write(DataPtr, ByteCount, m_Offset); + m_Offset += ByteCount; + } + + inline uint64_t CurrentOffset() const { return m_Offset; } - virtual void Write(const void* DataPtr, size_t ByteCount, uint64_t Offset) override; - virtual void Flush() override; inline const uint8_t* Data() const { return m_Buffer.data(); } inline const uint8_t* GetData() const { return m_Buffer.data(); } inline uint64_t Size() const { return m_Buffer.size(); } @@ -58,263 +37,45 @@ public: private: RwLock m_Lock; std::vector<uint8_t> m_Buffer; + uint64_t m_Offset = 0; + + void Write(const void* DataPtr, size_t ByteCount, uint64_t Offset); }; inline MemoryView -MakeMemoryView(const MemoryOutStream& Stream) +MakeMemoryView(const BinaryWriter& Stream) { return MemoryView(Stream.Data(), Stream.Size()); } /** - * Stream which reads from a memory buffer - */ -class MemoryInStream : public InStream -{ -public: - MemoryInStream(const void* Buffer, size_t Size); - MemoryInStream(MemoryView View) : MemoryInStream(View.GetData(), View.GetSize()) {} - ~MemoryInStream() = default; - - virtual void Read(void* DataPtr, size_t ByteCount, uint64_t ReadOffset) override; - virtual uint64_t Size() const override { return m_Buffer.size(); } - inline const uint8_t* Data() const { return m_Buffer.data(); } - inline const uint8_t* GetData() const { return m_Buffer.data(); } - -private: - RwLock m_Lock; - std::vector<uint8_t> m_Buffer; -}; - -/** - * Binary stream writer - */ - -class BinaryWriter -{ -public: - inline BinaryWriter(OutStream& Stream) : m_Stream(&Stream) {} - ~BinaryWriter() = default; - - inline void Write(const void* DataPtr, size_t ByteCount) - { - m_Stream->Write(DataPtr, ByteCount, m_Offset); - m_Offset += ByteCount; - } - - uint64_t CurrentOffset() const { return m_Offset; } - -private: - RefPtr<OutStream> m_Stream; - uint64_t m_Offset = 0; -}; - -inline BinaryWriter& -operator<<(BinaryWriter& Writer, bool Value) -{ - Writer.Write(&Value, sizeof Value); - return Writer; -} -inline BinaryWriter& -operator<<(BinaryWriter& Writer, int8_t Value) -{ - Writer.Write(&Value, sizeof Value); - return Writer; -} -inline BinaryWriter& -operator<<(BinaryWriter& Writer, int16_t Value) -{ - Writer.Write(&Value, sizeof Value); - return Writer; -} -inline BinaryWriter& -operator<<(BinaryWriter& Writer, int32_t Value) -{ - Writer.Write(&Value, sizeof Value); - return Writer; -} -inline BinaryWriter& -operator<<(BinaryWriter& Writer, int64_t Value) -{ - Writer.Write(&Value, sizeof Value); - return Writer; -} -inline BinaryWriter& -operator<<(BinaryWriter& Writer, uint8_t Value) -{ - Writer.Write(&Value, sizeof Value); - return Writer; -} -inline BinaryWriter& -operator<<(BinaryWriter& Writer, uint16_t Value) -{ - Writer.Write(&Value, sizeof Value); - return Writer; -} -inline BinaryWriter& -operator<<(BinaryWriter& Writer, uint32_t Value) -{ - Writer.Write(&Value, sizeof Value); - return Writer; -} -inline BinaryWriter& -operator<<(BinaryWriter& Writer, uint64_t Value) -{ - Writer.Write(&Value, sizeof Value); - return Writer; -} - -/** * Binary stream reader */ class BinaryReader { public: - inline BinaryReader(InStream& Stream) : m_Stream(&Stream) {} - ~BinaryReader() = default; - - inline void Read(void* DataPtr, size_t ByteCount) + inline BinaryReader(const void* Buffer, uint64_t Size) : m_BufferBase(reinterpret_cast<const uint8_t*>(Buffer)), m_BufferSize(Size) {} + inline BinaryReader(MemoryView Buffer) + : m_BufferBase(reinterpret_cast<const uint8_t*>(Buffer.GetData())) + , m_BufferSize(Buffer.GetSize()) { - m_Stream->Read(DataPtr, ByteCount, m_Offset); - m_Offset += ByteCount; } - void Seek(uint64_t Offset) - { - ZEN_ASSERT(Offset <= m_Stream->Size()); - m_Offset = Offset; - } - - void Skip(uint64_t SkipOffset) + inline void Read(void* DataPtr, size_t ByteCount) { - ZEN_ASSERT((m_Offset + SkipOffset) <= m_Stream->Size()); - m_Offset += SkipOffset; + memcpy(DataPtr, m_BufferBase + m_Offset, ByteCount); + m_Offset += ByteCount; } + inline uint64_t Size() const { return m_BufferSize; } + inline uint64_t GetSize() const { return Size(); } inline uint64_t CurrentOffset() const { return m_Offset; } - inline uint64_t AvailableBytes() const { return m_Stream->Size() - m_Offset; } - -private: - RefPtr<InStream> m_Stream; - uint64_t m_Offset = 0; -}; - -inline BinaryReader& -operator>>(BinaryReader& Reader, bool& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, int8_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, int16_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, int32_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, int64_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, uint8_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, uint16_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, uint32_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} -inline BinaryReader& -operator>>(BinaryReader& Reader, uint64_t& Value) -{ - Reader.Read(&Value, sizeof Value); - return Reader; -} - -/** - * Text stream writer - */ - -class TextWriter -{ -public: - ZENCORE_API TextWriter(OutStream& Stream); - ZENCORE_API ~TextWriter(); - - ZENCORE_API virtual void Write(const void* DataPtr, size_t ByteCount); - ZENCORE_API void Writef(const char* FormatString, ...); - - inline uint64_t CurrentOffset() const { return m_CurrentOffset; } - -private: - RefPtr<OutStream> m_Stream; - uint64_t m_CurrentOffset = 0; -}; - -ZENCORE_API TextWriter& operator<<(TextWriter& Writer, const char* Value); -ZENCORE_API TextWriter& operator<<(TextWriter& Writer, const std::string_view& Value); -ZENCORE_API TextWriter& operator<<(TextWriter& Writer, bool Value); -ZENCORE_API TextWriter& operator<<(TextWriter& Writer, int8_t Value); -ZENCORE_API TextWriter& operator<<(TextWriter& Writer, int16_t Value); -ZENCORE_API TextWriter& operator<<(TextWriter& Writer, int32_t Value); -ZENCORE_API TextWriter& operator<<(TextWriter& Writer, int64_t Value); -ZENCORE_API TextWriter& operator<<(TextWriter& Writer, uint8_t Value); -ZENCORE_API TextWriter& operator<<(TextWriter& Writer, uint16_t Value); -ZENCORE_API TextWriter& operator<<(TextWriter& Writer, uint32_t Value); -ZENCORE_API TextWriter& operator<<(TextWriter& Writer, uint64_t Value); - -class IndentTextWriter : public TextWriter -{ -public: - ZENCORE_API IndentTextWriter(OutStream& stream); - ZENCORE_API ~IndentTextWriter(); - - ZENCORE_API virtual void Write(const void* DataPtr, size_t ByteCount) override; - - inline void Indent(int Amount) { m_IndentAmount += Amount; } - - struct Scope - { - Scope(IndentTextWriter& Outer, int IndentAmount = 2) : m_Outer(Outer), m_IndentAmount(IndentAmount) - { - m_Outer.Indent(IndentAmount); - } - - ~Scope() { m_Outer.Indent(-m_IndentAmount); } - - private: - IndentTextWriter& m_Outer; - int m_IndentAmount; - }; private: - int m_IndentAmount = 0; - int m_LineCursor = 0; - char m_LineBuffer[2048]; + const uint8_t* m_BufferBase; + uint64_t m_BufferSize; + uint64_t m_Offset = 0; }; void stream_forcelink(); // internal diff --git a/zencore/include/zencore/streamutil.h b/zencore/include/zencore/streamutil.h deleted file mode 100644 index 190cd18eb..000000000 --- a/zencore/include/zencore/streamutil.h +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - -#pragma once - -#include <fmt/format.h> -#include <zencore/string.h> -#include <string> -#include <string_view> - -#include "blake3.h" -#include "iohash.h" -#include "sha1.h" -#include "stream.h" - -namespace zen { - -ZENCORE_API BinaryWriter& operator<<(BinaryWriter& writer, const std::string_view& value); -ZENCORE_API BinaryReader& operator>>(BinaryReader& reader, std::string& value); - -ZENCORE_API BinaryWriter& operator<<(BinaryWriter& writer, const std::wstring_view& value); -ZENCORE_API BinaryReader& operator>>(BinaryReader& reader, std::wstring& value); -ZENCORE_API TextWriter& operator<<(TextWriter& writer, const std::wstring_view& value); - -inline BinaryWriter& -operator<<(BinaryWriter& writer, const SHA1& value) -{ - writer.Write(value.Hash, sizeof value.Hash); - return writer; -} -inline BinaryReader& -operator>>(BinaryReader& reader, SHA1& value) -{ - reader.Read(value.Hash, sizeof value.Hash); - return reader; -} -ZENCORE_API TextWriter& operator<<(TextWriter& writer, const zen::SHA1& value); - -inline BinaryWriter& -operator<<(BinaryWriter& writer, const BLAKE3& value) -{ - writer.Write(value.Hash, sizeof value.Hash); - return writer; -} -inline BinaryReader& -operator>>(BinaryReader& reader, BLAKE3& value) -{ - reader.Read(value.Hash, sizeof value.Hash); - return reader; -} -ZENCORE_API TextWriter& operator<<(TextWriter& writer, const BLAKE3& value); - -inline BinaryWriter& -operator<<(BinaryWriter& writer, const IoHash& value) -{ - writer.Write(value.Hash, sizeof value.Hash); - return writer; -} -inline BinaryReader& -operator>>(BinaryReader& reader, IoHash& value) -{ - reader.Read(value.Hash, sizeof value.Hash); - return reader; -} -ZENCORE_API TextWriter& operator<<(TextWriter& writer, const IoHash& value); - -} // namespace zen - -////////////////////////////////////////////////////////////////////////// - -template<> -struct fmt::formatter<zen::IoHash> -{ - constexpr auto parse(format_parse_context& ctx) - { - // Parse the presentation format and store it in the formatter: - auto it = ctx.begin(), end = ctx.end(); - - // Check if reached the end of the range: - if (it != end && *it != '}') - throw format_error("invalid format"); - - // Return an iterator past the end of the parsed range: - return it; - } - - template<typename FormatContext> - auto format(const zen::IoHash& h, FormatContext& ctx) - { - zen::ExtendableStringBuilder<48> String; - h.ToHexString(String); - return format_to(ctx.out(), std::string_view(String)); - } -}; - -template<> -struct fmt::formatter<zen::BLAKE3> -{ - constexpr auto parse(format_parse_context& ctx) - { - // Parse the presentation format and store it in the formatter: - auto it = ctx.begin(), end = ctx.end(); - - // Check if reached the end of the range: - if (it != end && *it != '}') - throw format_error("invalid format"); - - // Return an iterator past the end of the parsed range: - return it; - } - - template<typename FormatContext> - auto format(const zen::BLAKE3& h, FormatContext& ctx) - { - zen::ExtendableStringBuilder<80> String; - h.ToHexString(String); - return format_to(ctx.out(), std::string_view(String)); - } -}; |