-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyNoteMemory.h
More file actions
199 lines (150 loc) · 3.31 KB
/
MyNoteMemory.h
File metadata and controls
199 lines (150 loc) · 3.31 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
/// auto pointer, reference counting always = 1
template <class _Tp>
class auto_ptr
{
private:
_Tp* _M_ptr;
public:
auto_ptr(auto_ptr& __a) __STL_NOTHROW : _M_ptr(__a.release()) {}
auto_ptr& operator=(auto_ptr& __a) __STL_NOTHROW
{
if (&__a != this)
{
delete _M_ptr;
_M_ptr = __a.release();
}
return *this;
}
~auto_ptr() { delete _M_ptr; }
_Tp& operator*() const __STL_NOTHROW
{
return *_M_ptr;
}
_Tp* operator->() const __STL_NOTHROW
{
return _M_ptr;
}
_Tp* get() const __STL_NOTHROW
{
return _M_ptr;
}
/// release:
/// 1. let the pointer no longer point to the object,
/// 2. return the pointed object to other auto pointer
_Tp* release() __STL_NOTHROW
{
_Tp* __tmp = _M_ptr;
_M_ptr = 0;
return __tmp;
}
void reset(_Tp* __p = 0) __STL_NOTHROW
{
if (__p != _M_ptr) {
delete _M_ptr;
_M_ptr = __p;
}
}
};
class sp_counted_base
{
public:
sp_counted_base(): use_count_(1), weak_count_(1)
{
}
void add_ref_copy() { ++use_count_; }
void release() // nothrow
{
{
long new_use_count = --use_count_;
if(new_use_count != 0) return;
}
dispose(); /// will trigger a delete operation
weak_release();
}
};
private:
long use_count_; // #shared
long weak_count_; // #weak + (#shared != 0)
};
class shared_count
{
private:
sp_counted_base * pi_;
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
int id_;
public:
shared_count(shared_count const & r): pi_(r.pi_) // nothrow
{
if(pi_!= 0) pi_->add_ref_copy();
}
~shared_count() // nothrow
{
if(pi_ != 0) pi_->release();
}
}
template<class T>
class shared_ptr
{
typedef shared_ptr<T> this_type;
private:
T * px; // contained pointer
boost::detail::shared_count pn; // reference counter
public:
shared_ptr & operator=( shared_ptr && r ) // never throws
{
this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
return *this;
}
template<class Y>
shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
{
this_type(r).swap(*this);
return *this;
}
typedef typename boost::detail::shared_ptr_traits<T>::reference reference;
reference operator* () const // never throws
{
BOOST_ASSERT(px != 0);
return *px;
}
T * operator-> () const // never throws
{
BOOST_ASSERT(px != 0);
return px;
}
shared_ptr(): px(0), pn() // never throws in 1.30+
{
}
void reset() // never throws in 1.30+
{
this_type().swap(*this); /// this_type() trigger shared_ptr() constructor
}
void swap(shared_ptr<T> & other) // never throws
{
std::swap(px, other.px);
pn.swap(other.pn);
}
}; // shared_ptr
/// examples
// auto_ptr example
#include <iostream>
#include <memory>
int main () {
std::auto_ptr<int> p1 (new int);
*p1.get()=10;
std::auto_ptr<int> p2 (p1);
std::cout << "p2 points to " << *p2 << '\n';
// (p1 is now null-pointer auto_ptr)
return 0;
}
// shared_ptr constructor example
#include <iostream>
#include <memory>
struct C {int* data;};
int main () {
std::shared_ptr<C> obj (new C);
std::shared_ptr<int> p (obj, obj->data);
std::cout << "use_count:\n";
std::cout << "p: " << p.use_count() << '\n';
return 0;
}