shushus

Спринт 12. Идея RAII.

Apr 6th, 2023
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <functional>
  2. #include <cassert>
  3. #include <cstddef>
  4.  
  5. using namespace std;
  6.  
  7. template <typename T>
  8. class UniquePtr {
  9. public:
  10.    
  11.     UniquePtr() : ptr_(nullptr) {}
  12.  
  13.     explicit UniquePtr(T* ptr) : ptr_(std::move(ptr)) {}
  14.  
  15.     UniquePtr(UniquePtr&& other) {
  16.         ptr_ = other.ptr_;
  17.         other.ptr_ = nullptr;
  18.     }
  19.  
  20.     UniquePtr& operator=(nullptr_t) {
  21.         delete ptr_;
  22.         ptr_ = nullptr;
  23.        
  24.         return *this;
  25.     }
  26.  
  27.     UniquePtr& operator=(UniquePtr&& other) {
  28.         ptr_ = other.ptr_;
  29.         other.ptr_ = nullptr;
  30.        
  31.         return *this;
  32.     }
  33.  
  34.     ~UniquePtr() {
  35.         delete ptr_;
  36.     }
  37.  
  38.     T& operator*() const {
  39.         return *ptr_;
  40.     }
  41.  
  42.     T* operator->() const {
  43.         return ptr_;
  44.     }
  45.  
  46.     T* Release() {
  47.         T* temp = ptr_;
  48.         ptr_ = nullptr;
  49.        
  50.         return temp;
  51.     }
  52.  
  53.     void Reset(T* ptr) {
  54.         auto store = ptr_;
  55.         ptr_ = ptr;
  56.        
  57.         if (store != nullptr) {
  58.             delete store;
  59.         }
  60.     }
  61.  
  62.     void Swap(UniquePtr& other) {
  63.         std::swap(ptr_, other.ptr_);
  64.     }
  65.  
  66.     T* Get() const {
  67.         return ptr_;
  68.     }
  69.    
  70. private:
  71.     T* ptr_;
  72. };
  73.  
  74. struct Item {
  75.     static int counter;
  76.     int value;
  77.    
  78.     Item(int v = 0) : value(v) {
  79.         ++counter;
  80.     }
  81.    
  82.     Item(const Item& other) : value(other.value) {
  83.         ++counter;
  84.     }
  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