#pragma once #include "shared_pointer.hh" template SharedPointer::SharedPointer(element_type* p) { if (p == nullptr) { data_ = nullptr; count_ = nullptr; } else { data_ = p; count_ = new long{ 1 }; } } template SharedPointer::~SharedPointer() { if (count_ != nullptr) { (*count_)--; if (*count_ == 0) { delete count_; if (data_ != nullptr) delete data_; } } } template SharedPointer::SharedPointer(const SharedPointer& other) { this->~SharedPointer(); data_ = other.data_; if (data_ == nullptr) count_ = nullptr; count_ = other.count_; if (count_ != nullptr) (*count_)++; } template void SharedPointer::reset(element_type* p) { if (data_ == p) return; this->~SharedPointer(); data_ = p; if (p == nullptr) count_ = nullptr; else count_ = new long{ 1 }; } template SharedPointer& SharedPointer::operator=(const SharedPointer& other) { this->~SharedPointer(); data_ = other.data_; count_ = other.count_; if (nullptr != other.data_) { (*this->count_)++; } return *this; } template T& SharedPointer::operator*() const { return *data_; } template T* SharedPointer::operator->() const { return data_; } template T* SharedPointer::get() const { return data_; } template long SharedPointer::use_count() const { if (count_ == nullptr) return 0; return *count_; } template template bool SharedPointer::operator==(const SharedPointer& rhs) const { if (rhs.data_ == this->data_) return true; return false; } template template bool SharedPointer::operator!=(const SharedPointer& rhs) const { return !(*this == rhs); } template bool SharedPointer::operator==(const T* p) const { return this->data_ == p; } template bool SharedPointer::operator!=(const T* p) const { return this->data_ != p; } template SharedPointer& SharedPointer::operator=(SharedPointer&& other) noexcept { this->~SharedPointer(); data_ = other.data_; count_ = other.count_; other.data_ = nullptr; other.count_ = nullptr; return *this; } template SharedPointer::SharedPointer(SharedPointer&& other) { this->~SharedPointer(); data_ = other.data_; count_ = other.count_; other.data_ = nullptr; other.count_ = nullptr; } template SharedPointer::operator bool() const { return data_ != nullptr; } template template bool SharedPointer::is_a() const { return dynamic_cast(data_); }