Wednesday, March 21, 2012

C++ Smart Pointer


The following is the finalized code for a simple C++ smart pointer as demonstrated by my youtube video: 


here


Usage:
void main(){
     ...
    Pointer<ObjectType> pointer(new ObjectType);
    ...
}


  1. /*
  2.  * Pointer.h
  3.  *
  4.  *  Created on: Mar 16, 2012
  5.  *      Author: ukaku
  6.  */

  7. #ifndef POINTER_H_
  8. #define POINTER_H_

  9. #include <iostream>
  10. using namespace std;

  11. /**
  12.  * Reference represents a counter that will be incremented or decremented as
  13.  * the number of references to a pecticular pointer is made;
  14.  *
  15.  * where data means: a symbol used to point to, or associate to another object
  16.  */
  17. class ReferenceCounter{
  18. private:
  19.     int counter;
  20. public:
  21.     ReferenceCounter(){
  22.         counter=0;  //initially set to zero
  23.     }
  24.     void increase(){
  25.         counter++;  //increase it
  26.     }
  27.     int decrease(){
  28.         --counter;  //decrease it
  29.         if(counter<0){
  30.             cout << "ReferenceCouner is < 0"<<endl;
  31.         }
  32.         return counter; //return its current value
  33.     }
  34.     int getCount(){
  35.         return counter; //simply return the current value
  36.     }
  37. };


  38. template<class T>
  39. /**
  40.  * Behaves as a smart pointer
  41.  */
  42. class Pointer {
  43.     T* data;
  44.     ReferenceCounter* dataReferenceCounter;
  45. public:
  46.     Pointer() : data(NULL), dataReferenceCounter(NULL){
  47.         //set everything to null but allocate a new reference counter
  48.         dataReferenceCounter = new ReferenceCounter();
  49.         dataReferenceCounter->increase();
  50.     }
  51.     Pointer(T* xReference) : data(xReference), dataReferenceCounter(NULL){
  52.         dataReferenceCounter = new ReferenceCounter();
  53.         dataReferenceCounter->increase();
  54.     }


  55.     Pointer(const Pointer<T> &copy) : data(copy.data), dataReferenceCounter(copy.dataReferenceCounter){
  56.         dataReferenceCounter->increase();
  57.     }
  58.     /**
  59.      * Assignment operator
  60.      */
  61.     Pointer<T>& operator=(const Pointer<T> &other) {
  62.         //prevent self assignment
  63.         if(this != &other){
  64.             //decrease this current instances counter because we are about to override it
  65.             if(dataReferenceCounter->decrease()==0){
  66.                 delete data;
  67.                 delete dataReferenceCounter;
  68.             }
  69.             data = other.data;
  70.             dataReferenceCounter   = other.dataReferenceCounter;
  71.             dataReferenceCounter->increase();
  72.         }
  73.         return *this;
  74.     }

  75.     virtual ~Pointer(){
  76.         //!Only delete the data if and only if no other smart pointer
  77.         //!has a data this to data
  78.         if(dataReferenceCounter->decrease() == 0){
  79.             delete data;
  80.             delete dataReferenceCounter;
  81.         }
  82.     }

  83.     T& operator*(){
  84.         return *data;
  85.     }
  86.     T* operator->(){
  87.         return data;
  88.     }
  89. };
  90. #endif /* POINTER_H_ */