This reverts commit db2ef3e90c3871290d114a9e6437b412e96c65aa [formerly a3f6ff88a25e63374eb6ce5b18269da2ba743b06] [formerly cfa12604fbb0da76fc27288b210ee1254a8b3a38 [formerly dee6ce858398e2de38ef1af00c1c630fd0126e09]] [formerly 1a23f0fddcd80ac33030061b7a00a3cfd43cb7fe [formerly d3cb17069c71449ae3f1cba6de55ea70f509e7a4] [formerly b2f8f82e9f3427310204f2e8a61d7bdfd96202d2 [formerly 5d117720d08d6d6243b3428d4b3f8aea1abef845]]]. Former-commit-id: 13a54266823fed88610753b1a90f4ef2c6fe932d [formerly 508d1a01cceee4a577063c684244fcf598ff6cb5] [formerly 2c99faae4d65dc222d05d2f98913a380b38d58ef [formerly 0117f7aec15e3db31cacdd0ef874064907c41ba8]] Former-commit-id: 140d69126443b821a232a1a8b012712fc2e74b0f [formerly 501a63bd435f64d9da932279c90400ed95d0c1be] Former-commit-id: d2d74a42d070eae9c1f158a59238c5f1ac40edb3
129 lines
2.7 KiB
C++
129 lines
2.7 KiB
C++
// 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 <Nazara/Core/RefCounted.hpp>
|
|
#include <Nazara/Core/Config.hpp>
|
|
#include <Nazara/Core/Error.hpp>
|
|
|
|
#if NAZARA_CORE_THREADSAFE && NAZARA_THREADSAFETY_REFCOUNTED
|
|
#include <Nazara/Core/ThreadSafety.hpp>
|
|
#else
|
|
#include <Nazara/Core/ThreadSafetyOff.hpp>
|
|
#endif
|
|
|
|
#include <Nazara/Core/Debug.hpp>
|
|
|
|
namespace Nz
|
|
{
|
|
/*!
|
|
* \ingroup core
|
|
* \class Nz::RefCounted
|
|
* \brief Core class that represents a reference with a counter
|
|
*/
|
|
|
|
/*!
|
|
* \brief Constructs a RefCounted object with a persistance aspect
|
|
*
|
|
* \param persistent if false, object is destroyed when no more referenced
|
|
*/
|
|
|
|
RefCounted::RefCounted(bool persistent) :
|
|
m_persistent(persistent),
|
|
m_referenceCount(0)
|
|
{
|
|
}
|
|
|
|
/*!
|
|
* \brief Destructs the object
|
|
*
|
|
* \remark Produces a NazaraWarning if still referenced with NAZARA_CORE_SAFE defined
|
|
*/
|
|
|
|
RefCounted::~RefCounted()
|
|
{
|
|
#if NAZARA_CORE_SAFE
|
|
if (m_referenceCount > 0)
|
|
NazaraWarning("Resource destroyed while still referenced " + String::Number(m_referenceCount) + " time(s)");
|
|
#endif
|
|
}
|
|
|
|
/*!
|
|
* \brief Adds a reference to the object
|
|
*/
|
|
|
|
void RefCounted::AddReference() const
|
|
{
|
|
m_referenceCount++;
|
|
}
|
|
|
|
/*!
|
|
* \brief Gets the number of references to the object
|
|
* \return Number of references
|
|
*/
|
|
|
|
unsigned int RefCounted::GetReferenceCount() const
|
|
{
|
|
return m_referenceCount;
|
|
}
|
|
|
|
/*!
|
|
* \brief Checks whether the object is persistent
|
|
* \return true if object is not destroyed when no more referenced
|
|
*/
|
|
|
|
bool RefCounted::IsPersistent() const
|
|
{
|
|
return m_persistent;
|
|
}
|
|
|
|
/*!
|
|
* \brief Removes a reference to the object
|
|
* \return true if object is deleted because no more referenced
|
|
*
|
|
* \remark Produces a NazaraError if counter is already 0 with NAZARA_CORE_SAFE defined
|
|
*/
|
|
|
|
bool RefCounted::RemoveReference() const
|
|
{
|
|
#if NAZARA_CORE_SAFE
|
|
if (m_referenceCount == 0)
|
|
{
|
|
NazaraError("Impossible to remove reference (Ref. counter is already 0)");
|
|
return false;
|
|
}
|
|
#endif
|
|
|
|
if (--m_referenceCount == 0 && !m_persistent)
|
|
{
|
|
delete this; // Suicide
|
|
|
|
return true;
|
|
}
|
|
else
|
|
return false;
|
|
}
|
|
|
|
/*!
|
|
* \brief Sets the persistence of the object
|
|
* \return true if object is deleted because no more referenced
|
|
*
|
|
* \param persistent Sets the persistence of the object
|
|
* \param checkReferenceCount Checks if the object should be destroyed if true
|
|
*/
|
|
|
|
bool RefCounted::SetPersistent(bool persistent, bool checkReferenceCount)
|
|
{
|
|
m_persistent = persistent;
|
|
|
|
if (checkReferenceCount && !persistent && m_referenceCount == 0)
|
|
{
|
|
delete this;
|
|
|
|
return true;
|
|
}
|
|
else
|
|
return false;
|
|
}
|
|
}
|