Core: Add MovablePtr class

This commit is contained in:
Lynix
2017-09-30 13:57:25 +02:00
parent f95fc332f1
commit 2cd9fa2b7a
29 changed files with 157 additions and 259 deletions

View File

@@ -8,6 +8,7 @@
#define NAZARA_CONDITIONVARIABLE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/MovablePtr.hpp>
namespace Nz
{
@@ -19,7 +20,7 @@ namespace Nz
public:
ConditionVariable();
ConditionVariable(const ConditionVariable&) = delete;
inline ConditionVariable(ConditionVariable&& condition) noexcept;
ConditionVariable(ConditionVariable&& condition) noexcept = default;
~ConditionVariable();
void Signal();
@@ -29,10 +30,10 @@ namespace Nz
bool Wait(Mutex* mutex, UInt32 timeout);
ConditionVariable& operator=(const ConditionVariable&) = delete;
ConditionVariable& operator=(ConditionVariable&& condition) noexcept;
ConditionVariable& operator=(ConditionVariable&& condition) noexcept = default;
private:
ConditionVariableImpl* m_impl;
MovablePtr<ConditionVariableImpl> m_impl;
};
}

View File

@@ -11,15 +11,6 @@ namespace Nz
/*!
* \class Nz::ConditionVariable
*/
/*!
* \brief Constructs a ConditionVariable object by moving another one
*/
inline ConditionVariable::ConditionVariable(ConditionVariable&& condition) noexcept :
m_impl(condition.m_impl)
{
condition.m_impl = nullptr;
}
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -8,6 +8,7 @@
#define NAZARA_DIRECTORY_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/MovablePtr.hpp>
#include <Nazara/Core/String.hpp>
#if defined(NAZARA_PLATFORM_WINDOWS)
@@ -35,7 +36,7 @@ namespace Nz
Directory();
Directory(const String& dirPath);
Directory(const Directory&) = delete;
Directory(Directory&&) = delete; ///TODO
Directory(Directory&&) noexcept = default;
~Directory();
void Close();
@@ -67,14 +68,14 @@ namespace Nz
static bool SetCurrent(const String& dirPath);
Directory& operator=(const Directory&) = delete;
Directory& operator=(Directory&&) = delete; ///TODO
Directory& operator=(Directory&&) noexcept = delete;
private:
NazaraMutexAttrib(m_mutex, mutable)
String m_dirPath;
String m_pattern;
DirectoryImpl* m_impl;
MovablePtr<DirectoryImpl> m_impl;
};
}

View File

@@ -8,6 +8,7 @@
#define NAZARA_DYNLIB_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/MovablePtr.hpp>
#include <Nazara/Core/String.hpp>
#if defined(NAZARA_PLATFORM_WINDOWS)
@@ -28,7 +29,7 @@
namespace Nz
{
using DynLibFunc = int (*)(); // "Generic" type of poiter to function
using DynLibFunc = int (*)(); // "Generic" type of pointer to function
class DynLibImpl;
@@ -37,7 +38,7 @@ namespace Nz
public:
DynLib();
DynLib(const DynLib&) = delete;
DynLib(DynLib&& lib);
DynLib(DynLib&&) noexcept = default;
~DynLib();
String GetLastError() const;
@@ -49,13 +50,13 @@ namespace Nz
void Unload();
DynLib& operator=(const DynLib&) = delete;
DynLib& operator=(DynLib&& lib);
DynLib& operator=(DynLib&& lib) noexcept = default;
private:
NazaraMutexAttrib(m_mutex, mutable)
mutable String m_lastError;
DynLibImpl* m_impl;
MovablePtr<DynLibImpl> m_impl;
};
}

View File

@@ -11,6 +11,7 @@
#include <Nazara/Core/ByteArray.hpp>
#include <Nazara/Core/Directory.hpp>
#include <Nazara/Core/Endianness.hpp>
#include <Nazara/Core/MovablePtr.hpp>
#include <Nazara/Core/Stream.hpp>
#include <Nazara/Core/String.hpp>
@@ -33,7 +34,7 @@ namespace Nz
File(const String& filePath);
File(const String& filePath, OpenModeFlags openMode);
File(const File&) = delete;
File(File&& file) noexcept;
File(File&& file) noexcept = default;
~File();
bool Copy(const String& newFilePath);
@@ -69,7 +70,7 @@ namespace Nz
File& operator=(const String& filePath);
File& operator=(const File&) = delete;
File& operator=(File&& file) noexcept;
File& operator=(File&& file) noexcept = default;
static String AbsolutePath(const String& filePath);
static inline ByteArray ComputeHash(HashType hash, const String& filePath);
@@ -95,7 +96,7 @@ namespace Nz
std::size_t WriteBlock(const void* buffer, std::size_t size) override;
String m_filePath;
FileImpl* m_impl;
MovablePtr<FileImpl> m_impl;
};
NAZARA_CORE_API bool HashAppend(AbstractHash* hash, const File& originalFile);

