1 // Copyright (c) 2014 Marshall A. Greenblatt. All rights reserved.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
13 // * Neither the name of Google Inc. nor the name Chromium Embedded
14 // Framework nor the names of its contributors may be used to endorse
15 // or promote products derived from this software without specific prior
16 // written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CEF_INCLUDE_INTERNAL_CEF_PTR_H_
32 #define CEF_INCLUDE_INTERNAL_CEF_PTR_H_
35 #include "include/base/cef_build.h"
36 #include "include/base/cef_ref_counted.h"
38 #if defined(USING_CHROMIUM_INCLUDES)
39 #include <memory> // For std::unique_ptr.
41 #include "include/base/cef_scoped_ptr.h"
45 // Smart pointer implementation that is an alias of scoped_refptr from
46 // include/base/cef_ref_counted.h.
48 // A smart pointer class for reference counted objects. Use this class instead
49 // of calling AddRef and Release manually on a reference counted object to
50 // avoid common memory leaks caused by forgetting to Release an object
51 // reference. Sample usage:
53 // class MyFoo : public CefBaseRefCounted {
57 // void some_function() {
58 // // The MyFoo object that |foo| represents starts with a single
60 // CefRefPtr<MyFoo> foo = new MyFoo();
61 // foo->Method(param);
62 // // |foo| is released when this function returns
65 // void some_other_function() {
66 // CefRefPtr<MyFoo> foo = new MyFoo();
68 // foo = NULL; // explicitly releases |foo|
71 // foo->Method(param);
74 // The above examples show how CefRefPtr<T> acts like a pointer to T.
75 // Given two CefRefPtr<T> classes, it is also possible to exchange
76 // references between the two objects, like so:
79 // CefRefPtr<MyFoo> a = new MyFoo();
80 // CefRefPtr<MyFoo> b;
83 // // now, |b| references the MyFoo object, and |a| references NULL.
86 // To make both |a| and |b| in the above example reference the same MyFoo
87 // object, simply use the assignment operator:
90 // CefRefPtr<MyFoo> a = new MyFoo();
91 // CefRefPtr<MyFoo> b;
94 // // now, |a| and |b| each own a reference to the same MyFoo object.
95 // // the reference count of the underlying MyFoo object will be 2.
98 // Reference counted objects can also be passed as function parameters and
99 // used as function return values:
101 // void some_func_with_param(CefRefPtr<MyFoo> param) {
102 // // A reference is added to the MyFoo object that |param| represents
103 // // during the scope of some_func_with_param() and released when
104 // // some_func_with_param() goes out of scope.
107 // CefRefPtr<MyFoo> some_func_with_retval() {
108 // // The MyFoo object that |foox| represents starts with a single
110 // CefRefPtr<MyFoo> foox = new MyFoo();
112 // // Creating the return value adds an additional reference.
115 // // When some_func_with_retval() goes out of scope the original |foox|
116 // // reference is released.
119 // void and_another_function() {
120 // CefRefPtr<MyFoo> foo = new MyFoo();
122 // // pass |foo| as a parameter.
123 // some_function(foo);
125 // CefRefPtr<MyFoo> foo2 = some_func_with_retval();
126 // // Now, since we kept a reference to the some_func_with_retval() return
127 // // value, |foo2| is the only class pointing to the MyFoo object created
128 // in some_func_with_retval(), and it has a reference count of 1.
130 // some_func_with_retval();
131 // // Now, since we didn't keep a reference to the some_func_with_retval()
132 // // return value, the MyFoo object created in some_func_with_retval()
133 // // will automatically be released.
136 // And in standard containers:
139 // // Create a vector that holds MyFoo objects.
140 // std::vector<CefRefPtr<MyFoo> > MyFooVec;
142 // // The MyFoo object that |foo| represents starts with a single
144 // CefRefPtr<MyFoo> foo = new MyFoo();
146 // // When the MyFoo object is added to |MyFooVec| the reference count
147 // // is increased to 2.
148 // MyFooVec.push_back(foo);
153 #if defined(HAS_CPP11_TEMPLATE_ALIAS_SUPPORT)
155 using CefRefPtr = scoped_refptr<T>;
157 // When template aliases are not supported use a define instead of subclassing
158 // because it's otherwise hard to get the constructors to behave correctly.
159 #define CefRefPtr scoped_refptr
164 // A CefOwnPtr<T> is like a T*, except that the destructor of CefOwnPtr<T>
165 // automatically deletes the pointer it holds (if any). That is, CefOwnPtr<T>
166 // owns the T object that it points to. Like a T*, a CefOwnPtr<T> may hold
167 // either NULL or a pointer to a T object. Also like T*, CefOwnPtr<T> is
168 // thread-compatible, and once you dereference it, you get the thread safety
171 #if defined(USING_CHROMIUM_INCLUDES)
172 // Implementation-side code uses std::unique_ptr instead of scoped_ptr.
173 template <class T, class D = std::default_delete<T>>
174 using CefOwnPtr = std::unique_ptr<T, D>;
175 #elif defined(HAS_CPP11_TEMPLATE_ALIAS_SUPPORT)
176 template <class T, class D = base::DefaultDeleter<T>>
177 using CefOwnPtr = scoped_ptr<T, D>;
179 // When template aliases are not supported use a define instead of subclassing
180 // because it's otherwise hard to get the constructors to behave correctly.
181 #define CefOwnPtr scoped_ptr
186 // A CefRawPtr<T> is the same as T*
188 #if defined(HAS_CPP11_TEMPLATE_ALIAS_SUPPORT)
189 #define CEF_RAW_PTR_GET(r) r
191 using CefRawPtr = T*;
193 // Simple wrapper implementation that behaves as much like T* as possible.
194 // CEF_RAW_PTR_GET is required for VS2008 compatibility (Issue #2155).
195 #define CEF_RAW_PTR_GET(r) r.get()
199 CefRawPtr() : ptr_(nullptr) {}
200 CefRawPtr(T* p) : ptr_(p) {}
201 CefRawPtr(const CefRawPtr& r) : ptr_(r.ptr_) {}
203 template <typename U>
204 CefRawPtr(const CefRawPtr<U>& r) : ptr_(r.get()) {}
206 T* get() const { return ptr_; }
208 // Allow CefRawPtr to be used in boolean expression and comparison operations.
209 operator T*() const { return ptr_; }
211 T* operator->() const {
212 assert(ptr_ != NULL);
216 CefRawPtr<T>& operator=(T* p) {
221 CefRawPtr<T>& operator=(const CefRawPtr<T>& r) {
222 return *this = r.ptr_;
225 template <typename U>
226 CefRawPtr<T>& operator=(const CefRawPtr<U>& r) {
227 return *this = r.get();
235 #endif // CEF_INCLUDE_INTERNAL_CEF_PTR_H_