SHARE
TWEET

Untitled

a guest Dec 6th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include "RefCounter.h"
  3.  
  4. using namespace std;
  5.  
  6. template <typename T> class MySmartPointer
  7. {
  8. public:
  9.     MySmartPointer(T* pointer);
  10.     MySmartPointer(const MySmartPointer& otherSmartPointer);
  11.     MySmartPointer(T* arrayPointer[], int size);
  12.     ~MySmartPointer();
  13.  
  14.     T* getPointer() const;
  15.     int getCounter();
  16.  
  17.     T& operator*() { return (*pointer); }
  18.     T* operator->() { return pointer; }
  19.  
  20.     void operator=(const MySmartPointer& otherSmartPointer);
  21.  
  22.     void copyPointer(const MySmartPointer& otherSmartPointer);
  23.     void decreasePointer();
  24.     void decreaseArrayPointer();
  25.  
  26. private:
  27.     T* pointer;
  28.     RefCounter* counter;
  29.     int arraySize = 0;
  30. };
  31.  
  32. template <typename T>
  33. MySmartPointer<T>::MySmartPointer(T* pointer) {
  34.     this->pointer = pointer;
  35.     counter = new RefCounter();
  36.     counter->add();
  37. }
  38.  
  39. template <typename T>
  40. MySmartPointer<T>::MySmartPointer(const MySmartPointer& otherSmartPointer) {
  41.     if (otherSmartPointer.arraySize != 0) {
  42.         this->pointer = otherSmartPointer.pointer;
  43.         this->arraySize = otherSmartPointer.arraySize;
  44.     }
  45.     else this->pointer = otherSmartPointer.pointer;
  46.     this->counter = otherSmartPointer.counter;
  47.     counter->add();
  48.  
  49. }
  50.  
  51. template <typename T>
  52. MySmartPointer<T>::MySmartPointer(T* arrayPointer[], int size) {
  53.     if (size <= 0) throw (-1);
  54.     arraySize = size;
  55.     this->pointer = *arrayPointer;
  56.     counter = new RefCounter();
  57.     counter->add();
  58. }
  59.  
  60. template <typename T>
  61. MySmartPointer<T>::~MySmartPointer() {
  62.     if (arraySize > 0) {
  63.         decreaseArrayPointer();
  64.     }
  65.     else decreasePointer();
  66. }
  67.  
  68. template <typename T>
  69. void MySmartPointer<T>::copyPointer(const MySmartPointer& otherSmartPointer) {
  70.     if (otherSmartPointer.arraySize != 0) {
  71.         arraySize = otherSmartPointer.arraySize;
  72.         this->pointer = otherSmartPointer.getArrayPointer();
  73.     }
  74.     else this->pointer = otherSmartPointer.getPointer();
  75.  
  76.     this->counter = otherSmartPointer.counter;
  77.     counter->add();
  78. }
  79.  
  80. template <typename T>
  81. void MySmartPointer<T>::decreasePointer() {
  82.     if (counter->get() == 1) {
  83.         delete pointer;
  84.         delete counter;
  85.     }
  86.     else {
  87.         counter->dec();
  88.     }
  89. }
  90.  
  91. template <typename T>
  92. void MySmartPointer<T>::decreaseArrayPointer() {
  93.     if (counter->get() == 1) {
  94.         delete [] pointer;
  95.         delete counter;
  96.     }
  97.     else {
  98.         counter->dec();
  99.     }
  100. }
  101.  
  102. template <typename T>
  103. void MySmartPointer<T>::operator=(const MySmartPointer& otherSmartPointer) {
  104.     if (counter->get() == 0) {
  105.         copyPointer(otherSmartPointer);
  106.     }
  107.     else {
  108.         if (arraySize != 0) { decreaseArrayPointer(); }
  109.         else decreasePointer();
  110.         copyPointer(otherSmartPointer);
  111.     }
  112. }
  113.  
  114.  
  115. template <typename T>
  116. T* MySmartPointer<T>::getPointer() const {
  117.     return pointer;
  118. }
  119.  
  120. template <typename T>
  121. int MySmartPointer<T>::getCounter() {
  122.     return counter->get();
  123. }
  124.  
  125.  
  126. // MAIN:
  127.  
  128. IntClass* array[10];
  129.     for (int i = 0; i < 10; i++) {
  130.         array[i] = new IntClass(i);
  131.     }
  132.  
  133.     MySmartPointer<IntClass> pointer(array, 10);
  134.     MySmartPointer<IntClass> pointer2(pointer);
  135.     cout << pointer.getCounter();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top