]> git.tdb.fi Git - libs/gl.git/commitdiff
Add a ResourceWatcher interface
authorMikko Rasa <tdb@tdb.fi>
Wed, 24 Sep 2014 12:49:41 +0000 (15:49 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 24 Sep 2014 12:49:41 +0000 (15:49 +0300)
source/resourcemanager.cpp
source/resourcemanager.h
source/resourcewatcher.h [new file with mode: 0644]

index a5c3691f9cb5caaae9d4f38af19ba6ac44c3bd7c..3456ac887a58f2e77d8e67ca7cf328a6fec8b1be 100644 (file)
@@ -1,5 +1,7 @@
+#include <algorithm>
 #include <msp/time/utils.h>
 #include "resourcemanager.h"
+#include "resourcewatcher.h"
 
 using namespace std;
 
@@ -110,6 +112,16 @@ void ResourceManager::remove_resource(Resource &r)
        remove_existing(resources, &r);
 }
 
+void ResourceManager::watch_resource(const Resource &r, ResourceWatcher &w)
+{
+       get_item(resources, &r).add_watcher(w);
+}
+
+void ResourceManager::unwatch_resource(const Resource &r, ResourceWatcher &w)
+{
+       get_item(resources, &r).remove_watcher(w);
+}
+
 void ResourceManager::tick()
 {
        LoadingThread::State thread_state = thread.get_state();
@@ -211,12 +223,31 @@ void ResourceManager::ManagedResource::finish_loading()
        delete io;
        io = 0;
        data_size = resource->get_data_size();
+
+       for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
+               (*i)->resource_loaded(*resource);
 }
 
 void ResourceManager::ManagedResource::unload()
 {
        resource->unload();
        loaded = false;
+
+       for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
+               (*i)->resource_unloaded(*resource);
+}
+
+void ResourceManager::ManagedResource::add_watcher(ResourceWatcher &w)
+{
+       if(find(watchers.begin(), watchers.end(), &w)==watchers.end())
+               watchers.push_back(&w);
+}
+
+void ResourceManager::ManagedResource::remove_watcher(ResourceWatcher &w)
+{
+       vector<ResourceWatcher *>::iterator end = remove(watchers.begin(), watchers.end(), &w);
+       if(end!=watchers.end())
+               watchers.erase(end, watchers.end());
 }
 
 
index 60f61a421fe4b2b54685a2366cd339e9bfa9be2d..f5ccac3bbf9a212eefe8611a865bb25a059737b2 100644 (file)
@@ -11,6 +11,8 @@
 namespace Msp {
 namespace GL {
 
+class ResourceWatcher;
+
 class ResourceManager
 {
 public:
@@ -32,12 +34,16 @@ private:
                bool loaded;
                unsigned last_used;
                UInt64 data_size;
+               std::vector<ResourceWatcher *> watchers;
 
                ManagedResource(Resource &);
 
                void start_loading();
                void finish_loading();
                void unload();
+
+               void add_watcher(ResourceWatcher &);
+               void remove_watcher(ResourceWatcher &);
        };
 
        class LoadingThread: public Thread
@@ -104,6 +110,9 @@ public:
        void resource_used(const Resource &);
        void remove_resource(Resource &);
 
+       void watch_resource(const Resource &, ResourceWatcher &);
+       void unwatch_resource(const Resource &, ResourceWatcher &);
+
        void tick();
        UInt64 get_total_data_size() const;
 };
diff --git a/source/resourcewatcher.h b/source/resourcewatcher.h
new file mode 100644 (file)
index 0000000..60ffe5b
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef RESOURCEWATCHER_H_
+#define RESOURCEWATCHER_H_
+
+namespace Msp {
+namespace GL {
+
+class Resource;
+
+class ResourceWatcher
+{
+protected:
+       ResourceWatcher() { }
+public:
+       virtual ~ResourceWatcher() { }
+
+       virtual void resource_loaded(Resource &) { }
+       virtual void resource_unloaded(Resource &) { }
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif