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 <memory> 35 36 #include "include/base/cef_build.h" 37 #include "include/base/cef_ref_counted.h" 38 39 /// 40 // Smart pointer implementation that is an alias of scoped_refptr from 41 // include/base/cef_ref_counted.h. 42 // <p> 43 // A smart pointer class for reference counted objects. Use this class instead 44 // of calling AddRef and Release manually on a reference counted object to 45 // avoid common memory leaks caused by forgetting to Release an object 46 // reference. Sample usage: 47 // <pre> 48 // class MyFoo : public CefBaseRefCounted { 49 // ... 50 // }; 51 // 52 // void some_function() { 53 // // The MyFoo object that |foo| represents starts with a single 54 // // reference. 55 // CefRefPtr<MyFoo> foo = new MyFoo(); 56 // foo->Method(param); 57 // // |foo| is released when this function returns 58 // } 59 // 60 // void some_other_function() { 61 // CefRefPtr<MyFoo> foo = new MyFoo(); 62 // ... 63 // foo = NULL; // explicitly releases |foo| 64 // ... 65 // if (foo) 66 // foo->Method(param); 67 // } 68 // </pre> 69 // The above examples show how CefRefPtr<T> acts like a pointer to T. 70 // Given two CefRefPtr<T> classes, it is also possible to exchange 71 // references between the two objects, like so: 72 // <pre> 73 // { 74 // CefRefPtr<MyFoo> a = new MyFoo(); 75 // CefRefPtr<MyFoo> b; 76 // 77 // b.swap(a); 78 // // now, |b| references the MyFoo object, and |a| references NULL. 79 // } 80 // </pre> 81 // To make both |a| and |b| in the above example reference the same MyFoo 82 // object, simply use the assignment operator: 83 // <pre> 84 // { 85 // CefRefPtr<MyFoo> a = new MyFoo(); 86 // CefRefPtr<MyFoo> b; 87 // 88 // b = a; 89 // // now, |a| and |b| each own a reference to the same MyFoo object. 90 // // the reference count of the underlying MyFoo object will be 2. 91 // } 92 // </pre> 93 // Reference counted objects can also be passed as function parameters and 94 // used as function return values: 95 // <pre> 96 // void some_func_with_param(CefRefPtr<MyFoo> param) { 97 // // A reference is added to the MyFoo object that |param| represents 98 // // during the scope of some_func_with_param() and released when 99 // // some_func_with_param() goes out of scope. 100 // } 101 // 102 // CefRefPtr<MyFoo> some_func_with_retval() { 103 // // The MyFoo object that |foox| represents starts with a single 104 // // reference. 105 // CefRefPtr<MyFoo> foox = new MyFoo(); 106 // 107 // // Creating the return value adds an additional reference. 108 // return foox; 109 // 110 // // When some_func_with_retval() goes out of scope the original |foox| 111 // // reference is released. 112 // } 113 // 114 // void and_another_function() { 115 // CefRefPtr<MyFoo> foo = new MyFoo(); 116 // 117 // // pass |foo| as a parameter. 118 // some_function(foo); 119 // 120 // CefRefPtr<MyFoo> foo2 = some_func_with_retval(); 121 // // Now, since we kept a reference to the some_func_with_retval() return 122 // // value, |foo2| is the only class pointing to the MyFoo object created 123 // in some_func_with_retval(), and it has a reference count of 1. 124 // 125 // some_func_with_retval(); 126 // // Now, since we didn't keep a reference to the some_func_with_retval() 127 // // return value, the MyFoo object created in some_func_with_retval() 128 // // will automatically be released. 129 // } 130 // </pre> 131 // And in standard containers: 132 // <pre> 133 // { 134 // // Create a vector that holds MyFoo objects. 135 // std::vector<CefRefPtr<MyFoo> > MyFooVec; 136 // 137 // // The MyFoo object that |foo| represents starts with a single 138 // // reference. 139 // CefRefPtr<MyFoo> foo = new MyFoo(); 140 // 141 // // When the MyFoo object is added to |MyFooVec| the reference count 142 // // is increased to 2. 143 // MyFooVec.push_back(foo); 144 // } 145 // </pre> 146 // </p> 147 /// 148 template <class T> 149 using CefRefPtr = scoped_refptr<T>; 150 151 /// 152 // A CefOwnPtr<T> is like a T*, except that the destructor of CefOwnPtr<T> 153 // automatically deletes the pointer it holds (if any). That is, CefOwnPtr<T> 154 // owns the T object that it points to. Like a T*, a CefOwnPtr<T> may hold 155 // either NULL or a pointer to a T object. Also like T*, CefOwnPtr<T> is 156 // thread-compatible, and once you dereference it, you get the thread safety 157 // guarantees of T. 158 /// 159 template <class T, class D = std::default_delete<T>> 160 using CefOwnPtr = std::unique_ptr<T, D>; 161 162 /// 163 // A CefRawPtr<T> is the same as T* 164 /// 165 template <class T> 166 using CefRawPtr = T*; 167 168 #endif // CEF_INCLUDE_INTERNAL_CEF_PTR_H_ 169