Преглед изворни кода

Changes some methods in Package to use std::vector instead of arrays. This makes error clean up simpler.

Darryl Gough пре 14 година
родитељ
комит
336e5181e8
2 измењених фајлова са 47 додато и 42 уклоњено
  1. 36 42
      gameplay/src/Package.cpp
  2. 11 0
      gameplay/src/Package.h

+ 36 - 42
gameplay/src/Package.cpp

@@ -66,12 +66,30 @@ bool Package::readArray(unsigned int* length, T** ptr)
     }
     }
     if (*length > 0)
     if (*length > 0)
     {
     {
-       *ptr = new T[*length];
-       if (fread(*ptr, sizeof(T), *length, _file) != *length)
-       {
-           SAFE_DELETE_ARRAY(*ptr);
-           return false;
-       }
+        *ptr = new T[*length];
+        if (fread(*ptr, sizeof(T), *length, _file) != *length)
+        {
+            SAFE_DELETE_ARRAY(*ptr);
+            return false;
+        }
+    }
+    return true;
+}
+
+template <class T>
+bool Package::readArray(unsigned int* length, std::vector<T>* values)
+{
+    if (!read(length))
+    {
+        return false;
+    }
+    if (*length > 0 && values)
+    {
+        values->resize(*length);
+        if (fread(&(*values)[0], sizeof(T), *length, _file) != *length)
+        {
+            return false;
+        }
     }
     }
     return true;
     return true;
 }
 }
@@ -87,20 +105,16 @@ std::string readString(FILE* fp)
     // Sanity check to detect if string length is far too big
     // Sanity check to detect if string length is far too big
     assert(length < PACKAGE_MAX_STRING_LENGTH);
     assert(length < PACKAGE_MAX_STRING_LENGTH);
 
 
-    char* str = new char[length + 1];
+    std::string str;
     if (length > 0)
     if (length > 0)
     {
     {
-        if (fread(str, 1, length, fp) != length)
+        str.resize(length);
+        if (fread(&str[0], 1, length, fp) != length)
         {
         {
-            SAFE_DELETE_ARRAY(str);
             return std::string();
             return std::string();
         }
         }
     }
     }
-
-    str[length] = '\0';
-    std::string result(str);
-    SAFE_DELETE_ARRAY(str);
-    return result;
+    return str;
 }
 }
 
 
 Package* Package::create(const char* path)
 Package* Package::create(const char* path)
@@ -907,11 +921,11 @@ Animation* Package::readAnimationChannel(Scene* scene, Animation* animation, con
         }
         }
     }
     }
 
 
-    unsigned long* keyTimes = NULL;
-    float* values = NULL;
-    float* tangentsIn = NULL;
-    float* tangentsOut = NULL;
-    unsigned int* interpolation = NULL;
+    std::vector<unsigned long> keyTimes;
+    std::vector<float> values;
+    std::vector<float> tangentsIn;
+    std::vector<float> tangentsOut;
+    std::vector<unsigned long> interpolation;
 
 
     // length of the arrays
     // length of the arrays
     unsigned int keyTimesCount;
     unsigned int keyTimesCount;
