From 8371ce068f0109c05717c4087741523f89e3e096 Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 17 Nov 2015 13:19:44 +0100 Subject: [PATCH] Core: Update Stream interface Add Open Mode to Stream level, Add IsReadable() and IsWritable() Former-commit-id: 0da5fa798c0f3bd3bf1545cb57f6bc923b222de8 --- include/Nazara/Core/File.hpp | 6 +- include/Nazara/Core/InputStream.hpp | 5 ++ include/Nazara/Core/InputStream.inl | 11 +++ include/Nazara/Core/OutputStream.hpp | 5 ++ include/Nazara/Core/OutputStream.inl | 11 +++ include/Nazara/Core/Stream.hpp | 16 +++-- include/Nazara/Core/Stream.inl | 37 ++++++++++ src/Nazara/Core/File.cpp | 103 ++++++--------------------- src/Nazara/Core/MemoryStream.cpp | 1 + src/Nazara/Core/Posix/FileImpl.cpp | 5 +- src/Nazara/Core/Posix/FileImpl.hpp | 2 +- src/Nazara/Core/Stream.cpp | 10 --- src/Nazara/Core/Win32/FileImpl.cpp | 2 +- src/Nazara/Core/Win32/FileImpl.hpp | 2 +- 14 files changed, 114 insertions(+), 102 deletions(-) create mode 100644 include/Nazara/Core/InputStream.inl create mode 100644 include/Nazara/Core/OutputStream.inl create mode 100644 include/Nazara/Core/Stream.inl diff --git a/include/Nazara/Core/File.hpp b/include/Nazara/Core/File.hpp index a01433be2..d6a819c9f 100644 --- a/include/Nazara/Core/File.hpp +++ b/include/Nazara/Core/File.hpp @@ -32,7 +32,7 @@ namespace Nz public: File(); File(const String& filePath); - File(const String& filePath, unsigned int openMode); + File(const String& filePath, UInt32 openMode); File(const File&) = delete; File(File&& file) noexcept; ~File(); @@ -71,7 +71,7 @@ namespace Nz bool SetCursorPos(UInt64 offset) override; void SetEndianness(Endianness endianness); bool SetFile(const String& filePath); - bool SetOpenMode(unsigned int openMode); + bool SetOpenMode(UInt32 openMode); using OutputStream::Write; std::size_t Write(const void* buffer, std::size_t size) override; @@ -102,8 +102,8 @@ namespace Nz Endianness m_endianness; String m_filePath; + UInt32 m_openMode; FileImpl* m_impl; - unsigned int m_openMode; }; template<> diff --git a/include/Nazara/Core/InputStream.hpp b/include/Nazara/Core/InputStream.hpp index 17539aaa1..ec67be839 100644 --- a/include/Nazara/Core/InputStream.hpp +++ b/include/Nazara/Core/InputStream.hpp @@ -23,7 +23,12 @@ namespace Nz virtual std::size_t Read(void* buffer, std::size_t size) = 0; virtual String ReadLine(unsigned int lineSize = 0); + + protected: + inline InputStream(); }; } +#include + #endif // NAZARA_INPUTSTREAM_HPP diff --git a/include/Nazara/Core/InputStream.inl b/include/Nazara/Core/InputStream.inl new file mode 100644 index 000000000..35912da00 --- /dev/null +++ b/include/Nazara/Core/InputStream.inl @@ -0,0 +1,11 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +namespace Nz +{ + inline InputStream::InputStream() : + Stream(OpenMode_Current) + { + } +} diff --git a/include/Nazara/Core/OutputStream.hpp b/include/Nazara/Core/OutputStream.hpp index 70d7ef40c..363270ea5 100644 --- a/include/Nazara/Core/OutputStream.hpp +++ b/include/Nazara/Core/OutputStream.hpp @@ -22,7 +22,12 @@ namespace Nz bool Write(const ByteArray& byteArray); bool Write(const String& string); virtual std::size_t Write(const void* buffer, std::size_t size) = 0; + + protected: + inline OutputStream(); }; } +#include + #endif // NAZARA_OUTPUTSTREAM_HPP diff --git a/include/Nazara/Core/OutputStream.inl b/include/Nazara/Core/OutputStream.inl new file mode 100644 index 000000000..51766a889 --- /dev/null +++ b/include/Nazara/Core/OutputStream.inl @@ -0,0 +1,11 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +namespace Nz +{ + inline OutputStream::OutputStream() : + Stream(OpenMode_Current) + { + } +} diff --git a/include/Nazara/Core/Stream.hpp b/include/Nazara/Core/Stream.hpp index 11154cb4d..a00c09661 100644 --- a/include/Nazara/Core/Stream.hpp +++ b/include/Nazara/Core/Stream.hpp @@ -16,7 +16,6 @@ namespace Nz class NAZARA_CORE_API Stream { public: - Stream() = default; Stream(const Stream&) = default; Stream(Stream&&) = default; virtual ~Stream(); @@ -24,17 +23,26 @@ namespace Nz virtual UInt64 GetCursorPos() const = 0; virtual String GetDirectory() const; virtual String GetPath() const; - unsigned int GetStreamOptions() const; + inline UInt32 GetOpenMode() const; + inline UInt32 GetStreamOptions() const; + + inline bool IsReadable() const; + inline bool IsWritable() const; virtual bool SetCursorPos(UInt64 offset) = 0; - void SetStreamOptions(unsigned int options); + void SetStreamOptions(UInt32 options); Stream& operator=(const Stream&) = default; Stream& operator=(Stream&&) = default; protected: - unsigned int m_streamOptions = 0; + inline Stream(UInt32 openMode); + + UInt32 m_openMode; + UInt32 m_streamOptions; }; } +#include + #endif // NAZARA_STREAM_HPP diff --git a/include/Nazara/Core/Stream.inl b/include/Nazara/Core/Stream.inl new file mode 100644 index 000000000..905c9276e --- /dev/null +++ b/include/Nazara/Core/Stream.inl @@ -0,0 +1,37 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +namespace Nz +{ + inline Stream::Stream(UInt32 openMode) : + m_openMode(openMode), + m_streamOptions(0) + { + } + + inline UInt32 Stream::GetOpenMode() const + { + return m_openMode; + } + + inline UInt32 Stream::GetStreamOptions() const + { + return m_streamOptions; + } + + inline bool Stream::IsReadable() const + { + return m_openMode & OpenMode_ReadOnly || m_openMode & OpenMode_ReadWrite; + } + + inline bool Stream::IsWritable() const + { + return m_openMode & OpenMode_ReadWrite || m_openMode & OpenMode_WriteOnly; + } + + inline void Stream::SetStreamOptions(UInt32 options) + { + m_streamOptions = options; + } +} diff --git a/src/Nazara/Core/File.cpp b/src/Nazara/Core/File.cpp index 977b0353f..8e3841866 100644 --- a/src/Nazara/Core/File.cpp +++ b/src/Nazara/Core/File.cpp @@ -31,29 +31,28 @@ namespace Nz { File::File() : + Stream(OpenMode_Current), m_endianness(Endianness_Unknown), - m_impl(nullptr), - m_openMode(OpenMode_Current) + m_impl(nullptr) { } File::File(const String& filePath) : - m_endianness(Endianness_Unknown), - m_impl(nullptr), - m_openMode(OpenMode_Current) + File() { SetFile(filePath); } - File::File(const String& filePath, unsigned int openMode) : - m_endianness(Endianness_Unknown), - m_impl(nullptr), - m_openMode(openMode) + File::File(const String& filePath, UInt32 openMode) : + File() { Open(filePath, openMode); } File::File(File&& file) noexcept : + Stream(std::move(file)), + InputStream(std::move(file)), + OutputStream(std::move(file)), m_endianness(file.m_endianness), m_filePath(std::move(file.m_filePath)), m_impl(file.m_impl), @@ -127,19 +126,8 @@ namespace Nz { NazaraLock(m_mutex) - #if NAZARA_CORE_SAFE - if (!IsOpen()) - { - NazaraError("File not opened"); - return; - } - - if ((m_openMode & OpenMode_ReadWrite) == 0 && (m_openMode & OpenMode_WriteOnly) == 0) - { - NazaraError("Cannot flush file without write access"); - return; - } - #endif + NazaraAssert(IsOpen(), "File is not open"); + NazaraAssert(IsWritable(), "File not opened with write access"); m_impl->Flush(); } @@ -155,13 +143,7 @@ namespace Nz { NazaraLock(m_mutex) - #if NAZARA_CORE_SAFE - if (!IsOpen()) - { - NazaraError("File not opened"); - return false; - } - #endif + NazaraAssert(IsOpen(), "File is not opened"); return m_impl->GetCursorPos(); } @@ -219,19 +201,8 @@ namespace Nz { NazaraLock(m_mutex) - #if NAZARA_CORE_SAFE - if (!IsOpen()) - { - NazaraError("File not opened"); - return 0; - } - - if ((m_openMode & OpenMode_ReadOnly) == 0 && (m_openMode & OpenMode_ReadWrite) == 0) - { - NazaraError("File not opened with read access"); - return 0; - } - #endif + NazaraAssert(IsOpen(), "File is not opened"); + NazaraAssert(IsReadable(), "File not opened with read access"); if (size == 0) return 0; @@ -245,7 +216,7 @@ namespace Nz m_impl->SetCursorPos(CursorPosition_AtCurrent, size); - return static_cast(m_impl->GetCursorPos()-currentPos); + return static_cast(m_impl->GetCursorPos() - currentPos); } } @@ -327,13 +298,7 @@ namespace Nz { NazaraLock(m_mutex) - #if NAZARA_CORE_SAFE - if (!IsOpen()) - { - NazaraError("File not opened"); - return false; - } - #endif + NazaraAssert(IsOpen(), "File is not opened"); return m_impl->SetCursorPos(pos, offset); } @@ -342,13 +307,7 @@ namespace Nz { NazaraLock(m_mutex) - #if NAZARA_CORE_SAFE - if (!IsOpen()) - { - NazaraError("File not opened"); - return false; - } - #endif + NazaraAssert(IsOpen(), "File is not opened"); return m_impl->SetCursorPos(CursorPosition_AtBegin, offset); } @@ -418,10 +377,11 @@ namespace Nz std::size_t File::Write(const void* buffer, std::size_t size) { - NazaraAssert(IsOpen(), "File is not open"); - NazaraAssert(m_openMode & OpenMode_ReadWrite || m_openMode & OpenMode_WriteOnly, "File not opened with write access"); NazaraLock(m_mutex) + NazaraAssert(IsOpen(), "File is not opened"); + NazaraAssert(IsWritable(), "File not opened with write access"); + if (!buffer || size == 0) return 0; @@ -430,26 +390,11 @@ namespace Nz std::size_t File::Write(const void* buffer, std::size_t typeSize, unsigned int count) { - NazaraLock(m_mutex) - - #if NAZARA_CORE_SAFE - if (!IsOpen()) - { - NazaraError("File not opened"); - return 0; - } - - if ((m_openMode & OpenMode_ReadWrite) == 0 && (m_openMode & OpenMode_WriteOnly) == 0) - { - NazaraError("File not opened with write access"); - return 0; - } - #endif - if (!buffer || count == 0 || typeSize == 0) return 0; - std::size_t bytesWritten; + NazaraLock(m_mutex) + if (m_endianness != Endianness_Unknown && m_endianness != GetPlatformEndianness() && typeSize != 1) { std::unique_ptr buf(new char[count*typeSize]); @@ -458,12 +403,10 @@ namespace Nz for (unsigned int i = 0; i < count; ++i) SwapBytes(&buf[i*typeSize], typeSize); - bytesWritten = m_impl->Write(buf.get(), count*typeSize); + return Write(buf.get(), count*typeSize); } else - bytesWritten = m_impl->Write(buffer, count*typeSize); - - return bytesWritten; + return Write(buffer, count*typeSize); } File& File::operator=(const String& filePath) diff --git a/src/Nazara/Core/MemoryStream.cpp b/src/Nazara/Core/MemoryStream.cpp index 2fb3ab3df..3c07d1fee 100644 --- a/src/Nazara/Core/MemoryStream.cpp +++ b/src/Nazara/Core/MemoryStream.cpp @@ -10,6 +10,7 @@ namespace Nz { MemoryStream::MemoryStream(const void* ptr, UInt64 size) : + Stream(OpenMode_ReadOnly), m_ptr(reinterpret_cast(ptr)), m_pos(0), m_size(size) diff --git a/src/Nazara/Core/Posix/FileImpl.cpp b/src/Nazara/Core/Posix/FileImpl.cpp index 9c1469817..d0d0bf7ee 100644 --- a/src/Nazara/Core/Posix/FileImpl.cpp +++ b/src/Nazara/Core/Posix/FileImpl.cpp @@ -18,7 +18,8 @@ namespace Nz void FileImpl::Close() { - close(m_fileDescriptor); + if (m_fileDescriptor != -1) + close(m_fileDescriptor); } bool FileImpl::EndOfFile() const @@ -48,7 +49,7 @@ namespace Nz return static_cast(position); } - bool FileImpl::Open(const String& filePath, unsigned int mode) + bool FileImpl::Open(const String& filePath, UInt32 mode) { int flags; mode_t permissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; diff --git a/src/Nazara/Core/Posix/FileImpl.hpp b/src/Nazara/Core/Posix/FileImpl.hpp index 6531dc70b..844bebd29 100644 --- a/src/Nazara/Core/Posix/FileImpl.hpp +++ b/src/Nazara/Core/Posix/FileImpl.hpp @@ -36,7 +36,7 @@ namespace Nz bool EndOfFile() const; void Flush(); UInt64 GetCursorPos() const; - bool Open(const String& filePath, unsigned int mode); + bool Open(const String& filePath, UInt32 mode); std::size_t Read(void* buffer, std::size_t size); bool SetCursorPos(CursorPosition pos, Int64 offset); std::size_t Write(const void* buffer, std::size_t size); diff --git a/src/Nazara/Core/Stream.cpp b/src/Nazara/Core/Stream.cpp index 9e43100bd..f3bdb8fe4 100644 --- a/src/Nazara/Core/Stream.cpp +++ b/src/Nazara/Core/Stream.cpp @@ -18,14 +18,4 @@ namespace Nz { return String(); } - - unsigned int Stream::GetStreamOptions() const - { - return m_streamOptions; - } - - void Stream::SetStreamOptions(unsigned int options) - { - m_streamOptions = options; - } } diff --git a/src/Nazara/Core/Win32/FileImpl.cpp b/src/Nazara/Core/Win32/FileImpl.cpp index 18f438ea5..113915c66 100644 --- a/src/Nazara/Core/Win32/FileImpl.cpp +++ b/src/Nazara/Core/Win32/FileImpl.cpp @@ -54,7 +54,7 @@ namespace Nz return position.QuadPart; } - bool FileImpl::Open(const String& filePath, unsigned int mode) + bool FileImpl::Open(const String& filePath, UInt32 mode) { DWORD access; DWORD shareMode = FILE_SHARE_READ; diff --git a/src/Nazara/Core/Win32/FileImpl.hpp b/src/Nazara/Core/Win32/FileImpl.hpp index 9bc484cc1..0f3745abc 100644 --- a/src/Nazara/Core/Win32/FileImpl.hpp +++ b/src/Nazara/Core/Win32/FileImpl.hpp @@ -29,7 +29,7 @@ namespace Nz bool EndOfFile() const; void Flush(); UInt64 GetCursorPos() const; - bool Open(const String& filePath, unsigned int mode); + bool Open(const String& filePath, UInt32 mode); std::size_t Read(void* buffer, std::size_t size); bool SetCursorPos(CursorPosition pos, Int64 offset); std::size_t Write(const void* buffer, std::size_t size);