diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/cpp/smtptr/shared_pointer.hxx | |
add: graphs et rushs
Diffstat (limited to 'graphs/cpp/smtptr/shared_pointer.hxx')
| -rw-r--r-- | graphs/cpp/smtptr/shared_pointer.hxx | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/graphs/cpp/smtptr/shared_pointer.hxx b/graphs/cpp/smtptr/shared_pointer.hxx new file mode 100644 index 0000000..fbc0792 --- /dev/null +++ b/graphs/cpp/smtptr/shared_pointer.hxx @@ -0,0 +1,144 @@ +#pragma once +#include "shared_pointer.hh" +template <typename T> +SharedPointer<T>::SharedPointer(element_type* p) +{ + if (p == nullptr) + { + data_ = nullptr; + count_ = nullptr; + } + else + { + data_ = p; + count_ = new long{ 1 }; + } +} +template <typename T> +SharedPointer<T>::~SharedPointer() +{ + if (count_ != nullptr) + { + (*count_)--; + if (*count_ == 0) + { + delete count_; + if (data_ != nullptr) + delete data_; + } + } +} +template <typename T> +SharedPointer<T>::SharedPointer(const SharedPointer<element_type>& other) +{ + this->~SharedPointer(); + data_ = other.data_; + if (data_ == nullptr) + count_ = nullptr; + count_ = other.count_; + if (count_ != nullptr) + (*count_)++; +} +template <typename T> +void SharedPointer<T>::reset(element_type* p) +{ + if (data_ == p) + return; + this->~SharedPointer(); + data_ = p; + if (p == nullptr) + count_ = nullptr; + else + count_ = new long{ 1 }; +} +template <typename T> +SharedPointer<T>& SharedPointer<T>::operator=(const SharedPointer<T>& other) +{ + this->~SharedPointer(); + + data_ = other.data_; + count_ = other.count_; + if (nullptr != other.data_) + { + (*this->count_)++; + } + return *this; +} +template <typename T> +T& SharedPointer<T>::operator*() const +{ + return *data_; +} + +template <typename T> +T* SharedPointer<T>::operator->() const +{ + return data_; +} +template <typename T> +T* SharedPointer<T>::get() const +{ + return data_; +} +template <typename T> +long SharedPointer<T>::use_count() const +{ + if (count_ == nullptr) + return 0; + return *count_; +} +template <typename T> +template <typename U> +bool SharedPointer<T>::operator==(const SharedPointer<U>& rhs) const +{ + if (rhs.data_ == this->data_) + return true; + return false; +} +template <typename T> +template <typename U> +bool SharedPointer<T>::operator!=(const SharedPointer<U>& rhs) const +{ + return !(*this == rhs); +} +template <typename T> +bool SharedPointer<T>::operator==(const T* p) const +{ + return this->data_ == p; +} +template <typename T> +bool SharedPointer<T>::operator!=(const T* p) const +{ + return this->data_ != p; +} +template <typename T> +SharedPointer<T>& SharedPointer<T>::operator=(SharedPointer<T>&& other) noexcept +{ + this->~SharedPointer(); + data_ = other.data_; + count_ = other.count_; + other.data_ = nullptr; + other.count_ = nullptr; + return *this; +} +template <typename T> +SharedPointer<T>::SharedPointer(SharedPointer&& other) +{ + this->~SharedPointer(); + data_ = other.data_; + count_ = other.count_; + + other.data_ = nullptr; + other.count_ = nullptr; +} +template <typename T> +SharedPointer<T>::operator bool() const +{ + return data_ != nullptr; +} +template <typename T> +template <typename U> +bool SharedPointer<T>::is_a() const +{ + return dynamic_cast<U*>(data_); +}
\ No newline at end of file |
