Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------------------------------
- main.cpp
- -----------------------------------------------------------------------
- #include "octopus.h"
- #include <cassert>
- #include <iostream>
- using namespace std;
- int main() {
- {
- Octopus default_octopus;
- assert(default_octopus.GetTentacleCount() == 8);
- Octopus quadropus(4);
- assert(quadropus.GetTentacleCount() == 4);
- Octopus coloboque(0);
- assert(coloboque.GetTentacleCount() == 0);
- }
- {
- Octopus octopus(1);
- Tentacle* t0 = &octopus.GetTentacle(0);
- Tentacle* t1 = &octopus.AddTentacle();
- assert(octopus.GetTentacleCount() == 2);
- Tentacle* t2 = &octopus.AddTentacle();
- assert(octopus.GetTentacleCount() == 3);
- assert(&octopus.GetTentacle(0) == t0);
- assert(&octopus.GetTentacle(1) == t1);
- assert(&octopus.GetTentacle(2) == t2);
- for (int i = 0; i < octopus.GetTentacleCount(); ++i) {
- assert(octopus.GetTentacle(i).GetId() == i + 1);
- }
- }
- {
- Octopus male(2);
- Octopus female(2);
- assert(male.GetTentacle(0).GetLinkedTentacle() == nullptr);
- male.GetTentacle(0).LinkTo(female.GetTentacle(1));
- assert(male.GetTentacle(0).GetLinkedTentacle() == &female.GetTentacle(1));
- male.GetTentacle(0).Unlink();
- assert(male.GetTentacle(0).GetLinkedTentacle() == nullptr);
- }
- {
- for (int num_tentacles = 0; num_tentacles < 10; ++num_tentacles) {
- Octopus male(num_tentacles);
- Octopus female(num_tentacles);
- for (int i = 0; i < num_tentacles; ++i) {
- male.GetTentacle(i).LinkTo(female.GetTentacle(num_tentacles - 1 - i));
- }
- Octopus male_copy(male);
- assert(male_copy.GetTentacleCount() == male.GetTentacleCount());
- for (int i = 0; i < male_copy.GetTentacleCount(); ++i) {
- assert(&male_copy.GetTentacle(i) != &male.GetTentacle(i));
- assert(male_copy.GetTentacle(i).GetLinkedTentacle() == male.GetTentacle(i).GetLinkedTentacle());
- }
- }
- cout << "Everything is OK"s << endl;
- }
- }
- -----------------------------------------------------------------------
- octopus.h
- -----------------------------------------------------------------------
- #include "octopus.h"
- #include <cassert>
- #include <iostream>
- using namespace std;
- int main() {
- {
- Octopus default_octopus;
- assert(default_octopus.GetTentacleCount() == 8);
- Octopus quadropus(4);
- assert(quadropus.GetTentacleCount() == 4);
- Octopus coloboque(0);
- assert(coloboque.GetTentacleCount() == 0);
- }
- {
- Octopus octopus(1);
- Tentacle* t0 = &octopus.GetTentacle(0);
- Tentacle* t1 = &octopus.AddTentacle();
- assert(octopus.GetTentacleCount() == 2);
- Tentacle* t2 = &octopus.AddTentacle();
- assert(octopus.GetTentacleCount() == 3);
- assert(&octopus.GetTentacle(0) == t0);
- assert(&octopus.GetTentacle(1) == t1);
- assert(&octopus.GetTentacle(2) == t2);
- for (int i = 0; i < octopus.GetTentacleCount(); ++i) {
- assert(octopus.GetTentacle(i).GetId() == i + 1);
- }
- }
- {
- Octopus male(2);
- Octopus female(2);
- assert(male.GetTentacle(0).GetLinkedTentacle() == nullptr);
- male.GetTentacle(0).LinkTo(female.GetTentacle(1));
- assert(male.GetTentacle(0).GetLinkedTentacle() == &female.GetTentacle(1));
- male.GetTentacle(0).Unlink();
- assert(male.GetTentacle(0).GetLinkedTentacle() == nullptr);
- }
- {
- for (int num_tentacles = 0; num_tentacles < 10; ++num_tentacles) {
- Octopus male(num_tentacles);
- Octopus female(num_tentacles);
- for (int i = 0; i < num_tentacles; ++i) {
- male.GetTentacle(i).LinkTo(female.GetTentacle(num_tentacles - 1 - i));
- }
- Octopus male_copy(male);
- assert(male_copy.GetTentacleCount() == male.GetTentacleCount());
- for (int i = 0; i < male_copy.GetTentacleCount(); ++i) {
- assert(&male_copy.GetTentacle(i) != &male.GetTentacle(i));
- assert(male_copy.GetTentacle(i).GetLinkedTentacle() == male.GetTentacle(i).GetLinkedTentacle());
- }
- }
- cout << "Everything is OK"s << endl;
- }
- }
- -----------------------------------------------------------------------
- scopedptr.h
- -----------------------------------------------------------------------
- #pragma once
- #include <stdexcept>
- template <typename T>
- class ScopedPtr {
- public:
- ScopedPtr() = default;
- explicit ScopedPtr(T* raw_ptr) noexcept
- : ptr_(raw_ptr) {
- }
- ScopedPtr(const ScopedPtr&) = delete;
- ~ScopedPtr() {
- delete ptr_;
- }
- T* GetRawPtr() const noexcept {
- return ptr_;
- }
- T* Release() noexcept {
- T* p = ptr_;
- ptr_ = nullptr;
- return p;
- }
- explicit operator bool() const {
- return ptr_ != nullptr;
- }
- T* operator->() const {
- using namespace std::literals;
- if (!ptr_) {
- throw std::logic_error("Scoped ptr is null"s);
- }
- return ptr_;
- }
- T& operator*() const {
- using namespace std::literals;
- if (!ptr_) {
- throw std::logic_error("Scoped ptr is null"s);
- }
- return *ptr_;
- }
- private:
- T* ptr_ = nullptr;
- };
- -----------------------------------------------------------------------
- ptrvector.h
- -----------------------------------------------------------------------
- #pragma once
- #include <vector>
- template <typename T>
- class PtrVector {
- public:
- PtrVector() = default;
- PtrVector(const PtrVector& other) {
- items_.reserve(other.items_.size());
- try {
- for (auto p : other.items_) {
- auto p_copy = p ? new T(*p) : nullptr;
- items_.push_back(p_copy);
- }
- } catch (...) {
- DeleteItems();
- throw;
- }
- }
- ~PtrVector() {
- DeleteItems();
- }
- std::vector<T*>& GetItems() noexcept {
- return items_;
- }
- std::vector<T*> const& GetItems() const noexcept {
- return items_;
- }
- private:
- void DeleteItems() noexcept {
- for (auto p : items_) {
- delete p;
- }
- }
- std::vector<T*> items_;
- };
Advertisement
Add Comment
Please, Sign In to add comment