-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared_ptr.h
More file actions
165 lines (145 loc) · 3.91 KB
/
Copy pathshared_ptr.h
File metadata and controls
165 lines (145 loc) · 3.91 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
//
// Created by Abderrahmane on 30/10/2016.
//
#ifndef _USERS_ABDERRAHMANE_CLIONPROJECTS_STD_SHARED_PTR_H_
#define _USERS_ABDERRAHMANE_CLIONPROJECTS_STD_SHARED_PTR_H_
#include <memory>
#include <utility>
template<typename T>
class shared_ptr {
typedef T element_type;
public:
// default constructor
shared_ptr() : ptr_(nullptr), ref_count_(nullptr) {}
// constructor from object
explicit shared_ptr(T *ptr);
// copy constructor
shared_ptr(const shared_ptr<T> ©);
// move constructor
shared_ptr(shared_ptr<T> &&origin);
// assignment operator
shared_ptr &operator=(const shared_ptr ©);
// move assignment operator
shared_ptr &operator=(shared_ptr &&origin);
// returns stored object reference
T &operator*() const;
// returns stored object pointer
T *operator->() const;
// comparison operator
bool operator!=(const shared_ptr<T> &rhs) { return !((ptr_ == rhs.ptr_) & (ref_count_ == rhs.ref_count_)); }
// destructor
virtual ~shared_ptr();
// return raw pointer to the object
element_type *get();
// return the number of instances referring to the object
int use_count();
// set references to nullptr
void reset();
private:
T *ptr_;
int *ref_count_;
};
template<typename T>
shared_ptr<T>::shared_ptr(T *ptr) : ptr_(ptr), ref_count_(nullptr) {
ref_count_ = new int(1);
}
// copy constructor
template<typename T>
shared_ptr<T>::shared_ptr(const shared_ptr<element_type> ©)
: ptr_(copy.ptr_), ref_count_(copy.ref_count_) {
if (ref_count_ != nullptr) {
++(*ref_count_);
}
}
// move constructor
template<typename T>
shared_ptr<T>::shared_ptr(shared_ptr<element_type> &&origin)
: ptr_(std::move(origin.ptr_)), ref_count_(std::move(origin.ref_count_)) {
// set to default state
origin.reset();
}
// assignment operator
template<typename T>
shared_ptr<T> &shared_ptr<T>::operator=(const shared_ptr ©) {
if (this != ©) { // comparison operators have to be implemented
// if the current pointer is the ONLY one referring to the object
// it has to be deleted
if (ref_count_ != nullptr) {
if (*ref_count_ == 1) {
std::cout << "destructor called on " << ptr_ << std::endl;
delete ptr_;
delete ref_count_;
reset();
}
}
ptr_ = copy.ptr_;
ref_count_ = copy.ref_count_;
if (ref_count_ != nullptr) {
++(*ref_count_);
}
}
return *this;
}
// move assignment operator
template<typename T>
shared_ptr<T> &shared_ptr<T>::operator=(shared_ptr &&origin) {
if (this != &origin) { // comparison operators have to be implemented
// if the current pointer is the ONLY one referring to the object
// it has to be deleted
if (ref_count_ != nullptr) {
if (*ref_count_ == 1) {
std::cout << "destructor called on " << ptr_ << std::endl;
delete ptr_;
delete ref_count_;
reset();
}
}
ptr_ = origin.ptr_;
ref_count_ = origin.ref_count_;
// set to default state
origin.reset();
}
return *this;
}
// returns stored object reference
template<typename T>
T &shared_ptr<T>::operator*() const {
return *ptr_;
}
// returns stored object pointer
template<typename T>
T *shared_ptr<T>::operator->() const {
return ptr_;
}
// destructor
template<typename T>
shared_ptr<T>::~shared_ptr() {
if (ref_count_ != nullptr) {
if (--(*ref_count_) == 0) {
std::cout << "destructor called on " << ptr_ << std::endl;
delete ptr_;
delete ref_count_;
}
}
reset();
}
// return raw pointer to the object
template<typename T>
T *shared_ptr<T>::get() {
return ptr_;
}
// return the number of instances referring to the object
template<typename T>
int shared_ptr<T>::use_count() {
if (ref_count_ != nullptr) {
return *ref_count_;
}
return 0;
}
// set references to nullptr
template<typename T>
void shared_ptr<T>::reset() {
ptr_ = nullptr;
ref_count_ = nullptr;
}
#endif // _USERS_ABDERRAHMANE_CLIONPROJECTS_STD_SHARED_PTR_H_