3 * Copyright 2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // Originally these classes are from Chromium.
29 // http://src.chromium.org/viewvc/chrome/trunk/src/base/memory/ref_counted.h?view=markup
32 // A smart pointer class for reference counted objects. Use this class instead
33 // of calling AddRef and Release manually on a reference counted object to
34 // avoid common memory leaks caused by forgetting to Release an object
35 // reference. Sample usage:
37 // class MyFoo : public RefCounted<MyFoo> {
41 // void some_function() {
42 // scoped_refptr<MyFoo> foo = new MyFoo();
43 // foo->Method(param);
44 // // |foo| is released when this function returns
47 // void some_other_function() {
48 // scoped_refptr<MyFoo> foo = new MyFoo();
50 // foo = NULL; // explicitly releases |foo|
53 // foo->Method(param);
56 // The above examples show how scoped_refptr<T> acts like a pointer to T.
57 // Given two scoped_refptr<T> classes, it is also possible to exchange
58 // references between the two objects, like so:
61 // scoped_refptr<MyFoo> a = new MyFoo();
62 // scoped_refptr<MyFoo> b;
65 // // now, |b| references the MyFoo object, and |a| references NULL.
68 // To make both |a| and |b| in the above example reference the same MyFoo
69 // object, simply use the assignment operator:
72 // scoped_refptr<MyFoo> a = new MyFoo();
73 // scoped_refptr<MyFoo> b;
76 // // now, |a| and |b| each own a reference to the same MyFoo object.
80 #ifndef TALK_BASE_SCOPED_REF_PTR_H_
81 #define TALK_BASE_SCOPED_REF_PTR_H_
90 scoped_refptr() : ptr_(NULL) {
93 scoped_refptr(T* p) : ptr_(p) {
98 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) {
103 template <typename U>
104 scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) {
114 T* get() const { return ptr_; }
115 operator T*() const { return ptr_; }
116 T* operator->() const { return ptr_; }
118 // Release a pointer.
119 // The return value is the current pointer held by this object.
120 // If this object holds a NULL pointer, the return value is NULL.
121 // After this operation, this object will hold a NULL pointer,
122 // and will not own the object any more.
129 scoped_refptr<T>& operator=(T* p) {
130 // AddRef first so that self assignment should work
139 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) {
140 return *this = r.ptr_;
143 template <typename U>
144 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) {
145 return *this = r.get();
154 void swap(scoped_refptr<T>& r) {
162 } // namespace talk_base
164 #endif // TALK_BASE_SCOPED_REF_PTR_H_