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: Merged filter parsing and normalization Created Feb. 4, 2016, 3:01 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 <algorithm>
13 #include <type_traits>
14
15 class ref_counted
16 {
17 public:
18 void AddRef()
19 {
20 mRefCount++;
21 }
22
23 void ReleaseRef()
24 {
25 if (--mRefCount == 0)
26 delete this;
27 }
28
29 protected:
30 ref_counted()
31 : mRefCount(0)
32 {
33 }
34
35 // We need this virtual destructor, otherwise pointers to ref_counted and
36 // pointers to derived classes won't have the same value (e.g. converting from
37 // Filter* to ref_counted* decreases pointer value by 4).
38 virtual ~ref_counted()
39 {
40 }
41
42 private:
43 int mRefCount;
44 };
45
46 template<typename T,
47 class = typename std::enable_if<std::is_base_of<ref_counted,T>::value>::type >
48 class intrusive_ptr
49 {
50 public:
51 intrusive_ptr()
52 : mPointer(nullptr)
53 {
54 }
55
56 intrusive_ptr(T* pointer)
57 : mPointer(pointer)
58 {
59 if (mPointer)
60 mPointer->AddRef();
61 }
62
63 intrusive_ptr(const intrusive_ptr& other)
64 : mPointer(other.mPointer)
65 {
66 if (mPointer)
67 mPointer->AddRef();
68 }
69
70 intrusive_ptr(intrusive_ptr&& other)
71 : mPointer(other.mPointer)
72 {
73 other.mPointer = nullptr;
74 }
75
76 ~intrusive_ptr()
77 {
78 if (mPointer)
79 mPointer->ReleaseRef();
80 }
81
82 intrusive_ptr& operator=(intrusive_ptr& other)
83 {
84 intrusive_ptr(other).swap(*this);
85 return *this;
86 }
87
88 intrusive_ptr& operator=(intrusive_ptr&& other)
89 {
90 intrusive_ptr(other).swap(*this);
91 return *this;
92 }
93
94 intrusive_ptr& operator=(T* other)
95 {
96 intrusive_ptr(other).swap(*this);
97 return *this;
98 }
99
100 void reset()
101 {
102 intrusive_ptr().swap(*this);
103 }
104
105 void reset(T* other)
106 {
107 intrusive_ptr(other).swap(*this);
108 }
109
110 T* get() const
111 {
112 return mPointer;
113 }
114
115 T& operator*() const
116 {
117 return *mPointer;
118 }
119
120 T* operator->() const
121 {
122 return mPointer;
123 }
124
125 operator bool() const
126 {
127 return mPointer != nullptr;
128 }
129
130 operator T*() const
131 {
132 return mPointer;
133 }
134
135 bool operator!() const
136 {
137 return mPointer == nullptr;
138 }
139
140 void swap(intrusive_ptr& other)
141 {
142 std::swap(mPointer, other.mPointer);
143 }
144
145 private:
146 T* mPointer;
147 };
148
149 template<typename T, typename 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<typename T, typename 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<typename T, typename U>
162 inline bool operator==(const intrusive_ptr<T>& a, const U* b)
163 {
164 return a.get() == b;
165 }
166
167 template<typename T, typename U>
168 inline bool operator!=(const intrusive_ptr<T>& a, const U* b)
169 {
170 return a.get() != b;
171 }
172
173 template<typename T, typename U>
174 inline bool operator==(const T* a, const intrusive_ptr<U>& b)
175 {
176 return a == b.get();
177 }
178
179 template<typename T, typename U>
180 inline bool operator!=(const T* a, intrusive_ptr<U> const& b)
181 {
182 return a != b.get();
183 }
184
185 #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