]> git.tdb.fi Git - libs/gl.git/blob - source/resourcemanager.h
Dissolve the association to resources when ResourceManager is destroyed
[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::map<const Resource *, ManagedResource> ResourceMap;
69         typedef std::list<ManagedResource *> LoadQueue;
70
71         LoadingPolicy policy;
72         bool async_loads;
73         ResourceMap resources;
74         LoadQueue queue;
75         LoadingThread thread;
76
77 public:
78         ResourceManager();
79         ~ResourceManager();
80
81         void set_loading_policy(LoadingPolicy);
82         void set_async_loads(bool);
83
84         void add_resource(Resource &);
85         void set_resource_location(Resource &, DataFile::Collection &, const std::string &);
86         void load_resource(const Resource &);
87         void remove_resource(Resource &);
88
89         void tick();
90 };
91
92 } // namespace GL
93 } // namespace Msp
94
95 #endif