-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshared_ptr_demo.cpp
More file actions
204 lines (178 loc) · 6.97 KB
/
shared_ptr_demo.cpp
File metadata and controls
204 lines (178 loc) · 6.97 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// demo of shared_ptr
/**
* Modified from cppreference.com's demo code of std::shared_ptr:
* https://en.cppreference.com/w/cpp/memory/shared_ptr,
* and cppreference.com's demo code of std::unique_ptr:
* https://en.cppreference.com/w/cpp/memory/unique_ptr,
* and cplusplus.com's demo code of std::shared_ptr constructors:
* http://www.cplusplus.com/reference/memory/shared_ptr/shared_ptr/
* and 陈硕's answer on Zhihu regarding owner_before:
* https://www.zhihu.com/question/24816143/answer/29080141
*/
#include <cstdio>
#include <cassert>
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <memory>
#include <thread>
#include <chrono>
#include <mutex>
// using std::unique_ptr;
// using std::shared_ptr;
// using std::weak_ptr;
// using std::make_shared;
// using std::default_delete;
// using std::get_deleter;
#include "smart_ptr.hpp"
using smart_ptr::unique_ptr;
using smart_ptr::shared_ptr;
using smart_ptr::weak_ptr;
using smart_ptr::default_delete;
using smart_ptr::make_shared;
using smart_ptr::get_deleter;
void close_file(std::FILE* fp) { std::fclose(fp); }
struct B {
B() = default;
~B() = default;
virtual void bar() { std::cout << "B::bar" << std::endl; }
};
struct D : B
{
D() { std::cout << "D::D" << std::endl; }
~D() { std::cout << "D::~D" << std::endl; }
void bar() override { std::cout << "D::bar" << std::endl; }
};
struct C {
int* data;
};
struct Base
{
Base() { std::cout << " Base::Base()\n"; }
// Note: non-virtual destructor is OK here
// See https://stackoverflow.com/questions/3899790/shared-ptr-magic
~Base() { std::cout << " Base::~Base()\n"; }
};
struct Derived: public Base
{
Derived() { std::cout << " Derived::Derived()\n"; }
~Derived() { std::cout << " Derived::~Derived()\n"; }
};
void thr(shared_ptr<Base> p)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
shared_ptr<Base> lp = p; // thread-safe, even though the
// shared use_count is incremented
{
static std::mutex io_mutex;
std::lock_guard<std::mutex> lk(io_mutex);
std::cout << "local pointer in a thread:\n"
<< " lp.get() = " << lp.get()
<< ", lp.use_count() = " << lp.use_count() << '\n';
}
}
int main()
{
std::cout << "===============shared_ptr demo===============" << std::endl;
std::cout << "\nShared ownership semantics demo\n";
{
auto sp = make_shared<D>(); // sp is a shared_ptr that manages a D
sp->bar(); // and p manages the D object
std::cout << sp.use_count() << std::endl;
auto sp2(sp); // copy constructs a new shared_ptr sp2
std::cout << sp.use_count() << std::endl;
} // ~D called here
std::cout << "\nRuntime polymorphism demo\n";
{
shared_ptr<B> sp(make_shared<D>()); // sp is a shared_ptr that manages a D as a pointer to B
sp->bar(); // virtual dispatch, calls D::bar
std::vector<shared_ptr<B>> v; // shared_ptr can be stored in a container
v.push_back(make_shared<D>());
v.push_back(std::move(sp));
v.emplace_back(new D{});
for (auto& sp: v) sp->bar(); // virtual dispatch, calls D::bar
} // ~D called 3 times
std::cout << "\nCustom deleter demo\n";
std::ofstream("demo.txt") << 'x'; // prepare the file to read
{
shared_ptr<std::FILE> fp(std::fopen("demo.txt", "r"), &close_file);
if(fp) { // fopen could have failed; in which case fp holds a null pointer
std::cout << static_cast<char>(std::fgetc(fp.get())) << std::endl;
}
} // fclose() called here, but only if FILE* is not a null pointer, i.e., if fopen succeeded
std::cout << "\nCustom lambda-expression deleter demo\n";
{
shared_ptr<D> sp(new D, [](D* ptr)
{
std::cout << "destroying from a custom deleter..." << std::endl;
delete ptr;
}); // up owns D
sp->bar();
} // the lambda above is called and D is destroyed
std::cout << "\nConstructor demo\n";
{
shared_ptr<int> p0;
shared_ptr<int> p1 (nullptr); // use_count = 0
shared_ptr<int> p2 (nullptr, default_delete<int>()); // use_count = 1 with custom deleter
shared_ptr<int> p3 (new int);
shared_ptr<int> p4 (new int, [](int* p){delete p;});
/* Custom allocator support is not implemented. */
// shared_ptr<int> p5 (new int, [](int* p){delete p;}, std::allocator<int>());
shared_ptr<int> p6 (p4);
shared_ptr<int> p7 (std::move(p6));
shared_ptr<int> p8 (unique_ptr<int>(new int));
shared_ptr<C> obj (new C);
shared_ptr<int> p9 (obj, obj->data); // aliasing constructor, different pointers, shared lifetime
std::cout << "use_count:\n";
std::cout << "p0: " << p1.use_count() << '\n';
std::cout << "p1: " << p1.use_count() << '\n';
std::cout << "p2: " << p2.use_count() << '\n';
std::cout << "p3: " << p3.use_count() << '\n';
std::cout << "p4: " << p4.use_count() << '\n';
/* Custom allocator support is not implemented. */
// std::cout << "p5: " << p5.use_count() << '\n';
std::cout << "p6: " << p6.use_count() << '\n';
std::cout << "p7: " << p7.use_count() << '\n';
std::cout << "p8: " << p8.use_count() << '\n';
std::cout << "p9: " << p9.use_count() << '\n';
}
std::cout << "\nMulti-threading demo\n";
{
shared_ptr<Base> p(new Derived());
std::cout << "Created a shared Derived (as a pointer to Base)\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n';
std::thread t1(thr, p), t2(thr, p), t3(thr, p);
p.reset(); // release ownership from main
std::cout << "Shared ownership between 3 threads and released\n"
<< "ownership from main:\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n';
t1.join(); t2.join(); t3.join();
std::cout << "All threads completed, the last one deleted Derived\n";
}
std::cout << "\nshared_ptr<void> and owner_before demo\n";
{
class BaseA { int a; };
class BaseB { double b; };
class Derived: public BaseA, public BaseB {};
shared_ptr<Derived> pd(new Derived);
shared_ptr<BaseB> pb(pd);
printf("%p %p\n", pd.get(), pb.get());
printf("%d %d\n", pd < pb, pb < pd); // 0 0
printf("%d %d\n", pd.owner_before(pb), pb.owner_before(pd)); // 0 0
shared_ptr<void> p0(pd), p1(pb);
printf("%p %p\n", p0.get(), p1.get());
printf("%d %d\n", p0 < p1, p1 < p0); // 1 0
printf("%d %d\n", p0.owner_before(p1), p1.owner_before(p0)); // 0 0
}
std::cout << "\nGet deleter demo\n";
{
auto sp = make_shared<D>();
D* p = new D;
auto del_p = get_deleter<default_delete<D>>(sp);
(*del_p)(p);
}
return 0;
}