]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/partcache.cpp
Make sure classes follow the rule of 0/3/5
[libs/gltk.git] / source / partcache.cpp
index 4747cb50b3e585fd45bb6a5be8fe864bd2937266..77b51d2cbc2096a101eb941b20e45f6b23568c6a 100644 (file)
@@ -6,11 +6,23 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
-CachedPart::CachedPart():
-       part(0),
-       texture(0),
-       mesh(0)
-{ }
+CachedPart::CachedPart(CachedPart &&other):
+       part(other.part),
+       texture(other.texture),
+       mesh(other.mesh)
+{
+       other.mesh = nullptr;
+}
+
+CachedPart &CachedPart::operator=(CachedPart &&other)
+{
+       delete mesh;
+       part = other.part;
+       texture = other.texture;
+       mesh = other.mesh;
+       other.mesh = nullptr;
+       return *this;
+}
 
 CachedPart::~CachedPart()
 {
@@ -20,6 +32,10 @@ CachedPart::~CachedPart()
 
 void PartCache::begin_rebuild()
 {
+       if(rebuilding)
+               throw logic_error("nested rebuild");
+       rebuilding = true;
+
        next = parts.begin();
        current = parts.end();
 }
@@ -28,6 +44,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 +66,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;
 
@@ -62,7 +83,7 @@ GL::Mesh &PartCache::create_mesh(const Part &part, const GL::Texture2D &tex)
        {
                current = parts.insert(next, CachedPart());
                current->texture = &tex;
-               current->mesh = new GL::Mesh((GL::TEXCOORD2, GL::COLOR4_UBYTE, GL::VERTEX2));
+               current->mesh = new GL::Mesh((GL::TEXCOORD2, GL::COLOR4,GL::UNSIGNED_BYTE, GL::VERTEX2));
        }
        else
                current->mesh->clear();
@@ -76,6 +97,7 @@ GL::Mesh &PartCache::create_mesh(const Part &part, const GL::Texture2D &tex)
 
 void PartCache::end_rebuild()
 {
+       rebuilding = false;
        parts.erase(next, parts.end());
 }