From a34d1e410ca5804835af23da6c062ea989517014 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 27 Nov 2016 13:40:47 +0100 Subject: [PATCH] Core/Enum: Convert OpenMode and StreamOption to the new flags system --- SDK/src/NDK/Lua/LuaBinding_Core.cpp | 16 ++++----- include/Nazara/Core/ByteStream.hpp | 6 ++-- include/Nazara/Core/Enums.hpp | 52 +++++++++++++++++++--------- include/Nazara/Core/File.hpp | 6 ++-- include/Nazara/Core/MemoryStream.hpp | 6 ++-- include/Nazara/Core/MemoryStream.inl | 2 +- include/Nazara/Core/Stream.hpp | 10 +++--- include/Nazara/Core/Stream.inl | 10 +++--- include/Nazara/Network/NetPacket.hpp | 4 +-- src/Nazara/Core/ByteStream.cpp | 4 +-- src/Nazara/Core/File.cpp | 6 ++-- src/Nazara/Core/MemoryStream.cpp | 2 +- src/Nazara/Core/Posix/FileImpl.cpp | 2 +- src/Nazara/Core/Posix/FileImpl.hpp | 2 +- src/Nazara/Core/Win32/FileImpl.cpp | 6 ++-- src/Nazara/Core/Win32/FileImpl.hpp | 2 +- src/Nazara/Network/NetPacket.cpp | 2 +- 17 files changed, 78 insertions(+), 60 deletions(-) diff --git a/SDK/src/NDK/Lua/LuaBinding_Core.cpp b/SDK/src/NDK/Lua/LuaBinding_Core.cpp index 844dad442..e808c3804 100644 --- a/SDK/src/NDK/Lua/LuaBinding_Core.cpp +++ b/SDK/src/NDK/Lua/LuaBinding_Core.cpp @@ -337,16 +337,16 @@ namespace Ndk instance.SetGlobal("HashType"); // Nz::OpenMode - static_assert(Nz::OpenMode_Max + 1 == 2 * (64), "Nz::OpenModeFlags has been updated but change was not reflected to Lua binding"); - instance.PushTable(0, 8); + static_assert(Nz::OpenMode_Max + 1 == 8, "Nz::OpenModeFlags has been updated but change was not reflected to Lua binding"); + instance.PushTable(0, Nz::OpenMode_Max + 1); { - instance.PushField("Append", Nz::OpenMode_Append); - instance.PushField("NotOpen", Nz::OpenMode_NotOpen); - instance.PushField("Lock", Nz::OpenMode_Lock); - instance.PushField("ReadOnly", Nz::OpenMode_ReadOnly); + instance.PushField("Append", Nz::OpenMode_Append); + instance.PushField("NotOpen", Nz::OpenMode_NotOpen); + instance.PushField("Lock", Nz::OpenMode_Lock); + instance.PushField("ReadOnly", Nz::OpenMode_ReadOnly); instance.PushField("ReadWrite", Nz::OpenMode_ReadWrite); - instance.PushField("Text", Nz::OpenMode_Text); - instance.PushField("Truncate", Nz::OpenMode_Truncate); + instance.PushField("Text", Nz::OpenMode_Text); + instance.PushField("Truncate", Nz::OpenMode_Truncate); instance.PushField("WriteOnly", Nz::OpenMode_WriteOnly); } instance.SetGlobal("OpenMode"); diff --git a/include/Nazara/Core/ByteStream.hpp b/include/Nazara/Core/ByteStream.hpp index fe1516b06..fab7c8f4a 100644 --- a/include/Nazara/Core/ByteStream.hpp +++ b/include/Nazara/Core/ByteStream.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015 Jérôme Leclercq +// 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 @@ -19,7 +19,7 @@ namespace Nz { public: inline ByteStream(Stream* stream = nullptr); - ByteStream(ByteArray* byteArray, UInt32 openMode = OpenMode_ReadWrite); + ByteStream(ByteArray* byteArray, OpenModeFlags openMode = OpenMode_ReadWrite); ByteStream(void* ptr, Nz::UInt64 size); ByteStream(const void* ptr, Nz::UInt64 size); ByteStream(const ByteStream&) = delete; @@ -36,7 +36,7 @@ namespace Nz inline void SetDataEndianness(Endianness endiannes); inline void SetStream(Stream* stream); - void SetStream(ByteArray* byteArray, UInt32 openMode = OpenMode_ReadWrite); + void SetStream(ByteArray* byteArray, OpenModeFlags openMode = OpenMode_ReadWrite); void SetStream(void* ptr, Nz::UInt64 size); void SetStream(const void* ptr, Nz::UInt64 size); diff --git a/include/Nazara/Core/Enums.hpp b/include/Nazara/Core/Enums.hpp index 7a5c161ab..270815087 100644 --- a/include/Nazara/Core/Enums.hpp +++ b/include/Nazara/Core/Enums.hpp @@ -7,6 +7,8 @@ #ifndef NAZARA_ENUMS_CORE_HPP #define NAZARA_ENUMS_CORE_HPP +#include + namespace Nz { enum CoordSys @@ -73,23 +75,31 @@ namespace Nz HashType_Max = HashType_Whirlpool }; - enum OpenModeFlags + enum OpenMode { - OpenMode_NotOpen = 0x00, // Use the current mod of opening + OpenMode_NotOpen, // Use the current mod of opening - OpenMode_Append = 0x01, // Disable writing on existing parts and put the cursor at the end - OpenMode_Lock = 0x02, // Disable modifying the file before it is open - OpenMode_MustExit = 0x04, // Fail if the file doesn't exists, even if opened in write mode - OpenMode_ReadOnly = 0x08, // Open in read only - OpenMode_Text = 0x10, // Open in text mod - OpenMode_Truncate = 0x20, // Create the file if it doesn't exist and empty it if it exists - OpenMode_WriteOnly = 0x40, // Open in write only, create the file if it doesn't exist + OpenMode_Append, // Disable writing on existing parts and put the cursor at the end + OpenMode_Lock, // Disable modifying the file before it is open + OpenMode_MustExit, // Fail if the file doesn't exists, even if opened in write mode + OpenMode_ReadOnly, // Open in read only + OpenMode_Text, // Open in text mod + OpenMode_Truncate, // Create the file if it doesn't exist and empty it if it exists + OpenMode_WriteOnly, // Open in write only, create the file if it doesn't exist - OpenMode_ReadWrite = OpenMode_ReadOnly | OpenMode_WriteOnly, // Open in read and write - - OpenMode_Max = OpenMode_WriteOnly * 2 - 1 + OpenMode_Max = OpenMode_WriteOnly }; + template<> + struct EnableFlagsOperators + { + static constexpr bool value = true; + }; + + using OpenModeFlags = Flags; + + constexpr OpenModeFlags OpenMode_ReadWrite = OpenMode_ReadOnly | OpenMode_WriteOnly; + enum ParameterType { ParameterType_Boolean, @@ -173,16 +183,24 @@ namespace Nz SphereType_Max = SphereType_UV }; - enum StreamOptionFlags + enum StreamOption { - StreamOption_None = 0, + StreamOption_None, - StreamOption_Sequential = 0x1, - StreamOption_Text = 0x2, + StreamOption_Sequential, + StreamOption_Text, - StreamOption_Max = StreamOption_Text * 2 - 1 + StreamOption_Max = StreamOption_Text }; + template<> + struct EnableFlagsOperators + { + static constexpr bool value = true; + }; + + using StreamOptionFlags = Flags; + enum Ternary { Ternary_False, diff --git a/include/Nazara/Core/File.hpp b/include/Nazara/Core/File.hpp index 59b2b5457..d4d1e6522 100644 --- a/include/Nazara/Core/File.hpp +++ b/include/Nazara/Core/File.hpp @@ -31,7 +31,7 @@ namespace Nz public: File(); File(const String& filePath); - File(const String& filePath, UInt32 openMode); + File(const String& filePath, OpenModeFlags openMode); File(const File&) = delete; File(File&& file) noexcept; ~File(); @@ -57,8 +57,8 @@ namespace Nz bool IsOpen() const; - bool Open(unsigned int openMode = OpenMode_NotOpen); - bool Open(const String& filePath, unsigned int openMode = OpenMode_NotOpen); + bool Open(OpenModeFlags openMode = OpenMode_NotOpen); + bool Open(const String& filePath, OpenModeFlags openMode = OpenMode_NotOpen); bool Rename(const String& newFilePath); diff --git a/include/Nazara/Core/MemoryStream.hpp b/include/Nazara/Core/MemoryStream.hpp index f5d15fcf0..929e8e5bd 100644 --- a/include/Nazara/Core/MemoryStream.hpp +++ b/include/Nazara/Core/MemoryStream.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015 Jérôme Leclercq +// 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 @@ -18,7 +18,7 @@ namespace Nz { public: inline MemoryStream(); - inline MemoryStream(ByteArray* byteArray, UInt32 openMode = OpenMode_ReadWrite); + inline MemoryStream(ByteArray* byteArray, OpenModeFlags openMode = OpenMode_ReadWrite); MemoryStream(const MemoryStream&) = default; MemoryStream(MemoryStream&&) = default; ~MemoryStream() = default; @@ -32,7 +32,7 @@ namespace Nz UInt64 GetCursorPos() const override; UInt64 GetSize() const override; - void SetBuffer(ByteArray* byteArray, UInt32 openMode = OpenMode_ReadWrite); + void SetBuffer(ByteArray* byteArray, OpenModeFlags openMode = OpenMode_ReadWrite); bool SetCursorPos(UInt64 offset) override; MemoryStream& operator=(const MemoryStream&) = default; diff --git a/include/Nazara/Core/MemoryStream.inl b/include/Nazara/Core/MemoryStream.inl index 79a19f467..6cdf06b26 100644 --- a/include/Nazara/Core/MemoryStream.inl +++ b/include/Nazara/Core/MemoryStream.inl @@ -24,7 +24,7 @@ namespace Nz * \param byteArray Bytes to stream * \param openMode Reading/writing mode for the stream */ - inline MemoryStream::MemoryStream(ByteArray* byteArray, UInt32 openMode) : + inline MemoryStream::MemoryStream(ByteArray* byteArray, OpenModeFlags openMode) : MemoryStream() { SetBuffer(byteArray, openMode); diff --git a/include/Nazara/Core/Stream.hpp b/include/Nazara/Core/Stream.hpp index 63d7e781b..e4949d441 100644 --- a/include/Nazara/Core/Stream.hpp +++ b/include/Nazara/Core/Stream.hpp @@ -32,8 +32,8 @@ namespace Nz virtual UInt64 GetCursorPos() const = 0; virtual String GetDirectory() const; virtual String GetPath() const; - inline UInt32 GetOpenMode() const; - inline UInt32 GetStreamOptions() const; + inline OpenModeFlags GetOpenMode() const; + inline StreamOptionFlags GetStreamOptions() const; virtual UInt64 GetSize() const = 0; @@ -55,14 +55,14 @@ namespace Nz Stream& operator=(Stream&&) = default; protected: - inline Stream(UInt32 streamOptions = StreamOption_None, UInt32 openMode = OpenMode_NotOpen); + inline Stream(StreamOptionFlags streamOptions = StreamOption_None, OpenModeFlags openMode = OpenMode_NotOpen); virtual void FlushStream() = 0; virtual std::size_t ReadBlock(void* buffer, std::size_t size) = 0; virtual std::size_t WriteBlock(const void* buffer, std::size_t size) = 0; - UInt32 m_openMode; - UInt32 m_streamOptions; + OpenModeFlags m_openMode; + StreamOptionFlags m_streamOptions; }; } diff --git a/include/Nazara/Core/Stream.inl b/include/Nazara/Core/Stream.inl index aae7fe155..f4f1557d7 100644 --- a/include/Nazara/Core/Stream.inl +++ b/include/Nazara/Core/Stream.inl @@ -15,7 +15,7 @@ namespace Nz * \param openMode Reading/writing mode for the stream */ - inline Stream::Stream(UInt32 streamOptions, UInt32 openMode) : + inline Stream::Stream(StreamOptionFlags streamOptions, OpenModeFlags openMode) : m_openMode(openMode), m_streamOptions(streamOptions) { @@ -53,7 +53,7 @@ namespace Nz * \return Reading/writing mode for the stream */ - inline UInt32 Stream::GetOpenMode() const + inline OpenModeFlags Stream::GetOpenMode() const { return m_openMode; } @@ -63,7 +63,7 @@ namespace Nz * \return Options of the stream */ - inline UInt32 Stream::GetStreamOptions() const + inline StreamOptionFlags Stream::GetStreamOptions() const { return m_streamOptions; } @@ -116,7 +116,7 @@ namespace Nz * \param size Size meant to be read * * \remark Produces a NazaraAssert if stream is not readable - * \remark If preallocated space of buffer is less than the size, the behaviour is undefined + * \remark If preallocated space of buffer is less than the size, the behavior is undefined */ inline std::size_t Stream::Read(void* buffer, std::size_t size) @@ -134,7 +134,7 @@ namespace Nz * \param size Size meant to be written * * \remark Produces a NazaraAssert if stream is not writable - * \remark If preallocated space of buffer is less than the size, the behaviour is undefined + * \remark If preallocated space of buffer is less than the size, the behavior is undefined */ inline std::size_t Stream::Write(const void* buffer, std::size_t size) diff --git a/include/Nazara/Network/NetPacket.hpp b/include/Nazara/Network/NetPacket.hpp index 272808a33..7eef2979a 100644 --- a/include/Nazara/Network/NetPacket.hpp +++ b/include/Nazara/Network/NetPacket.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015 Jérôme Leclercq +// 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 @@ -56,7 +56,7 @@ namespace Nz void OnEmptyStream() override; void FreeStream(); - void InitStream(std::size_t minCapacity, UInt64 cursorPos, UInt32 openMode); + void InitStream(std::size_t minCapacity, UInt64 cursorPos, OpenModeFlags openMode); static bool Initialize(); static void Uninitialize(); diff --git a/src/Nazara/Core/ByteStream.cpp b/src/Nazara/Core/ByteStream.cpp index bca3db240..bc52f1782 100644 --- a/src/Nazara/Core/ByteStream.cpp +++ b/src/Nazara/Core/ByteStream.cpp @@ -24,7 +24,7 @@ namespace Nz * \param openMode Reading/writing mode for the stream */ - ByteStream::ByteStream(ByteArray* byteArray, UInt32 openMode) : + ByteStream::ByteStream(ByteArray* byteArray, OpenModeFlags openMode) : ByteStream() { SetStream(byteArray, openMode); @@ -67,7 +67,7 @@ namespace Nz * \param openMode Reading/writing mode for the stream */ - void ByteStream::SetStream(ByteArray* byteArray, UInt32 openMode) + void ByteStream::SetStream(ByteArray* byteArray, OpenModeFlags openMode) { std::unique_ptr stream(new MemoryStream(byteArray, openMode)); diff --git a/src/Nazara/Core/File.cpp b/src/Nazara/Core/File.cpp index 1fd77546f..35706fb70 100644 --- a/src/Nazara/Core/File.cpp +++ b/src/Nazara/Core/File.cpp @@ -64,7 +64,7 @@ namespace Nz * \param openMode Flag of the file */ - File::File(const String& filePath, UInt32 openMode) : + File::File(const String& filePath, OpenModeFlags openMode) : File() { Open(filePath, openMode); @@ -311,7 +311,7 @@ namespace Nz * \remark Produces a NazaraError if OS error to open a file */ - bool File::Open(unsigned int openMode) + bool File::Open(OpenModeFlags openMode) { NazaraLock(m_mutex) @@ -352,7 +352,7 @@ namespace Nz * \remark Produces a NazaraError if OS error to open a file */ - bool File::Open(const String& filePath, unsigned int openMode) + bool File::Open(const String& filePath, OpenModeFlags openMode) { NazaraLock(m_mutex) diff --git a/src/Nazara/Core/MemoryStream.cpp b/src/Nazara/Core/MemoryStream.cpp index bb77f35f8..a2fb7ff2b 100644 --- a/src/Nazara/Core/MemoryStream.cpp +++ b/src/Nazara/Core/MemoryStream.cpp @@ -65,7 +65,7 @@ namespace Nz * \remark Produces a NazaraAssert if byteArray is nullptr */ - void MemoryStream::SetBuffer(ByteArray* byteArray, UInt32 openMode) + void MemoryStream::SetBuffer(ByteArray* byteArray, OpenModeFlags openMode) { NazaraAssert(byteArray, "Invalid ByteArray"); diff --git a/src/Nazara/Core/Posix/FileImpl.cpp b/src/Nazara/Core/Posix/FileImpl.cpp index 037d795f0..094e15eff 100644 --- a/src/Nazara/Core/Posix/FileImpl.cpp +++ b/src/Nazara/Core/Posix/FileImpl.cpp @@ -49,7 +49,7 @@ namespace Nz return static_cast(position); } - bool FileImpl::Open(const String& filePath, UInt32 mode) + bool FileImpl::Open(const String& filePath, OpenModeFlags 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 4981a832e..51f966980 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, UInt32 mode); + bool Open(const String& filePath, OpenModeFlags mode); std::size_t Read(void* buffer, std::size_t size); bool SetCursorPos(CursorPosition pos, Int64 offset); bool SetSize(UInt64 size); diff --git a/src/Nazara/Core/Win32/FileImpl.cpp b/src/Nazara/Core/Win32/FileImpl.cpp index 5ed051adb..709642e03 100644 --- a/src/Nazara/Core/Win32/FileImpl.cpp +++ b/src/Nazara/Core/Win32/FileImpl.cpp @@ -55,12 +55,12 @@ namespace Nz return position.QuadPart; } - bool FileImpl::Open(const String& filePath, UInt32 mode) + bool FileImpl::Open(const String& filePath, OpenModeFlags mode) { DWORD access = 0; DWORD shareMode = FILE_SHARE_READ; DWORD openMode = 0; - + if (mode & OpenMode_ReadOnly) { access |= GENERIC_READ; @@ -68,7 +68,7 @@ namespace Nz if (mode & OpenMode_MustExit || (mode & OpenMode_WriteOnly) == 0) openMode |= OPEN_EXISTING; } - + if (mode & OpenMode_WriteOnly) { if (mode & OpenMode_Append) diff --git a/src/Nazara/Core/Win32/FileImpl.hpp b/src/Nazara/Core/Win32/FileImpl.hpp index 9a669cb19..dabfc266c 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, UInt32 mode); + bool Open(const String& filePath, OpenModeFlags mode); std::size_t Read(void* buffer, std::size_t size); bool SetCursorPos(CursorPosition pos, Int64 offset); bool SetSize(UInt64 size); diff --git a/src/Nazara/Network/NetPacket.cpp b/src/Nazara/Network/NetPacket.cpp index dcac9e178..900170b89 100644 --- a/src/Nazara/Network/NetPacket.cpp +++ b/src/Nazara/Network/NetPacket.cpp @@ -127,7 +127,7 @@ namespace Nz * \remark Produces a NazaraAssert if cursor position is greather than the capacity */ - void NetPacket::InitStream(std::size_t minCapacity, UInt64 cursorPos, UInt32 openMode) + void NetPacket::InitStream(std::size_t minCapacity, UInt64 cursorPos, OpenModeFlags openMode) { NazaraAssert(minCapacity >= cursorPos, "Cannot init stream with a smaller capacity than wanted cursor pos");