diff options
| Commit | 5e9cb4ddc811465731596d8ee684f3d8cd5a6aea (patch) | |
|---|---|---|
| Author | 2026-05-02 16:05:06 +0300 | |
| Committer | 2026-05-02 16:05:06 +0300 | |
| Files | 33a9268d1136974d2ef138364dde508ba43b4a52 /Week11/Exercise03/SharedPointer.hpp | |
| oop-2025-solutions-main.tar oop-2025-solutions-main.tar.gz oop-2025-solutions-main.zip | ||
| Parent | 7546f0d0da487e16f0a8671c09a46e1cee2bc03a (diff) | |
1 files changed, 129 insertions, 0 deletions
| -rw-r--r-- | Week11/Exercise03/SharedPointer.hpp | 129 |
diff --git a/Week11/Exercise03/SharedPointer.hpp b/Week11/Exercise03/SharedPointer.hpp new file mode 100644 index 0000000..06f9bbb --- /dev/null +++ b/Week11/Exercise03/SharedPointer.hpp @@ -0,0 +1,129 @@ +#ifndef _SHAREDPOINTER +#define _SHAREDPOINTER + +template<class T> +class SharedPointer { + T* value; + unsigned* instances; + + void free(); + void copyFrom(const SharedPointer& other); + +public: + SharedPointer(T* value); + + SharedPointer(); + ~SharedPointer(); + SharedPointer(const SharedPointer& other); + SharedPointer& operator=(const SharedPointer& other); + + SharedPointer(SharedPointer&& other); + SharedPointer& operator=(SharedPointer&& other); + + T& operator*(); + const T& operator*() const; + T* operator->(); + const T* operator->() const; + + unsigned RefCount() const; +}; + +template <class T> +void SharedPointer<T>::free() { + if (instances == nullptr) + return; + + --*instances; + if (*instances == 0) { + delete instances; + delete value; + } +} + +template <class T> +void SharedPointer<T>::copyFrom(const SharedPointer& other) { + this->instances = other.instances; + this->value = other.value; + ++*instances; +} + +template <class T> +SharedPointer<T>::SharedPointer(T* value) : instances(new unsigned), value(value) +{ + *instances = 1; +} + +template <class T> +SharedPointer<T>::SharedPointer() : instances(), value() +{ } + +template <class T> +SharedPointer<T>::~SharedPointer() { + free(); +} + +template <class T> +SharedPointer<T>::SharedPointer(const SharedPointer& other) { + copyFrom(other); +} + +template <class T> +SharedPointer<T>& SharedPointer<T>::operator=(const SharedPointer& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +template <class T> +SharedPointer<T>::SharedPointer(SharedPointer&& other) { + instances = other.instances; + value = other.value; + + other.instances = nullptr; + other.value = nullptr; +} + +template <class T> +SharedPointer<T>& SharedPointer<T>::operator=(SharedPointer&& other) { + if (this != &other) { + free(); + + instances = other.instances; + value = other.value; + + other.instances = nullptr; + other.value = nullptr; + } + return *this; +} + +template <class T> +T& SharedPointer<T>::operator*() { + return *value; +} + +template <class T> +const T& SharedPointer<T>::operator*() const { + return *value; +} + +template <class T> +T* SharedPointer<T>::operator->() { + return value; +} + +template <class T> +const T* SharedPointer<T>::operator->() const { + return value; +} + +template <class T> +unsigned SharedPointer<T>::RefCount() const { + if (instances == nullptr) + return 0; + return *instances; +} + +#endif /* _SHAREDPOINTER */ |
