OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_MEMORY_REF_COUNTED_H_ | 5 #ifndef BASE_MEMORY_REF_COUNTED_H_ |
6 #define BASE_MEMORY_REF_COUNTED_H_ | 6 #define BASE_MEMORY_REF_COUNTED_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <cassert> | 10 #include <cassert> |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 } // namespace base | 217 } // namespace base |
218 | 218 |
219 // | 219 // |
220 // A smart pointer class for reference counted objects. Use this class instead | 220 // A smart pointer class for reference counted objects. Use this class instead |
221 // of calling AddRef and Release manually on a reference counted object to | 221 // of calling AddRef and Release manually on a reference counted object to |
222 // avoid common memory leaks caused by forgetting to Release an object | 222 // avoid common memory leaks caused by forgetting to Release an object |
223 // reference. Sample usage: | 223 // reference. Sample usage: |
224 // | 224 // |
225 // class MyFoo : public RefCounted<MyFoo> { | 225 // class MyFoo : public RefCounted<MyFoo> { |
226 // ... | 226 // ... |
| 227 // private: |
| 228 // friend class RefCounted<MyFoo>; // Allow destruction by RefCounted<>. |
| 229 // ~MyFoo(); // Destructor must be private/protected. |
227 // }; | 230 // }; |
228 // | 231 // |
229 // void some_function() { | 232 // void some_function() { |
230 // scoped_refptr<MyFoo> foo = new MyFoo(); | 233 // scoped_refptr<MyFoo> foo = new MyFoo(); |
231 // foo->Method(param); | 234 // foo->Method(param); |
232 // // |foo| is released when this function returns | 235 // // |foo| is released when this function returns |
233 // } | 236 // } |
234 // | 237 // |
235 // void some_other_function() { | 238 // void some_other_function() { |
236 // scoped_refptr<MyFoo> foo = new MyFoo(); | 239 // scoped_refptr<MyFoo> foo = new MyFoo(); |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { | 455 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { |
453 return !operator==(null, rhs); | 456 return !operator==(null, rhs); |
454 } | 457 } |
455 | 458 |
456 template <typename T> | 459 template <typename T> |
457 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 460 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
458 return out << p.get(); | 461 return out << p.get(); |
459 } | 462 } |
460 | 463 |
461 #endif // BASE_MEMORY_REF_COUNTED_H_ | 464 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |