Browse Source

Fixing the resource manager with shared libraries

Panagiotis Christopoulos Charitos 14 years ago
parent
commit
fa0feae142
5 changed files with 179 additions and 167 deletions
  1. 5 3
      src/rsrc/LightRsrc.cpp
  2. 156 12
      src/rsrc/ResourceManager.cpp
  3. 8 7
      src/rsrc/ResourceManager.h
  4. 0 143
      src/rsrc/ResourceManager.inl.h
  5. 10 2
      src/rsrc/RsrcPtr.h

+ 5 - 3
src/rsrc/LightRsrc.cpp

@@ -1,12 +1,14 @@
+#include "LightRsrc.h"
+#include "rsrc/Texture.h"
+#include "misc/PropertyTree.h"
+#include "core/Logger.h"
+#include "core/Globals.h"
 #include <cstring>
 #include <cstring>
 #include <string>
 #include <string>
 #include <boost/property_tree/ptree.hpp>
 #include <boost/property_tree/ptree.hpp>
 #include <boost/property_tree/xml_parser.hpp>
 #include <boost/property_tree/xml_parser.hpp>
 #include <boost/foreach.hpp>
 #include <boost/foreach.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/lexical_cast.hpp>
-#include "LightRsrc.h"
-#include "rsrc/Texture.h"
-#include "misc/PropertyTree.h"
 
 
 
 
 //==============================================================================
 //==============================================================================

+ 156 - 12
src/rsrc/ResourceManager.cpp

@@ -17,37 +17,181 @@
 #include "core/Logger.h"
 #include "core/Logger.h"
 
 
 
 
-//==============================================================================
-// Constructor                                                                 =
 //==============================================================================
 //==============================================================================
 ResourceManager::ResourceManager()
 ResourceManager::ResourceManager()
 {}
 {}
 
 
 
 
-//==============================================================================
-// Destructor                                                                  =
 //==============================================================================
 //==============================================================================
 ResourceManager::~ResourceManager()
 ResourceManager::~ResourceManager()
 {}
 {}
 
 
 
 
+//==============================================================================
+template<typename Type>
+void ResourceManager::allocAndLoadRsrc(const char* filename, Type*& newInstance)
+{
+	newInstance = NULL;
+
+	try
+	{
+		newInstance = new Type();
+		newInstance->load(filename);
+	}
+	catch(std::exception& e)
+	{
+		//ERROR("fuckkkkkkkkkk " << e.what());
+
+		/*if(newInstance != NULL)
+		{
+			delete newInstance;
+		}*/
+
+		throw EXCEPTION("Cannot load \"" + filename + "\": " + e.what());
+	}
+}
+
+
+//==============================================================================
+template<typename Type>
+typename ResourceManager::Types<Type>::Iterator ResourceManager::find(
+	const char* filename, typename Types<Type>::Container& container)
+{
+	typename Types<Type>::Iterator it = container.begin();
+	for(; it != container.end(); it++)
+	{
+		if(it->uuid == filename)
+		{
+			break;
+		}
+	}
+
+	return it;
+}
+
+
+//==============================================================================
+template<typename Type>
+typename ResourceManager::Types<Type>::Hook& ResourceManager::loadR(
+	const char* filename)
+{
+	// Chose container
+	typename Types<Type>::Container& c = choseContainer<Type>();
+
+	// Find
+	typename Types<Type>::Iterator it = find<Type>(filename, c);
+
+	// If already loaded
+	if(it != c.end())
+	{
+		++it->referenceCounter;
+	}
+	// else create new, load it and update the container
+	else
+	{
+		typename Types<Type>::Hook* hook = NULL;
+
+		try
+		{
+			hook = new typename Types<Type>::Hook;
+			hook->uuid = filename;
+			hook->referenceCounter = 1;
+			allocAndLoadRsrc<Type>(filename, hook->resource);
+
+			c.push_back(hook);
+
+			it = c.end();
+			--it;
+		}
+		catch(std::exception& e)
+		{
+			if(hook != NULL)
+			{
+				delete hook;
+			}
+
+			throw EXCEPTION("Cannot load \"" + filename + "\": " + e.what());
+		}
+	}
+
+	return *it;
+}
+
+
+//==============================================================================
+template<typename Type>
+void ResourceManager::unloadR(const typename Types<Type>::Hook& hook)
+{
+	typedef char TypeMustBeComplete[sizeof(Type) ? 1 : -1];
+    (void) sizeof(TypeMustBeComplete);
+
+	// Chose container
+	typename Types<Type>::Container& c = choseContainer<Type>();
+
+	// Find
+	typename Types<Type>::Iterator it = find<Type>(hook.uuid.c_str(), c);
+
+	// If not found
+	if(it == c.end())
+	{
+		throw EXCEPTION("Resource hook incorrect (\"" + hook.uuid + "\")");
+	}
+
+	if(it->uuid != hook.uuid)
+	{
+		INFO(it->uuid << " " << hook.uuid);
+	}
+
+	ERROR(it->uuid << " " << hook.uuid << " " << it->resource << " " <<
+		hook.resource << " " << dummyTex.get());
+	ASSERT(it->uuid == hook.uuid);
+	ASSERT(it->referenceCounter == hook.referenceCounter);
+
+	--it->referenceCounter;
+
+	// Delete the resource
+	if(it->referenceCounter == 0)
+	{
+		deallocRsrc(it->resource);
+		c.erase(it);
+	}
+}
+
+
+//==============================================================================
+template<typename T>
+void ResourceManager::deallocRsrc(T* rsrc)
+{
+	delete rsrc;
+}
+
+
+//==============================================================================
+// Template specialization                                                     =
+//==============================================================================
+
 // Because we are bored to write the same
 // Because we are bored to write the same
