]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add consistency checks to PartCache
authorMikko Rasa <tdb@tdb.fi>
Sat, 11 Jan 2014 18:05:51 +0000 (20:05 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 11 Jan 2014 18:05:51 +0000 (20:05 +0200)
source/partcache.cpp
source/partcache.h

index 4747cb50b3e585fd45bb6a5be8fe864bd2937266..3c3fbb169e3e2c20b707f20b6bdb25f656fc94fd 100644 (file)
@@ -18,8 +18,16 @@ CachedPart::~CachedPart()
 }
 
 
+PartCache::PartCache():
+       rebuilding(false)
+{ }
+
 void PartCache::begin_rebuild()
 {
+       if(rebuilding)
+               throw logic_error("nested rebuild");
+       rebuilding = true;
+
        next = parts.begin();
        current = parts.end();
 }
@@ -28,6 +36,8 @@ void PartCache::insert_special(const Part &part)
 {
        if(part.get_name().empty())
                throw invalid_argument("PartCache::insert_special");
+       if(!rebuilding)
+               throw logic_error("!rebuilding");
 
        for(current=next; current!=parts.end(); ++current)
                if(current->part==&part)
@@ -48,6 +58,9 @@ void PartCache::insert_special(const Part &part)
 
 GL::Mesh &PartCache::create_mesh(const Part &part, const GL::Texture2D &tex)
 {
+       if(!rebuilding)
+               throw logic_error("!rebuilding");
+
        if(current!=parts.end() && current->texture==&tex)
                return *current->mesh;
 
@@ -76,6 +89,7 @@ GL::Mesh &PartCache::create_mesh(const Part &part, const GL::Texture2D &tex)
 
 void PartCache::end_rebuild()
 {
+       rebuilding = false;
        parts.erase(next, parts.end());
 }
 
index 90d1c94f5e36e90a19614cc39a9e8a56033d7945..969912431271d501b68267f9f09bb7b05f0427b6 100644 (file)
@@ -25,11 +25,14 @@ public:
        typedef std::list<CachedPart> PartList;
 
 private:
+       bool rebuilding;
        PartList parts;
        PartList::iterator next;
        PartList::iterator current;
 
 public:
+       PartCache();
+
        void begin_rebuild();
        void insert_special(const Part &);
        GL::Mesh &create_mesh(const Part &, const GL::Texture2D &);