From 5e9cb4ddc811465731596d8ee684f3d8cd5a6aea Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 2 May 2026 16:05:06 +0300 Subject: feat(Week11): Add solutions --- Week11/Exercise03/SharedPointer.hpp | 129 ++++++++++++++++++++++++++++++++++++ Week11/Exercise03/main.cpp | 17 +++++ 2 files changed, 146 insertions(+) create mode 100644 Week11/Exercise03/SharedPointer.hpp create mode 100644 Week11/Exercise03/main.cpp (limited to 'Week11/Exercise03') 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 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 +void SharedPointer::free() { + if (instances == nullptr) + return; + + --*instances; + if (*instances == 0) { + delete instances; + delete value; + } +} + +template +void SharedPointer::copyFrom(const SharedPointer& other) { + this->instances = other.instances; + this->value = other.value; + ++*instances; +} + +template +SharedPointer::SharedPointer(T* value) : instances(new unsigned), value(value) +{ + *instances = 1; +} + +template +SharedPointer::SharedPointer() : instances(), value() +{ } + +template +SharedPointer::~SharedPointer() { + free(); +} + +template +SharedPointer::SharedPointer(const SharedPointer& other) { + copyFrom(other); +} + +template +SharedPointer& SharedPointer::operator=(const SharedPointer& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +template +SharedPointer::SharedPointer(SharedPointer&& other) { + instances = other.instances; + value = other.value; + + other.instances = nullptr; + other.value = nullptr; +} + +template +SharedPointer& SharedPointer::operator=(SharedPointer&& other) { + if (this != &other) { + free(); + + instances = other.instances; + value = other.value; + + other.instances = nullptr; + other.value = nullptr; + } + return *this; +} + +template +T& SharedPointer::operator*() { + return *value; +} + +template +const T& SharedPointer::operator*() const { + return *value; +} + +template +T* SharedPointer::operator->() { + return value; +} + +template +const T* SharedPointer::operator->() const { + return value; +} + +template +unsigned SharedPointer::RefCount() const { + if (instances == nullptr) + return 0; + return *instances; +} + +#endif /* _SHAREDPOINTER */ diff --git a/Week11/Exercise03/main.cpp b/Week11/Exercise03/main.cpp new file mode 100644 index 0000000..d8a4d66 --- /dev/null +++ b/Week11/Exercise03/main.cpp @@ -0,0 +1,17 @@ +#include "SharedPointer.hpp" +#include + +void inc(SharedPointer val) { + (*val)++; +} + +int main() { + SharedPointer x(new int); + SharedPointer y = x; + *x = 5; + std::cout << *x << ' ' << *y << std::endl; + inc(y); + std::cout << *x << ' ' << *y << std::endl; + + return 0; +} -- cgit v1.3.1-23-gd82d