]> Shamusworld >> Repos - architektonas/blobdiff - src/container.cpp
In the middle of refactoring objects for loading/saving.
[architektonas] / src / container.cpp
index 2ee9c7afa2291d9591302d14d051c1ffd82217d1..48e412a9dc29ee29eada20f753dfa5576a304156 100644 (file)
@@ -35,30 +35,29 @@ Container::Container(const Container & copy): Object(copy.position, copy.parent)
 
 Container::~Container()
 {
-#if 0
-       // No memory leaks!
-       while (objects.size() > 0)
-       {
-               delete objects[0];
-               objects.erase(objects.begin());
-       }
-#else
        Clear();
-#endif
 }
 
 
 // Assignment operator
-Container & Container::operator=(const Container & copy)
+Container & Container::operator=(const Container & from)
 {
        // Take care of self-assignment
-       if (this == &copy)
+       if (this == &from)
                return *this;
 
        Clear();
 
-       for(int i=0; i<(int)copy.objects.size(); i++)
-               objects.push_back(copy.objects[i]);
+       // Small problem with this approach: if the copied object goes out of scope,
+       // all of the objects we copied in here will be deleted. D'oh!
+       for(uint i=0; i<from.objects.size(); i++)
+       {
+               Object * object = from.objects[i];
+
+               // Need to copy the object here...
+
+               objects.push_back(object);
+       }
 
        return *this;
 }
@@ -67,7 +66,10 @@ Container & Container::operator=(const Container & copy)
 /*virtual*/ void Container::Draw(Painter * painter)
 {
        for(int i=0; i<(int)objects.size(); i++)
+       {
+printf("Container: About to draw (object = $%X)\n", objects[i]);
                objects[i]->Draw(painter);
+       }
 }
 
 
@@ -152,7 +154,9 @@ Like so:
                        {
 Dimension * dimension = objects[i]->GetAttachedDimension();
 
-                               objects.erase(objects.begin() + i);     // Calls the destructor, (deletes the object, I presume... O_o)
+                               Object * objectToDelete = objects[i];
+                               objects.erase(objects.begin() + i);     // Calls the destructor, (deletes the object, I presume... O_o) [NOPE! SURE DOESN'T!]
+                               delete objectToDelete;
 
 // If this object had an attached dimension, reattach it to another object, if any...
 // The only problem with that approach is if the object it gets attached to is deleted,
@@ -246,7 +250,7 @@ about keeping track of old states...
 {
        needUpdate = false;
 
-       for(int i=0; i<(int)objects.size(); i++)
+       for(uint i=0; i<objects.size(); i++)
        {
                if (objects[i]->NeedsUpdate())
                        needUpdate = true;
@@ -259,6 +263,7 @@ about keeping track of old states...
 /*virtual*/ void Container::Add(Object * object)
 {
        objects.push_back(object);
+printf("Container: Added object (=$%X). size = %li\n", object, objects.size());
 }
 
 
@@ -267,7 +272,23 @@ void Container::Clear(void)
        // No memory leaks!
        while (objects.size() > 0)
        {
+printf("Container: Deleting object ($%X)...\n", objects[0]);
                delete objects[0];
                objects.erase(objects.begin());
        }
 }
+
+
+/*virtual*/ void Container::Enumerate(FILE * file)
+{
+       // Only put "CONTAINER" markers if *not* the top level container
+       if (parent != NULL)
+               fprintf(file, "CONTAINER\n");
+
+       for(uint i=0; i<objects.size(); i++)
+               objects[i]->Enumerate(file);
+
+       if (parent != NULL)
+               fprintf(file, "ENDCONTAINER\n");
+}
+