shushus

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

Jan 12th, 2023 (edited)
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.58 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. main.cpp
  3. -----------------------------------------------------------------------
  4. #include "octopus.h"
  5.  
  6. #include <cassert>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. int main() {
  12.     {
  13.         Octopus default_octopus;
  14.         assert(default_octopus.GetTentacleCount() == 8);
  15.  
  16.         Octopus quadropus(4);
  17.         assert(quadropus.GetTentacleCount() == 4);
  18.  
  19.         Octopus coloboque(0);
  20.         assert(coloboque.GetTentacleCount() == 0);
  21.     }
  22.  
  23.     {
  24.         Octopus octopus(1);
  25.         Tentacle* t0 = &octopus.GetTentacle(0);
  26.         Tentacle* t1 = &octopus.AddTentacle();
  27.         assert(octopus.GetTentacleCount() == 2);
  28.         Tentacle* t2 = &octopus.AddTentacle();
  29.         assert(octopus.GetTentacleCount() == 3);
  30.  
  31.         assert(&octopus.GetTentacle(0) == t0);
  32.         assert(&octopus.GetTentacle(1) == t1);
  33.         assert(&octopus.GetTentacle(2) == t2);
  34.  
  35.         for (int i = 0; i < octopus.GetTentacleCount(); ++i) {
  36.             assert(octopus.GetTentacle(i).GetId() == i + 1);
  37.         }
  38.     }
  39.  
  40.     {
  41.         Octopus male(2);
  42.         Octopus female(2);
  43.  
  44.         assert(male.GetTentacle(0).GetLinkedTentacle() == nullptr);
  45.  
  46.         male.GetTentacle(0).LinkTo(female.GetTentacle(1));
  47.         assert(male.GetTentacle(0).GetLinkedTentacle() == &female.GetTentacle(1));
  48.  
  49.         male.GetTentacle(0).Unlink();
  50.         assert(male.GetTentacle(0).GetLinkedTentacle() == nullptr);
  51.     }
  52.  
  53.     {
  54.         for (int num_tentacles = 0; num_tentacles < 10; ++num_tentacles) {
  55.             Octopus male(num_tentacles);
  56.             Octopus female(num_tentacles);
  57.  
  58.             for (int i = 0; i < num_tentacles; ++i) {
  59.                 male.GetTentacle(i).LinkTo(female.GetTentacle(num_tentacles - 1 - i));
  60.             }
  61.  
  62.             Octopus male_copy(male);
  63.             assert(male_copy.GetTentacleCount() == male.GetTentacleCount());
  64.             for (int i = 0; i < male_copy.GetTentacleCount(); ++i) {
  65.                 assert(&male_copy.GetTentacle(i) != &male.GetTentacle(i));
  66.                 assert(male_copy.GetTentacle(i).GetLinkedTentacle() == male.GetTentacle(i).GetLinkedTentacle());
  67.             }
  68.         }
  69.  
  70.         cout << "Everything is OK"s << endl;
  71.     }
  72. }
  73. -----------------------------------------------------------------------
  74. octopus.h
  75. -----------------------------------------------------------------------
  76. #include "octopus.h"
  77.  
  78. #include <cassert>
  79. #include <iostream>
  80.  
  81. using namespace std;
  82.  
  83. int main() {
  84.     {
  85.         Octopus default_octopus;
  86.         assert(default_octopus.GetTentacleCount() == 8);
  87.  
  88.         Octopus quadropus(4);
  89.         assert(quadropus.GetTentacleCount() == 4);
  90.  
  91.         Octopus coloboque(0);
  92.         assert(coloboque.GetTentacleCount() == 0);
  93.     }
  94.  
  95.     {
  96.         Octopus octopus(1);
  97.         Tentacle* t0 = &octopus.GetTentacle(0);
  98.         Tentacle* t1 = &octopus.AddTentacle();
  99.         assert(octopus.GetTentacleCount() == 2);
  100.         Tentacle* t2 = &octopus.AddTentacle();
  101.         assert(octopus.GetTentacleCount() == 3);
  102.  
  103.         assert(&octopus.GetTentacle(0) == t0);
  104.         assert(&octopus.GetTentacle(1) == t1);
  105.         assert(&octopus.GetTentacle(2) == t2);
  106.  
  107.         for (int i = 0; i < octopus.GetTentacleCount(); ++i) {
  108.             assert(octopus.GetTentacle(i).GetId() == i + 1);
  109.         }
  110.     }
  111.  
  112.     {
  113.         Octopus male(2);
  114.         Octopus female(2);
  115.  
  116.         assert(male.GetTentacle(0).GetLinkedTentacle() == nullptr);
  117.  
  118.         male.GetTentacle(0).LinkTo(female.GetTentacle(1));
  119.         assert(male.GetTentacle(0).GetLinkedTentacle() == &female.GetTentacle(1));
  120.  
  121.         male.GetTentacle(0).Unlink();
  122.         assert(male.GetTentacle(0).GetLinkedTentacle() == nullptr);
  123.     }
  124.  
  125.     {
  126.         for (int num_tentacles = 0; num_tentacles < 10; ++num_tentacles) {
  127.             Octopus male(num_tentacles);
  128.             Octopus female(num_tentacles);
  129.  
  130.             for (int i = 0; i < num_tentacles; ++i) {
  131.                 male.GetTentacle(i).LinkTo(female.GetTentacle(num_tentacles - 1 - i));
  132.             }
  133.  
  134.             Octopus male_copy(male);
  135.             assert(male_copy.GetTentacleCount() == male.GetTentacleCount());
  136.             for (int i = 0; i < male_copy.GetTentacleCount(); ++i) {
  137.                 assert(&male_copy.GetTentacle(i) != &male.GetTentacle(i));
  138.                 assert(male_copy.GetTentacle(i).GetLinkedTentacle() == male.GetTentacle(i).GetLinkedTentacle());
  139.             }
  140.         }
  141.  
  142.         cout << "Everything is OK"s << endl;
  143.     }
  144. }
  145. -----------------------------------------------------------------------
  146. scopedptr.h
  147. -----------------------------------------------------------------------
  148. #pragma once
  149.  
  150. #include <stdexcept>
  151.  
  152. template <typename T>
  153. class ScopedPtr {
  154. public:
  155.     ScopedPtr() = default;
  156.  
  157.     explicit ScopedPtr(T* raw_ptr) noexcept
  158.         : ptr_(raw_ptr) {
  159.     }
  160.  
  161.     ScopedPtr(const ScopedPtr&) = delete;
  162.  
  163.     ~ScopedPtr() {
  164.         delete ptr_;
  165.     }
  166.  
  167.     T* GetRawPtr() const noexcept {
  168.         return ptr_;
  169.     }
  170.  
  171.     T* Release() noexcept {
  172.         T* p = ptr_;
  173.         ptr_ = nullptr;
  174.         return p;
  175.     }
  176.  
  177.     explicit operator bool() const {
  178.         return ptr_ != nullptr;
  179.     }
  180.  
  181.     T* operator->() const {
  182.         using namespace std::literals;
  183.         if (!ptr_) {
  184.             throw std::logic_error("Scoped ptr is null"s);
  185.         }
  186.         return ptr_;
  187.     }
  188.  
  189.     T& operator*() const {
  190.         using namespace std::literals;
  191.         if (!ptr_) {
  192.             throw std::logic_error("Scoped ptr is null"s);
  193.         }
  194.         return *ptr_;
  195.     }
  196.  
  197. private:
  198.     T* ptr_ = nullptr;
  199. };
  200. -----------------------------------------------------------------------
  201. ptrvector.h
  202. -----------------------------------------------------------------------
  203. #pragma once
  204.  
  205. #include <vector>
  206.  
  207. template <typename T>
  208. class PtrVector {
  209. public:
  210.     PtrVector() = default;
  211.  
  212.     PtrVector(const PtrVector& other) {
  213.         items_.reserve(other.items_.size());
  214.  
  215.         try {
  216.             for (auto p : other.items_) {
  217.                 auto p_copy = p ? new T(*p) : nullptr;
  218.  
  219.                 items_.push_back(p_copy);
  220.             }
  221.         } catch (...) {
  222.             DeleteItems();
  223.             throw;
  224.         }
  225.     }
  226.  
  227.     ~PtrVector() {
  228.         DeleteItems();
  229.     }
  230.  
  231.     std::vector<T*>& GetItems() noexcept {
  232.         return items_;
  233.     }
  234.  
  235.     std::vector<T*> const& GetItems() const noexcept {
  236.         return items_;
  237.     }
  238.  
  239. private:
  240.     void DeleteItems() noexcept {
  241.         for (auto p : items_) {
  242.             delete p;
  243.         }
  244.     }
  245.  
  246.     std::vector<T*> items_;
  247. };
Advertisement
Add Comment
Please, Sign In to add comment