]> git.tdb.fi Git - libs/gl.git/blobdiff - source/resourcemanager.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / resourcemanager.cpp
diff --git a/source/resourcemanager.cpp b/source/resourcemanager.cpp
deleted file mode 100644 (file)
index cc69b89..0000000
+++ /dev/null
@@ -1,213 +0,0 @@
-#include <msp/time/utils.h>
-#include "resourcemanager.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-ResourceManager::ResourceManager():
-       policy(LOAD_ON_DEMAND),
-       async_loads(true),
-       thread(*this)
-{ }
-
-ResourceManager::~ResourceManager()
-{
-       thread.terminate();
-
-       for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
-               i->second.resource->set_manager(0);
-}
-
-void ResourceManager::set_loading_policy(LoadingPolicy p)
-{
-       policy = p;
-}
-
-void ResourceManager::set_async_loads(bool a)
-{
-       async_loads = a;
-}
-
-void ResourceManager::add_resource(Resource &r)
-{
-       insert_unique(resources, &r, ManagedResource(r));
-}
-
-void *ResourceManager::get_data_for_resource(const Resource &r)
-{
-       return &get_item(resources, &r);
-}
-
-void ResourceManager::set_resource_location(Resource &r, DataFile::Collection &c, const string &n)
-{
-       ManagedResource &managed = get_item(resources, &r);
-       managed.collection = &c;
-       managed.name = n;
-
-       if(policy==LOAD_IMMEDIATELY)
-               load_resource(r);
-}
-
-void ResourceManager::load_resource(Resource &r)
-{
-       ManagedResource &managed = get_item(resources, &r);
-       if(!managed.collection)
-               throw runtime_error("no location");
-
-       if(managed.loader)
-               return;
-
-       managed.start_loading();
-
-       if(async_loads)
-               queue.push_back(&managed);
-       else
-       {
-               while(!managed.loader->process()) ;
-               managed.finish_loading();
-       }
-}
-
-void ResourceManager::resource_used(const Resource &r)
-{
-       ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
-       if(!managed->loaded && !managed->loader && policy!=LOAD_MANUALLY)
-               load_resource(*managed->resource);
-}
-
-void ResourceManager::remove_resource(Resource &r)
-{
-       ManagedResource *loading = thread.get_resource();
-       if(loading && loading->resource==&r)
-               thread.set_resource(0);
-
-       remove_existing(resources, &r);
-}
-
-void ResourceManager::tick()
-{
-       LoadingThread::State thread_state = thread.get_state();
-       if(thread_state==LoadingThread::SYNC_PENDING)
-               thread.sync();
-       else if(thread_state==LoadingThread::IDLE && !queue.empty())
-       {
-               ManagedResource *managed = queue.front();
-               queue.pop_front();
-               thread.set_resource(managed);
-       }
-}
-
-
-ResourceManager::ManagedResource::ManagedResource(Resource &r):
-       resource(&r),
-       collection(0),
-       io(0),
-       loader(0),
-       loaded(false)
-{ }
-
-void ResourceManager::ManagedResource::start_loading()
-{
-       io = collection->open_raw(name);
-       loader = resource->load(*io);
-       if(!loader)
-       {
-               delete io;
-               io = 0;
-               throw logic_error("no loader created");
-       }
-}
-
-void ResourceManager::ManagedResource::finish_loading()
-{
-       delete loader;
-       loader = 0;
-       loaded = true;
-       delete io;
-       io = 0;
-}
-
-
-ResourceManager::LoadingThread::LoadingThread(ResourceManager &m):
-       manager(m),
-       sem(1),
-       resource(0),
-       state(IDLE)
-{
-       launch();
-}
-
-void ResourceManager::LoadingThread::main()
-{
-       while(state!=TERMINATING)
-       {
-               sem.wait();
-
-               if(state==BUSY)
-               {
-                       Resource::AsyncLoader *ldr = resource->loader;
-                       bool finished = false;
-                       while(!finished && !ldr->needs_sync())
-                               finished = ldr->process();
-
-                       if(finished)
-                               state = LOAD_FINISHED;
-                       else
-                               state = SYNC_PENDING;
-               }
-       }
-}
-
-void ResourceManager::LoadingThread::set_resource(ManagedResource *r)
-{
-       if(state!=IDLE)
-       {
-               while(state==BUSY) ;
-               // Force finish to clean up the loader
-               state = LOAD_FINISHED;
-               sync();
-       }
-
-       resource = r;
-       state = BUSY;
-       sem.signal();
-}
-
-void ResourceManager::LoadingThread::sync()
-{
-       State s = state;
-       bool finished = (s==LOAD_FINISHED);
-       if(s==SYNC_PENDING)
-       {
-               Resource::AsyncLoader *ldr = resource->loader;
-               while(!finished && ldr->needs_sync())
-                       finished = ldr->process();
-
-               if(!finished)
-               {
-                       state = BUSY;
-                       sem.signal();
-                       return;
-               }
-       }
-
-       if(finished)
-       {
-               resource->finish_loading();
-               resource = 0;
-               state = IDLE;
-       }
-}
-
-void ResourceManager::LoadingThread::terminate()
-{
-       while(state==BUSY) ;
-       state = TERMINATING;
-       sem.signal();
-       join();
-}
-
-} // namespace GL
-} // namespace Msp