shushus

Спринт 7. Копирование объектов. Часть третья. Задание 1.

Jan 12th, 2023
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. template <typename T>
  8. class PtrVector {
  9. public:
  10.     PtrVector() = default;
  11.  
  12.     PtrVector(const PtrVector& other) {
  13.         items_.reserve(other.GetItems().size());  
  14.         for (auto item : other.GetItems()) {
  15.             if (!item) {
  16.                 items_.push_back(nullptr);
  17.             } else {
  18.                 T* item_copy = new T(*item);
  19.                 items_.push_back(item_copy);
  20.             }
  21.         }
  22.     }
  23.  
  24.     ~PtrVector() {
  25.         for(auto item : items_){
  26.             delete item;
  27.         }
  28.     }
  29.  
  30.     vector<T*>& GetItems() noexcept {
  31.         return items_;
  32.     }
  33.  
  34.     vector<T*> const& GetItems() const noexcept {
  35.         return items_;
  36.     }
  37.  
  38. private:
  39.     vector<T*> items_;
  40. };
  41.  
  42. int main() {
  43.     struct DeletionSpy {
  44.         explicit DeletionSpy(bool& is_deleted)
  45.             : is_deleted_(is_deleted) {
  46.         }
  47.         ~DeletionSpy() {
  48.             is_deleted_ = true;
  49.         }
  50.         bool& is_deleted_;
  51.     };
  52.  
  53.     {
  54.         bool spy1_is_deleted = false;
  55.         DeletionSpy* ptr1 = new DeletionSpy(spy1_is_deleted);
  56.         {
  57.             PtrVector<DeletionSpy> ptr_vector;
  58.             ptr_vector.GetItems().push_back(ptr1);
  59.             assert(!spy1_is_deleted);
  60.  
  61.             const auto& const_ptr_vector_ref(ptr_vector);
  62.             assert(&const_ptr_vector_ref.GetItems() == &ptr_vector.GetItems());
  63.         }
  64.         assert(spy1_is_deleted);
  65.     }
  66.  
  67.     struct CopyingSpy {
  68.         explicit CopyingSpy(int& copy_count)
  69.             : copy_count_(copy_count) {
  70.         }
  71.         CopyingSpy(const CopyingSpy& rhs)
  72.             : copy_count_(rhs.copy_count_)  
  73.         {
  74.             ++copy_count_;
  75.         }
  76.         int& copy_count_;
  77.     };
  78.  
  79.     {
  80.         vector<int> copy_counters(10);
  81.  
  82.         PtrVector<CopyingSpy> ptr_vector;
  83.         for (auto& counter : copy_counters) {
  84.             ptr_vector.GetItems().push_back(new CopyingSpy(counter));
  85.         }
  86.         ptr_vector.GetItems().push_back(nullptr);
  87.  
  88.         auto ptr_vector_copy(ptr_vector);
  89.         assert(ptr_vector_copy.GetItems().size() == ptr_vector.GetItems().size());
  90.  
  91.         assert(ptr_vector_copy.GetItems() != ptr_vector.GetItems());
  92.         assert(ptr_vector_copy.GetItems().back() == nullptr);
  93.         assert(all_of(copy_counters.begin(), copy_counters.end(), [](int counter) {
  94.             return counter == 1;
  95.         }));
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment