-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathali_unique_ptr.h
More file actions
101 lines (90 loc) · 3.36 KB
/
ali_unique_ptr.h
File metadata and controls
101 lines (90 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// Created by Ali Moammeri on 3/28/22.
//
#ifndef ALI_UNIQUE_PTR_H
#define ALI_UNIQUE_PTR_H
#include <iostream>
#include <cctype>
#include <compare>
namespace ali {
template<class T> class unique_ptr {
friend std::ostream& operator<<(std::ostream&, const unique_ptr<T>&);
public:
unique_ptr() noexcept= default;
explicit unique_ptr(std::nullptr_t nptr) noexcept: _pointer{nptr} {}
explicit unique_ptr(T* pointer) noexcept : _pointer{pointer} {}
unique_ptr(const unique_ptr&) noexcept= delete;
unique_ptr(unique_ptr&& other) noexcept : _pointer {other.release()}{};
~unique_ptr() { delete _pointer; }
T* get() const noexcept { return _pointer; }
T* release() noexcept {
T* temp_ptr {get()};
_pointer = nullptr;
return temp_ptr;
}
void reset(T* pointer = T()) noexcept {
delete _pointer;
_pointer = pointer;
}
void reset(std::nullptr_t nptr) noexcept {
delete _pointer;
_pointer = nptr;
}
void swap(unique_ptr& other) { std::swap(_pointer, other._pointer); }
unique_ptr& operator=(unique_ptr&& other) noexcept {
reset(other.release());
return *this;
}
unique_ptr& operator=(const unique_ptr&) noexcept = delete;
auto operator<=>(const unique_ptr& rhs) const = default;
explicit operator bool() const noexcept { return _pointer != nullptr; }
T& operator*() { return *get(); }
T* operator->() { return get(); }
private:
T* _pointer{nullptr};
};
// Specialization for arrays:
template <class T> class unique_ptr<T[]> {
friend std::ostream& operator<<(std::ostream&, const unique_ptr<T[]>&);
public:
unique_ptr() noexcept= default;
explicit unique_ptr(std::nullptr_t nptr) noexcept: _pointer{nptr} {}
explicit unique_ptr(T pointer[]) noexcept : _pointer{pointer} {}
unique_ptr(const unique_ptr&) noexcept= delete;
unique_ptr(unique_ptr&& other) noexcept : _pointer {other.release()}{};
~unique_ptr() { delete[] _pointer; }
T* get() const noexcept { return _pointer; }
T* release() noexcept {
T* temp_ptr {get()};
_pointer = nullptr;
return temp_ptr;
}
void reset(T* pointer = T()) noexcept {
delete _pointer;
_pointer = pointer;
}
void reset(std::nullptr_t nptr) noexcept {
delete _pointer;
_pointer = nptr;
}
void swap(unique_ptr& other) { std::swap(_pointer, other._pointer); }
unique_ptr& operator=(unique_ptr&& other) noexcept {
reset(other.release());
return *this;
}
unique_ptr& operator=(const unique_ptr&) noexcept = delete;
auto operator<=>(const unique_ptr& rhs) const = default;
explicit operator bool() const noexcept { return _pointer != nullptr; }
T& operator*() { return *get(); }
T* operator->() { return get(); }
T& operator[](const std::size_t& index) { return *(get() + index); }
private:
T* _pointer{nullptr};
};
}
template<typename T>
std::ostream& operator<<(std::ostream& out, const ali::unique_ptr<T>& u_ptr) {
out << u_ptr.get();
return out;
}
#endif //ALI_UNIQUE_PTR_H