Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: compiled/intrusive_ptr.h

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Replaced shared_ptr by boost-like intrusive_ptr Created Jan. 28, 2016, 7:50 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « compiled/debug.h ('k') | compiled/shell.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Parts of this code have been copied from boost/smart_ptr/intrusive_ptr.hpp.
2 //
3 // Copyright (c) 2001, 2002 Peter Dimov
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8
9 #ifndef ADBLOCK_PLUS_INTRUSIVE_PTR_H
10 #define ADBLOCK_PLUS_INTRUSIVE_PTR_H
11
12 #include <type_traits>
13
14 class ref_counted
15 {
16 public:
17 void AddRef()
18 {
19 mRefCount++;
20 }
21
22 void ReleaseRef()
23 {
24 if (--mRefCount == 0)
25 delete this;
26 }
27
28 protected:
29 ref_counted()
30 : mRefCount(0)
31 {
32 }
33
34 private:
35 int mRefCount;
36 };
37
38 template<class T,
39 class = typename std::enable_if<std::is_base_of<ref_counted,T>::value>::type >
40 class intrusive_ptr
41 {
42 public:
43 intrusive_ptr()
44 : mPointer(nullptr)
45 {
46 }
47
48 intrusive_ptr(T* pointer)
49 : mPointer(pointer)
50 {
51 if (mPointer)
52 mPointer->AddRef();
53 }
54
55 intrusive_ptr(const intrusive_ptr& other)
56 : mPointer(other.mPointer)
57 {
58 if (mPointer)
59 mPointer->AddRef();
60 }
61
62 intrusive_ptr(intrusive_ptr&& other)
63 : mPointer(other.mPointer)
64 {
65 other.mPointer = nullptr;
66 }
67
68 ~intrusive_ptr()
69 {
70 if (mPointer)
71 mPointer->ReleaseRef();
72 }
73
74 intrusive_ptr& operator=(intrusive_ptr& other)
75 {
76 intrusive_ptr(other).swap(*this);
77 return *this;
78 }
79
80 intrusive_ptr& operator=(intrusive_ptr&& other)
81 {
82 intrusive_ptr(other).swap(*this);
83 return *this;
84 }
85
86 intrusive_ptr& operator=(T* other)
87 {
88 intrusive_ptr(other).swap(*this);
89 }
90
91 void reset()
92 {
93 intrusive_ptr().swap(*this);
94 }
95
96 void reset(T* other)
97 {
98 intrusive_ptr(other).swap(*this);
99 }
100
101 T* get() const
102 {
103 return mPointer;
104 }
105
106 T& operator*() const
107 {
108 return *mPointer;
109 }
110
111 T* operator->() const
112 {
113 return mPointer;
114 }
115
116 operator bool() const
117 {
118 return mPointer != nullptr;
119 }
120
121 operator T*() const
122 {
123 return mPointer;
124 }
125
126 bool operator!() const
127 {
128 return mPointer == nullptr;
129 }
130
131 void swap(intrusive_ptr& other)
132 {
133 T* tmp = mPointer;
134 mPointer = other.mPointer;
135 other.mPointer = tmp;
136 }
137
138 private:
139 T* mPointer;
140 };
141
142 template<class T, class U>
143 inline bool operator==(const intrusive_ptr<T>& a, const intrusive_ptr<U>& b)
144 {
145 return a.get() == b.get();
146 }
147
148 template<class T, class U>
149 inline bool operator!=(const intrusive_ptr<T>& a, const intrusive_ptr<U>& b)
150 {
151 return a.get() != b.get();
152 }
153
154 template<class T, class U>
155 inline bool operator==(const intrusive_ptr<T>& a, const U* b)
156 {
157 return a.get() == b;
158 }
159
160 template<class T, class U>
161 inline bool operator!=(const intrusive_ptr<T>& a, const U* b)
162 {
163 return a.get() != b;
164 }
165
166 template<class T, class U>
167 inline bool operator==(const T* a, const intrusive_ptr<U>& b)
168 {
169 return a == b.get();
170 }
171
172 template<class T, class U>
173 inline bool operator!=(const T* a, intrusive_ptr<U> const& b)
174 {
175 return a != b.get();
176 }
177
178 #endif
OLDNEW
« no previous file with comments | « compiled/debug.h ('k') | compiled/shell.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld