Core/Enum: Convert OpenMode and StreamOption to the new flags system

This commit is contained in:
Lynix 2016-11-27 13:40:47 +01:00
parent 1a5617bc55
commit a34d1e410c
17 changed files with 78 additions and 60 deletions

View File

@ -337,16 +337,16 @@ namespace Ndk
instance.SetGlobal("HashType"); instance.SetGlobal("HashType");
// Nz::OpenMode // Nz::OpenMode
static_assert(Nz::OpenMode_Max + 1 == 2 * (64), "Nz::OpenModeFlags has been updated but change was not reflected to Lua binding"); static_assert(Nz::OpenMode_Max + 1 == 8, "Nz::OpenModeFlags has been updated but change was not reflected to Lua binding");
instance.PushTable(0, 8); instance.PushTable(0, Nz::OpenMode_Max + 1);
{ {
instance.PushField("Append", Nz::OpenMode_Append); instance.PushField("Append", Nz::OpenMode_Append);
instance.PushField("NotOpen", Nz::OpenMode_NotOpen); instance.PushField("NotOpen", Nz::OpenMode_NotOpen);
instance.PushField("Lock", Nz::OpenMode_Lock); instance.PushField("Lock", Nz::OpenMode_Lock);
instance.PushField("ReadOnly", Nz::OpenMode_ReadOnly); instance.PushField("ReadOnly", Nz::OpenMode_ReadOnly);
instance.PushField("ReadWrite", Nz::OpenMode_ReadWrite); instance.PushField("ReadWrite", Nz::OpenMode_ReadWrite);
instance.PushField("Text", Nz::OpenMode_Text); instance.PushField("Text", Nz::OpenMode_Text);
instance.PushField("Truncate", Nz::OpenMode_Truncate); instance.PushField("Truncate", Nz::OpenMode_Truncate);
instance.PushField("WriteOnly", Nz::OpenMode_WriteOnly); instance.PushField("WriteOnly", Nz::OpenMode_WriteOnly);
} }
instance.SetGlobal("OpenMode"); instance.SetGlobal("OpenMode");

View File

@ -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" // This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
@ -19,7 +19,7 @@ namespace Nz
{ {
public: public:
inline ByteStream(Stream* stream = nullptr); 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(void* ptr, Nz::UInt64 size);
ByteStream(const void* ptr, Nz::UInt64 size); ByteStream(const void* ptr, Nz::UInt64 size);
ByteStream(const ByteStream&) = delete; ByteStream(const ByteStream&) = delete;
@ -36,7 +36,7 @@ namespace Nz
inline void SetDataEndianness(Endianness endiannes); inline void SetDataEndianness(Endianness endiannes);
inline void SetStream(Stream* stream); 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(void* ptr, Nz::UInt64 size);
void SetStream(const void* ptr, Nz::UInt64 size); void SetStream(const void* ptr, Nz::UInt64 size);

View File

@ -7,6 +7,8 @@
#ifndef NAZARA_ENUMS_CORE_HPP #ifndef NAZARA_ENUMS_CORE_HPP
#define NAZARA_ENUMS_CORE_HPP #define NAZARA_ENUMS_CORE_HPP
#include <Nazara/Core/Flags.hpp>
namespace Nz namespace Nz
{ {
enum CoordSys enum CoordSys
@ -73,23 +75,31 @@ namespace Nz
HashType_Max = HashType_Whirlpool 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_Append, // Disable writing on existing parts and put the cursor at the end
OpenMode_Lock = 0x02, // Disable modifying the file before it is open OpenMode_Lock, // 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_MustExit, // Fail if the file doesn't exists, even if opened in write mode
OpenMode_ReadOnly = 0x08, // Open in read only OpenMode_ReadOnly, // Open in read only
OpenMode_Text = 0x10, // Open in text mod OpenMode_Text, // Open in text mod
OpenMode_Truncate = 0x20, // Create the file if it doesn't exist and empty it if it exists OpenMode_Truncate, // 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_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
OpenMode_Max = OpenMode_WriteOnly * 2 - 1
}; };
template<>
struct EnableFlagsOperators<OpenMode>
{
static constexpr bool value = true;
};
using OpenModeFlags = Flags<OpenMode>;
constexpr OpenModeFlags OpenMode_ReadWrite = OpenMode_ReadOnly | OpenMode_WriteOnly;
enum ParameterType enum ParameterType
{ {
ParameterType_Boolean, ParameterType_Boolean,
@ -173,16 +183,24 @@ namespace Nz
SphereType_Max = SphereType_UV SphereType_Max = SphereType_UV
}; };
enum StreamOptionFlags enum StreamOption
{ {
StreamOption_None = 0, StreamOption_None,
StreamOption_Sequential = 0x1, StreamOption_Sequential,
StreamOption_Text = 0x2, StreamOption_Text,
StreamOption_Max = StreamOption_Text * 2 - 1 StreamOption_Max = StreamOption_Text
}; };
template<>
struct EnableFlagsOperators<StreamOption>
{
static constexpr bool value = true;
};
using StreamOptionFlags = Flags<StreamOption>;
enum Ternary enum Ternary
{ {
Ternary_False, Ternary_False,

View File

@ -31,7 +31,7 @@ namespace Nz
public: public:
File(); File();
File(const String& filePath); File(const String& filePath);
File(const String& filePath, UInt32 openMode); File(const String& filePath, OpenModeFlags openMode);
File(const File&) = delete; File(const File&) = delete;
File(File&& file) noexcept; File(File&& file) noexcept;
~File(); ~File();
@ -57,8 +57,8 @@ namespace Nz
bool IsOpen() const; bool IsOpen() const;
bool Open(unsigned int openMode = OpenMode_NotOpen); bool Open(OpenModeFlags openMode = OpenMode_NotOpen);
bool Open(const String& filePath, unsigned int openMode = OpenMode_NotOpen); bool Open(const String& filePath, OpenModeFlags openMode = OpenMode_NotOpen);
bool Rename(const String& newFilePath); bool Rename(const String& newFilePath);

View File

@ -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" // This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
@ -18,7 +18,7 @@ namespace Nz
{ {
public: public:
inline MemoryStream(); inline MemoryStream();
inline MemoryStream(ByteArray* byteArray, UInt32 openMode = OpenMode_ReadWrite); inline MemoryStream(ByteArray* byteArray, OpenModeFlags openMode = OpenMode_ReadWrite);
MemoryStream(const MemoryStream&) = default; MemoryStream(const MemoryStream&) = default;
MemoryStream(MemoryStream&&) = default; MemoryStream(MemoryStream&&) = default;
~MemoryStream() = default; ~MemoryStream() = default;
@ -32,7 +32,7 @@ namespace Nz
UInt64 GetCursorPos() const override; UInt64 GetCursorPos() const override;
UInt64 GetSize() 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; bool SetCursorPos(UInt64 offset) override;
MemoryStream& operator=(const MemoryStream&) = default; MemoryStream& operator=(const MemoryStream&) = default;

View File

@ -24,7 +24,7 @@ namespace Nz
* \param byteArray Bytes to stream * \param byteArray Bytes to stream
* \param openMode Reading/writing mode for the stream * \param openMode Reading/writing mode for the stream
*/ */
inline MemoryStream::MemoryStream(ByteArray* byteArray, UInt32 openMode) : inline MemoryStream::MemoryStream(ByteArray* byteArray, OpenModeFlags openMode) :
MemoryStream() MemoryStream()
{ {
SetBuffer(byteArray, openMode); SetBuffer(byteArray, openMode);

View File

@ -32,8 +32,8 @@ namespace Nz
virtual UInt64 GetCursorPos() const = 0; virtual UInt64 GetCursorPos() const = 0;
virtual String GetDirectory() const; virtual String GetDirectory() const;
virtual String GetPath() const; virtual String GetPath() const;
inline UInt32 GetOpenMode() const; inline OpenModeFlags GetOpenMode() const;
inline UInt32 GetStreamOptions() const; inline StreamOptionFlags GetStreamOptions() const;
virtual UInt64 GetSize() const = 0; virtual UInt64 GetSize() const = 0;
@ -55,14 +55,14 @@ namespace Nz
Stream& operator=(Stream&&) = default; Stream& operator=(Stream&&) = default;
protected: 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 void FlushStream() = 0;
virtual std::size_t ReadBlock(void* buffer, std::size_t size) = 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; virtual std::size_t WriteBlock(const void* buffer, std::size_t size) = 0;
UInt32 m_openMode; OpenModeFlags m_openMode;
UInt32 m_streamOptions; StreamOptionFlags m_streamOptions;
}; };
} }

View File

@ -15,7 +15,7 @@ namespace Nz
* \param openMode Reading/writing mode for the stream * \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_openMode(openMode),
m_streamOptions(streamOptions) m_streamOptions(streamOptions)
{ {
@ -53,7 +53,7 @@ namespace Nz
* \return Reading/writing mode for the stream * \return Reading/writing mode for the stream
*/ */
inline UInt32 Stream::GetOpenMode() const inline OpenModeFlags Stream::GetOpenMode() const
{ {
return m_openMode; return m_openMode;
} }
@ -63,7 +63,7 @@ namespace Nz
* \return Options of the stream * \return Options of the stream
*/ */
inline UInt32 Stream::GetStreamOptions() const inline StreamOptionFlags Stream::GetStreamOptions() const
{ {
return m_streamOptions; return m_streamOptions;
} }
@ -116,7 +116,7 @@ namespace Nz
* \param size Size meant to be read * \param size Size meant to be read
* *
* \remark Produces a NazaraAssert if stream is not readable * \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) 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 * \param size Size meant to be written
* *
* \remark Produces a NazaraAssert if stream is not writable * \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) inline std::size_t Stream::Write(const void* buffer, std::size_t size)

View File

@ -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" // This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
@ -56,7 +56,7 @@ namespace Nz
void OnEmptyStream() override; void OnEmptyStream() override;
void FreeStream(); 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 bool Initialize();
static void Uninitialize(); static void Uninitialize();

View File

@ -24,7 +24,7 @@ namespace Nz
* \param openMode Reading/writing mode for the stream * \param openMode Reading/writing mode for the stream
*/ */
ByteStream::ByteStream(ByteArray* byteArray, UInt32 openMode) : ByteStream::ByteStream(ByteArray* byteArray, OpenModeFlags openMode) :
ByteStream() ByteStream()
{ {
SetStream(byteArray, openMode); SetStream(byteArray, openMode);
@ -67,7 +67,7 @@ namespace Nz
* \param openMode Reading/writing mode for the stream * \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> stream(new MemoryStream(byteArray, openMode)); std::unique_ptr<Stream> stream(new MemoryStream(byteArray, openMode));

View File

@ -64,7 +64,7 @@ namespace Nz
* \param openMode Flag of the file * \param openMode Flag of the file
*/ */
File::File(const String& filePath, UInt32 openMode) : File::File(const String& filePath, OpenModeFlags openMode) :
File() File()
{ {
Open(filePath, openMode); Open(filePath, openMode);
@ -311,7 +311,7 @@ namespace Nz
* \remark Produces a NazaraError if OS error to open a file * \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) NazaraLock(m_mutex)
@ -352,7 +352,7 @@ namespace Nz
* \remark Produces a NazaraError if OS error to open a file * \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) NazaraLock(m_mutex)

View File

@ -65,7 +65,7 @@ namespace Nz
* \remark Produces a NazaraAssert if byteArray is nullptr * \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"); NazaraAssert(byteArray, "Invalid ByteArray");

View File

@ -49,7 +49,7 @@ namespace Nz
return static_cast<UInt64>(position); return static_cast<UInt64>(position);
} }
bool FileImpl::Open(const String& filePath, UInt32 mode) bool FileImpl::Open(const String& filePath, OpenModeFlags mode)
{ {
int flags; int flags;
mode_t permissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; mode_t permissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;

View File

@ -36,7 +36,7 @@ namespace Nz
bool EndOfFile() const; bool EndOfFile() const;
void Flush(); void Flush();
UInt64 GetCursorPos() const; 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); std::size_t Read(void* buffer, std::size_t size);
bool SetCursorPos(CursorPosition pos, Int64 offset); bool SetCursorPos(CursorPosition pos, Int64 offset);
bool SetSize(UInt64 size); bool SetSize(UInt64 size);

View File

@ -55,12 +55,12 @@ namespace Nz
return position.QuadPart; return position.QuadPart;
} }
bool FileImpl::Open(const String& filePath, UInt32 mode) bool FileImpl::Open(const String& filePath, OpenModeFlags mode)
{ {
DWORD access = 0; DWORD access = 0;
DWORD shareMode = FILE_SHARE_READ; DWORD shareMode = FILE_SHARE_READ;
DWORD openMode = 0; DWORD openMode = 0;
if (mode & OpenMode_ReadOnly) if (mode & OpenMode_ReadOnly)
{ {
access |= GENERIC_READ; access |= GENERIC_READ;
@ -68,7 +68,7 @@ namespace Nz
if (mode & OpenMode_MustExit || (mode & OpenMode_WriteOnly) == 0) if (mode & OpenMode_MustExit || (mode & OpenMode_WriteOnly) == 0)
openMode |= OPEN_EXISTING; openMode |= OPEN_EXISTING;
} }
if (mode & OpenMode_WriteOnly) if (mode & OpenMode_WriteOnly)
{ {
if (mode & OpenMode_Append) if (mode & OpenMode_Append)

View File

@ -29,7 +29,7 @@ namespace Nz
bool EndOfFile() const; bool EndOfFile() const;
void Flush(); void Flush();
UInt64 GetCursorPos() const; 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); std::size_t Read(void* buffer, std::size_t size);
bool SetCursorPos(CursorPosition pos, Int64 offset); bool SetCursorPos(CursorPosition pos, Int64 offset);
bool SetSize(UInt64 size); bool SetSize(UInt64 size);

View File

@ -127,7 +127,7 @@ namespace Nz
* \remark Produces a NazaraAssert if cursor position is greather than the capacity * \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"); NazaraAssert(minCapacity >= cursorPos, "Cannot init stream with a smaller capacity than wanted cursor pos");