From: Mikko Rasa Date: Sat, 11 Jan 2014 18:05:51 +0000 (+0200) Subject: Add consistency checks to PartCache X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=caa04d76bc031dd0c4a4157732deb340d934ceeb;p=libs%2Fgltk.git Add consistency checks to PartCache --- diff --git a/source/partcache.cpp b/source/partcache.cpp index 4747cb5..3c3fbb1 100644 --- a/source/partcache.cpp +++ b/source/partcache.cpp @@ -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()); } diff --git a/source/partcache.h b/source/partcache.h index 90d1c94..9699124 100644 --- a/source/partcache.h +++ b/source/partcache.h @@ -25,11 +25,14 @@ public: typedef std::list 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 &);