Core/Stream: Add MemoryMapped stream options (allowing for direct memory access)
This commit is contained in:
@@ -228,6 +228,7 @@ namespace Nz
|
||||
{
|
||||
None,
|
||||
|
||||
MemoryMapped,
|
||||
Sequential,
|
||||
Text,
|
||||
Unbuffered,
|
||||
|
||||
@@ -37,6 +37,7 @@ namespace Nz
|
||||
|
||||
private:
|
||||
void FlushStream() override;
|
||||
void* GetMemoryMappedPointer() const override;
|
||||
std::size_t ReadBlock(void* buffer, std::size_t size) override;
|
||||
bool SeekStreamCursor(UInt64 offset) override;
|
||||
UInt64 TellStreamCursor() const override;
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace Nz
|
||||
* \brief Constructs a MemoryStream object by default
|
||||
*/
|
||||
inline MemoryStream::MemoryStream() :
|
||||
Stream(StreamOption::None, OpenMode_ReadWrite),
|
||||
Stream(StreamOption::MemoryMapped, OpenMode_ReadWrite),
|
||||
m_pos(0)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -18,20 +18,21 @@ namespace Nz
|
||||
MemoryView(void* ptr, UInt64 size);
|
||||
MemoryView(const void* ptr, UInt64 size);
|
||||
MemoryView(const MemoryView&) = delete;
|
||||
MemoryView(MemoryView&&) = delete; ///TODO
|
||||
MemoryView(MemoryView&&) noexcept = default;
|
||||
~MemoryView() = default;
|
||||
|
||||
UInt64 GetSize() const override;
|
||||
|
||||
MemoryView& operator=(const MemoryView&) = delete;
|
||||
MemoryView& operator=(MemoryView&&) = delete; ///TODO
|
||||
MemoryView& operator=(MemoryView&&) noexcept = default;
|
||||
|
||||
private:
|
||||
void FlushStream() override;
|
||||
void* GetMemoryMappedPointer() const override;
|
||||
std::size_t ReadBlock(void* buffer, std::size_t size) override;
|
||||
bool SeekStreamCursor(UInt64 offset) override;
|
||||
UInt64 TellStreamCursor() const override;
|
||||
bool TestStreamEnd() const override;
|
||||
UInt64 TellStreamCursor() const override;
|
||||
std::size_t WriteBlock(const void* buffer, std::size_t size) override;
|
||||
|
||||
UInt8* m_ptr;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class AbstractHash;
|
||||
class ByteArray;
|
||||
|
||||
class NAZARA_CORE_API Stream
|
||||
@@ -33,18 +34,20 @@ namespace Nz
|
||||
|
||||
inline void Flush();
|
||||
|
||||
virtual std::filesystem::path GetDirectory() const;
|
||||
virtual std::filesystem::path GetPath() const;
|
||||
inline OpenModeFlags GetOpenMode() const;
|
||||
inline StreamOptionFlags GetStreamOptions() const;
|
||||
|
||||
UInt64 GetCursorPos() const;
|
||||
virtual std::filesystem::path GetDirectory() const;
|
||||
inline const void* GetMappedPointer() const;
|
||||
inline void* GetMappedPointerMutable();
|
||||
inline OpenModeFlags GetOpenMode() const;
|
||||
virtual std::filesystem::path GetPath() const;
|
||||
virtual UInt64 GetSize() const = 0;
|
||||
inline StreamOptionFlags GetStreamOptions() const;
|
||||
|
||||
std::size_t Read(void* buffer, std::size_t size);
|
||||
virtual std::string ReadLine(unsigned int lineSize = 0);
|
||||
|
||||
inline bool IsBufferingEnabled() const;
|
||||
inline bool IsMemoryMapped() const;
|
||||
inline bool IsReadable() const;
|
||||
inline bool IsSequential() const;
|
||||
inline bool IsTextModeEnabled() const;
|
||||
@@ -65,6 +68,7 @@ namespace Nz
|
||||
inline Stream(StreamOptionFlags streamOptions = StreamOption::None, OpenModeFlags openMode = OpenMode::NotOpen);
|
||||
|
||||
virtual void FlushStream() = 0;
|
||||
virtual void* GetMemoryMappedPointer() const;
|
||||
virtual std::size_t ReadBlock(void* buffer, std::size_t size) = 0;
|
||||
virtual bool SeekStreamCursor(UInt64 offset) = 0;
|
||||
virtual UInt64 TellStreamCursor() const = 0;
|
||||
|
||||
@@ -16,7 +16,6 @@ namespace Nz
|
||||
* \param streamOptions Options for the stream
|
||||
* \param openMode Reading/writing mode for the stream
|
||||
*/
|
||||
|
||||
inline Stream::Stream(StreamOptionFlags streamOptions, OpenModeFlags openMode) :
|
||||
m_bufferCapacity(0),
|
||||
m_bufferOffset(0),
|
||||
@@ -32,12 +31,11 @@ namespace Nz
|
||||
*
|
||||
* \param textMode Enables the mode or disables
|
||||
*/
|
||||
|
||||
inline void Stream::EnableBuffering(bool buffering, std::size_t bufferSize)
|
||||
{
|
||||
if (buffering)
|
||||
{
|
||||
m_streamOptions &= ~StreamOption::Unbuffered;
|
||||
m_streamOptions.Clear(StreamOption::Unbuffered);
|
||||
if (m_bufferCapacity != bufferSize)
|
||||
{
|
||||
m_buffer = std::make_unique<UInt8[]>(bufferSize);
|
||||
@@ -49,7 +47,7 @@ namespace Nz
|
||||
}
|
||||
else
|
||||
{
|
||||
m_streamOptions |= StreamOption::Unbuffered;
|
||||
m_streamOptions.Set(StreamOption::Unbuffered);
|
||||
m_buffer.reset();
|
||||
m_bufferCapacity = 0;
|
||||
}
|
||||
@@ -58,9 +56,9 @@ namespace Nz
|
||||
inline void Stream::EnableTextMode(bool textMode)
|
||||
{
|
||||
if (textMode)
|
||||
m_streamOptions |= StreamOption::Text;
|
||||
m_streamOptions.Set(StreamOption::Text);
|
||||
else
|
||||
m_streamOptions &= ~StreamOption::Text;
|
||||
m_streamOptions.Clear(StreamOption::Text);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -76,11 +74,24 @@ namespace Nz
|
||||
FlushStream();
|
||||
}
|
||||
|
||||
inline const void* Stream::GetMappedPointer() const
|
||||
{
|
||||
NazaraAssert(IsMemoryMapped(), "Stream is not memory-mapped");
|
||||
NazaraAssert(IsReadable(), "Stream is not readable");
|
||||
return GetMemoryMappedPointer();
|
||||
}
|
||||
|
||||
inline void* Stream::GetMappedPointerMutable()
|
||||
{
|
||||
NazaraAssert(IsMemoryMapped(), "Stream is not memory-mapped");
|
||||
NazaraAssert(IsWritable(), "Stream is not writable");
|
||||
return GetMemoryMappedPointer();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the open mode of the stream
|
||||
* \return Reading/writing mode for the stream
|
||||
*/
|
||||
|
||||
inline OpenModeFlags Stream::GetOpenMode() const
|
||||
{
|
||||
return m_openMode;
|
||||
@@ -90,7 +101,6 @@ namespace Nz
|
||||
* \brief Gets the options of the stream
|
||||
* \return Options of the stream
|
||||
*/
|
||||
|
||||
inline StreamOptionFlags Stream::GetStreamOptions() const
|
||||
{
|
||||
return m_streamOptions;
|
||||
@@ -100,45 +110,46 @@ namespace Nz
|
||||
* \brief Checks whether the stream is readable
|
||||
* \return true if it is the case
|
||||
*/
|
||||
|
||||
inline bool Stream::IsBufferingEnabled() const
|
||||
{
|
||||
return (m_streamOptions & StreamOption::Unbuffered) == 0;
|
||||
return m_streamOptions.Test(StreamOption::Unbuffered);
|
||||
}
|
||||
|
||||
inline bool Stream::IsMemoryMapped() const
|
||||
{
|
||||
return m_streamOptions.Test(StreamOption::MemoryMapped);
|
||||
}
|
||||
|
||||
inline bool Stream::IsReadable() const
|
||||
{
|
||||
return (m_openMode & OpenMode::ReadOnly) != 0;
|
||||
return m_openMode.Test(OpenMode::ReadOnly);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether the stream is sequential
|
||||
* \return true if it is the case
|
||||
*/
|
||||
|
||||
inline bool Stream::IsSequential() const
|
||||
{
|
||||
return (m_streamOptions & StreamOption::Sequential) != 0;
|
||||
return m_streamOptions.Test(StreamOption::Sequential);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether the stream has text mode enabled
|
||||
* \return true if it is the case
|
||||
*/
|
||||
|
||||
inline bool Stream::IsTextModeEnabled() const
|
||||
{
|
||||
return (m_streamOptions & StreamOption::Text) != 0;
|
||||
return m_streamOptions.Test(StreamOption::Text);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether the stream can be written
|
||||
* \return true if it is the case
|
||||
*/
|
||||
|
||||
inline bool Stream::IsWritable() const
|
||||
{
|
||||
return (m_openMode & OpenMode::WriteOnly) != 0;
|
||||
return m_openMode.Test(OpenMode::WriteOnly);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
Reference in New Issue
Block a user