<commit_before>// Copyright (C) 2013 <NAME>
// 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/Initializer.hpp>
#include <Nazara/Core/Debug.hpp>

template<typename T>
NzResourceRef<T>::NzResourceRef(T* resource) :
m_resource(resource)
{
	if (m_resource)
		m_resource->AddResourceReference();
}

template<typename T>
NzResourceRef<T>::NzResourceRef(const NzResourceRef& ref) :
m_resource(ref.m_resource)
{
	if (m_resource)
		m_resource->AddResourceReference();
}

template<typename T>
NzResourceRef<T>::NzResourceRef(NzResourceRef&& ref) :
m_resource(ref.m_resource)
{
	ref.m_resource = nullptr; // On vole la référence
}

template<typename T>
NzResourceRef<T>::~NzResourceRef()
{
	if (m_resource)
		m_resource->RemoveResourceReference();
}

template<typename T>
bool NzResourceRef<T>::IsValid() const
{
	return m_resource != nullptr;
}

template<typename T>
T* NzResourceRef<T>::Release()
{
	T* resource = m_resource;
	m_resource = nullptr;

	return resource;
}

template<typename T>
bool NzResourceRef<T>::Reset(T* resource)
{
	bool destroyed = false;
	if (m_resource)
	{
		destroyed = m_resource->RemoveResourceReference();
		m_resource = nullptr;
	}

	m_resource = resource;
	if (m_resource)
		m_resource->AddResourceReference();

	return destroyed;
}

template<typename T>
NzResourceRef<T>& NzResourceRef<T>::Swap(NzResourceRef& ref)
{
	std::swap(m_resource, ref.m_resource);

	return *this;
}

template<typename T>
NzResourceRef<T>::operator bool() const
{
	return IsValid();
}

template<typename T>
NzResourceRef<T>::operator T*() const
{
	return m_resource;
}

template<typename T>
T* NzResourceRef<T>::operator->() const
{
	return m_resource;
}

template<typename T>
NzResourceRef<T>& NzResourceRef<T>::operator=(T* resource)
{
	if (m_resource != resource)
	{
		Release();

		if (resource)
		{
			m_resource = resource;
			m_resource->AddResourceReference();
		}
	}

	return *this;
}

template<typename T>
NzResourceRef<T>& NzResourceRef<T>::operator=(const NzResourceRef& ref)
{
	if (m_resource != ref.m_resource)
	{
		Release();

		if (ref)
		{
			m_resource = ref.m_resource;
			m_resource->AddResourceReference();
		}
	}

	return *this;
}

template<typename T>
NzResourceRef<T>& NzResourceRef<T>::operator=(NzResourceRef&& ref)
{
	Release();

	std::swap(m_resource, ref.m_resource);

	return *this;
}

#include <Nazara/Core/DebugOff.hpp><commit_msg>Fixed ResourceRef not removing references<commit_after>// Copyright (C) 2013 <NAME>
// 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/Initializer.hpp>
#include <Nazara/Core/Debug.hpp>

template<typename T>
NzResourceRef<T>::NzResourceRef(T* resource) :
m_resource(resource)
{
	if (m_resource)
		m_resource->AddResourceReference();
}

template<typename T>
NzResourceRef<T>::NzResourceRef(const NzResourceRef& ref) :
m_resource(ref.m_resource)
{
	if (m_resource)
		m_resource->AddResourceReference();
}

template<typename T>
NzResourceRef<T>::NzResourceRef(NzResourceRef&& ref) :
m_resource(ref.m_resource)
{
	ref.m_resource = nullptr; // On vole la référence
}

template<typename T>
NzResourceRef<T>::~NzResourceRef()
{
	if (m_resource)
		m_resource->RemoveResourceReference();
}

template<typename T>
bool NzResourceRef<T>::IsValid() const
{
	return m_resource != nullptr;
}

template<typename T>
T* NzResourceRef<T>::Release()
{
	T* resource = m_resource;
	m_resource = nullptr;

	return resource;
}

template<typename T>
bool NzResourceRef<T>::Reset(T* resource)
{
	bool destroyed = false;
	if (m_resource)
	{
		destroyed = m_resource->RemoveResourceReference();
		m_resource = nullptr;
	}

	m_resource = resource;
	if (m_resource)
		m_resource->AddResourceReference();

	return destroyed;
}

template<typename T>
NzResourceRef<T>& NzResourceRef<T>::Swap(NzResourceRef& ref)
{
	std::swap(m_resource, ref.m_resource);

	return *this;
}

template<typename T>
NzResourceRef<T>::operator bool() const
{
	return IsValid();
}

template<typename T>
NzResourceRef<T>::operator T*() const
{
	return m_resource;
}

template<typename T>
T* NzResourceRef<T>::operator->() const
{
	return m_resource;
}

template<typename T>
NzResourceRef<T>& NzResourceRef<T>::operator=(T* resource)
{
	if (m_resource != resource)
	{
		Reset();

		if (resource)
		{
			m_resource = resource;
			m_resource->AddResourceReference();
		}
	}

	return *this;
}

template<typename T>
NzResourceRef<T>& NzResourceRef<T>::operator=(const NzResourceRef& ref)
{
	if (m_resource != ref.m_resource)
	{
		Reset();

		if (ref)
		{
			m_resource = ref.m_resource;
			m_resource->AddResourceReference();
		}
	}

	return *this;
}

template<typename T>
NzResourceRef<T>& NzResourceRef<T>::operator=(NzResourceRef&& ref)
{
	Reset();

	std::swap(m_resource, ref.m_resource);

	return *this;
}

#include <Nazara/Core/DebugOff.hpp>
{'fraction_non_alphanumeric': 0.1407258064516129, 'fraction_numerical': 0.0016129032258064516, 'mean_word_length': 5.731343283582089, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2537690', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1792, 'n_words': 361}