Jelajahi Sumber

- Removing Arr
- AsyncLoader

Panagiotis Christopoulos Charitos 15 tahun lalu
induk
melakukan
84d887981c
3 mengubah file dengan 47 tambahan dan 129 penghapusan
  1. 26 69
      src/Core/AsyncLoader.cpp
  2. 21 20
      src/Core/AsyncLoader.h
  3. 0 40
      src/Util/Arr.h

+ 26 - 69
src/Core/AsyncLoader.cpp

@@ -13,29 +13,20 @@ void AsyncLoader::start()
 
 
 //======================================================================================================================
-// loadInPreallocatedBuff                                                                                              =
+// load                                                                                                                =
 //======================================================================================================================
-void AsyncLoader::loadInPreallocatedBuff(const char* filename, void* buff, size_t size)
+void AsyncLoader::load(const char* filename, bool (*func)(const char*, void*), void* storage)
 {
-	//std::cout << "pushing " << filename << "..." << std::endl;
-	boost::mutex::scoped_lock lock(mutexIn);
-	Request f = {filename, buff, size};
-	in.push_back(f);
+	INFO("New load request for \"" << filename << "\"");
+	boost::mutex::scoped_lock lock(mutexReq);
+	Request f = {filename, func, storage};
+	requests.push_back(f);
 	lock.unlock();
 
 	condVar.notify_one();
 }
 
 
-//======================================================================================================================
-// loadInNewBuff                                                                                                       =
-//======================================================================================================================
-void AsyncLoader::loadInNewBuff(const char* filename)
-{
-	loadInPreallocatedBuff(filename, NULL, 0);
-}
-
-
 //======================================================================================================================
 // workingFunc                                                                                                         =
 //======================================================================================================================
@@ -43,67 +34,34 @@ void AsyncLoader::workingFunc()
 {
 	while(1)
 	{
-		Request f;
+		Request req;
 
 		// Wait for something
 		{
-			boost::mutex::scoped_lock lock(mutexIn);
-			while(in.empty())
+			boost::mutex::scoped_lock lock(mutexReq);
+			while(requests.empty())
 			{
 				INFO("Waiting...");
 				condVar.wait(lock);
 			}
 
-			f = in.front();
-			in.pop_front();
-		}
-
-		// Load the file
-		INFO("Loading \"" << f.filename << "\"...");
-
-		std::ifstream is;
-		is.open(f.filename.c_str(), std::ios::binary);
-
-		if(!is.good())
-		{
-			ERROR("Cannot open \"" << f.filename << "\"");
-			continue;
+			req = requests.front();
+			requests.pop_front();
 		}
 
-		// Get size of file
-		is.seekg(0, std::ios::end);
-		size_t size = is.tellg();
-		is.seekg(0, std::ios::beg);
+		// Exec the loader
+		bool ok = req.func(req.filename.c_str(), req.storage);
 
-		// Alloc (if needed)
-		if(f.data == NULL && f.size == 0)
+		if(!ok)
 		{
-			f.size = size;
-			f.data = new char[f.size];
+			ERROR("Loading \"" << req.filename << "\" failed");
 		}
-		else if(f.size != size)
-		{
-			ERROR("Size mismatch \"" << f.filename << "\"");
-			is.close();
-			continue;
-		}
-
-		is.read((char*)f.data, f.size);
-
-		if(!is.good())
-		{
-			ERROR("Cannot read \"" << f.filename << "\"");
-			is.close();
-			continue;
-		}
-
-		is.close();
-		INFO("Request \"" << f.filename << "\"");
 
-		// Put the data in the out
+		// Put back the response
 		{
-			boost::mutex::scoped_lock lock(mutexOut);
-			out.push_back(f);
+			boost::mutex::scoped_lock lock(mutexResp);
+			Response resp = {req.filename, req.storage, ok};
+			responses.push_back(resp);
 		}
 	} // end thread loop
 }
