Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cassert>
- #include <stdexcept>
- #include <vector>
- #include <new>
- //#include "scopedptr.h"
- using namespace std;
- 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& operator=(const PtrVector& rhs) {
- if (this != &rhs) {
- auto rhs_copy(rhs);
- items_.swap(rhs_copy.items_);
- }
- return *this;
- }
- void swap(PtrVector& other) noexcept;
- ~PtrVector() {
- DeleteItems();
- }
- vector<T*>& GetItems() noexcept {
- return items_;
- }
- vector<T*> const& GetItems() const noexcept {
- return items_;
- }
- private:
- void DeleteItems() noexcept {
- for (auto p : items_) {
- delete p;
- }
- }
- vector<T*> items_;
- };
- class Tentacle {
- public:
- explicit Tentacle(int id) noexcept : id_(id) {}
- int GetId() const noexcept {
- return id_;
- }
- Tentacle* GetLinkedTentacle() const noexcept {
- return linked_tentacle_;
- }
- void LinkTo(Tentacle& tentacle) noexcept {
- linked_tentacle_ = &tentacle;
- }
- void Unlink() noexcept {
- linked_tentacle_ = nullptr;
- }
- private:
- int id_ = 0;
- Tentacle* linked_tentacle_ = nullptr;
- };
- class Octopus {
- public:
- Octopus()
- : Octopus(8) {
- }
- explicit Octopus(int num_tentacles) {
- Tentacle* ttentacle = nullptr;
- try {
- for (int i = 1; i <= num_tentacles; ++i) {
- ttentacle = new Tentacle(i);
- tentacles_.GetItems().push_back(ttentacle);
- ttentacle = nullptr;
- }
- } catch (const std::bad_alloc&) {
- Cleanup();
- delete ttentacle;
- throw;
- }
- }
- ~Octopus() {
- Cleanup();
- }
- Tentacle& AddTentacle() {
- Tentacle* ttentacle = new Tentacle(tentacles_.GetItems().size() + 1);
- tentacles_.GetItems().push_back(ttentacle);
- return *ttentacle;
- }
- int GetTentacleCount() const noexcept {
- return static_cast<int>(tentacles_.GetItems().size());
- }
- const Tentacle& GetTentacle(size_t index) const {
- return *tentacles_.GetItems().at(index);
- }
- Tentacle& GetTentacle(size_t index) {
- return *tentacles_.GetItems().at(index);
- }
- private:
- void Cleanup() {
- for (Tentacle* ttentacle : tentacles_.GetItems()) {
- delete ttentacle;
- }
- tentacles_.GetItems().clear();
- }
- PtrVector<Tentacle> tentacles_;
- };
- int main() {
- {
- Octopus octopus1(3);
- octopus1.GetTentacle(2).LinkTo(octopus1.GetTentacle(1));
- Octopus octopus2(10);
- octopus2 = octopus1;
- assert(octopus2.GetTentacleCount() == octopus1.GetTentacleCount());
- for (int i = 0; i < octopus2.GetTentacleCount(); ++i) {
- auto& tentacle1 = octopus1.GetTentacle(i);
- auto& tentacle2 = octopus2.GetTentacle(i);
- assert(&tentacle2 != &tentacle1);
- assert(tentacle2.GetId() == tentacle1.GetId());
- assert(tentacle2.GetLinkedTentacle() == tentacle1.GetLinkedTentacle());
- }
- }
- {
- Octopus octopus(3);
- octopus.GetTentacle(0).LinkTo(octopus.GetTentacle(1));
- vector<pair<Tentacle*, Tentacle*>> tentacles;
- for (int i = 0; i < octopus.GetTentacleCount(); ++i) {
- tentacles.push_back({&octopus.GetTentacle(i), octopus.GetTentacle(i).GetLinkedTentacle()});
- }
- octopus = octopus;
- assert(octopus.GetTentacleCount() == static_cast<int>(tentacles.size()));
- for (int i = 0; i < octopus.GetTentacleCount(); ++i) {
- auto& tentacle_with_link = tentacles.at(i);
- assert(&octopus.GetTentacle(i) == tentacle_with_link.first);
- assert(octopus.GetTentacle(i).GetLinkedTentacle() == tentacle_with_link.second);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment