Skip to main content

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_ */

Comments

Popular posts from this blog

Creating local variables In Assembly

Lets go over how to create local variables inside of a pure assembly source code. Much like always, you will start with a *.asm file that looks like this: source code SECTION .data SECTION .bss SECTION .text global main ;make main available to operating system(os) main: ;create the stack frame push ebp push mov ebp, esp ;destroy the stack frame mov esp, ebp pop ebp ret So, the above is the general layout of an NASM source file.  Our goal here is to create a local variable inside of the main method.  The only way to create a local variable is by using the stack.  Why?  Because we can only declare variable in storage locations and the only available storage locations are: text, bss, and data.  However, text section is only for code, so it is out of the question.  The bss and data sections are appealing, but to declare our "local" variable in these sections will defeat the purpose of these variables being local, t

Introduction to Linux Kernel Programming

The Linux kernel is designed as a mixture of a monolithic binary image and a micro-kernel.  This combination allows for the best of both worlds.  On the monolithic side, all the code for the kernel to work with the user and hardware is already installed and ready for fast access, but the downside is that to add more functionality you need to rebuild the entire kernel.   In a different manner, a micro-kernel is composed of small pieces  of code that can be meshed today and more pieces can be added or removed as needed.  However, the downside to micro-kernel is a slower performance. Adding a module to the Kernel Linux is organized as both monolithic, one huge binary, and micro-kernel, as you can add more functionality to it.  The process of adding more functionality to the kernel can be illustrated by the crude image to the left. The process begins by using the command insmod with the name of the kernel module you want (which usually ends with extension *.ko).  From here, the mod

NASM Programming

Many of you, if you are like me, might be interested in how assembly works.  You will be very surprised that assembly is very very easy, especially after you write a couple of simple programs.  But don't get me wrong, you will be frustrated at first, however that frustration, if you channel it right, will lead to serious life long learning and will give you a deeper appreciation of the beauty of assembly. For more tutorial on assembly and visualization of these information, visit my youtube channel . Okay so lets get started. We will be using Netwide Assembler (NASM) to write our program. The general format of NASM file is this: ;This is a comment SECTION .data ;declare variable here SECTION .bss ;declare actual, dynamic variable SECTION .text ;where your program code/assembly code lives ; Working with Data Section In your .data section, you can declare variables like this: nameOfVariable: db 32 ;this declares a variable names nameOfVariable with byte valu