]> git.tdb.fi Git - libs/gl.git/blobdiff - source/resourcemanager.cpp
Throw a resource_load_error if the source could not be opened
[libs/gl.git] / source / resourcemanager.cpp
index c6e3f357b9ce67b6f72fef04f30b5ff5e96483ab..733cfaff8c4c37ba5ae1ffbd605ceb3f8d6a58df 100644 (file)
@@ -11,6 +11,10 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
+resource_load_error::resource_load_error(const string &name, const string &err):
+       runtime_error(format("%s: %s", name, err))
+{ }
+
 resource_load_error::resource_load_error(const string &name, const exception &exc):
        runtime_error(format("%s: %s: %s", name, Debug::demangle(typeid(exc).name()), exc.what()))
 { }
@@ -70,19 +74,29 @@ void *ResourceManager::get_data_for_resource(const Resource &r)
 }
 
 void ResourceManager::set_resource_location(Resource &r, DataFile::Collection &c, const string &n)
+{
+       set_resource_location(r, ResourceLocation(c, n));
+}
+
+void ResourceManager::set_resource_location(Resource &r, const ResourceLocation &l)
 {
        ManagedResource &managed = get_item(resources, &r);
-       managed.collection = &c;
-       managed.name = n;
+       managed.location = l;
 
        if(policy==LOAD_IMMEDIATELY)
                load_resource(r);
 }
 
+const ResourceManager::ResourceLocation *ResourceManager::get_resource_location(const Resource &r) const
+{
+       const ManagedResource &managed = get_item(resources, &r);
+       return managed.location.collection ? &managed.location : 0;
+}
+
 void ResourceManager::load_resource(Resource &r)
 {
        ManagedResource &managed = get_item(resources, &r);
-       if(!managed.collection)
+       if(!managed.location.collection)
                throw runtime_error("no location");
 
        if(managed.state!=ManagedResource::NOT_LOADED)
@@ -101,9 +115,17 @@ void ResourceManager::load_resource(Resource &r)
        }
 }
 
+bool ResourceManager::is_resource_loaded(const Resource &r) const
+{
+       ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
+       return managed ? managed->state==ManagedResource::LOADED : false;
+}
+
 void ResourceManager::resource_used(const Resource &r)
 {
        ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
+       if(!managed)
+               return;
        if(managed->state==ManagedResource::NOT_LOADED && policy!=LOAD_MANUALLY)
                load_resource(*managed->resource);
 
@@ -236,9 +258,18 @@ bool ResourceManager::age_order(ManagedResource *mr1, ManagedResource *mr2)
 }
 
 
+ResourceManager::ResourceLocation::ResourceLocation():
+       collection(0)
+{ }
+
+ResourceManager::ResourceLocation::ResourceLocation(DataFile::Collection &c, const string &n):
+       collection(&c),
+       name(n)
+{ }
+
+
 ResourceManager::ManagedResource::ManagedResource(Resource &r):
        resource(&r),
-       collection(0),
        io(0),
        loader(0),
        state(NOT_LOADED),
@@ -248,7 +279,10 @@ ResourceManager::ManagedResource::ManagedResource(Resource &r):
 
 void ResourceManager::ManagedResource::start_loading()
 {
-       io = collection->open_raw(name);
+       io = location.collection->open_raw(location.name);
+       if(!io)
+               throw resource_load_error(location.name, "open failed");
+
        loader = resource->load(*io);
        if(!loader)
        {
@@ -344,7 +378,7 @@ void ResourceManager::LoadingThread::main()
                        catch(const exception &e)
                        {
                                MutexLock lock(queue_mutex);
-                               error_queue.push_back(resource_load_error(managed->name, e));
+                               error_queue.push_back(resource_load_error(managed->location.name, e));
                                managed->state = ManagedResource::LOAD_ERROR;
                        }