Vector and PMC ChangeLog
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 54 c064ce9f40f5
parent 53 6b3d7e3418c1
child 55 2a20d0184041
Vector and PMC ChangeLog * global var refCounter counts the objects * Copy Constructor implemented -> Surface cannot be copied ==> CC disabled -> Vector can be copied -> shows a warning committer: Markus Bröker <mbroeker@largo.homelinux.org>
pmc/cube.cc
pmc/include/cube.h
pmc/include/drawable.h
pmc/include/object.h
pmc/include/rectangle.h
pmc/include/surface.h
pmc/include/vector.h
pmc/main.cc
pmc/object.cc
pmc/rectangle.cc
pmc/surface.cc
pmc/vector.cc
vector.cc
--- a/pmc/cube.cc
+++ b/pmc/cube.cc
@@ -23,6 +23,8 @@
     P[5] = new Vector (125, 25);
     P[6] = new Vector (125, 125);
     P[7] = new Vector (25, 125);
+
+    refCounter++;
 }
 
 Cube::Cube (Surface * s, Vector & p1, Vector & p2, Vector & p3, Vector & p4, int h)
@@ -39,9 +41,11 @@
     P[3] = new Vector (p4);
 
     for (int i = 0; i < 4; i++) {
-        P[4 + i] = new Vector (*P[i]);
+        P[4 + i] = new Vector ();
         *P[4 + i] = *P[i] + location;
     }
+
+    refCounter++;
 }
 
 Cube::Cube (Surface * s, Vector p[4], int h)
@@ -57,6 +61,13 @@
         P[4 + i] = new Vector (*P[i]);
         *P[4 + i] = *P[i] + location;
     }
+
+    refCounter++;
+}
+
+Cube::Cube (const Cube & copy)
+{
+    fprintf (stderr, "Copy Constructor in Cube disabled...\n");
 }
 
 Cube::~Cube ()
--- a/pmc/include/cube.h
+++ b/pmc/include/cube.h
@@ -19,8 +19,9 @@
     Cube (Surface *, algebra::Vector &, algebra::Vector &, algebra::Vector &, algebra::Vector &, int);
     Cube (Surface *, algebra::Vector[4], int);
     Cube (Surface *);
+    Cube (const Cube &);
+    virtual ~ Cube ();
 
-    virtual ~ Cube ();
     void show ();
     void move (algebra::Vector);
 };
--- a/pmc/include/drawable.h
+++ b/pmc/include/drawable.h
@@ -16,6 +16,7 @@
 
   public:
     virtual ~ Drawable () { };
+
     virtual void move (algebra::Vector) = 0;
     virtual void show () = 0;
 };
--- a/pmc/include/object.h
+++ b/pmc/include/object.h
@@ -8,13 +8,17 @@
 
 #include <string>
 
+extern int refCounter;
+
 class Object {
   protected:
     std::string name;
 
   public:
     virtual ~ Object ();
+
     virtual std::string getName ();
     virtual Object getClass ();
+    int getInstances ();
 };
 #endif
--- a/pmc/include/rectangle.h
+++ b/pmc/include/rectangle.h
@@ -18,7 +18,9 @@
     Rectangle (Surface *, algebra::Vector & p1, algebra::Vector & p2, algebra::Vector & p3, algebra::Vector & p4);
     Rectangle (Surface *, algebra::Vector[4]);
     Rectangle (Surface *);
+    Rectangle (const Rectangle &);
     virtual ~ Rectangle ();
+
     void move (algebra::Vector);
     void show ();
 };
--- a/pmc/include/surface.h
+++ b/pmc/include/surface.h
@@ -20,17 +20,14 @@
     SDL_Surface *screen;
 
   public:
-    enum foregroundColor { BLACK=1, RED, GREEN, BLUE };
-      Surface (int w, int h, int d);
-     ~Surface ();
+    enum foregroundColor { BLACK = 1, RED, GREEN, BLUE };
 
-    int getWidth () {
-        return width;
-    };
+    Surface (int w, int h, int d);
+    Surface (const Surface &) { /* Copy Constructor disabled */ };
+    virtual ~Surface ();
 
-    int getHeight () {
-        return height;
-    };
+    int getWidth () { return width; };
+    int getHeight () { return height; };
 
     void drawPixel (int x, int y);
     void drawLine (int x1, int y1, int x2, int y2);
--- a/pmc/include/vector.h
+++ b/pmc/include/vector.h
@@ -14,6 +14,7 @@
         enum Mode { DEG, RAD, GRAD };
 
         Vector (int xx = 0, int yy = 0);
+        Vector (const Vector &);
         virtual ~ Vector ();
 
         int X ();
@@ -29,6 +30,7 @@
 
       private:
         Mode mode;
+
         int x;
         int y;
     };
--- a/pmc/main.cc
+++ b/pmc/main.cc
@@ -91,6 +91,11 @@
                 for (i = 0; i < MAX; i++)
                     d[i]->move ((Vector (x + STEP, y)));
                 break;
