-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
245 lines (190 loc) · 6.08 KB
/
main.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <iostream>
#include <adamcuda.h>
#include <iostream>
class Container {
int * m_Data;
public:
Container() {
//Allocate an array of 20 int on heap
m_Data = new int[20];
std::cout << "Constructor: Allocation 20 int" << std::endl;
}
~Container() {
if (m_Data) {
delete[] m_Data;
m_Data = NULL;
}
}
// Copy constructor
Container(const Container & obj) {
//Allocate an array of 20 int on heap
m_Data = new int[20];
//Copy the data from passed object
for (int i = 0; i < 20; i++)
m_Data[i] = obj.m_Data[i];
std::cout << "Copy Constructor: Allocation 20 int" << std::endl;
}
//Assignment Operator
Container & operator=(const Container & obj) {
if(this != &obj)
{
//Allocate an array of 20 int on heap
m_Data = new int[20];
//Copy the data from passed object
for (int i = 0; i < 20; i++)
m_Data[i] = obj.m_Data[i];
std::cout << "Assigment Operator: Allocation 20 int" << std::endl;
}
}
// Move Constructor
Container(Container && obj){
// Just copy the pointer
m_Data = obj.m_Data;
// Set the passed object's member to NULL
obj.m_Data = NULL;
std::cout<<"Move Constructor"<<std::endl;
}
// Move Assignment Operator
Container& operator=(Container && obj){
if(this != &obj)
{
// Just copy the pointer
m_Data = obj.m_Data;
// Set the passed object's member to NULL
obj.m_Data = NULL;
std::cout<<"Move Assignment Operator"<<std::endl;
}
}
};
Container getContainer(){
Container c;
return c;
}
namespace raaj{
template<typename T>
class shared_ptr{
public:
T* object_;
unsigned int* reference_counter_ = nullptr;
std::mutex lock_;
unsigned int use_count(){
return *reference_counter_;
}
T* get(){
return object_;
}
shared_ptr(){
std::cout << "Empty Constructor" << std::endl;
reference_counter_ = new unsigned int[1];
*reference_counter_ = 0;
}
// Normal Constructor
shared_ptr(T* object){
std::cout << "Normal Constructor" << std::endl;
object_ = object;
reference_counter_ = new unsigned int[1];
*reference_counter_ = 1;
}
// Copy Constructor (CANT USE CONST HERE)
shared_ptr(const shared_ptr & obj) {
std::cout << "Copy Constructor" << std::endl;
//lock_.lock();
// increase ref
*obj.reference_counter_ += 1;
// set reset
reference_counter_ = obj.reference_counter_;
object_ = obj.object_;
//lock_.unlock();
}
//Assignment Operator
shared_ptr & operator=(const shared_ptr & obj) {
if(this != &obj)
{
std::cout << "Assignment Operator" << std::endl;
// increase ref
*obj.reference_counter_ += 1;
// set reset
//delete reference_counter_;
reference_counter_ = obj.reference_counter_; // I am overriding this?
object_ = obj.object_;
}
}
// Destructor
~shared_ptr(){
std::cout << "Destructor" << std::endl;
*reference_counter_ -= 1;
if(use_count() == 0){
delete object_;
delete reference_counter_;
}
}
};
}
class Object{
public:
Object(){}
~Object(){}
};
//// // Move Constructor
//// shared_ptr(shared_ptr && obj){
//// std::cout<<"Move Constructor"<<std::endl;
//// }
// // Move Assignment Operator
// shared_ptr& operator=(shared_ptr && obj){
// if(this != &obj)
// {
// std::cout<<"Move Assignment Operator"<<std::endl;
// }
// }
void function(raaj::shared_ptr<Object> object_ptr)
{
std::cout << object_ptr.use_count() << std::endl;
//object_ptr.reset();
}
int main(int argc, char* argv[])
{
// // Create a vector of Container Type
// std::cout << "-1-" << std::endl;
// std::vector<Container> vecOfContainers;
// std::cout << "-2-" << std::endl;
// Container cont; // Regular Constructor
// std::cout << "-3-" << std::endl;
// vecOfContainers.push_back(cont); // Copy Constructor - Copy data from passed object
// std::cout << "-4-" << std::endl;
// vecOfContainers.clear(); //
// std::cout << "-5-" << std::endl;
// vecOfContainers.reserve(3);
// std::cout << "-6-" << std::endl;
// vecOfContainers.push_back(Container()); // Constructor, then Move (just set the pointer directly)
// std::cout << "-7-" << std::endl;
// //Container cont2 = cont;
// cont = getContainer(); // Constructor, then Move Assignment (For return operation, same as above)
// std::cout << "-8-" << std::endl;
// Container contx;
// contx = cont; // Assignment Operator // Copy data from passed object if not the same
// exit(-1);
////////////////////
raaj::shared_ptr<Object> object_ptr = raaj::shared_ptr<Object>(new Object());
// Copy Constructor
{
raaj::shared_ptr<Object> object_ptr_ref = object_ptr;
std::cout << object_ptr.use_count() << std::endl;
function(object_ptr);
}
// Assignment Operator
{
raaj::shared_ptr<Object> hold;
hold = object_ptr;
std::cout << object_ptr.use_count() << std::endl;
}
std::cout << object_ptr.use_count() << std::endl;
////////////////////
// std::shared_ptr<Object> object_ptr = std::shared_ptr<Object>(new Object());
// {
// std::shared_ptr<Object> object_ptr_ref = object_ptr;
// std::cout << object_ptr.use_count() << std::endl;
// }
// function(object_ptr);
// std::cout << object_ptr.use_count() << std::endl;
return 0;
}