diff --git a/include/Nazara/Core/LockGuard.hpp b/include/Nazara/Core/LockGuard.hpp index 2dac4b578..d9ef5aa85 100644 --- a/include/Nazara/Core/LockGuard.hpp +++ b/include/Nazara/Core/LockGuard.hpp @@ -13,15 +13,22 @@ namespace Nz { class Mutex; - class NAZARA_CORE_API LockGuard + class LockGuard { public: - LockGuard(Mutex& mutex); - ~LockGuard(); + inline LockGuard(Mutex& mutex, bool lock = true); + inline ~LockGuard(); + + inline void Lock(); + inline bool TryLock(); + inline void Unlock(); private: Mutex& m_mutex; + bool m_locked; }; } +#include + #endif // NAZARA_LOCKGUARD_HPP diff --git a/include/Nazara/Core/LockGuard.inl b/include/Nazara/Core/LockGuard.inl new file mode 100644 index 000000000..73beab112 --- /dev/null +++ b/include/Nazara/Core/LockGuard.inl @@ -0,0 +1,83 @@ +// 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 + +#include +#include +#include +#include + +namespace Nz +{ + /*! + * \ingroup core + * \class Nz::LockGuard + * \brief Core class that represents a mutex wrapper that provides a convenient RAII-style mechanism + */ + + /*! + * \brief Constructs a LockGuard object with a mutex + * + * \param mutex Mutex to lock + * \param lock Should the mutex be locked by the constructor + */ + inline LockGuard::LockGuard(Mutex& mutex, bool lock) : + m_mutex(mutex), + m_locked(false) + { + if (lock) + { + m_mutex.Lock(); + m_locked = true; + } + } + + /*! + * \brief Destructs a LockGuard object and unlocks the mutex if it was previously locked + */ + inline LockGuard::~LockGuard() + { + if (m_locked) + m_mutex.Unlock(); + } + + /*! + * \brief Locks the underlying mutex + * + * \see Mutex::Lock + */ + inline void LockGuard::Lock() + { + NazaraAssert(!m_locked, "Mutex is already locked"); + + m_mutex.Lock(); + } + + /*! + * \brief Tries to lock the underlying mutex + * + * \see Mutex::TryLock + * + * \return true if the lock was acquired successfully + */ + inline bool LockGuard::TryLock() + { + NazaraAssert(!m_locked, "Mutex is already locked"); + + return m_mutex.TryLock(); + } + + /*! + * \brief Unlocks the underlying mutex + * + * \see Mutex::Unlock + */ + inline void LockGuard::Unlock() + { + NazaraAssert(m_locked, "Mutex is not locked"); + + m_mutex.Unlock(); + } +} + +#include diff --git a/src/Nazara/Core/LockGuard.cpp b/src/Nazara/Core/LockGuard.cpp deleted file mode 100644 index e714f05f7..000000000 --- a/src/Nazara/Core/LockGuard.cpp +++ /dev/null @@ -1,37 +0,0 @@ -// 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 - -#include -#include -#include - -namespace Nz -{ - /*! - * \ingroup core - * \class Nz::LockGuard - * \brief Core class that represents a mutex wrapper that provides a convenient RAII-style mechanism - */ - - /*! - * \brief Constructs a LockGuard object with a mutex - * - * \param mutex Mutex to lock - */ - - LockGuard::LockGuard(Mutex& mutex) : - m_mutex(mutex) - { - m_mutex.Lock(); - } - - /*! - * \brief Destructs a LockGuard object and unlocks the mutex - */ - - LockGuard::~LockGuard() - { - m_mutex.Unlock(); - } -}