+
+            case SDLK_RETURN:
+                fprintf (stderr, "Objects remaining: %d\n", surface->getInstances ());
+                break;
+
             default:
                 break;
             }
@@ -108,5 +113,10 @@
 
     delete surface;
 
+    /*
+     * the remaining objects will be removed on exit
+     */
+    fprintf (stderr, "Objects remaining on exit: %d\n", refCounter);
+
     return EXIT_SUCCESS;
 }
--- a/pmc/object.cc
+++ b/pmc/object.cc
@@ -7,11 +7,14 @@
 
 #include <iostream>
 
+int refCounter = 0;
+
 Object::~Object ()
 {
 #ifdef DEBUG
     std::cerr << "Freeing Object " << name << std::endl;
 #endif
+    refCounter--;
 }
 
 std::string Object::getName ()
@@ -23,3 +26,8 @@
 {
     return *this;
 }
+
+int Object::getInstances ()
+{
+    return refCounter;
+}
--- a/pmc/rectangle.cc
+++ b/pmc/rectangle.cc
@@ -19,6 +19,8 @@
     P[1] = new Vector (0, 100);
     P[2] = new Vector (100, 100);
     P[3] = new Vector (0, 100);
+
+    refCounter++;
 }
 
 Rectangle::Rectangle (Surface * s, Vector & p1, Vector & p2, Vector & p3, Vector & p4)
@@ -32,6 +34,8 @@
     P[1] = new Vector (p2);
     P[2] = new Vector (p3);
     P[3] = new Vector (p4);
+
+    refCounter++;
 }
 
 Rectangle::Rectangle (Surface * s, Vector p[4])
@@ -44,6 +48,13 @@
     for (int i = 0; i < 4; i++) {
         P[i] = new Vector (p[i]);
     }
+
+    refCounter++;
+}
+
+Rectangle::Rectangle (const Rectangle & copy)
+{
+    fprintf (stderr, "Copy Constructor in Rectangle disabled...\n");
 }
 
 Rectangle::~Rectangle ()
--- a/pmc/surface.cc
+++ b/pmc/surface.cc
@@ -25,10 +25,13 @@
 
     color = red;
     bpp = screen->format->BytesPerPixel;
+
+    refCounter++;
 }
 
 Surface::~Surface ()
 {
+    SDL_FreeSurface (screen);
     SDL_Quit ();
 }
 
--- a/pmc/vector.cc
+++ b/pmc/vector.cc
@@ -17,11 +17,22 @@
     y = yy;
     mode = RAD;
 
+    refCounter++;
+
 #ifdef DEBUG
     fprintf (stderr, "New Vector: (%.3d, %.3d)\n", x, y);
 #endif
 }
 
+Vector::Vector (const Vector & copy)
+:x (copy.x), y (copy.y)
+{
+    fprintf (stderr, "Warning: Copy Constructor: ");
+    vector ();
+    fprintf (stderr, "\n");
+    refCounter++;
+}
+
 Vector::~Vector ()
 {
 #ifdef DEBUG
--- a/vector.cc
+++ b/vector.cc
@@ -10,6 +10,8 @@
 #define GETRANDOM(max) (1+(int)((float)max*rand()/RAND_MAX+1.0))
 #define MAX 6
 
+static int refCounter = 0;
+
 namespace algebra {
     class Vector {
       private:
@@ -18,6 +20,7 @@
 
       public:
         Vector (int xx = 0, int yy = 0);
+        Vector (const Vector &);
        ~Vector ();
 
         int X () { return x; };
@@ -33,10 +36,19 @@
     Vector::Vector (int xx, int yy) {
         x = xx;
         y = yy;
+
+        refCounter++;
+    }
+
+    Vector::Vector (const Vector & copy)
+    : x (copy.x), y (copy.y) {
+        fprintf (stderr, "Warning Copy Constructor\n");
+        refCounter++;
     }
 
     Vector::~Vector () {
         fprintf (stderr, "Removing Vector (%.3d, %.3d)\n", x, y);
+        refCounter--;
     }
 
     Vector Vector::operator+ (Vector & v) {
@@ -60,7 +72,7 @@
 
 int main (int argc, char **argv)
 {
-    Vector *v[MAX];
+    Vector **v;
     Vector result;
 
     srand (time (NULL));
@@ -70,6 +82,8 @@
         return EXIT_FAILURE;
     }
 
+    v = new Vector *[MAX];
+
     for (int i = 0; i < MAX; i++) {
         v[i] = new Vector (GETRANDOM (100), GETRANDOM (100));
     }
@@ -96,10 +110,12 @@
         printf (" = %3.2f\n", result.abs ());
     }
 
-    printf ("\nCLEANUP\n");
-
     for (int i = 0; i < MAX; i++)
         delete v[i];
 
+    delete [] v;
+
+    fprintf (stderr, "Objects remaining on exit: %d\n", refCounter);
+
     return EXIT_SUCCESS;
 }