OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 | 278 |
279 // Constructor. Defaults to initializing with NULL. | 279 // Constructor. Defaults to initializing with NULL. |
280 // There is no way to create an uninitialized scoped_ptr. | 280 // There is no way to create an uninitialized scoped_ptr. |
281 // The input parameter must be allocated with an allocator that matches the | 281 // The input parameter must be allocated with an allocator that matches the |
282 // Free functor. For the default Free functor, this is malloc, calloc, or | 282 // Free functor. For the default Free functor, this is malloc, calloc, or |
283 // realloc. | 283 // realloc. |
284 explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {} | 284 explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {} |
285 | 285 |
286 // Destructor. If there is a C object, call the Free functor. | 286 // Destructor. If there is a C object, call the Free functor. |
287 ~scoped_ptr_malloc() { | 287 ~scoped_ptr_malloc() { |
288 free_(ptr_); | 288 FreeProc()(ptr_); |
awong
2011/08/29 21:45:11
nit: What about saving it into a temporary before
| |
289 } | 289 } |
290 | 290 |
291 // Reset. Calls the Free functor on the current owned object, if any. | 291 // Reset. Calls the Free functor on the current owned object, if any. |
292 // Then takes ownership of a new object, if given. | 292 // Then takes ownership of a new object, if given. |
293 // this->reset(this->get()) works. | 293 // this->reset(this->get()) works. |
294 void reset(C* p = NULL) { | 294 void reset(C* p = NULL) { |
295 if (ptr_ != p) { | 295 if (ptr_ != p) { |
296 free_(ptr_); | 296 FreeProc()(ptr_); |
297 ptr_ = p; | 297 ptr_ = p; |
298 } | 298 } |
299 } | 299 } |
300 | 300 |
301 // Get the current object. | 301 // Get the current object. |
302 // operator* and operator-> will cause an assert() failure if there is | 302 // operator* and operator-> will cause an assert() failure if there is |
303 // no current object. | 303 // no current object. |
304 C& operator*() const { | 304 C& operator*() const { |
305 assert(ptr_ != NULL); | 305 assert(ptr_ != NULL); |
306 return *ptr_; | 306 return *ptr_; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
348 | 348 |
349 private: | 349 private: |
350 C* ptr_; | 350 C* ptr_; |
351 | 351 |
352 // no reason to use these: each scoped_ptr_malloc should have its own object | 352 // no reason to use these: each scoped_ptr_malloc should have its own object |
353 template <class C2, class GP> | 353 template <class C2, class GP> |
354 bool operator==(scoped_ptr_malloc<C2, GP> const& p) const; | 354 bool operator==(scoped_ptr_malloc<C2, GP> const& p) const; |
355 template <class C2, class GP> | 355 template <class C2, class GP> |
356 bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const; | 356 bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const; |
357 | 357 |
358 static FreeProc const free_; | |
359 | |
360 // Disallow evil constructors | 358 // Disallow evil constructors |
361 scoped_ptr_malloc(const scoped_ptr_malloc&); | 359 scoped_ptr_malloc(const scoped_ptr_malloc&); |
362 void operator=(const scoped_ptr_malloc&); | 360 void operator=(const scoped_ptr_malloc&); |
363 }; | 361 }; |
364 | 362 |
365 template<class C, class FP> | |
366 FP const scoped_ptr_malloc<C, FP>::free_ = FP(); | |
367 | |
368 template<class C, class FP> inline | 363 template<class C, class FP> inline |
369 void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) { | 364 void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) { |
370 a.swap(b); | 365 a.swap(b); |
371 } | 366 } |
372 | 367 |
373 template<class C, class FP> inline | 368 template<class C, class FP> inline |
374 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { | 369 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { |
375 return p == b.get(); | 370 return p == b.get(); |
376 } | 371 } |
377 | 372 |
378 template<class C, class FP> inline | 373 template<class C, class FP> inline |
379 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { | 374 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { |
380 return p != b.get(); | 375 return p != b.get(); |
381 } | 376 } |
382 | 377 |
383 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 378 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
OLD | NEW |