Added ResourceRef (Automatic resource reference)

Former-commit-id: 97a0b2732f4dc443b8e1676e68b33b1b53ddf4fb
This commit is contained in:
Lynix
2013-03-15 03:09:58 +01:00
parent 4ee6ca05ed
commit 6c2fb1eb89
29 changed files with 344 additions and 299 deletions

View File

@@ -78,11 +78,11 @@ class NAZARA_API NzModel : public NzSceneNode, public NzUpdatable
void UpdateBoundingBox() const;
bool VisibilityTest(const NzFrustumf& frustum) override;
std::vector<NzMaterial*> m_materials;
std::vector<NzMaterialRef> m_materials;
mutable NzBoundingBoxf m_boundingBox;
NzSkeleton m_skeleton; // Uniquement pour les animations squelettiques
NzAnimation* m_animation;
NzMesh* m_mesh;
NzAnimationRef m_animation;
NzMeshRef m_mesh;
const NzSequence* m_currentSequence;
bool m_animationEnabled;
mutable bool m_boundingBoxUpdated;

View File

@@ -42,7 +42,7 @@ class NAZARA_API NzSound : public NzSoundEmitter
void Stop();
private:
const NzSoundBuffer* m_buffer = nullptr;
NzSoundBufferConstRef m_buffer;
};
#endif // NAZARA_SOUND_HPP

View File

