Browse Source

Remove ReferenceCountable from TextureResource

Michael Ragazzon 6 years ago
parent
commit
fd3e6268f6

+ 0 - 1
Include/RmlUi/Core/Decorator.h

@@ -42,7 +42,6 @@ class Element;
 class PropertyDictionary;
 class PropertyDictionary;
 class Property;
 class Property;
 struct Texture;
 struct Texture;
-class TextureResource;
 
 
 /**
 /**
 	The abstract base class for any visual object that can be attached to any element.
 	The abstract base class for any visual object that can be attached to any element.

+ 2 - 11
Include/RmlUi/Core/Texture.h

@@ -47,12 +47,6 @@ class RenderInterface;
 struct RMLUICORE_API Texture
 struct RMLUICORE_API Texture
 {
 {
 public:
 public:
-	/// Constructs an unloaded texture with no resource.
-	Texture();
-	/// Constructs a texture sharing the resource of another.
-	Texture(const Texture&);
-	~Texture();
-
 	/// Attempts to load a texture.
 	/// Attempts to load a texture.
 	/// @param[in] source The name of the texture.
 	/// @param[in] source The name of the texture.
 	/// @param[in] source_path The path of the resource that is requesting the texture (ie, the RCSS file in which it was specified, etc).
 	/// @param[in] source_path The path of the resource that is requesting the texture (ie, the RCSS file in which it was specified, etc).
@@ -61,7 +55,7 @@ public:
 
 
 	/// Returns the texture's source name. This is usually the name of the file the texture was loaded from.
 	/// Returns the texture's source name. This is usually the name of the file the texture was loaded from.
 	/// @return The name of the this texture's source. This will be the empty string if this texture is not loaded.
 	/// @return The name of the this texture's source. This will be the empty string if this texture is not loaded.
-	String GetSource() const;
+	const String& GetSource() const;
 	/// Returns the texture's handle.
 	/// Returns the texture's handle.
 	/// @param[in] The render interface that is requesting the handle.
 	/// @param[in] The render interface that is requesting the handle.
 	/// @return The texture's handle. This will be NULL if the texture isn't loaded.
 	/// @return The texture's handle. This will be NULL if the texture isn't loaded.
@@ -71,9 +65,6 @@ public:
 	/// @return The texture's dimensions. This will be (0, 0) if the texture isn't loaded.
 	/// @return The texture's dimensions. This will be (0, 0) if the texture isn't loaded.
 	Vector2i GetDimensions(RenderInterface* render_interface) const;
 	Vector2i GetDimensions(RenderInterface* render_interface) const;
 
 
-	/// Releases this texture's resource (if any), and sets it to another texture's resource.
-	const Texture& operator=(const Texture&);
-
 	/// Returns true if the texture points to the same underlying resource.
 	/// Returns true if the texture points to the same underlying resource.
 	bool operator==(const Texture&) const;
 	bool operator==(const Texture&) const;
 
 
@@ -81,7 +72,7 @@ public:
 	operator bool() const;
 	operator bool() const;
 
 
 private:
 private:
-	TextureResource* resource;
+	SharedPtr<TextureResource> resource;
 };
 };
 
 
 }
 }

+ 0 - 1
Source/Core/Decorator.cpp

@@ -29,7 +29,6 @@
 #include "precompiled.h"
 #include "precompiled.h"
 #include "../../Include/RmlUi/Core/Decorator.h"
 #include "../../Include/RmlUi/Core/Decorator.h"
 #include "TextureDatabase.h"
 #include "TextureDatabase.h"
-#include "TextureResource.h"
 #include "../../Include/RmlUi/Core/PropertyDefinition.h"
 #include "../../Include/RmlUi/Core/PropertyDefinition.h"
 
 
 namespace Rml {
 namespace Rml {

+ 0 - 1
Source/Core/DecoratorTiledBox.cpp

@@ -28,7 +28,6 @@
 
 
 #include "precompiled.h"
 #include "precompiled.h"
 #include "DecoratorTiledBox.h"
 #include "DecoratorTiledBox.h"
-#include "TextureResource.h"
 #include "../../Include/RmlUi/Core/Element.h"
 #include "../../Include/RmlUi/Core/Element.h"
 #include "../../Include/RmlUi/Core/Geometry.h"
 #include "../../Include/RmlUi/Core/Geometry.h"
 
 

+ 0 - 1
Source/Core/ElementImage.cpp

@@ -30,7 +30,6 @@
 #include "ElementImage.h"
 #include "ElementImage.h"
 #include "../../Include/RmlUi/Core.h"
 #include "../../Include/RmlUi/Core.h"
 #include "TextureDatabase.h"
 #include "TextureDatabase.h"
-#include "TextureResource.h"
 
 
 namespace Rml {
 namespace Rml {
 namespace Core {
 namespace Core {

+ 0 - 2
Source/Core/ElementImage.h

@@ -37,8 +37,6 @@
 namespace Rml {
 namespace Rml {
 namespace Core {
 namespace Core {
 
 
-class TextureResource;
-
 /**
 /**
 	The 'img' element. The image element can have a rectangular sub-region of its source texture
 	The 'img' element. The image element can have a rectangular sub-region of its source texture
 	specified with the 'coords' attribute; the element will render this region rather than the
 	specified with the 'coords' attribute; the element will render this region rather than the

+ 7 - 41
Source/Core/Texture.cpp

@@ -34,40 +34,19 @@
 namespace Rml {
 namespace Rml {
 namespace Core {
 namespace Core {
 
 
-// Constructs an unloaded texture with no resource.
-Texture::Texture()
-{
-	resource = NULL;
-}
-
-// Constructs a texture sharing the resource of another.
-Texture::Texture(const Texture& copy)
-{
-	resource = NULL;
-	*this = copy;
-}
-
-Texture::~Texture()
-{
-	if (resource)
-		resource->RemoveReference();
-}
-
 // Attempts to load a texture.
 // Attempts to load a texture.
 bool Texture::Load(const String& source, const String& source_path)
 bool Texture::Load(const String& source, const String& source_path)
 {
 {
-	if (resource != NULL)
-		resource->RemoveReference();
-
 	resource = TextureDatabase::Fetch(source, source_path);
 	resource = TextureDatabase::Fetch(source, source_path);
-	return resource != NULL;
+	return resource != nullptr;
 }
 }
 
 
 // Returns the texture's source name. This is usually the name of the file the texture was loaded from.
 // Returns the texture's source name. This is usually the name of the file the texture was loaded from.
-String Texture::GetSource() const
+const String& Texture::GetSource() const
 {
 {
-	if (resource == NULL)
-		return String();
+	static String empty_string;
+	if (!resource)
+		return empty_string;
 
 
 	return resource->GetSource();
 	return resource->GetSource();
 }
 }
@@ -75,7 +54,7 @@ String Texture::GetSource() const
 // Returns the texture's handle. 
 // Returns the texture's handle. 
 TextureHandle Texture::GetHandle(RenderInterface* render_interface) const
 TextureHandle Texture::GetHandle(RenderInterface* render_interface) const
 {
 {
-	if (resource == NULL)
+	if (!resource)
 		return 0;
 		return 0;
 
 
 	return resource->GetHandle(render_interface);
 	return resource->GetHandle(render_interface);
@@ -84,25 +63,12 @@ TextureHandle Texture::GetHandle(RenderInterface* render_interface) const
 // Returns the texture's dimensions.
 // Returns the texture's dimensions.
 Vector2i Texture::GetDimensions(RenderInterface* render_interface) const
 Vector2i Texture::GetDimensions(RenderInterface* render_interface) const
 {
 {
-	if (resource == NULL)
+	if (!resource)
 		return Vector2i(0, 0);
 		return Vector2i(0, 0);
 
 
 	return resource->GetDimensions(render_interface);
 	return resource->GetDimensions(render_interface);
 }
 }
 
 
-// Releases this texture's resource (if any), and sets it to another texture's resource.
-const Texture& Texture::operator=(const Texture& copy)
-{
-	if (resource != NULL)
-		resource->RemoveReference();
-
-	resource = copy.resource;
-	if (resource != NULL)
-		resource->AddReference();
-
-	return *this;
-}
-
 bool Texture::operator==(const Texture& other) const
 bool Texture::operator==(const Texture& other) const
 {
 {
 	return resource == other.resource;
 	return resource == other.resource;

+ 6 - 8
Source/Core/TextureDatabase.cpp

@@ -60,7 +60,7 @@ void TextureDatabase::Shutdown()
 
 
 // If the requested texture is already in the database, it will be returned with an extra reference count. If not, it
 // If the requested texture is already in the database, it will be returned with an extra reference count. If not, it
 // will be loaded through the application's render interface.
 // will be loaded through the application's render interface.
-TextureResource* TextureDatabase::Fetch(const String& source, const String& source_directory)
+SharedPtr<TextureResource> TextureDatabase::Fetch(const String& source, const String& source_directory)
 {
 {
 	String path;
 	String path;
 	if (source.size() > 0 && source[0] == '?')
 	if (source.size() > 0 && source[0] == '?')
@@ -71,15 +71,13 @@ TextureResource* TextureDatabase::Fetch(const String& source, const String& sour
 	TextureMap::iterator iterator = instance->textures.find(path);
 	TextureMap::iterator iterator = instance->textures.find(path);
 	if (iterator != instance->textures.end())
 	if (iterator != instance->textures.end())
 	{
 	{
-		(*iterator).second->AddReference();
-		return (*iterator).second;
+		return iterator->second;
 	}
 	}
 
 
-	TextureResource* resource = new TextureResource();
+	auto resource = std::make_shared<TextureResource>();
 	if (!resource->Load(path))
 	if (!resource->Load(path))
 	{
 	{
-		resource->RemoveReference();
-		return NULL;
+		return nullptr;
 	}
 	}
 
 
 	instance->textures[resource->GetSource()] = resource;
 	instance->textures[resource->GetSource()] = resource;
@@ -96,7 +94,7 @@ void TextureDatabase::ReleaseTextures()
 // Removes a texture from the database.
 // Removes a texture from the database.
 void TextureDatabase::RemoveTexture(TextureResource* texture)
 void TextureDatabase::RemoveTexture(TextureResource* texture)
 {
 {
-	if (instance != NULL)
+	if (instance)
 	{
 	{
 		TextureMap::iterator iterator = instance->textures.find(texture->GetSource());
 		TextureMap::iterator iterator = instance->textures.find(texture->GetSource());
 		if (iterator != instance->textures.end())
 		if (iterator != instance->textures.end())
@@ -107,7 +105,7 @@ void TextureDatabase::RemoveTexture(TextureResource* texture)
 // Release all textures bound through a render interface.
 // Release all textures bound through a render interface.
 void TextureDatabase::ReleaseTextures(RenderInterface* render_interface)
 void TextureDatabase::ReleaseTextures(RenderInterface* render_interface)
 {
 {
-	if (instance != NULL)
+	if (instance)
 	{
 	{
 		for (TextureMap::iterator i = instance->textures.begin(); i != instance->textures.end(); ++i)
 		for (TextureMap::iterator i = instance->textures.begin(); i != instance->textures.end(); ++i)
 			i->second->Release(render_interface);
 			i->second->Release(render_interface);

+ 2 - 2
Source/Core/TextureDatabase.h

@@ -50,7 +50,7 @@ public:
 
 
 	/// If the requested texture is already in the database, it will be returned with an extra
 	/// If the requested texture is already in the database, it will be returned with an extra
 	/// reference count. If not, it will be loaded through the application's render interface.
 	/// reference count. If not, it will be loaded through the application's render interface.
-	static TextureResource* Fetch(const String& source, const String& source_directory);
+	static SharedPtr<TextureResource> Fetch(const String& source, const String& source_directory);
 
 
 	/// Releases all textures in the database.
 	/// Releases all textures in the database.
 	static void ReleaseTextures();
 	static void ReleaseTextures();
@@ -65,7 +65,7 @@ private:
 	TextureDatabase();
 	TextureDatabase();
 	~TextureDatabase();
 	~TextureDatabase();
 
 
-	typedef UnorderedMap< String, TextureResource* > TextureMap;
+	typedef UnorderedMap< String, SharedPtr<TextureResource> > TextureMap;
 	TextureMap textures;
 	TextureMap textures;
 };
 };
 
 

+ 13 - 20
Source/Core/TextureResource.cpp

@@ -41,7 +41,7 @@ TextureResource::TextureResource()
 
 
 TextureResource::~TextureResource()
 TextureResource::~TextureResource()
 {
 {
-	TextureDatabase::RemoveTexture(this);
+	Release();
 }
 }
 
 
 // Attempts to load a texture from the application into the resource.
 // Attempts to load a texture from the application into the resource.
@@ -54,9 +54,9 @@ bool TextureResource::Load(const String& _source)
 }
 }
 
 
 // Returns the resource's underlying texture.
 // Returns the resource's underlying texture.
-TextureHandle TextureResource::GetHandle(RenderInterface* render_interface) const
+TextureHandle TextureResource::GetHandle(RenderInterface* render_interface)
 {
 {
-	TextureDataMap::iterator texture_iterator = texture_data.find(render_interface);
+	auto texture_iterator = texture_data.find(render_interface);
 	if (texture_iterator == texture_data.end())
 	if (texture_iterator == texture_data.end())
 	{
 	{
 		Load(render_interface);
 		Load(render_interface);
@@ -67,9 +67,9 @@ TextureHandle TextureResource::GetHandle(RenderInterface* render_interface) cons
 }
 }
 
 
 // Returns the dimensions of the resource's texture.
 // Returns the dimensions of the resource's texture.
-const Vector2i& TextureResource::GetDimensions(RenderInterface* render_interface) const
+const Vector2i& TextureResource::GetDimensions(RenderInterface* render_interface)
 {
 {
-	TextureDataMap::iterator texture_iterator = texture_data.find(render_interface);
+	auto texture_iterator = texture_data.find(render_interface);
 	if (texture_iterator == texture_data.end())
 	if (texture_iterator == texture_data.end())
 	{
 	{
 		Load(render_interface);
 		Load(render_interface);
@@ -88,13 +88,13 @@ const String& TextureResource::GetSource() const
 // Releases the texture's handle.
 // Releases the texture's handle.
 void TextureResource::Release(RenderInterface* render_interface)
 void TextureResource::Release(RenderInterface* render_interface)
 {
 {
-	if (render_interface == NULL)
+	if (!render_interface)
 	{
 	{
-		for (TextureDataMap::iterator texture_iterator = texture_data.begin(); texture_iterator != texture_data.end(); ++texture_iterator)
+		for (auto& interface_data_pair : texture_data)
 		{
 		{
-			TextureHandle handle = texture_iterator->second.first;
+			TextureHandle handle = interface_data_pair.second.first;
 			if (handle)
 			if (handle)
-				texture_iterator->first->ReleaseTexture(handle);
+				interface_data_pair.first->ReleaseTexture(handle);
 		}
 		}
 
 
 		texture_data.clear();
 		texture_data.clear();
@@ -114,16 +114,15 @@ void TextureResource::Release(RenderInterface* render_interface)
 }
 }
 
 
 // Attempts to load the texture from the source.
 // Attempts to load the texture from the source.
-bool TextureResource::Load(RenderInterface* render_interface) const
+bool TextureResource::Load(RenderInterface* render_interface)
 {
 {
 	// Check for special loader tokens.
 	// Check for special loader tokens.
-	if (!source.empty() &&
-		source[0] == '?')
+	if (!source.empty() && source[0] == '?')
 	{
 	{
 		Vector2i dimensions;
 		Vector2i dimensions;
 
 
 		bool delete_data = false;
 		bool delete_data = false;
-		const byte* data = NULL;
+		const byte* data = nullptr;
 
 
 		// Find the generation protocol and generate the data accordingly.
 		// Find the generation protocol and generate the data accordingly.
 		String protocol = source.substr(1, source.find("::") - 1);
 		String protocol = source.substr(1, source.find("::") - 1);
@@ -147,7 +146,7 @@ bool TextureResource::Load(RenderInterface* render_interface) const
 
 
 		// If texture data was generated, great! Otherwise, fallback to the LoadTexture() code and
 		// If texture data was generated, great! Otherwise, fallback to the LoadTexture() code and
 		// hope the client knows what the hell to do with the question mark in their file name.
 		// hope the client knows what the hell to do with the question mark in their file name.
-		if (data != NULL)
+		if (data)
 		{
 		{
 			TextureHandle handle;
 			TextureHandle handle;
 			bool success = render_interface->GenerateTexture(handle, data, dimensions);
 			bool success = render_interface->GenerateTexture(handle, data, dimensions);
@@ -184,11 +183,5 @@ bool TextureResource::Load(RenderInterface* render_interface) const
 	return true;
 	return true;
 }
 }
 
 
-void TextureResource::OnReferenceDeactivate()
-{
-	Release();
-	delete this;
-}
-
 }
 }
 }
 }

+ 8 - 14
Source/Core/TextureResource.h

@@ -42,12 +42,11 @@ namespace Core {
 	@author Peter Curry
 	@author Peter Curry
  */
  */
 
 