-#define SPECIALIZE_TEMPLATE_STUFF(type__, container__) \
+#define SPECIALIZE_TEMPLATE_STUFF(type, container) \
+	template<> \
+	ResourceManager::Types<type>::Hook& \
+		ResourceManager::load<type>(const char* filename) \
+	{ \
+		return loadR<type>(filename); \
+	} \
+	\
 	template<> \
 	template<> \
-	ResourceManager::Types<type__>::Container& \
-		ResourceManager::choseContainer<type__>() \
+	void ResourceManager::unload<type>(const Types<type>::Hook& info) \
 	{ \
 	{ \
-		return container__; \
+		unloadR<type>(info); \
 	} \
 	} \
 	\
 	\
 	template<> \
 	template<> \
-	void ResourceManager::deallocRsrc<type__>(type__* rsrc) \
+	ResourceManager::Types<type>::Container& \
+		ResourceManager::choseContainer<type>() \
 	{ \
 	{ \
-		delete rsrc; \
+		return container; \
 	}
 	}
 
 
-
-
 SPECIALIZE_TEMPLATE_STUFF(Texture, textures)
 SPECIALIZE_TEMPLATE_STUFF(Texture, textures)
 SPECIALIZE_TEMPLATE_STUFF(ShaderProgram, shaderProgs)
 SPECIALIZE_TEMPLATE_STUFF(ShaderProgram, shaderProgs)
 SPECIALIZE_TEMPLATE_STUFF(Material, materials)
 SPECIALIZE_TEMPLATE_STUFF(Material, materials)

+ 8 - 7
src/rsrc/ResourceManager.h

@@ -87,6 +87,14 @@ class ResourceManager
 		/// The same as dummyTex but for normals
 		/// The same as dummyTex but for normals
 		boost::scoped_ptr<Texture> dummyNormTex;
 		boost::scoped_ptr<Texture> dummyNormTex;
 
 
+		/// The real load function
+		template<typename Type>
+		typename Types<Type>::Hook& loadR(const char* filename);
+
+		/// The real unload function
+		template<typename Type>
+		void unloadR(const typename Types<Type>::Hook& info);
+
 		/// Find a resource using the filename
 		/// Find a resource using the filename
 		template<typename Type>
 		template<typename Type>
 		typename Types<Type>::Iterator find(const char* filename,
 		typename Types<Type>::Iterator find(const char* filename,
@@ -95,10 +103,6 @@ class ResourceManager
 		/// Specialized func
 		/// Specialized func
 		template<typename Type>
 		template<typename Type>
 		typename Types<Type>::Container& choseContainer();
 		typename Types<Type>::Container& choseContainer();
-
-		/// Unload a resource if no one uses it. This is the real deal
-		template<typename Type>
-		void unloadR(const typename Types<Type>::Hook& info);
 		
 		
 		/// Allocate and load a resource.
 		/// Allocate and load a resource.
 		/// This method allocates memory for a resource and loads it (calls the
 		/// This method allocates memory for a resource and loads it (calls the
@@ -128,7 +132,4 @@ inline size_t ResourceManager::getAsyncLoadingRequestsNum() const
 }
 }
 
 
 
 
-#include "ResourceManager.inl.h"
-
-
 #endif
 #endif

+ 0 - 143
src/rsrc/ResourceManager.inl.h

