// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#pragma once
#include "ReadStream.h"
#include "Engine/Platform/Platform.h"
///
/// Super fast advanced data reading from raw bytes without any overhead at all
///
class FLAXENGINE_API MemoryReadStream : public ReadStream
{
private:
const byte* _buffer;
const byte* _position;
uint32 _length;
public:
///
/// Init (empty, cannot access before Init())
///
MemoryReadStream();
///
/// Init
///
/// Bytes with data to read from it (no memory cloned, using input buffer)
/// Amount of bytes
MemoryReadStream(const byte* bytes, uint32 length);
///
/// Init
///
/// Array with data to read from
template
MemoryReadStream(const Array& 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.
///
/// The position of the buffer in memory.
const byte* GetPositionHandle() const
{
return _position;
}
public:
using ReadStream::Read;
///
/// Reads bytes without copying the data.
///
/// The amount of bytes to read.
/// The pointer to the data in memory.
void* Read(uint32 bytes)
{
ASSERT(GetLength() - GetPosition() >= bytes);
const auto result = (void*)_position;
_position += bytes;
return result;
}
///
/// Reads given data type from the stream.
///
/// The pointer to the data in memory.
template
FORCE_INLINE T* Read()
{
return static_cast(Read(sizeof(T)));
}
///
/// Reads array of given data type from the stream.
///
/// The amount of items to read.
/// The pointer to the data in memory.
template
FORCE_INLINE T* Read(uint32 count)
{
return static_cast(Read(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;
};