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: Reworked JS binding generation Created Feb. 1, 2016, 9:14 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
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)
René Jeschke 2016/02/02 11:11:23 Nit: having a check on `0` here feels wrong and ma
Wladimir Palant 2016/02/02 17:55:19 It's quite the opposite I think - if we are alread
25 delete this;
26 }
27
28 protected:
29 ref_counted()
30 : mRefCount(0)
31 {
32 }
33
34 // We need this virtual destructor, otherwise pointers to ref_counted and
35 // pointers to derived classes won't have the same value (e.g. converting from
36 // Filter* to ref_counted* decreases pointer value by 4).
37 virtual ~ref_counted()
38 {
39 }
40
41 private:
42 int mRefCount;
43 };
44
45 template<class T,
46 class = typename std::enable_if<std::is_base_of<ref_counted,T>::value>::type >
47 class intrusive_ptr
48 {
49 public:
50 intrusive_ptr()
51 : mPointer(nullptr)
52 {
53 }
54
55 intrusive_ptr(T* pointer)
56 : mPointer(pointer)
57 {
58 if (mPointer)
59 mPointer->AddRef();
60 }
61
62 intrusive_ptr(const intrusive_ptr& other)
63 : mPointer(other.mPointer)
64 {
65 if (mPointer)
66 mPointer->AddRef();
67 }
68
69 intrusive_ptr(intrusive_ptr&& other)
70 : mPointer(other.mPointer)
71 {
72 other.mPointer = nullptr;
73 }
74
75 ~intrusive_ptr()
76 {
77 if (mPointer)
78 mPointer->ReleaseRef();
79 }
80
81 intrusive_ptr& operator=(intrusive_ptr& other)
82 {
83 intrusive_ptr(other).swap(*this);
84 return *this;
85 }
86
87 intrusive_ptr& operator=(intrusive_ptr&& other)
88 {
89 intrusive_ptr(other).swap(*this);
90 return *this;
91 }
92
93 intrusive_ptr& operator=(T* other)
94 {
95 intrusive_ptr(other).swap(*this);
96 }
97
98 void reset()
99 {
100 intrusive_ptr().swap(*this);
101 }
102
103 void reset(T* other)
104 {
105 intrusive_ptr(other).swap(*this);
106 }
107
108 T* get() const
109 {
110 return mPointer;
111 }
112
113 T& operator*() const
114 {
115 return *mPointer;
116 }
117
118 T* operator->() const
119 {
120 return mPointer;
121 }
122
123 operator bool() const
124 {
125 return mPointer != nullptr;
126 }
127
128 operator T*() const
129 {
130 return mPointer;
131 }
132
133 bool operator!() const
134 {
135 return mPointer == nullptr;
136 }
137
138 void swap(intrusive_ptr& other)
139 {
140 T* tmp = mPointer;
René Jeschke 2016/02/02 11:11:24 Nit: is there a specific reason why you're not usi
Wladimir Palant 2016/02/02 17:55:19 You have to ask Boost devs :) I haven't heard abo
141 mPointer = other.mPointer;
142 other.mPointer = tmp;
143 }
144
145 private:
146 T* mPointer;
147 };
148
149 template<class T, class U>
150 inline bool operator==(const intrusive_ptr<T>& a, const intrusive_ptr<U>& b)
151 {
152 return a.get() == b.get();
153 }
154
155 template<class T, class U>
156 inline bool operator!=(const intrusive_ptr<T>& a, const intrusive_ptr<U>& b)
157 {
158 return a.get() != b.get();
159 }
160
161 template<class T, class U>
162 inline bool operator==(const intrusive_ptr<T>& a, const U* b)
163 {
164 return a.get() == b;
165 }
166
167 template<class T, class U>
168 inline bool operator!=(const intrusive_ptr<T>& a, const U* b)
169 {
170 return a.get() != b;
171 }
172
173 template<class T, class U>
174 inline bool operator==(const T* a, const intrusive_ptr<U>& b)
175 {
176 return a == b.get();
177 }
178
179 template<class T, class U>
180 inline bool operator!=(const T* a, intrusive_ptr<U> const& b)
181 {
182 return a != b.get();
183 }
184
185 #endif
OLDNEW
« compiled/StringMap.h ('K') | « compiled/debug.h ('k') | compiled/shell.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld