Jelajahi Sumber

Fixing some bugs with inheritance in Properties files by writing a proper (private) copy constructor for Properties.

Adam Blake 14 tahun lalu
induk
melakukan
33a9914bc7
2 mengubah file dengan 30 tambahan dan 9 penghapusan
  1. 27 8
      gameplay/src/Properties.cpp
  2. 3 1
      gameplay/src/Properties.h

+ 27 - 8
gameplay/src/Properties.cpp

@@ -6,6 +6,28 @@
 namespace gameplay
 {
 
+Properties::Properties()
+{
+}
+
+Properties::Properties(const Properties& copy)
+{
+    _namespace = copy._namespace;
+    _id = copy._id;
+    _parentID = copy._parentID;
+
+    _properties = copy._properties;
+    _propertiesItr = _properties.end();
+    
+    _namespaces = std::vector<Properties*>();
+    std::vector<Properties*>::const_iterator it;
+    for (it = copy._namespaces.begin(); it < copy._namespaces.end(); it++)
+    {
+        _namespaces.push_back(new Properties(**it));
+    }
+    _namespacesItr = _namespaces.end();
+}
+
 Properties::Properties(FILE* file)
 {
     readProperties(file);
@@ -275,17 +297,15 @@ void Properties::resolveInheritance(const char* id)
 
                 // Copy the child.
                 Properties* overrides = new Properties(*derived);
-                overrides->_propertiesItr = overrides->_properties.end();
-                overrides->_namespacesItr = overrides->_namespaces.end();
 
                 // Copy data from the parent into the child.
                 derived->_properties = parent->_properties;
                 derived->_propertiesItr = derived->_properties.end();
                 derived->_namespaces = std::vector<Properties*>();
-                std::vector<Properties*>::const_iterator it;
-                for (it = parent->_namespaces.begin(); it < parent->_namespaces.end(); it++)
+                std::vector<Properties*>::const_iterator itt;
+                for (itt = parent->_namespaces.begin(); itt < parent->_namespaces.end(); itt++)
                 {
-                    derived->_namespaces.push_back(new Properties(**it));
+                    derived->_namespaces.push_back(new Properties(**itt));
                 }
                 derived->_namespacesItr = derived->_namespaces.end();
 
@@ -316,6 +336,7 @@ void Properties::mergeWith(Properties* overrides)
 {
     // Overwrite or add each property found in child.
     char* value = new char[255];
+    overrides->rewind();
     const char* name = overrides->getNextProperty(&value);
     while (name)
     {
@@ -336,7 +357,7 @@ void Properties::mergeWith(Properties* overrides)
         {
             if (strcmp(derivedNamespace->getNamespace(), overridesNamespace->getNamespace()) == 0 &&
                 strcmp(derivedNamespace->getId(), overridesNamespace->getId()) == 0)
-            {
+            {   
                 derivedNamespace->mergeWith(overridesNamespace);
                 merged = true;
             }
@@ -348,8 +369,6 @@ void Properties::mergeWith(Properties* overrides)
         {
             // Add this new namespace.
             Properties* newNamespace = new Properties(*overridesNamespace);
-            newNamespace->_propertiesItr = newNamespace->_properties.end();
-            newNamespace->_namespacesItr = newNamespace->_namespaces.end();
 
             this->_namespaces.push_back(newNamespace);
             this->_namespacesItr = this->_namespaces.end();

+ 3 - 1
gameplay/src/Properties.h

@@ -371,9 +371,11 @@ public:
 private:
     
     /**
-     * Constructor.
+     * Constructors.
      */
+    Properties();
     Properties(FILE* file);
+    Properties(const Properties& copy);
 
     /**
      * Constructor. Read from the beginning of namespace specified