diff --git a/homework/unique_ptr/unique_ptr.cpp b/homework/unique_ptr/unique_ptr.cpp new file mode 100644 index 00000000..ea940248 --- /dev/null +++ b/homework/unique_ptr/unique_ptr.cpp @@ -0,0 +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; +} + +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_; + } +}; + +} // namespace my \ 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..15059935 --- /dev/null +++ b/homework/unique_ptr/unique_ptr.hpp @@ -0,0 +1,25 @@ + + +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_; +}; + +}; // namespace my \ No newline at end of file