@@ -1,143 +0,0 @@
-#include "util/Exception.h"
-#include "core/Globals.h"
-#include "core/Logger.h"
-#include "util/Assert.h"
-
-
-//==============================================================================
-// allocAndLoadRsrc                                                            =
-//==============================================================================
-template<typename Type>
-void ResourceManager::allocAndLoadRsrc(const char* filename, Type*& newInstance)
-{
-	newInstance = NULL;
-
-	try
-	{
-		newInstance = new Type();
-		newInstance->load(filename);
-	}
-	catch(std::exception& e)
-	{
-		//ERROR("fuckkkkkkkkkk " << e.what());
-
-		/*if(newInstance != NULL)
-		{
-			delete newInstance;
-		}*/
-		
-		throw EXCEPTION("Cannot load \"" + filename + "\": " + e.what());
-	}
-}
-
-
-//==============================================================================
-// load                                                                        =
-//==============================================================================
-template<typename Type>
-typename ResourceManager::Types<Type>::Hook& ResourceManager::load(
-	const char* filename)
-{
-	// Chose container
-	typename Types<Type>::Container& c = choseContainer<Type>();
-
-	// Find
-	typename Types<Type>::Iterator it = find<Type>(filename, c);
-
-	// If already loaded
-	if(it != c.end())
-	{
-		++it->referenceCounter;
-	}
-	// else create new, load it and update the container
-	else
-	{
-		typename Types<Type>::Hook* hook = NULL;
-
-		try
-		{
-			hook = new typename Types<Type>::Hook;
-			hook->uuid = filename;
-			hook->referenceCounter = 1;
-			allocAndLoadRsrc<Type>(filename, hook->resource);
-
-			c.push_back(hook);
-
-			it = c.end();
-			--it;
-		}
-		catch(std::exception& e)
-		{
-			if(hook != NULL)
-			{
-				delete hook;
-			}
-
-			throw EXCEPTION("Cannot load \"" + filename + "\": " + e.what());
-		}
-	}
-
-	return *it;
-}
-
-
-//==============================================================================
-// unload                                                                      =
-//==============================================================================
-template<typename Type>
-void ResourceManager::unload(const typename Types<Type>::Hook& hook)
-{
-	typedef char TypeMustBeComplete[sizeof(Type) ? 1 : -1];
-    (void) sizeof(TypeMustBeComplete);
-
-	// Chose container
-	typename Types<Type>::Container& c = choseContainer<Type>();
-
-	// Find
-	typename Types<Type>::Iterator it = find<Type>(hook.uuid.c_str(), c);
-
-	// If not found
-	if(it == c.end())
-	{
-		throw EXCEPTION("Resource hook incorrect (\"" + hook.uuid + "\")");
-	}
-
-	if(it->uuid != hook.uuid)
-	{
-		INFO(it->uuid << " " << hook.uuid);
-	}
-
-	ERROR(it->uuid << " " << hook.uuid << " " << it->resource << " " <<
-		hook.resource << " " << dummyTex.get());
-	ASSERT(it->uuid == hook.uuid);
-	ASSERT(it->referenceCounter == hook.referenceCounter);
-
-	--it->referenceCounter;
-
-	// Delete the resource
-	if(it->referenceCounter == 0)
-	{
-		deallocRsrc(it->resource);
-		c.erase(it);
-	}
-}
-
-
-//==============================================================================
-// find [char*]                                                                =
-//==============================================================================
-template<typename Type>
-typename ResourceManager::Types<Type>::Iterator ResourceManager::find(
-	const char* filename, typename Types<Type>::Container& container)
-{
-	typename Types<Type>::Iterator it = container.begin();
-	for(; it != container.end(); it++)
-	{
-		if(it->uuid == filename)
-		{
-			break;
-		}
-	}
-
-	return it;
-}

+ 10 - 2
src/rsrc/RsrcPtr.h

@@ -2,6 +2,7 @@
 #define RSRC_PTR_H
 #define RSRC_PTR_H
 
 
 #include "ResourceManager.h"
 #include "ResourceManager.h"
+#include "core/Globals.h"
 
 
 
 
 /// This is a special smart pointer that points to Resource derivatives. It
 /// This is a special smart pointer that points to Resource derivatives. It
@@ -12,7 +13,9 @@ class RsrcPtr
 {
 {
 	public:
 	public:
 		/// Default constructor
 		/// Default constructor
-		RsrcPtr(): hook(NULL) {}
+		RsrcPtr()
+		:	hook(NULL)
+		{}
 
 
 		/// Copy constructor
 		/// Copy constructor
 		RsrcPtr(const RsrcPtr<Type>& a);
 		RsrcPtr(const RsrcPtr<Type>& a);
@@ -27,7 +30,12 @@ class RsrcPtr
 
 
 		Type& operator*() const;
 		Type& operator*() const;
 		Type* operator->() const;
 		Type* operator->() const;
-		Type* get() const {return hook->resource;}
+
+		Type* get() const
+		{
+			return hook->resource;
+		}
+
 		const std::string& getRsrcName() const;
 		const std::string& getRsrcName() const;
 
 
 	private:
 	private: