Advertisement
kutuzzzov

Урок 3 Идея РАИИ

May 31st, 2023
1,081
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <cassert>
  2. #include <cstddef>  // нужно для nullptr_t
  3.  
  4. using namespace std;
  5.  
  6. // Реализуйте шаблон класса UniquePtr
  7. template <typename T>
  8. class UniquePtr {
  9. private:
  10.     T* ptr_;
  11. public:
  12.     UniquePtr()
  13.         : ptr_(nullptr) {
  14.     };
  15.     explicit UniquePtr(T* ptr)
  16.         : ptr_(ptr) {
  17.     };
  18.     UniquePtr(const UniquePtr&) = delete;
  19.     UniquePtr(UniquePtr&& other) {
  20.         if (this != &other) {
  21.             ptr_ = other.ptr_;
  22.             other.ptr_ = nullptr;
  23.         }
  24.     }
  25.     UniquePtr& operator=(const UniquePtr&) = delete;
  26.     UniquePtr& operator=(nullptr_t) {
  27.         delete ptr_;
  28.         ptr_ = nullptr;
  29.         return *this;
  30.     }
  31.     UniquePtr& operator=(UniquePtr&& other) {
  32.         if (this == &other) {
  33.             return *this;
  34.         }
  35.         delete ptr_;
  36.         ptr_ = other.ptr_;
  37.         other.ptr_ = nullptr;
  38.         return *this;
  39.     }
  40.     ~UniquePtr() {
  41.         delete ptr_;
  42.         ptr_ = nullptr;
  43.     }
  44.  
  45.     T& operator*() const {
  46.         return *ptr_;
  47.     }
  48.  
  49.     T* operator->() const {
  50.         return ptr_;
  51.     }
  52.  
  53.     T* Release() {
  54.         T* temp = ptr_;
  55.         ptr_ = nullptr;
  56.         return temp;
  57.     }
  58.     void Reset(T* ptr) {
  59.         delete ptr_;
  60.         ptr_ = ptr;
  61.     }
  62.     void Swap(UniquePtr& other) {
  63.         T* temp = other.ptr_;
  64.         other.ptr_ = ptr_;
  65.         ptr_ = temp;
  66.     }
  67.  
  68.     T* Get() const {
  69.         return ptr_;
  70.     }
  71. };
  72.  
  73. struct Item {
  74.     static int counter;
  75.     int value;
  76.     Item(int v = 0)
  77.         : value(v)
  78.     {
  79.         ++counter;
  80.     }
  81.     Item(const Item& other)
  82.         : value(other.value)
  83.     {
  84.         ++counter;
  85.     }
  86.     ~Item() {
  87.         --counter;
  88.     }
  89. };
  90.  
  91. int Item::counter = 0;
  92.  
  93. void TestLifetime() {
  94.     Item::counter = 0;
  95.     {
  96.         UniquePtr<Item> ptr(new Item);
  97.         assert(Item::counter == 1);
  98.  
  99.         ptr.Reset(new Item);
  100.         assert(Item::counter == 1);
  101.     }
  102.     assert(Item::counter == 0);
  103.  
  104.     {
  105.         UniquePtr<Item> ptr(new Item);
  106.         assert(Item::counter == 1);
  107.  
  108.         auto rawPtr = ptr.Release();
  109.         assert(Item::counter == 1);
  110.  
  111.         delete rawPtr;
  112.         assert(Item::counter == 0);
  113.     }
  114.     assert(Item::counter == 0);
  115. }
  116.  
  117. void TestGetters() {
  118.     UniquePtr<Item> ptr(new Item(42));
  119.     assert(ptr.Get()->value == 42);
  120.     assert((*ptr).value == 42);
  121.     assert(ptr->value == 42);
  122. }
  123.  
  124. int main() {
  125.     TestLifetime();
  126.     TestGetters();
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement