#pragma once #include #include #include "Test.hpp" namespace smart_pointer { // `exception` class definition class exception : std::exception { using base_class = std::exception; using base_class::base_class; }; // `SmartPointer` class declaration template class SmartPointer { // don't remove this macro ENABLE_CLASS_TESTS; public: using value_type = T1; explicit SmartPointer(value_type *value = nullptr) { if (value) core = new Core(value); else core = nullptr; } // copy constructor SmartPointer(const SmartPointer &sp) { core = sp.core; if (*this) core->Increment(); } // move constructor SmartPointer(SmartPointer &&sp) { core = sp.core; sp.core = nullptr; } // copy assigment SmartPointer &operator=(const SmartPointer &sp) { this->~SmartPointer(); core = sp.core; if (*this) core->Increment(); return *this; } // move assigment SmartPointer &operator=(SmartPointer &&sp) { this->~SmartPointer(); core = sp.core; sp.core = nullptr; return *this; } // SmartPointer &operator=(value_type *sp) { this->~SmartPointer(); if (sp) core = new Core(sp); else core = nullptr; return *this; } ~SmartPointer() { if (*this) { core->Decrement(); if (core->CountOwners() == 0) delete core; } } // return reference to the object of class/type T1 // if SmartPointer contains nullptr throw `SmartPointer::exception` value_type &operator*() { if (!*this) throw smart_pointer::exception(); return *(core->Value()); } const value_type &operator*() const { if (!*this) throw smart_pointer::exception(); return *(core->Value()); } // return pointer to the object of class/type T1 value_type *operator->() const { if (*this) return core->Value(); return nullptr; } value_type *get() const { if (*this) return core->Value(); return nullptr; } // if pointer == nullptr => return false operator bool() const { return !(core == nullptr || core->Value() == nullptr); } // if pointers points to the same address or both null => true template bool operator==(const SmartPointer &sp) const { if (!*this && !sp) return true; if (!std::is_same::value) return false; return reinterpret_cast(get()) == reinterpret_cast(sp.get()); } // if pointers points to the same address or both null => false template bool operator!=(const SmartPointer &sp) const { if (!*this && !sp) return false; if (!std::is_same::value) return true; return reinterpret_cast(get()) != reinterpret_cast(sp.get()); } // if smart pointer contains non-nullptr => return count owners // if smart pointer contains nullptr => return 0 std::size_t count_owners() const { if (*this) return core->CountOwners(); return 0; } private: class Core { public: explicit Core(value_type *value) : value_(value), count_owners_(0) { Increment(); } ~Core() { delete value_; value_ = nullptr; } value_type *Value() { return value_; } value_type *Value() const { return value_; } void Increment() { count_owners_++; } void Decrement() { count_owners_--; } std::size_t CountOwners() const { return count_owners_; } private: value_type *value_; std::size_t count_owners_; }; Core *core; }; }