From 3c09b67afba6d560eab5817016a98268e1544bc4 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 24 Sep 2014 15:49:41 +0300 Subject: [PATCH] Add a ResourceWatcher interface --- source/resourcemanager.cpp | 31 +++++++++++++++++++++++++++++++ source/resourcemanager.h | 9 +++++++++ source/resourcewatcher.h | 23 +++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 source/resourcewatcher.h diff --git a/source/resourcemanager.cpp b/source/resourcemanager.cpp index a5c3691f..3456ac88 100644 --- a/source/resourcemanager.cpp +++ b/source/resourcemanager.cpp @@ -1,5 +1,7 @@ +#include #include #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::const_iterator i=watchers.begin(); i!=watchers.end(); ++i) + (*i)->resource_loaded(*resource); } void ResourceManager::ManagedResource::unload() { resource->unload(); loaded = false; + + for(vector::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::iterator end = remove(watchers.begin(), watchers.end(), &w); + if(end!=watchers.end()) + watchers.erase(end, watchers.end()); } diff --git a/source/resourcemanager.h b/source/resourcemanager.h index 60f61a42..f5ccac3b 100644 --- a/source/resourcemanager.h +++ b/source/resourcemanager.h @@ -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 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 index 00000000..60ffe5ba --- /dev/null +++ b/source/resourcewatcher.h @@ -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 -- 2.45.2