]> git.tdb.fi Git - libs/gltk.git/blob - source/partcache.h
Use RAII to ensure consistent PartCache state
[libs/gltk.git] / source / partcache.h
1 #ifndef MSP_GLTK_PARTCACHE_H_
2 #define MSP_GLTK_PARTCACHE_H_
3
4 #include <msp/gl/mesh.h>
5 #include <msp/gl/texture2d.h>
6
7 namespace Msp {
8 namespace GLtk {
9
10 class Part;
11
12 struct CachedPart
13 {
14         const Part *part;
15         const GL::Texture2D *texture;
16         GL::Mesh *mesh;
17
18         CachedPart();
19         ~CachedPart();
20 };
21
22 class PartCache
23 {
24 public:
25         class Rebuild
26         {
27         private:
28                 PartCache &cache;
29
30         public:
31                 Rebuild(PartCache &c): cache(c) { cache.begin_rebuild(); }
32                 ~Rebuild() { cache.end_rebuild(); }
33         };
34
35         typedef std::list<CachedPart> PartList;
36
37 private:
38         bool rebuilding;
39         PartList parts;
40         PartList::iterator next;
41         PartList::iterator current;
42
43 public:
44         PartCache();
45
46         void begin_rebuild();
47         void insert_special(const Part &);
48         GL::Mesh &create_mesh(const Part &, const GL::Texture2D &);
49         void end_rebuild();
50
51         const PartList &get_parts() const { return parts; }
52 };
53
54 } // namespace GLtk
55 } // namespace Msp
56
57 #endif