-class TextureResource : public ReferenceCountable
+class TextureResource : public NonCopyMoveable
 {
 {
-friend class TextureDatabase;
-
 public:
 public:
-	virtual ~TextureResource();
+	TextureResource();
+	~TextureResource();
 
 
 	/// Attempts to load a texture from the application into the resource. Note that this always
 	/// Attempts to load a texture from the application into the resource. Note that this always
 	/// succeeds now; as texture loading is now delayed until the texture is accessed by a specific
 	/// succeeds now; as texture loading is now delayed until the texture is accessed by a specific
@@ -55,31 +54,26 @@ public:
 	bool Load(const String& source);
 	bool Load(const String& source);
 
 
 	/// Returns the resource's underlying texture handle.
 	/// Returns the resource's underlying texture handle.
-	TextureHandle GetHandle(RenderInterface* render_interface) const;
+	TextureHandle GetHandle(RenderInterface* render_interface);
 	/// Returns the dimensions of the resource's texture.
 	/// Returns the dimensions of the resource's texture.
-	const Vector2i& GetDimensions(RenderInterface* render_interface) const;
+	const Vector2i& GetDimensions(RenderInterface* render_interface);
 
 
 	/// Returns the resource's source.
 	/// Returns the resource's source.
 	const String& GetSource() const;
 	const String& GetSource() const;
 
 
 	/// Releases the texture's handle.
 	/// Releases the texture's handle.
-	void Release(RenderInterface* render_interface = NULL);
+	void Release(RenderInterface* render_interface = nullptr);
 
 
 protected:
 protected:
 	/// Attempts to load the texture from the source.
 	/// Attempts to load the texture from the source.
-	bool Load(RenderInterface* render_interface) const;
-
-	/// Releases the texture and destroys the resource.
-	virtual void OnReferenceDeactivate();
+	bool Load(RenderInterface* render_interface);
 
 
 private:
 private:
-	TextureResource();
-
 	String source;
 	String source;
 
 
 	typedef std::pair< TextureHandle, Vector2i > TextureData;
 	typedef std::pair< TextureHandle, Vector2i > TextureData;
 	typedef SmallUnorderedMap< RenderInterface*, TextureData > TextureDataMap;
 	typedef SmallUnorderedMap< RenderInterface*, TextureData > TextureDataMap;
-	mutable TextureDataMap texture_data;
+	TextureDataMap texture_data;
 };
 };
 
 
 }
 }