Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1.  
  2. namespace stepik
  3. {
  4.     template <typename T>
  5.     class shared_ptr
  6.     {
  7.         template <typename U>
  8.         friend class shared_ptr;
  9.  
  10.     public:
  11.  
  12.         explicit shared_ptr(T *ptr = nullptr) {
  13.             ptr_ = ptr;
  14.         if (ptr_) {
  15.             count_ = new int(1);
  16.         } else {
  17.             count_ = 0;
  18.         }
  19.     }
  20.         ~shared_ptr()
  21.         {
  22.             decrement_counter();
  23.         }
  24.         template <typename U>
  25.         shared_ptr(const shared_ptr<U> & other)
  26.         {
  27.             ptr_ = other.ptr_;
  28.             count_ = other.count_;
  29.             if (ptr_) {
  30.                 ++(*count_);
  31.             }
  32.         }
  33.         template <typename U>
  34.         shared_ptr& operator=(const shared_ptr<U> & other)
  35.         {
  36.             if (this != &other) {
  37.                 decrement_counter();
  38.                 ptr_ = other.ptr_;
  39.                 count_ = other.count_;
  40.                 if (other.ptr_) {
  41.                     ++(*count_);
  42.                 }
  43.             }
  44.                 return *this;
  45.             }
  46.  
  47.                 void decrement_counter() {
  48.             if (count_ && ptr_ && !--(*count_)) {
  49.                 delete ptr_;
  50.                 delete count_;
  51.                 count_ = 0;
  52.             }
  53.         }
  54.             explicit operator bool() const
  55.             {
  56.                 return (ptr_ != nullptr);
  57.             }
  58.  
  59.             T* get() const
  60.             {
  61.                 return ptr_;
  62.             }
  63.  
  64.             long use_count() const
  65.             {
  66.                 return (count_ ? *count_ : 0);
  67.             }
  68.  
  69.             T& operator*() const
  70.             {
  71.                 return *ptr_;
  72.             }
  73.  
  74.             T* operator->() const
  75.             {
  76.                 return ptr_;
  77.             }
  78.  
  79.             void swap(shared_ptr& x) noexcept
  80.             {
  81.                 std::swap(ptr_, x.ptr_);
  82.                 std::swap(count_, x.count_);
  83.             }
  84.  
  85.             void reset(T *ptr = 0)
  86.             {
  87.                 if (ptr_ != ptr) {
  88.                     decrement_counter();
  89.                     ptr_ = ptr;
  90.                     if (ptr) {
  91.                         count_ = new int(0);
  92.                         if (ptr_) {
  93.                             ++(*count_);
  94.                         }
  95.                     }
  96.                 }
  97.             }
  98.  
  99.         private:
  100.             T* ptr_;
  101.          int* count_;
  102.  
  103.         };
  104.     } // namespace stepik
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement