Advertisement
vguk

Untitled

Nov 13th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #ifndef UNIQ_PTR_H
  2. #define UNIQ_PTR_H
  3. #pragma once
  4.  
  5. namespace uniq_ptr
  6. {
  7.     template <typename T>
  8.     class UniqPtr
  9.     {
  10.     public:
  11.         UniqPtr()
  12.         {}
  13.         UniqPtr(T* ptr)
  14.         {
  15.             this->Reset(ptr);
  16.         }
  17.         UniqPtr(UniqPtr &&ptr) : ptr_(ptr.ptr_)
  18.         {
  19.             ptr.ptr_ = nullptr;
  20.         }
  21.         UniqPtr(const UniqPtr &ptr) = delete;
  22.         ~UniqPtr()
  23.         {
  24.             this->Reset();
  25.         }
  26.  
  27.         T* operator->() const
  28.         {
  29.             return this->ptr_;
  30.         }
  31.         T& operator*() const
  32.         {
  33.             return *(this->ptr_);
  34.         }
  35.         operator T*() const
  36.         {
  37.             return this->ptr_;
  38.         }
  39.         UniqPtr& operator=(UniqPtr& ptr) = delete;
  40.         UniqPtr& operator=(UniqPtr&& ptr)
  41.         {
  42.             //if (!this->Empty()) delete this->ptr_;  //почему????
  43.             this->ptr_ = ptr.ptr_;
  44.             ptr.ptr_ = nullptr;
  45.             return *this;
  46.         }
  47.         //UniqPtr& operator=(const T* ptr) = delete;
  48.         /*UniqPtr& operator=(const T* &&ptr)
  49.         {
  50.             this->Reset(ptr);
  51.             return *this;
  52.         }*/
  53.         UniqPtr& operator=(T* ptr)
  54.         {
  55.             this->Reset(ptr);
  56.             return *this;
  57.         }
  58.         bool Empty() const
  59.         {
  60.             return !this->ptr_;
  61.         }
  62.         void Reset(T* ptr = nullptr)
  63.         {
  64.             if (!this->Empty()) delete this->ptr_;
  65.             this->ptr_ = ptr;
  66.         }
  67.         T* Release()
  68.         {
  69.             T* ptr = this->ptr_;
  70.             this->ptr_ = nullptr;
  71.             return ptr;
  72.         }
  73.     private:
  74.         T* ptr_ = nullptr;
  75.     };
  76. }
  77. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement