From 3005b1a728fd8559ca57f6623ea2e0e1dd4cf664 Mon Sep 17 00:00:00 2001 From: karol Date: Tue, 8 Jul 2025 18:23:26 +0200 Subject: [PATCH 1/2] Added implementation of unique_ptr --- homework/unique_ptr/unique_ptr.cpp | 58 ++++++++++++++++++++++++++++++ homework/unique_ptr/unique_ptr.hpp | 26 ++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 homework/unique_ptr/unique_ptr.cpp create mode 100644 homework/unique_ptr/unique_ptr.hpp diff --git a/homework/unique_ptr/unique_ptr.cpp b/homework/unique_ptr/unique_ptr.cpp new file mode 100644 index 00000000..74811615 --- /dev/null +++ b/homework/unique_ptr/unique_ptr.cpp @@ -0,0 +1,58 @@ +#include "unique_ptr.hpp" + +namespace my +{ + + + unique_ptr::unique_ptr(T* ptr) {}; + + unique_ptr::~unique_ptr() { + if(!ptr_) + { + delete ptr_; + } + }; + + unique_ptr::unique_ptr(unique_ptr&& other){ + if(!ptr_){ + delete ptr_; + } + T* ptr = other.release(); + ptr_ = ptr; + } + + unique_ptr::unique_ptr& operator=(const unique_ptr& other) = delete; + + unique_ptr& unique_ptr::operator=(unique_ptr&& other){ + if(!ptr_){ + delete ptr_; + } + T* ptr = other.release(); + ptr_ = ptr; + } + + T& unique_ptr::operator*() { + return *ptr_; + }; + + T* unique_ptr::operator->() { + return ptr_; + }; + + + T* unique_ptr::get() const {return ptr_}; + + T* unique_ptr::release() { + T* ptr = ptr_; + ptr_ = nullptr; + return ptr; + }; + + void unique_ptr::reset(T* ptr){ + if(!ptr_) + { + delete ptr_; + } + }; + +} \ No newline at end of file diff --git a/homework/unique_ptr/unique_ptr.hpp b/homework/unique_ptr/unique_ptr.hpp new file mode 100644 index 00000000..d19b9182 --- /dev/null +++ b/homework/unique_ptr/unique_ptr.hpp @@ -0,0 +1,26 @@ + + +namespace my +{ + +template +class unique_ptr +{ +public: + unique_ptr(T* ptr) : ptr_(ptr); + unique_ptr(const unique_ptr&); + ~unique_ptr(); + unique_ptr(unique_ptr&& other); + unique_ptr& operator=(const unique_ptr& other) = delete; + unique_ptr& operator=(unique_ptr&& other); + T& operator*(); + T* operator->(); + T* get() const; + T* release(); + void reset(T* ptr); +private: + T* ptr_; +}; + + +}; \ No newline at end of file From 3cecf794241f348fdf24f6ee44fc18f04f28b78c Mon Sep 17 00:00:00 2001 From: karol Date: Tue, 8 Jul 2025 18:25:22 +0200 Subject: [PATCH 2/2] Fixed formating --- homework/unique_ptr/unique_ptr.cpp | 87 ++++++++++++++---------------- homework/unique_ptr/unique_ptr.hpp | 15 +++--- 2 files changed, 48 insertions(+), 54 deletions(-) diff --git a/homework/unique_ptr/unique_ptr.cpp b/homework/unique_ptr/unique_ptr.cpp index 74811615..ea940248 100644 --- a/homework/unique_ptr/unique_ptr.cpp +++ b/homework/unique_ptr/unique_ptr.cpp @@ -1,58 +1,53 @@ #include "unique_ptr.hpp" -namespace my -{ - - - unique_ptr::unique_ptr(T* ptr) {}; - - unique_ptr::~unique_ptr() { - if(!ptr_) - { - delete ptr_; - } - }; - - unique_ptr::unique_ptr(unique_ptr&& other){ - if(!ptr_){ - delete ptr_; - } - T* ptr = other.release(); - ptr_ = ptr; - } +namespace my { + +unique_ptr::unique_ptr(T* ptr){}; - unique_ptr::unique_ptr& operator=(const unique_ptr& other) = delete; +unique_ptr::~unique_ptr() { + if (!ptr_) { + delete ptr_; + } +}; - unique_ptr& unique_ptr::operator=(unique_ptr&& other){ - if(!ptr_){ - delete ptr_; - } - T* ptr = other.release(); - ptr_ = ptr; +unique_ptr::unique_ptr(unique_ptr&& other) { + if (!ptr_) { + delete ptr_; } + T* ptr = other.release(); + ptr_ = ptr; +} - T& unique_ptr::operator*() { - return *ptr_; - }; +unique_ptr::unique_ptr& operator=(const unique_ptr& other) = delete; - T* unique_ptr::operator->() { - return ptr_; - }; +unique_ptr& unique_ptr::operator=(unique_ptr&& other) { + if (!ptr_) { + delete ptr_; + } + T* ptr = other.release(); + ptr_ = ptr; +} + +T& unique_ptr::operator*() { + return *ptr_; +}; +T* unique_ptr::operator->() { + return ptr_; +}; - T* unique_ptr::get() const {return ptr_}; +T* unique_ptr::get() const { return ptr_ }; - T* unique_ptr::release() { - T* ptr = ptr_; - ptr_ = nullptr; - return ptr; - }; +T* unique_ptr::release() { + T* ptr = ptr_; + ptr_ = nullptr; + return ptr; +}; - void unique_ptr::reset(T* ptr){ - if(!ptr_) - { - delete ptr_; - } - }; +void unique_ptr::reset(T* ptr) { + if (!ptr_) { + delete ptr_; + } +}; -} \ No newline at end of file +} // namespace my \ No newline at end of file diff --git a/homework/unique_ptr/unique_ptr.hpp b/homework/unique_ptr/unique_ptr.hpp index d19b9182..15059935 100644 --- a/homework/unique_ptr/unique_ptr.hpp +++ b/homework/unique_ptr/unique_ptr.hpp @@ -1,13 +1,12 @@ -namespace my -{ +namespace my { -template -class unique_ptr -{ +template +class unique_ptr { public: - unique_ptr(T* ptr) : ptr_(ptr); + unique_ptr(T* ptr) + : ptr_(ptr); unique_ptr(const unique_ptr&); ~unique_ptr(); unique_ptr(unique_ptr&& other); @@ -18,9 +17,9 @@ class unique_ptr T* get() const; T* release(); void reset(T* ptr); + private: T* ptr_; }; - -}; \ No newline at end of file +}; // namespace my \ No newline at end of file