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 reset(); |
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 free_proc; |
darin (slow to review)
2011/08/29 23:32:10
unrelated to this change, but since free_proc(ptr_
| |
297 free_proc(ptr_); | |
297 ptr_ = p; | 298 ptr_ = p; |
298 } | 299 } |
299 } | 300 } |
300 | 301 |
301 // Get the current object. | 302 // Get the current object. |
302 // operator* and operator-> will cause an assert() failure if there is | 303 // operator* and operator-> will cause an assert() failure if there is |
303 // no current object. | 304 // no current object. |
304 C& operator*() const { | 305 C& operator*() const { |
305 assert(ptr_ != NULL); | 306 assert(ptr_ != NULL); |
306 return *ptr_; | 307 return *ptr_; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
348 | 349 |
349 private: | 350 private: |
350 C* ptr_; | 351 C* ptr_; |
351 | 352 |
352 // no reason to use these: each scoped_ptr_malloc should have its own object | 353 // no reason to use these: each scoped_ptr_malloc should have its own object |
353 template <class C2, class GP> | 354 template <class C2, class GP> |
354 bool operator==(scoped_ptr_malloc<C2, GP> const& p) const; | 355 bool operator==(scoped_ptr_malloc<C2, GP> const& p) const; |
355 template <class C2, class GP> | 356 template <class C2, class GP> |
356 bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const; | 357 bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const; |
357 | 358 |
358 static FreeProc const free_; | |
359 | |
360 // Disallow evil constructors | 359 // Disallow evil constructors |
361 scoped_ptr_malloc(const scoped_ptr_malloc&); | 360 scoped_ptr_malloc(const scoped_ptr_malloc&); |
362 void operator=(const scoped_ptr_malloc&); | 361 void operator=(const scoped_ptr_malloc&); |
363 }; | 362 }; |
364 | 363 |
365 template<class C, class FP> | |
366 FP const scoped_ptr_malloc<C, FP>::free_ = FP(); | |
367 | |
368 template<class C, class FP> inline | 364 template<class C, class FP> inline |
369 void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) { | 365 void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) { |
370 a.swap(b); | 366 a.swap(b); |
371 } | 367 } |
372 | 368 |
373 template<class C, class FP> inline | 369 template<class C, class FP> inline |
374 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { | 370 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { |
375 return p == b.get(); | 371 return p == b.get(); |
376 } | 372 } |
377 | 373 |
378 template<class C, class FP> inline | 374 template<class C, class FP> inline |
379 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { | 375 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { |
380 return p != b.get(); | 376 return p != b.get(); |
381 } | 377 } |
382 | 378 |
383 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 379 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
OLD | NEW |