aboutsummaryrefslogtreecommitdiff
path: root/zencore/include
diff options
context:
space:
mode:
Diffstat (limited to 'zencore/include')
-rw-r--r--zencore/include/zencore/stream.h149
-rw-r--r--zencore/include/zencore/streamutil.h69
2 files changed, 26 insertions, 192 deletions
diff --git a/zencore/include/zencore/stream.h b/zencore/include/zencore/stream.h
index bab349068..9d1a7628c 100644
--- a/zencore/include/zencore/stream.h
+++ b/zencore/include/zencore/stream.h
@@ -5,85 +5,49 @@
#include "zencore.h"
#include <zencore/memory.h>
-#include <zencore/refcount.h>
#include <zencore/thread.h>
-#include <string_view>
#include <vector>
namespace zen {
/**
- * Stream which writes into a growing memory buffer
- */
-class MemoryOutStream : public RefCounted
-{
-public:
- MemoryOutStream() = default;
- ~MemoryOutStream() = default;
-
- void Write(const void* DataPtr, size_t ByteCount, uint64_t Offset);
- void Flush();
- 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(); }
- inline uint64_t GetSize() const { return m_Buffer.size(); }
-
-private:
- RwLock m_Lock;
- std::vector<uint8_t> m_Buffer;
-};
-
-inline MemoryView
-MakeMemoryView(const MemoryOutStream& Stream)
-{
- return MemoryView(Stream.Data(), Stream.Size());
-}
-
-/**
* Binary stream writer
*/
class BinaryWriter
{
public:
- inline BinaryWriter(MemoryOutStream& Stream) : m_Stream(&Stream) {}
- ~BinaryWriter() = default;
+ inline BinaryWriter() = default;
+ ~BinaryWriter() = default;
inline void Write(const void* DataPtr, size_t ByteCount)
{
- m_Stream->Write(DataPtr, ByteCount, m_Offset);
+ Write(DataPtr, ByteCount, m_Offset);
m_Offset += ByteCount;
}
inline uint64_t CurrentOffset() const { return m_Offset; }
-private:
- RefPtr<MemoryOutStream> m_Stream;
- uint64_t m_Offset = 0;
-};
-
-/**
- * Stream which reads from a memory buffer
- */
-class MemoryInStream : public RefCounted
-{
-public:
- MemoryInStream(const void* Buffer, size_t Size);
- MemoryInStream(MemoryView View) : MemoryInStream(View.GetData(), View.GetSize()) {}
- ~MemoryInStream() = default;
-
- void Read(void* DataPtr, size_t ByteCount, uint64_t ReadOffset);
- uint64_t Size() const { return m_Buffer.size(); }
- uint64_t GetSize() const { return Size(); }
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(); }
+ inline uint64_t GetSize() const { return m_Buffer.size(); }
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 BinaryWriter& Stream)
+{
+ return MemoryView(Stream.Data(), Stream.Size());
+}
+
/**
* Binary stream reader
*/
@@ -91,90 +55,29 @@ private:
class BinaryReader
{
public:
- inline BinaryReader(MemoryInStream& Stream) : m_Stream(&Stream) {}
- ~BinaryReader() = default;
-
- inline void Read(void* DataPtr, size_t ByteCount)
- {
- m_Stream->Read(DataPtr, ByteCount, m_Offset);
- m_Offset += ByteCount;
- }
-
- void Seek(uint64_t Offset)
+ 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())
{
- 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<MemoryInStream> m_Stream;
- uint64_t m_Offset = 0;
+ const uint8_t* m_BufferBase;
+ uint64_t m_BufferSize;
+ 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;
-}
-
void stream_forcelink(); // internal
} // namespace zen
diff --git a/zencore/include/zencore/streamutil.h b/zencore/include/zencore/streamutil.h
deleted file mode 100644
index d9b6b5167..000000000
--- a/zencore/include/zencore/streamutil.h
+++ /dev/null
@@ -1,69 +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 {
-
-} // 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));
- }
-};