Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions homework/unique_ptr/unique_ptr.cpp
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions homework/unique_ptr/unique_ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@


namespace my {

template <typename T>
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
Loading