forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcef_ptr.h
More file actions
230 lines (215 loc) · 7.82 KB
/
cef_ptr.h
File metadata and controls
230 lines (215 loc) · 7.82 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) 2014 Marshall A. Greenblatt. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the name Chromium Embedded
// Framework nor the names of its contributors may be used to endorse
// or promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef CEF_INCLUDE_INTERNAL_CEF_PTR_H_
#define CEF_INCLUDE_INTERNAL_CEF_PTR_H_
#pragma once
#include "include/base/cef_build.h"
#include "include/base/cef_ref_counted.h"
#if defined(USING_CHROMIUM_INCLUDES)
#include <memory> // For std::unique_ptr.
#else
#include "include/base/cef_scoped_ptr.h"
#endif
///
// Smart pointer implementation that is an alias of scoped_refptr from
// include/base/cef_ref_counted.h.
// <p>
// A smart pointer class for reference counted objects. Use this class instead
// of calling AddRef and Release manually on a reference counted object to
// avoid common memory leaks caused by forgetting to Release an object
// reference. Sample usage:
// <pre>
// class MyFoo : public CefBaseRefCounted {
// ...
// };
//
// void some_function() {
// // The MyFoo object that |foo| represents starts with a single
// // reference.
// CefRefPtr<MyFoo> foo = new MyFoo();
// foo->Method(param);
// // |foo| is released when this function returns
// }
//
// void some_other_function() {
// CefRefPtr<MyFoo> foo = new MyFoo();
// ...
// foo = NULL; // explicitly releases |foo|
// ...
// if (foo)
// foo->Method(param);
// }
// </pre>
// The above examples show how CefRefPtr<T> acts like a pointer to T.
// Given two CefRefPtr<T> classes, it is also possible to exchange
// references between the two objects, like so:
// <pre>
// {
// CefRefPtr<MyFoo> a = new MyFoo();
// CefRefPtr<MyFoo> b;
//
// b.swap(a);
// // now, |b| references the MyFoo object, and |a| references NULL.
// }
// </pre>
// To make both |a| and |b| in the above example reference the same MyFoo
// object, simply use the assignment operator:
// <pre>
// {
// CefRefPtr<MyFoo> a = new MyFoo();
// CefRefPtr<MyFoo> b;
//
// b = a;
// // now, |a| and |b| each own a reference to the same MyFoo object.
// // the reference count of the underlying MyFoo object will be 2.
// }
// </pre>
// Reference counted objects can also be passed as function parameters and
// used as function return values:
// <pre>
// void some_func_with_param(CefRefPtr<MyFoo> param) {
// // A reference is added to the MyFoo object that |param| represents
// // during the scope of some_func_with_param() and released when
// // some_func_with_param() goes out of scope.
// }
//
// CefRefPtr<MyFoo> some_func_with_retval() {
// // The MyFoo object that |foox| represents starts with a single
// // reference.
// CefRefPtr<MyFoo> foox = new MyFoo();
//
// // Creating the return value adds an additional reference.
// return foox;
//
// // When some_func_with_retval() goes out of scope the original |foox|
// // reference is released.
// }
//
// void and_another_function() {
// CefRefPtr<MyFoo> foo = new MyFoo();
//
// // pass |foo| as a parameter.
// some_function(foo);
//
// CefRefPtr<MyFoo> foo2 = some_func_with_retval();
// // Now, since we kept a reference to the some_func_with_retval() return
// // value, |foo2| is the only class pointing to the MyFoo object created
// in some_func_with_retval(), and it has a reference count of 1.
//
// some_func_with_retval();
// // Now, since we didn't keep a reference to the some_func_with_retval()
// // return value, the MyFoo object created in some_func_with_retval()
// // will automatically be released.
// }
// </pre>
// And in standard containers:
// <pre>
// {
// // Create a vector that holds MyFoo objects.
// std::vector<CefRefPtr<MyFoo> > MyFooVec;
//
// // The MyFoo object that |foo| represents starts with a single
// // reference.
// CefRefPtr<MyFoo> foo = new MyFoo();
//
// // When the MyFoo object is added to |MyFooVec| the reference count
// // is increased to 2.
// MyFooVec.push_back(foo);
// }
// </pre>
// </p>
///
#if defined(HAS_CPP11_TEMPLATE_ALIAS_SUPPORT)
template <class T>
using CefRefPtr = scoped_refptr<T>;
#else
// When template aliases are not supported use a define instead of subclassing
// because it's otherwise hard to get the constructors to behave correctly.
#define CefRefPtr scoped_refptr
#endif
///
// A CefOwnPtr<T> is like a T*, except that the destructor of CefOwnPtr<T>
// automatically deletes the pointer it holds (if any). That is, CefOwnPtr<T>
// owns the T object that it points to. Like a T*, a CefOwnPtr<T> may hold
// either NULL or a pointer to a T object. Also like T*, CefOwnPtr<T> is
// thread-compatible, and once you dereference it, you get the thread safety
// guarantees of T.
///
#if defined(USING_CHROMIUM_INCLUDES)
// Implementation-side code uses std::unique_ptr instead of scoped_ptr.
template <class T, class D = std::default_delete<T>>
using CefOwnPtr = std::unique_ptr<T, D>;
#elif defined(HAS_CPP11_TEMPLATE_ALIAS_SUPPORT)
template <class T, class D = base::DefaultDeleter<T>>
using CefOwnPtr = scoped_ptr<T, D>;
#else
// When template aliases are not supported use a define instead of subclassing
// because it's otherwise hard to get the constructors to behave correctly.
#define CefOwnPtr scoped_ptr
#endif
///
// A CefRawPtr<T> is the same as T*
///
#if defined(HAS_CPP11_TEMPLATE_ALIAS_SUPPORT)
#define CEF_RAW_PTR_GET(r) r
template <class T>
using CefRawPtr = T*;
#else
// Simple wrapper implementation that behaves as much like T* as possible.
// CEF_RAW_PTR_GET is required for VS2008 compatibility (Issue #2155).
#define CEF_RAW_PTR_GET(r) r.get()
template <class T>
class CefRawPtr {
public:
CefRawPtr() : ptr_(nullptr) {}
CefRawPtr(T* p) : ptr_(p) {}
CefRawPtr(const CefRawPtr& r) : ptr_(r.ptr_) {}
template <typename U>
CefRawPtr(const CefRawPtr<U>& r) : ptr_(r.get()) {}
T* get() const { return ptr_; }
// Allow CefRawPtr to be used in boolean expression and comparison operations.
operator T*() const { return ptr_; }
T* operator->() const {
assert(ptr_ != NULL);
return ptr_;
}
CefRawPtr<T>& operator=(T* p) {
ptr_ = p;
return *this;
}
CefRawPtr<T>& operator=(const CefRawPtr<T>& r) { return *this = r.ptr_; }
template <typename U>
CefRawPtr<T>& operator=(const CefRawPtr<U>& r) {
return *this = r.get();
}
private:
T* ptr_;
};
#endif
#endif // CEF_INCLUDE_INTERNAL_CEF_PTR_H_