markyrocks

smartpointer demo

Aug 20th, 2021 (edited)
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. template <typename T>
  2. class ptr {
  3.     size_t c;
  4. public:
  5.     T* p;
  6.     size_t* refcount;
  7.     ptr() {
  8.         c = 0;
  9.         refcount = &c;
  10.         *refcount = 0;
  11.         p = new T[1];
  12.     }
  13.     ptr(ptr& other) {
  14.         copy(other);
  15.     }
  16.     ptr& operator =(ptr& other) {
  17.         this->copy(other);
  18.         return *this;
  19.     }
  20.     ~ptr() {
  21.             delete p;
  22.     }
  23.     T& operator*() {
  24.         return *p;
  25.     }
  26. private:
  27.     void copy(ptr& other) {
  28.         p = other.p;
  29.         refcount = other.refcount;
  30.         c = other.c;
  31.     }
  32.     ptr& copy() {
  33.         return ptr(this);
  34.     }
  35. };
  36.  
  37. template<typename T>
  38. class ptr_unique {
  39.     ptr<T> p;
  40. public:
  41.     ptr_unique():p() {}
  42.     ptr_unique(ptr_unique& other) = delete;
  43.     T& operator*() {
  44.         return *p;
  45.     }
  46.     void operator=(T& val) {
  47.         *p = val;
  48.     }
  49. };
  50.  
  51. template<typename T>
  52. class ptr_shared {
  53. public:
  54. ptr<T> p;
  55.     ptr_shared() :p() {
  56.         (*p.refcount)++;
  57.     }
  58.     ptr_shared(ptr_shared& other) {
  59.         copy(other);
  60.     }
  61.     ~ptr_shared() {
  62.         (*p.refcount)--;
  63.         if (*p.refcount) {
  64.             p.p = nullptr;
  65.         }
  66.     }
  67.     ptr_shared& operator=(ptr_shared& other) {
  68.         this->copy(other);
  69.         return *this;
  70.     }
  71.     T& operator*(){
  72.         return *p;
  73.     }
  74. private:
  75.     void copy(ptr_shared& other) {
  76.         this->p = other.p;
  77.         (*p.refcount)++;
  78.     }
  79.     ptr_shared& copy() {
  80.         (*p.refcount)++;
  81.         return ptr_shared(this);
  82.     }
  83. };
  84.  
  85. template<typename T>
  86. class ptr_weak {
  87.     ptr<T> p;
  88. public:
  89.     ptr_weak() :p(){}
  90.     ptr_weak(ptr_weak& other) {
  91.         copy(other);
  92.     }
  93.     ptr_weak(ptr_shared<T>& shared) {
  94.         copy(shared);
  95.     }
  96.     ~ptr_weak() {
  97.         p.p = nullptr;
  98.     }
  99.     T& operator*() {
  100.             return *p;
  101.      }
  102.     ptr_weak& operator=(ptr_weak& other) {
  103.         this->copy(other);
  104.         return *this;
  105.     }
  106.     ptr_weak& operator=(ptr_shared<T>& shared) {
  107.         this->copy(shared);
  108.         return *this;
  109.     }
  110.     ptr_shared<T>& make_shared() {
  111.         (*p.refcount)++;
  112.         return (ptr_shared<T>&)*this;
  113.     }
  114.     void make_shared(ptr_weak& weak) {
  115.         *this = weak.make_shared();
  116.     }
  117.     bool still_alive() {
  118.         if (*p.refcount) { return true; }
  119.         return false;
  120.     }
  121. private:
  122.     void copy(ptr_shared<T>& other) {
  123.         p = other.p;
  124.     }
  125.     void copy(ptr_weak& other) {
  126.         p = other.p;
  127.     }
  128.     ptr_weak& copy() {
  129.         return *this;
  130.     }
  131. };
  132.  
  133. int main() {
  134.     //ptr_unique<int> a;
  135.     ////ptr_unique<int>b = a;  //errors as it should bc no copies
  136.  
  137.     //*a = 5;
  138.     //cout << *a << "\n";
  139. ptr_weak<int> w;
  140. {  
  141.     ptr_shared<int> a;
  142.     ptr_shared<int> b = a;
  143.     w = b;
  144.     ptr_shared<int> c = a;
  145.  
  146.     *a = 8;
  147. }
  148.     if (w.still_alive()) {
  149.         cout << *w;
  150.     }
  151.     else { cout << "dead"; }
  152. }
Add Comment
Please, Sign In to add comment