@@ -13,6 +13,7 @@
#include <Nazara/Core/NonCopyable.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceRef.hpp>
struct NzSoundBufferParams
{
@@ -22,7 +23,9 @@ struct NzSoundBufferParams
class NzSound;
class NzSoundBuffer;
using NzSoundBufferConstRef = NzResourceRef<const NzSoundBuffer>;
using NzSoundBufferLoader = NzResourceLoader<NzSoundBuffer, NzSoundBufferParams>;
using NzSoundBufferRef = NzResourceRef<NzSoundBuffer>;
struct NzSoundBufferImpl;

View File

@@ -49,8 +49,8 @@ class NAZARA_API NzResource
bool IsPersistent() const;
void RemoveResourceListener(NzResourceListener* listener) const;
void RemoveResourceReference() const;
bool RemoveResourceListener(NzResourceListener* listener) const;
bool RemoveResourceReference() const;
void SetPersistent(bool persistent = true, bool checkReferenceCount = true);

View File

@@ -0,0 +1,44 @@
// Copyright (C) 2013 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_RESOURCEREF_HPP
#define NAZARA_RESOURCEREF_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Resource.hpp>
#include <type_traits>
template<typename T>
class NzResourceRef
{
static_assert(std::is_base_of<NzResource, T>::value, "ResourceRef should only be used with resource type");
public:
NzResourceRef() = default;
NzResourceRef(T* resource);
NzResourceRef(const NzResourceRef& ref);
NzResourceRef(NzResourceRef&& ref);
~NzResourceRef();
bool IsValid() const;
T* Release();
bool Reset(T* resource = nullptr);
NzResourceRef& Swap(NzResourceRef& ref);
operator bool() const;
operator T*() const;
T* operator->() const;
NzResourceRef& operator=(const NzResourceRef& ref);
NzResourceRef& operator=(NzResourceRef&& ref);
private:
T* m_resource = nullptr;
};
#include <Nazara/Core/ResourceRef.inl>
#endif // NAZARA_RESOURCEREF_HPP

View File

@@ -0,0 +1,125 @@
// Copyright (C) 2013 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
// http://www.easyrgb.com/index.php?X=MATH
#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=(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>

View File

@@ -9,8 +9,14 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Renderer/ContextParameters.hpp>
class NzContext;
using NzContextConstRef = NzResourceRef<const NzContext>;
using NzContextRef = NzResourceRef<NzContext>;
class NzContextImpl;
class NAZARA_API NzContext : public NzResource

View File

@@ -9,8 +9,11 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Color.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Renderer/Shader.hpp>
#include <Nazara/Renderer/Texture.hpp>
#include <Nazara/Renderer/TextureSampler.hpp>
@@ -23,9 +26,10 @@ struct NAZARA_API NzMaterialParams
};
class NzMaterial;
class NzShader;
using NzMaterialConstRef = NzResourceRef<const NzMaterial>;
using NzMaterialLoader = NzResourceLoader<NzMaterial, NzMaterialParams>;
using NzMaterialRef = NzResourceRef<NzMaterial>;
class NAZARA_API NzMaterial : public NzResource
{
@@ -35,7 +39,7 @@ class NAZARA_API NzMaterial : public NzResource
NzMaterial();
NzMaterial(const NzMaterial& material);
NzMaterial(NzMaterial&& material);
~NzMaterial();
~NzMaterial() = default;
void Apply(const NzShader* shader) const;
@@ -102,6 +106,8 @@ class NAZARA_API NzMaterial : public NzResource
static NzMaterial* GetDefault();
private:
void Copy(const NzMaterial& material);
nzBlendFunc m_dstBlend;
nzBlendFunc m_srcBlend;
nzFaceCulling m_faceCulling;
@@ -113,11 +119,11 @@ class NAZARA_API NzMaterial : public NzResource
NzColor m_specularColor;
NzTextureSampler m_diffuseSampler;
NzTextureSampler m_specularSampler;
mutable const NzShader* m_customShader;
NzTexture* m_diffuseMap;
NzTexture* m_heightMap;
NzTexture* m_normalMap;
NzTexture* m_specularMap;
mutable NzShaderConstRef m_customShader;
NzTextureRef m_diffuseMap;
NzTextureRef m_heightMap;
NzTextureRef m_normalMap;
NzTextureRef m_specularMap;
bool m_alphaBlendingEnabled;
bool m_faceCullingEnabled;
bool m_lightingEnabled;

View File

@@ -11,6 +11,7 @@
#include <Nazara/Core/Color.hpp>
#include <Nazara/Core/NonCopyable.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Math/Vector2.hpp>
@@ -18,9 +19,14 @@
#include <Nazara/Math/Vector4.hpp>
#include <Nazara/Renderer/Enums.hpp>
class NzShaderImpl;
class NzShader;
class NzTexture;
using NzShaderConstRef = NzResourceRef<const NzShader>;
using NzShaderRef = NzResourceRef<NzShader>;
class NzShaderImpl;
class NAZARA_API NzShader : public NzResource, NzNonCopyable
{
friend class NzRenderer;

View File

@@ -9,11 +9,18 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/NonCopyable.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Utility/Image.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
class NzRenderTexture;
class NzTexture;
using NzTextureConstRef = NzResourceRef<const NzTexture>;
using NzTextureRef = NzResourceRef<NzTexture>;
struct NzTextureImpl;
class NAZARA_API NzTexture : public NzResource, NzNonCopyable

View File

@@ -10,6 +10,7 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Utility/Sequence.hpp>
@@ -27,7 +28,9 @@ struct NAZARA_API NzAnimationParams
class NzAnimation;
class NzSkeleton;
using NzAnimationConstRef = NzResourceRef<const NzAnimation>;
using NzAnimationLoader = NzResourceLoader<NzAnimation, NzAnimationParams>;
using NzAnimationRef = NzResourceRef<NzAnimation>;
struct NzAnimationImpl;

View File

@@ -10,8 +10,14 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/NonCopyable.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Utility/Enums.hpp>
class NzBuffer;
using NzBufferConstRef = NzResourceRef<const NzBuffer>;
using NzBufferRef = NzResourceRef<NzBuffer>;
class NzBufferImpl;
class NAZARA_API NzBuffer : public NzResource, NzNonCopyable

View File

@@ -12,6 +12,7 @@
#include <Nazara/Core/InputStream.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Math/Cube.hpp>
#include <Nazara/Math/Rect.hpp>
#include <Nazara/Math/Vector3.hpp>
@@ -39,7 +40,9 @@ struct NAZARA_API NzImageParams
class NzImage;
using NzImageConstRef = NzResourceRef<const NzImage>;
using NzImageLoader = NzResourceLoader<NzImage, NzImageParams>;
using NzImageRef = NzResourceRef<NzImage>;
class NAZARA_API NzImage : public NzResource
{

View File

@@ -9,8 +9,14 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Utility/Buffer.hpp>
class NzIndexBuffer;
using NzIndexBufferConstRef = NzResourceRef<const NzIndexBuffer>;
using NzIndexBufferRef = NzResourceRef<NzIndexBuffer>;
class NAZARA_API NzIndexBuffer : public NzResource
{
public:
@@ -40,7 +46,7 @@ class NAZARA_API NzIndexBuffer : public NzResource
void Unmap() const;
private:
NzBuffer* m_buffer;
NzBufferRef m_buffer;
bool m_ownsBuffer;
unsigned int m_indexCount;
unsigned int m_startIndex;

View File

@@ -12,6 +12,7 @@
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceListener.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Math/Cube.hpp>
#include <Nazara/Utility/Skeleton.hpp>
@@ -36,7 +37,9 @@ class NzMesh;
typedef NzVertexStruct_XYZ_Normal_UV_Tangent NzMeshVertex;
using NzMeshConstRef = NzResourceRef<const NzMesh>;
using NzMeshLoader = NzResourceLoader<NzMesh, NzMeshParams>;
using NzMeshRef = NzResourceRef<NzMesh>;
struct NzMeshImpl;

View File

@@ -24,6 +24,11 @@ struct NzWeight
unsigned int jointIndex;
};
class NzSkeletalMesh;
using NzSkeletalMeshConstRef = NzResourceRef<const NzSkeletalMesh>;
using NzSkeletalMeshRef = NzResourceRef<NzSkeletalMesh>;
struct NzSkeletalMeshImpl;
class NAZARA_API NzSkeletalMesh final : public NzSubMesh

View File

@@ -11,6 +11,11 @@
#include <Nazara/Core/ResourceListener.hpp>
#include <Nazara/Utility/SubMesh.hpp>
class NzStaticMesh;
using NzStaticMeshConstRef = NzResourceRef<const NzStaticMesh>;
using NzStaticMeshRef = NzResourceRef<NzStaticMesh>;
class NAZARA_API NzStaticMesh final : public NzSubMesh, NzResourceListener
{
public:

View File

@@ -9,6 +9,7 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Math/Cube.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Utility/IndexBuffer.hpp>
@@ -16,6 +17,10 @@
#include <Nazara/Utility/VertexDeclaration.hpp>
class NzMesh;
class NzSubMesh;
using NzSubMeshConstRef = NzResourceRef<const NzSubMesh>;
using NzSubMeshRef = NzResourceRef<NzSubMesh>;
class NAZARA_API NzSubMesh : public NzResource
{

View File

@@ -9,9 +9,15 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Utility/Buffer.hpp>
#include <Nazara/Utility/VertexDeclaration.hpp>
class NzVertexBuffer;
using NzVertexBufferConstRef = NzResourceRef<NzVertexBuffer>;
using NzVertexBufferRef = NzResourceRef<NzVertexBuffer>;
class NAZARA_API NzVertexBuffer : public NzResource
{
public:
@@ -40,8 +46,8 @@ class NAZARA_API NzVertexBuffer : public NzResource
void Unmap() const;
private:
NzBuffer* m_buffer;
const NzVertexDeclaration* m_vertexDeclaration;
NzBufferRef m_buffer;
NzVertexDeclarationConstRef m_vertexDeclaration;
bool m_ownsBuffer;
unsigned int m_startVertex;
unsigned int m_vertexCount;

View File

@@ -9,6 +9,7 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Utility/Enums.hpp>
struct NzVertexElement
@@ -20,6 +21,11 @@ struct NzVertexElement
nzElementUsage usage;
};
class NzVertexDeclaration;
using NzVertexDeclarationConstRef = NzResourceRef<const NzVertexDeclaration>;
using NzVertexDeclarationRef = NzResourceRef<NzVertexDeclaration>;
struct NzVertexDeclarationImpl;
class NAZARA_API NzVertexDeclaration : public NzResource