Advertisement
Art_Uspen

unique

Oct 9th, 2021
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. template <typename T, typename Deleter = Slug<T>>
  2. class UniquePtr {
  3. public:
  4.     ////////////////////////////////////////////////////////////////////////////////////////////////
  5.     // Constructors
  6.     explicit UniquePtr(T* ptr = nullptr) : comp_pair(ptr, Deleter()) {
  7.     }
  8.     template <typename Del>
  9.     UniquePtr(T* ptr, Del&& deleter) : comp_pair(ptr, std::forward<Del>(deleter)) {
  10.     }
  11.  
  12.     UniquePtr(UniquePtr&& other) noexcept :comp_pair(nullptr,Deleter()){
  13.         comp_pair.GetFirst() = other.comp_pair.GetFirst();
  14.         other.comp_pair.GetFirst() = nullptr;
  15.     }
  16.     template <typename T1>
  17.     UniquePtr(UniquePtr<T1>&& other) noexcept :comp_pair(nullptr,Deleter()){
  18.         comp_pair.GetFirst() = other.comp_pair.GetFirst();
  19.         other.Release();
  20.     }
  21.  
  22.     ///////////////////////////////////////////////////////////////////////////////////////////////
  23.     // `operator=`-s
  24.  
  25.     UniquePtr& operator=(UniquePtr&& other) noexcept {
  26.         if (comp_pair.GetFirst() == other.comp_pair.GetFirst()) {
  27.             return *this;
  28.         }
  29.         comp_pair.GetFirst() = other.comp_pair.GetFirst();
  30.         comp_pair.GetSecond() = std::forward<Deleter>(other.comp_pair.GetSecond());
  31.         Reset(other.Release());
  32.         return *this;
  33.     }
  34.     UniquePtr& operator=(std::nullptr_t) noexcept {
  35.         Reset();
  36.         return *this;
  37.     }
  38.  
  39.     ////////////////////////////////////////////////////////////////////////////////////////////////
  40.     // Destructor
  41.  
  42.     ~UniquePtr() {
  43.         comp_pair.GetSecond()(comp_pair.GetFirst());
  44.     }
  45.  
  46.     ////////////////////////////////////////////////////////////////////////////////////////////////
  47.     // Modifiers
  48.  
  49.     T* Release() {
  50.         T* result = nullptr;
  51.         std::swap(result, comp_pair.GetFirst());
  52.         return result;
  53.     }
  54.     void Reset(T* ptr = nullptr) {
  55.         T* old_p = comp_pair.GetFirst();
  56.         comp_pair.GetFirst() = ptr;
  57.         if (old_p) {
  58.             comp_pair.GetSecond()(old_p);
  59.         }
  60.     }
  61.     void Swap(UniquePtr& other) noexcept {
  62.         std::swap(comp_pair.GetFirst(), other.comp_pair.GetFirst());
  63.         std::swap(comp_pair.GetSecond(), other.comp_pair.GetSecond());
  64.     }
  65.  
  66.     ////////////////////////////////////////////////////////////////////////////////////////////////
  67.     // Observers
  68.  
  69.     T* Get() const {
  70.         return comp_pair.GetFirst();
  71.     }
  72.     T* Get() {
  73.         return comp_pair.GetFirst();
  74.     }
  75.     Deleter& GetDeleter() {
  76.         return comp_pair.GetSecond();
  77.     }
  78.     const Deleter& GetDeleter() const {
  79.         return comp_pair.GetSecond();
  80.     }
  81.     explicit operator bool() const {
  82.         return comp_pair.GetFirst();
  83.     }
  84.  
  85.     ////////////////////////////////////////////////////////////////////////////////////////////////
  86.     // Single-object dereference operators
  87.  
  88.     std::add_lvalue_reference_t<T> operator*() const {
  89.         return *comp_pair.GetFirst();
  90.     }
  91.     T* operator->() const {
  92.         return comp_pair.GetFirst();
  93.     }
  94.  
  95. private:
  96.     CompressedPair<T, Deleter> comp_pair;
  97.     //    T* ptr_;
  98.     //    Deleter deleter_;
  99. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement