-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmart_ptr.cpp
More file actions
183 lines (161 loc) · 5.11 KB
/
Smart_ptr.cpp
File metadata and controls
183 lines (161 loc) · 5.11 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <exception>
using namespace std;
class null_ptr_exception : public std::exception{
public:
null_ptr_exception() : std::exception() {}
virtual const char* what() const noexcept{
return "nullptr exception";
}
};
template <typename T>
class smart_ptr {
private:
T* ptr_; // pointer to the referred object
int* ref_; // pointer to a reference count
//auxilary function used to check if there is room for allocation
bool check(int* &reference) {
int* temp_num;
try {
temp_num = new int(1);
}
catch(bad_alloc) {
reference = nullptr;
return false;
}
delete temp_num;
return true;
}
//auxilary function used to delete a smart pointer
void remove() {
if (ref_ != nullptr) {
if (*ref_ == 1) {
delete ptr_;
delete ref_;
}
else {
*ref_ = *ref_ - 1;
}
ptr_ = nullptr;
ref_ = nullptr;
}
}
public:
//tries to create a new pointer that is set to nullptr, if cant -> throw exception
smart_ptr() noexcept {
this->ptr_ = nullptr;
this->ref_ = nullptr;
}
// Create a smart_ptr that is initialized to nullptr. The reference count
// should be initialized to nullptr.
//tries to create a new pointer with the value given and tries to set count to 1
//if cant -> throw exception
explicit smart_ptr(T* &raw_ptr){
if (check(ref_) == false) {
throw bad_alloc();
}
this->ptr_ = raw_ptr;
this->ref_ = new int(1);
}
//exception leaves func f
// Create a smart_ptr that is initialized to raw_ptr. The reference count
// should be one. No change is made to raw_ptr.
//copy constructor for rvalue
explicit smart_ptr(T* &&raw_ptr){
if (check(ref_) == false) {
delete raw_ptr;
throw bad_alloc();
}
this->ptr_ = raw_ptr;
ref_ = new int(1);
}
//want sp to delete but exception to leave func
// Create a smart_ptr that is initialized to raw_ptr. The reference count
// should be one. If the constructor fails raw_ptr is deleted.
//copy constructor
smart_ptr(const smart_ptr& rhs) noexcept{
this->ptr_ = rhs.ptr_;
this->ref_ = rhs.ref_;
if (ref_ != nullptr) {
*ref_ = *ref_ + 1;
}
}
// Copy construct a pointer from rhs. The reference count should be
// incremented by one.
//move constructor
smart_ptr(smart_ptr&& rhs) noexcept{
this->ptr_ = rhs.ptr_;
this->ref_ = rhs.ref_;
rhs.ptr_ = nullptr;
rhs.ref_ = nullptr;
}
// Move construct a pointer from rhs.
smart_ptr& operator=(const smart_ptr& rhs) noexcept{
if (this != &rhs) {
remove();
ptr_ = rhs.ptr_;
ref_ = rhs.ref_;
if (ref_ != nullptr) {
*ref_ = *ref_ + 1;
}
}
return *this;
}
// This assignment should make a shallow copy of the right-hand side's
// pointer data. The reference count should be incremented as appropriate.
smart_ptr& operator=(smart_ptr&& rhs) noexcept{
this->ptr_ = rhs.ptr_;
this->ref_ = rhs.ref_;
rhs.ptr_ = nullptr;
rhs.ref_ = nullptr;
return *this;
}
// This move assignment should steal the right-hand side's pointer data.
bool clone() {
if (ptr_ == nullptr || *ref_ == 1){
return false;
}
T* temp = new T(*ptr_);
//decrease the objects current ref count
remove();
//check for bad_alloc
if (check(ref_) == false) {
throw bad_alloc();
}
ptr_ = temp;
ref_ = new int(1);
return true;
} //exception leaves function
// If the smart_ptr is either nullptr or has a reference count of one, this
// function will do nothing and return false. Otherwise, the referred to
// object's reference count will be decreased and a new deep copy of the
// object will be created. This new copy will be the object that this
// smart_ptr points and its reference count will be one.
int ref_count() const noexcept{
if (ref_ == nullptr) {
return 0;
}
return *ref_;
}
// Returns the reference count of the pointed to data.
T& operator*(){
if (ptr_ != nullptr){
return *ptr_;
}
throw null_ptr_exception();
}
// The dereference operator shall return a reference to the referred object.
// Throws null_ptr_exception on invalid access.
T* operator->(){
if (ptr_) {
return ptr_;
}
throw null_ptr_exception();
}
// The arrow operator shall return the pointer ptr_. Throws null_ptr_exception
// on invalid access.
~smart_ptr() noexcept{
remove();
}
// deallocate all dynamic memory
};