Repository navigation
Expand file tree
/
Copy pathtest_shared_ptr.cc
More file actions
143 lines (115 loc) · 3.28 KB
/
Copy pathtest_shared_ptr.cc
File metadata and controls
143 lines (115 loc) · 3.28 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
#include <atomic>
#include <iostream>
class SharedCount {
public:
SharedCount() : count_{1} {}
void add() { ++count_; }
void minus() { --count_; }
int get() const { return count_; }
private:
std::atomic<int> count_;
};
template <typename T>
class SharedPtr {
public:
template <typename U>
friend class SharedPtr;
template <typename K, typename U>
friend SharedPtr<K> static_pointer_cast(const SharedPtr<U>& p);
SharedPtr(T* ptr) : ptr_{ptr}, ref_count_{new SharedCount} {}
SharedPtr() : ptr_{nullptr}, ref_count_{new SharedCount} {}
~SharedPtr() { clean(); }
SharedPtr(const SharedPtr& p) {
this->ptr_ = p.ptr_;
this->ref_count_ = p.ref_count_;
ref_count_->add();
}
SharedPtr(SharedPtr&& p) {
this->ptr_ = p.ptr_;
this->ref_count_ = p.ref_count_;
p.ptr_ = nullptr;
p.ref_count_ = nullptr;
}
SharedPtr& operator=(const SharedPtr& p) {
if (this == &p) return *this;
clean();
this->ptr_ = p.ptr_;
this->ref_count_ = p.ref_count_;
ref_count_->add();
return *this;
}
SharedPtr& operator=(SharedPtr&& p) {
if (this == &p) return *this;
clean();
this->ptr_ = p.ptr_;
this->ref_count_ = p.ref_count_;
p.ptr_ = nullptr;
p.ref_count_ = nullptr;
return *this;
}
int use_count() { return ref_count_->get(); }
T* get() const { return ptr_; }
T* operator->() const { return ptr_; }
T& operator*() const { return *ptr_; }
operator bool() const { return ptr_; }
private:
template <typename U>
SharedPtr(const SharedPtr<U>& p, T* ptr) {
this->ptr_ = ptr;
this->ref_count_ = p.ref_count_;
ref_count_->add();
}
void clean() {
if (ref_count_) {
ref_count_->minus();
if (ref_count_->get() == 0) {
if (ptr_) delete ptr_;
delete ref_count_;
}
}
}
T* ptr_;
SharedCount* ref_count_;
};
template <typename T, typename U>
SharedPtr<T> static_pointer_cast(const SharedPtr<U>& p) {
T* ptr = static_cast<T*>(p.get());
return SharedPtr<T>(p, ptr);
}
struct A {
A() { std::cout << "A() \n"; }
~A() { std::cout << "~A() \n"; }
};
struct B : A {
B() { std::cout << "B() \n"; }
~B() { std::cout << "~B() \n"; }
};
void test_cast_shared() {
B* a = new B;
SharedPtr<B> ptr(a);
{
std::cout << ptr.use_count() << std::endl;
SharedPtr<A> b = static_pointer_cast<A>(ptr);
std::cout << ptr.use_count() << std::endl;
SharedPtr<B> c = ptr;
std::cout << ptr.use_count() << std::endl;
SharedPtr<B> d = ptr;
std::cout << ptr.use_count() << std::endl;
}
std::cout << ptr.use_count() << std::endl;
}
void test_simple_shared() {
A* a = new A;
SharedPtr<A> ptr(a);
{
std::cout << ptr.use_count() << std::endl;
SharedPtr<A> b = ptr;
std::cout << ptr.use_count() << std::endl;
SharedPtr<A> c = ptr;
std::cout << ptr.use_count() << std::endl;
SharedPtr<A> d = std::move(b);
std::cout << ptr.use_count() << std::endl;
}
std::cout << ptr.use_count() << std::endl;
}
int main() { test_simple_shared(); }