-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathsmart_ptr.cpp
155 lines (120 loc) · 2.79 KB
/
smart_ptr.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
// Copyright (c) 2020 by Chrono
//
// g++ smart_ptr.cpp -std=c++11 -o a.out;./a.out
// g++ smart_ptr.cpp -std=c++14 -o a.out;./a.out
// g++ smart_ptr.cpp -std=c++14 -I../common -o a.out;./a.out
#include <cassert>
#include <iostream>
#include <memory>
#include <string>
template<class T, class... Args>
std::unique_ptr<T>
my_make_unique(Args&&... args)
{
return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
}
void case1()
{
using namespace std;
unique_ptr<int> ptr1(new int(10));
assert(*ptr1 == 10);
assert(ptr1 != nullptr);
unique_ptr<string> ptr2(new string("hello"));
assert(*ptr2 == "hello");
assert(ptr2->size() == 5);
//ptr1++;
//ptr2 += 2;
//unique_ptr<int> ptr3;
//*ptr3 = 42;
auto ptr3 = make_unique<int>(42);
assert(ptr3 && *ptr3 == 42);
auto ptr4 = make_unique<string>("god of war");
assert(!ptr4->empty());
auto ptr5 = my_make_unique<long>(100L);
assert(*ptr5 == 100);
}
void case2()
{
using namespace std;
auto ptr1 = make_unique<int>(42);
assert(ptr1 && *ptr1 == 42);
auto ptr2 = std::move(ptr1);
assert(!ptr1 && ptr2);
}
void case3()
{
using namespace std;
shared_ptr<int> ptr1(new int(10));
assert(*ptr1 = 10);
shared_ptr<string> ptr2(new string("hello"));
assert(*ptr2 == "hello");
auto ptr3 = make_shared<int>(42);
assert(ptr3 && *ptr3 == 42);
auto ptr4 = make_shared<string>("zelda");
assert(!ptr4->empty());
}
void case4()
{
using namespace std;
auto ptr1 = make_shared<int>(42);
assert(ptr1 && ptr1.unique() );
auto ptr2 = ptr1;
assert(ptr1 && ptr2);
assert(ptr1 == ptr2);
assert(!ptr1.unique() && ptr1.use_count() == 2);
assert(!ptr2.unique() && ptr2.use_count() == 2);
}
class DemoShared final
{
public:
DemoShared() = default;
~DemoShared()
{
// do some blocking thing ...
}
};
class Node final
{
public:
using this_type = Node;
//using shared_type = std::shared_ptr<this_type>;
using shared_type = std::weak_ptr<this_type>;
public:
shared_type next;
public:
Node() = default;
~Node()
{
using namespace std;
cout << "node dtor" << endl;
}
};
void case5()
{
using namespace std;
auto n1 = make_shared<Node>();
auto n2 = make_shared<Node>();
assert(n1.use_count() == 1);
assert(n2.use_count() == 1);
n1->next = n2;
n2->next = n1;
assert(n1.use_count() == 1);
assert(n2.use_count() == 1);
if (!n1->next.expired()) {
auto ptr = n1->next.lock();
assert(ptr == n2);
}
//assert(n1.use_count() == 2);
//assert(n2.use_count() == 2);
}
int main()
{
using namespace std;
case1();
case2();
case3();
case4();
case5();
cout << "smart_ptr demo" << endl;
}