@@ -112,21 +70,20 @@ void AsyncLoader::workingFunc()
 //======================================================================================================================
 // getLoaded                                                                                                           =
 //======================================================================================================================
-bool AsyncLoader::getLoaded(std::string& filename, void*& buff, size_t& size)
+bool AsyncLoader::getLoaded(std::string& filename, void* buff, bool& ok)
 {
-	boost::mutex::scoped_lock lock(mutexOut);
-	if(out.empty())
+	boost::mutex::scoped_lock lock(mutexResp);
+	if(responses.empty())
 	{
 		return false;
 	}
 
-	Request f = out.back();
-	out.pop_back();
+	Response resp = responses.back();
+	responses.pop_back();
 	lock.unlock();
 
-	filename = f.filename;
-	buff = f.data;
-	size = f.size;
+	filename = resp.filename;
+	buff = resp.storage;
 	return true;
 }
 

+ 21 - 20
src/Core/AsyncLoader.h

@@ -18,42 +18,43 @@ class AsyncLoader
 		/// Do nothing
 		~AsyncLoader() {}
 
-		/// Load a binary file and put the data in the preallocated buffer
-		void loadInPreallocatedBuff(const char* filename, void* buff, size_t size);
-
-		/// Load in a new buff
-		void loadInNewBuff(const char* filename);
-
-		void load(const char* filename, void (*func)(const char*, void*));
+		/// Tell me what to load, how to load it and where to store it
+		/// @param filename The file to load
+		/// @param func How to load the file
+		/// @param storage This points to the storage
+		void load(const char* filename, bool (*func)(const char*, void*), void* storage);
 
 		/// Query the loader and see if its got something
 		/// @param[out] filename The file that finished loading
-		/// @param[out] buff The data are stored in this buffer
-		/// @param[out] size The buffer size
+		/// @param[out] storage The data are stored in this buffer
 		/// @return Return true if there is something that finished loading
-		bool getLoaded(std::string& filename, void*& buff, size_t& size);
+		bool getLoaded(std::string& filename, void* storage, bool& ok);
 
 	private:
 		struct Request
 		{
 			std::string filename;
-			void* data;
-			size_t size;
-
+			bool (*func)(const char*, void*);
+			void* storage;
+		};
 
+		/// It contains a few things to identify the response
+		struct Response
+		{
+			std::string filename;
+			void* storage;
+			bool ok;
 		};
 
-		std::list<Request> in;
-		std::list<Request> out;
-		boost::mutex mutexIn;
-		boost::mutex mutexOut;
+		std::list<Request> requests;
+		std::list<Response> responses;
+		boost::mutex mutexReq;
+		boost::mutex mutexResp;
 		boost::thread thread;
 		boost::condition_variable condVar;
 
 		void workingFunc(); ///< The thread function
-
-		/// Start thread
-		void start();
+		void start(); ///< Start thread
 };
 
 

+ 0 - 40
src/Util/Arr.h

@@ -1,40 +0,0 @@
-#ifndef ARR_H
-#define ARR_H
-
-#include <boost/array.hpp>
-#include "Exception.h"
-
-
-/// This is a wrapper of array that adds new functionality
-template<typename Type, size_t nn>
-class Arr: public boost::array<Type, nn>
-{
-	public:
-		Type& operator[](size_t n);
-		const Type& operator[](size_t n) const;
-};
-
-
-//======================================================================================================================
-// operator[]                                                                                                          =
-//======================================================================================================================
-template<typename Type, size_t nn>
-Type& Arr<Type, nn>::operator[](size_t n)
-{
-	RASSERT_THROW_EXCEPTION(n >= nn);
-	return boost::array<Type, nn>::operator [](n);
-}
-
-
-//======================================================================================================================
-// operator[]                                                                                                          =
-//======================================================================================================================
-template<typename Type, size_t nn>
-const Type& Arr<Type, nn>::operator[](size_t n) const
-{
-	RASSERT_THROW_EXCEPTION(n >= nn);
-	return boost::array<Type, nn>::operator [](n);
-}
-
-
-#endif