// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. #pragma once #include "ReadStream.h" #include "Engine/Platform/Platform.h" /// /// Direct memory reading stream that uses a single allocation buffer. /// class FLAXENGINE_API MemoryReadStream : public ReadStream { private: const byte* _buffer; const byte* _position; uint32 _length; public: /// /// Init (empty, cannot access before Init()) /// /// /// Initializes a new instance of the class. /// MemoryReadStream(); /// /// Initializes a new instance of the class. /// /// Bytes with data to read from it (no memory cloned, using input buffer). /// Amount of bytes MemoryReadStream(const byte* bytes, uint32 length); /// /// Initializes a new instance of the class. /// /// Array with data to read from. template MemoryReadStream(const Array& data) : MemoryReadStream(data.Get(), data.Count() * sizeof(T)) { } /// /// Initializes a new instance of the class. /// /// Span with data to read from. template MemoryReadStream(const Span& data) : MemoryReadStream(data.Get(), data.Count() * sizeof(T)) { } public: /// /// Init stream to the custom buffer location. /// /// Bytes with data to read from it (no memory cloned, using input buffer). /// Amount of bytes. void Init(const byte* bytes, uint32 length); /// /// Init stream to the custom buffer location. /// /// Array with data to read from. template FORCE_INLINE void Init(const Array& data) { Init(data.Get(), data.Count() * sizeof(T)); } /// /// Gets the current handle to position in buffer. /// const byte* GetPositionHandle() const { return _position; } public: /// /// Skips the data from the target buffer without reading from it. Moves the read pointer in the buffer forward. /// /// The amount of bytes to read. /// The pointer to the data in memory. void* Move(uint32 bytes) { ASSERT(GetLength() - GetPosition() >= bytes); const auto result = (void*)_position; _position += bytes; return result; } /// /// Skips the data from the target buffer without reading from it. Moves the read pointer in the buffer forward. /// /// The pointer to the data in memory. template FORCE_INLINE T* Move() { return static_cast(Move(sizeof(T))); } /// /// Skips the data from the target buffer without reading from it. Moves the read pointer in the buffer forward. /// /// The amount of items to read. /// The pointer to the data in memory. template FORCE_INLINE T* Move(uint32 count) { return static_cast(Move(sizeof(T) * count)); } public: // [ReadStream] void Flush() override; void Close() override; uint32 GetLength() override; uint32 GetPosition() override; void SetPosition(uint32 seek) override; void ReadBytes(void* data, uint32 bytes) override; };