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_ref_counted.h"
38 // Smart pointer implementation that is an alias of scoped_refptr from
39 // include/base/cef_ref_counted.h.
41 // A smart pointer class for reference counted objects. Use this class instead
42 // of calling AddRef and Release manually on a reference counted object to
43 // avoid common memory leaks caused by forgetting to Release an object
44 // reference. Sample usage:
46 // class MyFoo : public CefBase {
50 // void some_function() {
51 // // The MyFoo object that |foo| represents starts with a single
53 // CefRefPtr<MyFoo> foo = new MyFoo();
54 // foo->Method(param);
55 // // |foo| is released when this function returns
58 // void some_other_function() {
59 // CefRefPtr<MyFoo> foo = new MyFoo();
61 // foo = NULL; // explicitly releases |foo|
64 // foo->Method(param);
67 // The above examples show how CefRefPtr<T> acts like a pointer to T.
68 // Given two CefRefPtr<T> classes, it is also possible to exchange
69 // references between the two objects, like so:
72 // CefRefPtr<MyFoo> a = new MyFoo();
73 // CefRefPtr<MyFoo> b;
76 // // now, |b| references the MyFoo object, and |a| references NULL.
79 // To make both |a| and |b| in the above example reference the same MyFoo
80 // object, simply use the assignment operator:
83 // CefRefPtr<MyFoo> a = new MyFoo();
84 // CefRefPtr<MyFoo> b;
87 // // now, |a| and |b| each own a reference to the same MyFoo object.
88 // // the reference count of the underlying MyFoo object will be 2.
91 // Reference counted objects can also be passed as function parameters and
92 // used as function return values:
94 // void some_func_with_param(CefRefPtr<MyFoo> param) {
95 // // A reference is added to the MyFoo object that |param| represents
96 // // during the scope of some_func_with_param() and released when
97 // // some_func_with_param() goes out of scope.
100 // CefRefPtr<MyFoo> some_func_with_retval() {
101 // // The MyFoo object that |foox| represents starts with a single
103 // CefRefPtr<MyFoo> foox = new MyFoo();
105 // // Creating the return value adds an additional reference.
108 // // When some_func_with_retval() goes out of scope the original |foox|
109 // // reference is released.
112 // void and_another_function() {
113 // CefRefPtr<MyFoo> foo = new MyFoo();
115 // // pass |foo| as a parameter.
116 // some_function(foo);
118 // CefRefPtr<MyFoo> foo2 = some_func_with_retval();
119 // // Now, since we kept a reference to the some_func_with_retval() return
120 // // value, |foo2| is the only class pointing to the MyFoo object created
121 // in some_func_with_retval(), and it has a reference count of 1.
123 // some_func_with_retval();
124 // // Now, since we didn't keep a reference to the some_func_with_retval()
125 // // return value, the MyFoo object created in some_func_with_retval()
126 // // will automatically be released.
129 // And in standard containers:
132 // // Create a vector that holds MyFoo objects.
133 // std::vector<CefRefPtr<MyFoo> > MyFooVec;
135 // // The MyFoo object that |foo| represents starts with a single
137 // CefRefPtr<MyFoo> foo = new MyFoo();
139 // // When the MyFoo object is added to |MyFooVec| the reference count
140 // // is increased to 2.
141 // MyFooVec.push_back(foo);
147 class CefRefPtr : public scoped_refptr<T> {
149 typedef scoped_refptr<T> parent;
151 CefRefPtr() : parent() {}
153 CefRefPtr(T* p) : parent(p) {}
155 CefRefPtr(const scoped_refptr<T>& r) : parent(r) {}
157 template <typename U>
158 CefRefPtr(const scoped_refptr<U>& r) : parent(r) {}
161 #endif // CEF_INCLUDE_INTERNAL_CEF_PTR_H_