View File

@@ -0,0 +1,38 @@
// Copyright (C) 2017 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
#pragma once
#ifndef NAZARA_MOVABLE_PTR_HPP
#define NAZARA_MOVABLE_PTR_HPP
namespace Nz
{
template<typename T>
class MovablePtr
{
public:
MovablePtr(T* value = nullptr);
MovablePtr(const MovablePtr&) = default;
MovablePtr(MovablePtr&& ptr) noexcept;
~MovablePtr() = default;
T* Get() const;
T* operator->() const;
operator T*() const;
MovablePtr& operator=(T* value);
MovablePtr& operator=(const MovablePtr&) = default;
MovablePtr& operator=(MovablePtr&& ptr) noexcept;
private:
T* m_value;
};
}
#include <Nazara/Core/MovablePtr.inl>
#endif // NAZARA_MOVABLE_PTR_HPP

View File

@@ -0,0 +1,61 @@
// Copyright (C) 2017 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
#include <Nazara/Core/MovablePtr.hpp>
#include <cassert>
namespace Nz
{
/*!
* \ingroup core
* \class Nz::MovablePtr
* \brief Wraps a raw (non-proprietary) to allows it to be moved implicitly
*/
template<typename T>
MovablePtr<T>::MovablePtr(T* value) :
m_value(value)
{
}
template<typename T>
MovablePtr<T>::MovablePtr(MovablePtr&& ptr) noexcept :
m_value(ptr.m_value)
{
ptr.m_value = nullptr;
}
template<typename T>
inline T* MovablePtr<T>::Get() const
{
return m_value;
}
template<typename T>
T* MovablePtr<T>::operator->() const
{
return m_value;
}
template<typename T>
MovablePtr<T>::operator T*() const
{
return m_value;
}
template<typename T>
inline MovablePtr<T>& MovablePtr<T>::operator=(T* value)
{
m_value = value;
return *this;
}
template<typename T>
MovablePtr<T>& MovablePtr<T>::operator=(MovablePtr&& ptr) noexcept
{
std::swap(m_value, ptr.m_value);
return *this;
}
}

View File

@@ -8,6 +8,7 @@
#define NAZARA_MUTEX_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/MovablePtr.hpp>
namespace Nz
{
@@ -20,7 +21,7 @@ namespace Nz
public:
Mutex();
Mutex(const Mutex&) = delete;
inline Mutex(Mutex&& mutex) noexcept;
Mutex(Mutex&&) noexcept = default;
~Mutex();
void Lock();
@@ -28,10 +29,10 @@ namespace Nz
void Unlock();
Mutex& operator=(const Mutex&) = delete;
Mutex& operator=(Mutex&& mutex) noexcept;
Mutex& operator=(Mutex&&) noexcept = default;
private:
MutexImpl* m_impl;
MovablePtr<MutexImpl> m_impl;
};
}

View File

@@ -12,15 +12,6 @@ namespace Nz
* \ingroup core
* \class Nz::Mutex
*/
/*!
* \brief Constructs a Mutex object by moving another one
*/
inline Mutex::Mutex(Mutex&& mutex) noexcept :
m_impl(mutex.m_impl)
{
mutex.m_impl = nullptr;
}
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -9,6 +9,7 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Functor.hpp>
#include <Nazara/Core/MovablePtr.hpp>
#include <iosfwd>
namespace Nz
@@ -25,7 +26,7 @@ namespace Nz
template<typename F, typename... Args> Thread(F function, Args&&... args);
template<typename C> Thread(void (C::*function)(), C* object);
Thread(const Thread&) = delete;
Thread(Thread&& other) noexcept;
Thread(Thread&& other) noexcept = default;
~Thread();
void Detach();
@@ -35,7 +36,7 @@ namespace Nz
void SetName(const String& name);
Thread& operator=(const Thread&) = delete;
Thread& operator=(Thread&& thread);
Thread& operator=(Thread&& thread) noexcept = default;
static unsigned int HardwareConcurrency();
static void SetCurrentThreadName(const String& name);
@@ -44,7 +45,7 @@ namespace Nz
private:
void CreateImpl(Functor* functor);
ThreadImpl* m_impl;
MovablePtr<ThreadImpl> m_impl;
};
class NAZARA_CORE_API Thread::Id