SHARE
TWEET

Untitled

ilyasizov Oct 19th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. template<typename T>
  4. class SharedPtr {
  5.     T * p;
  6.     int *cnt;
  7.  
  8. public:
  9.  
  10.     SharedPtr() {
  11.         p = nullptr;
  12.     }
  13.  
  14.     SharedPtr(T * other) {
  15.         p = other;
  16.         cnt = new int;
  17.         *cnt = 1;
  18.     }
  19.  
  20.     SharedPtr(const SharedPtr& other) {
  21.         p = other.p;
  22.         cnt = other.cnt;
  23.         ++(*cnt);
  24.     }
  25.  
  26.     SharedPtr(SharedPtr&& other) {
  27.         p = std::move(other.p);
  28.         other.p = nullptr;
  29.         cnt = std::move(other.cnt);
  30.         other.cnt = nullptr;
  31.     }
  32.  
  33.     SharedPtr& operator = (T * other) {
  34.         T* d = p;
  35.         if (p != nullptr) {
  36.             *(--cnt);
  37.         }
  38.         p = other;
  39.         cnt = new int;
  40.         *cnt = 1;
  41.         if (d != nullptr) {
  42.             delete d;
  43.         }
  44.         return *this;
  45.     }
  46.  
  47.     SharedPtr& operator = (const SharedPtr& other) {
  48.         T* d = p;
  49.         if (p != nullptr) {
  50.             *(--cnt);
  51.         }
  52.         p = other.p;
  53.         cnt = other.cnt;
  54.         ++(*cnt);
  55.         if (d != nullptr) {
  56.             delete d;
  57.         }
  58.         return *this;
  59.     }
  60.  
  61.     SharedPtr& operator = (SharedPtr&& other) {
  62.         T* d = p;
  63.         if (p != nullptr) {
  64.             *(--cnt);
  65.         }
  66.         p = std::move(other.p);
  67.         cnt = std::move(other.cnt);
  68.         other.p = nullptr;
  69.         other.cnt = nullptr;
  70.         if (d != nullptr) {
  71.             delete d;
  72.         }
  73.         return *this;
  74.     }
  75.  
  76.     ~SharedPtr() {
  77.         if (p != nullptr) {
  78.             *(--cnt);
  79.             if (cnt == 0) {
  80.                 delete p;
  81.             }
  82.         }
  83.     }
  84.  
  85.     T& operator * () const {
  86.         return *p;
  87.     }
  88.  
  89.     T& operator * () {
  90.         return *p;
  91.     }
  92.  
  93.     T* operator -> () const {
  94.         return p;
  95.     }
  96.  
  97.     void reset(T* other) {
  98.         T* d = p;
  99.         if (p != nullptr) {
  100.             *(--cnt);
  101.         }
  102.         p = other;
  103.         cnt = new int;
  104.         *cnt = 1;
  105.         if (d != nullptr) {
  106.             delete d;
  107.         }
  108.     }
  109.  
  110.     void swap(SharedPtr& other) {
  111.         std::swap(p, other.p);
  112.         std::swap(cnt, other.cnt);
  113.     }
  114.  
  115.     T* get() const {
  116.         return p;
  117.     }
  118.  
  119.     explicit operator bool() const {
  120.         if (p != nullptr) {
  121.             return true;
  122.         }
  123.         return false;
  124.     }
  125. };
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top