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