#pragma once #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< typename T, typename Allocator > class SmartPointer { // don't remove this macro ENABLE_CLASS_TESTS; public: using value_type = T; explicit SmartPointer(value_type *p = nullptr) { if (p == nullptr) { core = nullptr; } else { core = new Core; core->m_obj = p; core->count_ptr = 1; } } // copy constructor SmartPointer(const SmartPointer &p) { core = p.core; if (core != nullptr) core->count_ptr += 1; } // move constructor SmartPointer(SmartPointer &&p) { core = p.core; p.core = nullptr; } // copy assigment SmartPointer &operator=(const SmartPointer &p) { if (p.get() == get()) return *this; core = p.core; if (core != nullptr) core->count_ptr += 1; return *this; } // move assigment SmartPointer &operator=(SmartPointer &&p) { if (p.get() == get()) { p.core = nullptr; return *this; } core->count_ptr = 0; delete core->m_obj; core = p.core; p.core = nullptr; return *this; } // SmartPointer &operator=(value_type *p) { if (get() == p) return *this; if (core != nullptr) { if (core->count_ptr > 1) core->count_ptr -= 1; else delete core->m_obj; } core = new Core; core->m_obj = p; core->count_ptr = 1; if (p == nullptr) core = nullptr; return *this; } ~SmartPointer() { if (core == nullptr) return; if (core->count_ptr > 1) { core->count_ptr -= 1; } else { delete core; } } // return reference to the object of class/type T // if SmartPointer contains nullptr throw `SmartPointer::exception` value_type &operator*() { if (get() == nullptr) { throw exception(); } else { return *core->m_obj; } } const value_type &operator*() const { if (get() == nullptr) { throw exception(); } else { const T *p = core->m_obj; return *p; } } // return pointer to the object of class/type T value_type *operator->() const { return core ? core->m_obj : nullptr; } value_type *get() const { return core ? core->m_obj : nullptr; } // if pointer == nullptr => return false operator bool() const { return core != nullptr; } // if pointers points to the same address or both null => true template bool operator==(const SmartPointer &p) const { return (p.get() == nullptr && get() == nullptr) || (p.get() != nullptr && get() != nullptr && *p.get() == *get() && static_cast(p.get()) == static_cast(get())); } // if pointers points to the same address or both null => false template bool operator!=(const SmartPointer &p) const { return !((p.get() == nullptr && get() == nullptr) || (p.get() != nullptr && get() != nullptr && *p.get() == *get() && static_cast(p.get()) == static_cast(get()))); } // if smart pointer contains non-nullptr => return count owners // if smart pointer contains nullptr => return 0 std::size_t count_owners() const { if (get() != nullptr) return core->count_ptr; return 0; } private: class Core { public: T *m_obj; std::size_t count_ptr; }; Core *core; }; } // namespace smart_pointer