1 // Copyright (c) 2014 Marshall A. Greenblatt. All rights reserved. 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 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 12 // distribution. 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. 17 // 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. 29 30 #ifndef CEF_INCLUDE_INTERNAL_CEF_PTR_H_ 31 #define CEF_INCLUDE_INTERNAL_CEF_PTR_H_ 32 #pragma once 33 34 #include "include/base/cef_build.h" 35 #include "include/base/cef_ref_counted.h" 36 37 #if defined(USING_CHROMIUM_INCLUDES) 38 #include <memory> // For std::unique_ptr. 39 #else 40 #include "include/base/cef_scoped_ptr.h" 41 #endif 42 43 /// 44 // Smart pointer implementation that is an alias of scoped_refptr from 45 // include/base/cef_ref_counted.h. 46 // <p> 47 // A smart pointer class for reference counted objects. Use this class instead 48 // of calling AddRef and Release manually on a reference counted object to 49 // avoid common memory leaks caused by forgetting to Release an object 50 // reference. Sample usage: 51 // <pre> 52 // class MyFoo : public CefBaseRefCounted { 53 // ... 54 // }; 55 // 56 // void some_function() { 57 // // The MyFoo object that |foo| represents starts with a single 58 // // reference. 59 // CefRefPtr<MyFoo> foo = new MyFoo(); 60 // foo->Method(param); 61 // // |foo| is released when this function returns 62 // } 63 // 64 // void some_other_function() { 65 // CefRefPtr<MyFoo> foo = new MyFoo(); 66 // ... 67 // foo = NULL; // explicitly releases |foo| 68 // ... 69 // if (foo) 70 // foo->Method(param); 71 // } 72 // </pre> 73 // The above examples show how CefRefPtr<T> acts like a pointer to T. 74 // Given two CefRefPtr<T> classes, it is also possible to exchange 75 // references between the two objects, like so: 76 // <pre> 77 // { 78 // CefRefPtr<MyFoo> a = new MyFoo(); 79 // CefRefPtr<MyFoo> b; 80 // 81 // b.swap(a); 82 // // now, |b| references the MyFoo object, and |a| references NULL. 83 // } 84 // </pre> 85 // To make both |a| and |b| in the above example reference the same MyFoo 86 // object, simply use the assignment operator: 87 // <pre> 88 // { 89 // CefRefPtr<MyFoo> a = new MyFoo(); 90 // CefRefPtr<MyFoo> b; 91 // 92 // b = a; 93 // // now, |a| and |b| each own a reference to the same MyFoo object. 94 // // the reference count of the underlying MyFoo object will be 2. 95 // } 96 // </pre> 97 // Reference counted objects can also be passed as function parameters and 98 // used as function return values: 99 // <pre> 100 // void some_func_with_param(CefRefPtr<MyFoo> param) { 101 // // A reference is added to the MyFoo object that |param| represents 102 // // during the scope of some_func_with_param() and released when 103 // // some_func_with_param() goes out of scope. 104 // } 105 // 106 // CefRefPtr<MyFoo> some_func_with_retval() { 107 // // The MyFoo object that |foox| represents starts with a single 108 // // reference. 109 // CefRefPtr<MyFoo> foox = new MyFoo(); 110 // 111 // // Creating the return value adds an additional reference. 112 // return foox; 113 // 114 // // When some_func_with_retval() goes out of scope the original |foox| 115 // // reference is released. 116 // } 117 // 118 // void and_another_function() { 119 // CefRefPtr<MyFoo> foo = new MyFoo(); 120 // 121 // // pass |foo| as a parameter. 122 // some_function(foo); 123 // 124 // CefRefPtr<MyFoo> foo2 = some_func_with_retval(); 125 // // Now, since we kept a reference to the some_func_with_retval() return 126 // // value, |foo2| is the only class pointing to the MyFoo object created 127 // in some_func_with_retval(), and it has a reference count of 1. 128 // 129 // some_func_with_retval(); 130 // // Now, since we didn't keep a reference to the some_func_with_retval() 131 // // return value, the MyFoo object created in some_func_with_retval() 132 // // will automatically be released. 133 // } 134 // </pre> 135 // And in standard containers: 136 // <pre> 137 // { 138 // // Create a vector that holds MyFoo objects. 139 // std::vector<CefRefPtr<MyFoo> > MyFooVec; 140 // 141 // // The MyFoo object that |foo| represents starts with a single 142 // // reference. 143 // CefRefPtr<MyFoo> foo = new MyFoo(); 144 // 145 // // When the MyFoo object is added to |MyFooVec| the reference count 146 // // is increased to 2. 147 // MyFooVec.push_back(foo); 148 // } 149 // </pre> 150 // </p> 151 /// 152 #if defined(HAS_CPP11_TEMPLATE_ALIAS_SUPPORT) 153 template <class T> 154 using CefRefPtr = scoped_refptr<T>; 155 #else 156 // When template aliases are not supported use a define instead of subclassing 157 // because it's otherwise hard to get the constructors to behave correctly. 158 #define CefRefPtr scoped_refptr 159 #endif 160 161 /// 162 // A CefOwnPtr<T> is like a T*, except that the destructor of CefOwnPtr<T> 163 // automatically deletes the pointer it holds (if any). That is, CefOwnPtr<T> 164 // owns the T object that it points to. Like a T*, a CefOwnPtr<T> may hold 165 // either NULL or a pointer to a T object. Also like T*, CefOwnPtr<T> is 166 // thread-compatible, and once you dereference it, you get the thread safety 167 // guarantees of T. 168 /// 169 #if defined(USING_CHROMIUM_INCLUDES) 170 // Implementation-side code uses std::unique_ptr instead of scoped_ptr. 171 template <class T, class D = std::default_delete<T>> 172 using CefOwnPtr = std::unique_ptr<T, D>; 173 #elif defined(HAS_CPP11_TEMPLATE_ALIAS_SUPPORT) 174 template <class T, class D = base::DefaultDeleter<T>> 175 using CefOwnPtr = scoped_ptr<T, D>; 176 #else 177 // When template aliases are not supported use a define instead of subclassing 178 // because it's otherwise hard to get the constructors to behave correctly. 179 #define CefOwnPtr scoped_ptr 180 #endif 181 182 /// 183 // A CefRawPtr<T> is the same as T* 184 /// 185 #if defined(HAS_CPP11_TEMPLATE_ALIAS_SUPPORT) 186 #define CEF_RAW_PTR_GET(r) r 187 template <class T> 188 using CefRawPtr = T*; 189 #else 190 // Simple wrapper implementation that behaves as much like T* as possible. 191 // CEF_RAW_PTR_GET is required for VS2008 compatibility (Issue #2155). 192 #define CEF_RAW_PTR_GET(r) r.get() 193 template <class T> 194 class CefRawPtr { 195 public: CefRawPtr()196 CefRawPtr() : ptr_(nullptr) {} CefRawPtr(T * p)197 CefRawPtr(T* p) : ptr_(p) {} CefRawPtr(const CefRawPtr & r)198 CefRawPtr(const CefRawPtr& r) : ptr_(r.ptr_) {} 199 200 template <typename U> CefRawPtr(const CefRawPtr<U> & r)201 CefRawPtr(const CefRawPtr<U>& r) : ptr_(r.get()) {} 202 get()203 T* get() const { return ptr_; } 204 205 // Allow CefRawPtr to be used in boolean expression and comparison operations. 206 operator T*() const { return ptr_; } 207 208 T* operator->() const { 209 assert(ptr_ != NULL); 210 return ptr_; 211 } 212 213 CefRawPtr<T>& operator=(T* p) { 214 ptr_ = p; 215 return *this; 216 } 217 218 CefRawPtr<T>& operator=(const CefRawPtr<T>& r) { return *this = r.ptr_; } 219 220 template <typename U> 221 CefRawPtr<T>& operator=(const CefRawPtr<U>& r) { 222 return *this = r.get(); 223 } 224 225 private: 226 T* ptr_; 227 }; 228 #endif 229 230 #endif // CEF_INCLUDE_INTERNAL_CEF_PTR_H_ 231