Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <utility>
  3.  
  4. using namespace std;
  5.  
  6. struct ref_counter
  7. {
  8.     int c;
  9.     ref_counter() noexcept
  10.         : c(0)
  11.     {
  12.     }
  13.     ref_counter(int c) noexcept
  14.         : c(c)
  15.     {
  16.     }
  17.     void inc_count() noexcept {
  18.         ++c;
  19.     }
  20.     void dec_count() noexcept {
  21.         --c;
  22.     }
  23. };
  24.  
  25. template<typename T>
  26. class smart_ptr
  27. {
  28.     T* data;
  29.     ref_counter *ref_count;
  30. public:
  31.     template<typename U> friend class smart_ptr;
  32.  
  33.     smart_ptr() noexcept
  34.         : data(nullptr), ref_count(nullptr)
  35.     {
  36.     }
  37.     smart_ptr(T* data) noexcept
  38.         : data(data), ref_count(new ref_counter(1))
  39.     {
  40.     }
  41.     smart_ptr(const smart_ptr& a) noexcept
  42.         : data(a.data), ref_count(a.ref_count)
  43.     {
  44.         if (ref_count) {
  45.             ref_count->inc_count();
  46.         }
  47.     }
  48.     template<typename U>
  49.     smart_ptr(const smart_ptr<U>& a) noexcept
  50.         : data(a.data), ref_count(a.ref_count)
  51.     {
  52.         if (ref_count) {
  53.             ref_count->inc_count();
  54.         }
  55.     }
  56.     smart_ptr(smart_ptr&& a) noexcept
  57.     {
  58.         data = exchange(a.data, nullptr);
  59.         ref_count = exchange(a.ref_count, nullptr);
  60.     }
  61.     template<typename U>
  62.     smart_ptr(smart_ptr<U>&& a) noexcept
  63.     {
  64.         data = exchange(a.data, nullptr);
  65.         ref_count = exchange(a.ref_count, nullptr);
  66.     }
  67.     smart_ptr& operator=(const smart_ptr& a) noexcept {
  68.         if (this == &a) {
  69.             return *this;
  70.         }
  71.         if (data != nullptr) {
  72.             ref_count->dec_count();
  73.             if (!ref_count->c) {
  74.                 delete data;
  75.             }
  76.         }
  77.         data = a.data;
  78.         ref_count = a.ref_count;
  79.         ref_count->inc_count();
  80.         return *this;
  81.     }
  82.     smart_ptr& operator=(smart_ptr&& a) noexcept {
  83.          if (this == &a) {
  84.             return *this;
  85.         }
  86.         if (data != nullptr) {
  87.             ref_count->dec_count();
  88.             if (!ref_count->c) {
  89.                 delete data;
  90.             }
  91.         }
  92.         data = exchange(a.data, nullptr);
  93.         ref_count = exchange(a.ref_count, nullptr);
  94.         return *this;
  95.     }
  96.     template<typename U>
  97.     smart_ptr& operator=(const smart_ptr<U>& a) noexcept {
  98.         if (this == &a) {
  99.             return *this;
  100.         }
  101.         if (data != nullptr) {
  102.             ref_count->dec_count();
  103.             if (!ref_count->c) {
  104.                 delete data;
  105.             }
  106.         }
  107.         data = a.data;
  108.         ref_count = a.ref_count;
  109.         ref_count->inc_count();
  110.         return *this;
  111.     }
  112.     template<typename U>
  113.     smart_ptr& operator=(smart_ptr<U>&& a) noexcept {
  114.          if (this == &a) {
  115.             return *this;
  116.         }
  117.         if (data != nullptr) {
  118.             ref_count->dec_count();
  119.             if (!ref_count->c) {
  120.                 delete data;
  121.             }
  122.         }
  123.         data = exchange(a.data, nullptr);
  124.         ref_count = exchange(a.ref_count, nullptr);
  125.         return *this;
  126.     }
  127.    
  128.     T& operator*() const noexcept {
  129.         return *data;
  130.     }
  131.  
  132.     T* operator->() const noexcept {
  133.         return data;
  134.     }
  135.  
  136.     T* get() const noexcept {
  137.         return data;
  138.     }
  139.  
  140.     int use_count() const noexcept {
  141.         return ref_count == nullptr ? 0 : ref_count->c;
  142.     }
  143.  
  144.     explicit operator bool() const noexcept {
  145.         return data != nullptr;
  146.     }
  147.    
  148.     bool unique() const noexcept {
  149.         return ref_count != nullptr && ref_count->c == 1;
  150.     }
  151.  
  152.     ~smart_ptr() noexcept {
  153.         if (data) {
  154.             ref_count->dec_count();
  155.             if (ref_count->c == 0) {
  156.                 delete data;
  157.                 delete ref_count;
  158.             }
  159.         }
  160.     }
  161.  
  162. };
  163.  
  164. template<typename T, typename... Args>
  165. smart_ptr<T>& make_smart(Args&&... args) {
  166.     return smart_ptr<T>(forwad<Args>(args)...);
  167. }
  168.  
  169. int main()
  170. {
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement