]> git.tdb.fi Git - libs/gl.git/blobdiff - source/resourcemanager.h
Foundation for a resource management system
[libs/gl.git] / source / resourcemanager.h
diff --git a/source/resourcemanager.h b/source/resourcemanager.h
new file mode 100644 (file)
index 0000000..fb43533
--- /dev/null
@@ -0,0 +1,94 @@
+#ifndef MSP_GL_RESOURCEMANAGER_H_
+#define MSP_GL_RESOURCEMANAGER_H_
+
+#include <msp/core/mutex.h>
+#include <msp/core/semaphore.h>
+#include <msp/core/thread.h>
+#include <msp/datafile/collection.h>
+#include "resource.h"
+
+namespace Msp {
+namespace GL {
+
+class ResourceManager
+{
+public:
+       enum LoadingPolicy
+       {
+               LOAD_IMMEDIATELY,
+               LOAD_ON_DEMAND,
+               LOAD_MANUALLY
+       };
+
+private:
+       struct ManagedResource
+       {
+               Resource *resource;
+               DataFile::Collection *collection;
+               std::string name;
+               IO::Seekable *io;
+               Resource::AsyncLoader *loader;
+
+               ManagedResource(Resource &);
+       };
+
+       class LoadingThread: public Thread
+       {
+       public:
+               enum State
+               {
+                       IDLE,
+                       SYNC_PENDING,
+                       BUSY,
+                       LOAD_FINISHED,
+                       TERMINATING
+               };
+
+       private:
+               ResourceManager &manager;
+               Semaphore sem;
+               ManagedResource *volatile resource;
+               volatile State state;
+
+       public:
+               LoadingThread(ResourceManager &);
+
+       private:
+               virtual void main();
+
+       public:
+               void set_resource(ManagedResource *);
+               ManagedResource *get_resource() const { return resource; }
+               void sync();
+               State get_state() const { return state; }
+
+               void terminate();
+       };
+
+       typedef std::list<ManagedResource *> LoadQueue;
+
+       LoadingPolicy policy;
+       bool async_loads;
+       std::map<const Resource *, ManagedResource> resources;
+       std::list<ManagedResource *> queue;
+       LoadingThread thread;
+
+public:
+       ResourceManager();
+       ~ResourceManager();
+
+       void set_loading_policy(LoadingPolicy);
+       void set_async_loads(bool);
+
+       void add_resource(Resource &);
+       void set_resource_location(Resource &, DataFile::Collection &, const std::string &);
+       void load_resource(const Resource &);
+       void remove_resource(Resource &);
+
+       void tick();
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif