From caa04d76bc031dd0c4a4157732deb340d934ceeb Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 11 Jan 2014 20:05:51 +0200 Subject: [PATCH] Add consistency checks to PartCache --- source/partcache.cpp | 14 ++++++++++++++ source/partcache.h | 3 +++ 2 files changed, 17 insertions(+) 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 &); -- 2.45.2