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 // Scopers help you manage ownership of a pointer, helping you easily manage the | 5 // Scopers help you manage ownership of a pointer, helping you easily manage the |
6 // a pointer within a scope, and automatically destroying the pointer at the | 6 // a pointer within a scope, and automatically destroying the pointer at the |
7 // end of a scope. There are two main classes you will use, which correspond | 7 // end of a scope. There are two main classes you will use, which correspond |
8 // to the operators new/delete and new[]/delete[]. | 8 // to the operators new/delete and new[]/delete[]. |
9 // | 9 // |
10 // Example usage (scoped_ptr): | 10 // Example usage (scoped_ptr): |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // automatically deletes the pointer it holds (if any). | 87 // automatically deletes the pointer it holds (if any). |
88 // That is, scoped_ptr<T> owns the T object that it points to. | 88 // That is, scoped_ptr<T> owns the T object that it points to. |
89 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object. | 89 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object. |
90 // Also like T*, scoped_ptr<T> is thread-compatible, and once you | 90 // Also like T*, scoped_ptr<T> is thread-compatible, and once you |
91 // dereference it, you get the thread safety guarantees of T. | 91 // dereference it, you get the thread safety guarantees of T. |
92 // | 92 // |
93 // The size of a scoped_ptr is small: | 93 // The size of a scoped_ptr is small: |
94 // sizeof(scoped_ptr<C>) == sizeof(C*) | 94 // sizeof(scoped_ptr<C>) == sizeof(C*) |
95 template <class C> | 95 template <class C> |
96 class scoped_ptr { | 96 class scoped_ptr { |
97 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue); | 97 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) |
98 | 98 |
99 public: | 99 public: |
100 | 100 |
101 // The element type | 101 // The element type |
102 typedef C element_type; | 102 typedef C element_type; |
103 | 103 |
104 // Constructor. Defaults to initializing with NULL. | 104 // Constructor. Defaults to initializing with NULL. |
105 // There is no way to create an uninitialized scoped_ptr. | 105 // There is no way to create an uninitialized scoped_ptr. |
106 // The input parameter must be allocated with new. | 106 // The input parameter must be allocated with new. |
107 explicit scoped_ptr(C* p = NULL) : ptr_(p) { } | 107 explicit scoped_ptr(C* p = NULL) : ptr_(p) { } |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 // with new [] and the destructor deletes objects with delete []. | 213 // with new [] and the destructor deletes objects with delete []. |
214 // | 214 // |
215 // As with scoped_ptr<C>, a scoped_array<C> either points to an object | 215 // As with scoped_ptr<C>, a scoped_array<C> either points to an object |
216 // or is NULL. A scoped_array<C> owns the object that it points to. | 216 // or is NULL. A scoped_array<C> owns the object that it points to. |
217 // scoped_array<T> is thread-compatible, and once you index into it, | 217 // scoped_array<T> is thread-compatible, and once you index into it, |
218 // the returned objects have only the thread safety guarantees of T. | 218 // the returned objects have only the thread safety guarantees of T. |
219 // | 219 // |
220 // Size: sizeof(scoped_array<C>) == sizeof(C*) | 220 // Size: sizeof(scoped_array<C>) == sizeof(C*) |
221 template <class C> | 221 template <class C> |
222 class scoped_array { | 222 class scoped_array { |
223 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_array, RValue); | 223 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_array, RValue) |
224 | 224 |
225 public: | 225 public: |
226 | 226 |
227 // The element type | 227 // The element type |
228 typedef C element_type; | 228 typedef C element_type; |
229 | 229 |
230 // Constructor. Defaults to initializing with NULL. | 230 // Constructor. Defaults to initializing with NULL. |
231 // There is no way to create an uninitialized scoped_array. | 231 // There is no way to create an uninitialized scoped_array. |
232 // The input parameter must be allocated with new []. | 232 // The input parameter must be allocated with new []. |
233 explicit scoped_array(C* p = NULL) : array_(p) { } | 233 explicit scoped_array(C* p = NULL) : array_(p) { } |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 inline void operator()(void* x) const { | 328 inline void operator()(void* x) const { |
329 free(x); | 329 free(x); |
330 } | 330 } |
331 }; | 331 }; |
332 | 332 |
333 // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a | 333 // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a |
334 // second template argument, the functor used to free the object. | 334 // second template argument, the functor used to free the object. |
335 | 335 |
336 template<class C, class FreeProc = ScopedPtrMallocFree> | 336 template<class C, class FreeProc = ScopedPtrMallocFree> |
337 class scoped_ptr_malloc { | 337 class scoped_ptr_malloc { |
338 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr_malloc, RValue); | 338 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr_malloc, RValue) |
339 | 339 |
340 public: | 340 public: |
341 | 341 |
342 // The element type | 342 // The element type |
343 typedef C element_type; | 343 typedef C element_type; |
344 | 344 |
345 // Constructor. Defaults to initializing with NULL. | 345 // Constructor. Defaults to initializing with NULL. |
346 // There is no way to create an uninitialized scoped_ptr. | 346 // There is no way to create an uninitialized scoped_ptr. |
347 // The input parameter must be allocated with an allocator that matches the | 347 // The input parameter must be allocated with an allocator that matches the |
348 // Free functor. For the default Free functor, this is malloc, calloc, or | 348 // Free functor. For the default Free functor, this is malloc, calloc, or |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { | 441 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { |
442 return p == b.get(); | 442 return p == b.get(); |
443 } | 443 } |
444 | 444 |
445 template<class C, class FP> inline | 445 template<class C, class FP> inline |
446 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { | 446 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { |
447 return p != b.get(); | 447 return p != b.get(); |
448 } | 448 } |
449 | 449 |
450 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 450 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
OLD | NEW |