Prechádzať zdrojové kódy

Using an ugly hack to avoid writing my own auto_ptr

Panagiotis Christopoulos Charitos 15 rokov pred
rodič
commit
e303940728

+ 13 - 0
src/Main.cpp

@@ -412,6 +412,19 @@ int main(int argc, char* argv[])
 {
 	new App(argc, argv);
 
+	/*{
+		RsrcPtr<LightProps> l(RsrcMngr::lightProps.load("/users/panoscc/Desktop/test.txt"));
+		//l = RsrcMngr::lightProps.load("/users/panoscc/Desktop/test.txt");
+		INFO(l->getRsrcReferencesNum());
+		{
+			RsrcPtr<LightProps> l2 = RsrcMngr::lightProps.load("/users/panoscc/Desktop/test.txt");
+			INFO(l->getRsrcReferencesNum());
+		}
+		INFO(l->getRsrcReferencesNum());
+	}
+
+	return 0;*/
+
 	init();
 
 	mainLoop();

+ 8 - 11
src/Resources/Core/RsrcContainer.h

@@ -17,20 +17,17 @@ class RsrcContainer: public Vec<Type*>
 	private:
 		typedef Vec<Type*> BaseClass;
 		typedef typename BaseClass::iterator Iterator; ///< Just to save me time from typing
-		typedef RsrcPtr<Type> RsrcPtrType;
 
 	public:
-		RsrcPtrType load(const char* fname)
-		{
-			Type* p = load2(fname);
+		RsrcContainer() {}
+		~RsrcContainer();
 
-			if(!p)
-			{
-				ERROR("See prev error");
-			}
-
-			return RsrcPtrType(p);
-		}
+		/**
+		 * The one and only public func
+		 * @param fname The file to load
+		 * @return A new resource ptr
+		 */
+		RsrcPtr<Type> load(const char* fname);
 
 	private:
 		/**

+ 13 - 3
src/Resources/Core/RsrcContainer.inl.h

@@ -2,6 +2,16 @@
 #include "RsrcContainer.h"
 
 
+//======================================================================================================================
+// Destructor                                                                                                          =
+//======================================================================================================================
+template<typename Type>
+RsrcContainer<Type>::~RsrcContainer()
+{
+	DEBUG_ERR(BaseClass::size() != 0); // this means that somehow a resource is still loaded
+}
+
+
 //======================================================================================================================
 // findByName                                                                                                          =
 //======================================================================================================================
@@ -95,8 +105,8 @@ Type* RsrcContainer<Type>::load2(const char* fname)
 //======================================================================================================================
 // load                                                                                                                =
 //======================================================================================================================
-/*template<typename Type>
-RsrcPtrType RsrcContainer<Type>::load(const char* fname)
+template<typename Type>
+RsrcPtr<Type> RsrcContainer<Type>::load(const char* fname)
 {
 	Type* p = load2(fname);
 
@@ -106,7 +116,7 @@ RsrcPtrType RsrcContainer<Type>::load(const char* fname)
 	}
 
 	return RsrcPtr<Type>(p);
-}*/
+}
 
 
 //======================================================================================================================

+ 31 - 42
src/Resources/Core/RsrcPtr.h

@@ -5,36 +5,39 @@
 
 
 /**
- * This is a special pointer to a resource. Its not smart or anything, the only difference is that when its out of scope
- * it tries to unload the resource
+ * This is a special smart pointer that points to Resource derivatives. It looks like auto_ptr but the main difference
+ * is that when its out of scope it tries to unload the resource. The bad thing about this pointer it contains an ugly
+ * hack. Without this hack we should have build our own version of auto_ptr
  */
 template<typename Type>
 class RsrcPtr
 {
 	public:
 		/**
-		 * This constructor transfers ownership just like auto_ptr
+		 * This constructor transfers ownership just like auto_ptr despite the const reference (hack)
 		 */
-		template<typename Type1>
-		RsrcPtr(RsrcPtr<Type1>& a);
+		RsrcPtr(const RsrcPtr& a);
 
 		/**
 		 * This constructor is for resource container only
 		 */
 		explicit RsrcPtr(Type* p_ = NULL);
 
+		/**
+		 * It unloads the resource or it decreases its reference counter
+		 */
 		~RsrcPtr();
 
+		Type* release();
 
-		template<typename Type1>
-		RsrcPtr<Type1>& operator=(RsrcPtr<Type1>& a);
+		/**
+		 * It transfers ownership despite the const reference (hack)
+		 */
+		RsrcPtr& operator=(const RsrcPtr& a);
 
-		Type& operator*();
-		const Type& operator*() const;
-		Type* operator->();
-		const Type* operator->() const;
-		Type* get();
-		const Type* get() const;
+		Type& operator*() const;
+		Type* operator->() const;
+		Type* get() const;
 
 	private:
 		Type* p;
@@ -46,12 +49,9 @@ class RsrcPtr
 //======================================================================================================================
 
 template<typename Type>
-template<typename Type1>
-RsrcPtr<Type>::RsrcPtr(RsrcPtr<Type1>& a)
-{
-	p = a.p;
-	a.p = NULL;
-}
+RsrcPtr<Type>::RsrcPtr(const RsrcPtr& a):
+	p(const_cast<RsrcPtr&>(a).release())
+{}
 
 
 template<typename Type>
@@ -64,60 +64,49 @@ template<typename Type>
 RsrcPtr<Type>::~RsrcPtr()
 {
 	if(p != NULL)
+	{
 		p->tryToUnoadMe();
+		release();
+	}
 }
 
 
 template<typename Type>
-template<typename Type1>
-RsrcPtr<Type1>& RsrcPtr<Type>::operator=(RsrcPtr<Type1>& a)
+RsrcPtr<Type>& RsrcPtr<Type>::operator=(const RsrcPtr<Type>& a)
 {
 	DEBUG_ERR(p != NULL);
-	p = a.p;
-	a.p = NULL;
+	p = const_cast<RsrcPtr&>(a).release();
 	return *this;
 }
 
 
 template<typename Type>
-Type& RsrcPtr<Type>::operator*()
-{
-	return *p;
-}
-
-
-template<typename Type>
-const Type& RsrcPtr<Type>::operator*() const
+Type& RsrcPtr<Type>::operator*() const
 {
 	return *p;
 }
 
 
 template<typename Type>
-Type* RsrcPtr<Type>::operator->()
-{
-	return p;
-}
-
-
-template<typename Type>
-const Type* RsrcPtr<Type>::operator->() const
+Type* RsrcPtr<Type>::operator->() const
 {
 	return p;
 }
 
 
 template<typename Type>
-Type* RsrcPtr<Type>::get()
+Type* RsrcPtr<Type>::get() const
 {
 	return p;
 }
 
 
 template<typename Type>
-const Type* RsrcPtr<Type>::get() const
+Type* RsrcPtr<Type>::release()
 {
-	return p;
+	Type* p_ = p;
+	p = NULL;
+	return p_;
 }
 
 #endif

+ 1 - 1
src/Resources/LightProps.h

@@ -25,7 +25,7 @@ class LightProps: public Resource
 		LightProps();
 		virtual ~LightProps() {}
 		bool load(const char* filename);
-		void unload() {};
+		void unload() {}
 		const Texture* getTexture() const;
 
 	private:

+ 2 - 8
src/Util/Common.cpp

@@ -46,9 +46,6 @@ static string getFunctionFromPrettyFunction(const char* prettyFunction)
 //======================================================================================================================
 ostream& msgPrefix(MsgType msgType, const char* file, int line, const char* func)
 {
-	if(app == NULL)
-		::exit(1);
-
 	// select c stream
 	ostream* cs;
 
@@ -71,7 +68,7 @@ ostream& msgPrefix(MsgType msgType, const char* file, int line, const char* func
 
 
 	// print terminal color
-	if(app->isTerminalColoringEnabled())
+	if(app && app->isTerminalColoringEnabled())
 	{
 		(*cs) << terminalColors[msgType];
 	}
@@ -117,10 +114,7 @@ ostream& msgPrefix(MsgType msgType, const char* file, int line, const char* func
 //======================================================================================================================
 ostream& msgSuffix(ostream& cs)
 {
-	if(app == NULL)
-		::exit(1);
-
-	if(app->isTerminalColoringEnabled())
+	if(app && app->isTerminalColoringEnabled())
 		cs << terminalColors[MT_NUM];
 
 	cs << endl;