]> git.tdb.fi Git - libs/gl.git/blob - source/resourcemanager.h
Foundation for a resource management system
[libs/gl.git] / source / resourcemanager.h
1 #ifndef MSP_GL_RESOURCEMANAGER_H_
2 #define MSP_GL_RESOURCEMANAGER_H_
3
4 #include <msp/core/mutex.h>
5 #include <msp/core/semaphore.h>
6 #include <msp/core/thread.h>
7 #include <msp/datafile/collection.h>
8 #include "resource.h"
9
10 namespace Msp {
11 namespace GL {
12
13 class ResourceManager
14 {
15 public:
16         enum LoadingPolicy
17         {
18                 LOAD_IMMEDIATELY,
19                 LOAD_ON_DEMAND,
20                 LOAD_MANUALLY
21         };
22
23 private:
24         struct ManagedResource
25         {
26                 Resource *resource;
27                 DataFile::Collection *collection;
28                 std::string name;
29                 IO::Seekable *io;
30                 Resource::AsyncLoader *loader;
31
32                 ManagedResource(Resource &);
33         };
34
35         class LoadingThread: public Thread
36         {
37         public:
38                 enum State
39                 {
40                         IDLE,
41                         SYNC_PENDING,
42                         BUSY,
43                         LOAD_FINISHED,
44                         TERMINATING
45                 };
46
47         private:
48                 ResourceManager &manager;
49                 Semaphore sem;
50                 ManagedResource *volatile resource;
51                 volatile State state;
52
53         public:
54                 LoadingThread(ResourceManager &);
55
56         private:
57                 virtual void main();
58
59         public:
60                 void set_resource(ManagedResource *);
61                 ManagedResource *get_resource() const { return resource; }
62                 void sync();
63                 State get_state() const { return state; }
64
65                 void terminate();
66         };
67
68         typedef std::list<ManagedResource *> LoadQueue;
69
70         LoadingPolicy policy;
71         bool async_loads;
72         std::map<const Resource *, ManagedResource> resources;
73         std::list<ManagedResource *> queue;
74         LoadingThread thread;
75
76 public:
77         ResourceManager();
78         ~ResourceManager();
79
80         void set_loading_policy(LoadingPolicy);
81         void set_async_loads(bool);
82
83         void add_resource(Resource &);
84         void set_resource_location(Resource &, DataFile::Collection &, const std::string &);
85         void load_resource(const Resource &);
86         void remove_resource(Resource &);
87
88         void tick();
89 };
90
91 } // namespace GL
92 } // namespace Msp
93
94 #endif