@@ -924,7 +938,6 @@ Animation* Package::readAnimationChannel(Scene* scene, Animation* animation, con
     if (!readArray(&keyTimesCount, &keyTimes))
     if (!readArray(&keyTimesCount, &keyTimes))
     {
     {
         LOG_ERROR_VARG("Failed to read %s for %s: %s", "keyTimes", "animation", id);
         LOG_ERROR_VARG("Failed to read %s for %s: %s", "keyTimes", "animation", id);
-        SAFE_DELETE_ARRAY(keyTimes);
         return NULL;
         return NULL;
     }
     }
     
     
@@ -932,8 +945,6 @@ Animation* Package::readAnimationChannel(Scene* scene, Animation* animation, con
     if (!readArray(&valuesCount, &values))
     if (!readArray(&valuesCount, &values))
     {
     {
         LOG_ERROR_VARG("Failed to read %s for %s: %s", "values", "animation", id);
         LOG_ERROR_VARG("Failed to read %s for %s: %s", "values", "animation", id);
-        SAFE_DELETE_ARRAY(keyTimes);
-        SAFE_DELETE_ARRAY(values);
         return NULL;
         return NULL;
     }
     }
     
     
@@ -941,9 +952,6 @@ Animation* Package::readAnimationChannel(Scene* scene, Animation* animation, con
     if (!readArray(&tangentsInCount, &tangentsIn))
     if (!readArray(&tangentsInCount, &tangentsIn))
     {
     {
         LOG_ERROR_VARG("Failed to read %s for %s: %s", "tangentsIn", "animation", id);
         LOG_ERROR_VARG("Failed to read %s for %s: %s", "tangentsIn", "animation", id);
-        SAFE_DELETE_ARRAY(keyTimes);
-        SAFE_DELETE_ARRAY(values);
-        SAFE_DELETE_ARRAY(tangentsIn);
         return NULL;
         return NULL;
     }
     }
     
     
@@ -951,10 +959,6 @@ Animation* Package::readAnimationChannel(Scene* scene, Animation* animation, con
     if (!readArray(&tangentsOutCount, &tangentsOut))
     if (!readArray(&tangentsOutCount, &tangentsOut))
     {
     {
         LOG_ERROR_VARG("Failed to read %s for %s: %s", "tangentsOut", "animation", id);
         LOG_ERROR_VARG("Failed to read %s for %s: %s", "tangentsOut", "animation", id);
-        SAFE_DELETE_ARRAY(keyTimes);
-        SAFE_DELETE_ARRAY(values);
-        SAFE_DELETE_ARRAY(tangentsIn);
-        SAFE_DELETE_ARRAY(tangentsOut);
         return NULL;
         return NULL;
     }
     }
     
     
@@ -962,11 +966,6 @@ Animation* Package::readAnimationChannel(Scene* scene, Animation* animation, con
     if (!readArray(&interpolationCount, &interpolation))
     if (!readArray(&interpolationCount, &interpolation))
     {
     {
         LOG_ERROR_VARG("Failed to read %s for %s: %s", "interpolation", "animation", id);
         LOG_ERROR_VARG("Failed to read %s for %s: %s", "interpolation", "animation", id);
-        SAFE_DELETE_ARRAY(keyTimes);
-        SAFE_DELETE_ARRAY(values);
-        SAFE_DELETE_ARRAY(tangentsIn);
-        SAFE_DELETE_ARRAY(tangentsOut);
-        SAFE_DELETE_ARRAY(interpolation);
         return NULL;
         return NULL;
     }
     }
 
 
@@ -976,23 +975,18 @@ Animation* Package::readAnimationChannel(Scene* scene, Animation* animation, con
     // TODO: Handle other target attributes later.
     // TODO: Handle other target attributes later.
     if (targetAttribute > 0)
     if (targetAttribute > 0)
     {
     {
+        assert(keyTimes.size() > 0 && values.size() > 0);
         if (animation == NULL)
         if (animation == NULL)
         {
         {
             // TODO: This code currently assumes LINEAR only
             // TODO: This code currently assumes LINEAR only
-            animation = controller->createAnimation(animationId, target, targetAttribute, keyTimesCount, keyTimes, values, Curve::LINEAR);
+            animation = controller->createAnimation(animationId, target, targetAttribute, keyTimesCount, &keyTimes[0], &values[0], Curve::LINEAR);
         }
         }
         else
         else
         {
         {
-            animation->createChannel(target, targetAttribute, keyTimesCount, keyTimes, values, Curve::LINEAR);
+            animation->createChannel(target, targetAttribute, keyTimesCount, &keyTimes[0], &values[0], Curve::LINEAR);
         }
         }
     }
     }
 
 
-    SAFE_DELETE_ARRAY(keyTimes);
-    SAFE_DELETE_ARRAY(values);
-    SAFE_DELETE_ARRAY(tangentsIn);
-    SAFE_DELETE_ARRAY(tangentsOut);
-    SAFE_DELETE_ARRAY(interpolation);
-
     return animation;
     return animation;
 }
 }
 
 

+ 11 - 0
gameplay/src/Package.h

@@ -252,6 +252,17 @@ private:
     template <class T>
     template <class T>
     bool readArray(unsigned int* length, T** ptr);
     bool readArray(unsigned int* length, T** ptr);
 
 
+    /**
+     * Reads an array of values and the array length from the current file position.
+     * 
+     * @param length A pointer to where the length of the array will be copied to.
+     * @param values A pointer to the vector to copy the values to. The vector will be resized if it is smaller than length.
+     * 
+     * @return True if successful, false if an error occurred.
+     */
+    template <class T>
+    bool readArray(unsigned int* length, std::vector<T>* values);
+
     /**
     /**
      * Reads 16 floats from the current file position.
      * Reads 16 floats from the current file position.
      *
      *