| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 // scoped_ptr<Foo> CreateFoo() { | 47 // scoped_ptr<Foo> CreateFoo() { |
| 48 // // No need for calling Pass() because we are constructing a temporary | 48 // // No need for calling Pass() because we are constructing a temporary |
| 49 // // for the return value. | 49 // // for the return value. |
| 50 // return scoped_ptr<Foo>(new Foo("new")); | 50 // return scoped_ptr<Foo>(new Foo("new")); |
| 51 // } | 51 // } |
| 52 // scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) { | 52 // scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) { |
| 53 // return arg.Pass(); | 53 // return arg.Pass(); |
| 54 // } | 54 // } |
| 55 // | 55 // |
| 56 // { | 56 // { |
| 57 // scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay)" | 57 // scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay"). |
| 58 // TakesOwnership(ptr.Pass()); // ptr no longer owns Foo("yay"). | 58 // TakesOwnership(ptr.Pass()); // ptr no longer owns Foo("yay"). |
| 59 // scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo. | 59 // scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo. |
| 60 // scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2. | 60 // scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2. |
| 61 // PassThru(ptr2.Pass()); // ptr2 is correspondingly NULL. | 61 // PassThru(ptr2.Pass()); // ptr2 is correspondingly NULL. |
| 62 // } | 62 // } |
| 63 // | 63 // |
| 64 // Notice that if you do not call Pass() when returning from PassThru(), or | 64 // Notice that if you do not call Pass() when returning from PassThru(), or |
| 65 // when invoking TakesOwnership(), the code will not compile because scopers | 65 // when invoking TakesOwnership(), the code will not compile because scopers |
| 66 // are not copyable; they only implement move semantics which require calling | 66 // are not copyable; they only implement move semantics which require calling |
| 67 // the Pass() function to signify a destructive transfer of state. CreateFoo() | 67 // the Pass() function to signify a destructive transfer of state. CreateFoo() |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // There is no way to create an uninitialized scoped_ptr. | 145 // There is no way to create an uninitialized scoped_ptr. |
| 146 // The input parameter must be allocated with new. | 146 // The input parameter must be allocated with new. |
| 147 explicit scoped_ptr(C* p = NULL) : ptr_(p) { } | 147 explicit scoped_ptr(C* p = NULL) : ptr_(p) { } |
| 148 | 148 |
| 149 // Constructor. Allows construction from a scoped_ptr rvalue for a | 149 // Constructor. Allows construction from a scoped_ptr rvalue for a |
| 150 // convertible type. | 150 // convertible type. |
| 151 template <typename U> | 151 template <typename U> |
| 152 scoped_ptr(scoped_ptr<U> other) : ptr_(other.release()) { } | 152 scoped_ptr(scoped_ptr<U> other) : ptr_(other.release()) { } |
| 153 | 153 |
| 154 // Constructor. Move constructor for C++03 move emulation of this type. | 154 // Constructor. Move constructor for C++03 move emulation of this type. |
| 155 scoped_ptr(RValue& other) | 155 scoped_ptr(RValue rvalue) |
| 156 // The type of the underlying object is scoped_ptr; we have to | 156 : ptr_(rvalue.object->release()) { |
| 157 // reinterpret_cast back to the original type for the call to release to | |
| 158 // be valid. (See C++11 5.2.10.7) | |
| 159 : ptr_(reinterpret_cast<scoped_ptr&>(other).release()) { | |
| 160 } | 157 } |
| 161 | 158 |
| 162 // Destructor. If there is a C object, delete it. | 159 // Destructor. If there is a C object, delete it. |
| 163 // We don't need to test ptr_ == NULL because C++ does that for us. | 160 // We don't need to test ptr_ == NULL because C++ does that for us. |
| 164 ~scoped_ptr() { | 161 ~scoped_ptr() { |
| 165 enum { type_must_be_complete = sizeof(C) }; | 162 enum { type_must_be_complete = sizeof(C) }; |
| 166 delete ptr_; | 163 delete ptr_; |
| 167 } | 164 } |
| 168 | 165 |
| 169 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible | 166 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible |
| 170 // type. | 167 // type. |
| 171 template <typename U> | 168 template <typename U> |
| 172 scoped_ptr& operator=(scoped_ptr<U> rhs) { | 169 scoped_ptr& operator=(scoped_ptr<U> rhs) { |
| 173 reset(rhs.release()); | 170 reset(rhs.release()); |
| 174 return *this; | 171 return *this; |
| 175 } | 172 } |
| 176 | 173 |
| 177 // operator=. Move operator= for C++03 move emulation of this type. | 174 // operator=. Move operator= for C++03 move emulation of this type. |
| 178 scoped_ptr& operator=(RValue& rhs) { | 175 scoped_ptr& operator=(RValue rhs) { |
| 179 swap(rhs); | 176 swap(*rhs->object); |
| 180 return *this; | 177 return *this; |
| 181 } | 178 } |
| 182 | 179 |
| 183 // Reset. Deletes the current owned object, if any. | 180 // Reset. Deletes the current owned object, if any. |
| 184 // Then takes ownership of a new object, if given. | 181 // Then takes ownership of a new object, if given. |
| 185 // this->reset(this->get()) works. | 182 // this->reset(this->get()) works. |
| 186 void reset(C* p = NULL) { | 183 void reset(C* p = NULL) { |
| 187 if (p != ptr_) { | 184 if (p != ptr_) { |
| 188 enum { type_must_be_complete = sizeof(C) }; | 185 enum { type_must_be_complete = sizeof(C) }; |
| 189 delete ptr_; | 186 delete ptr_; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 | 278 |
| 282 // The element type | 279 // The element type |
| 283 typedef C element_type; | 280 typedef C element_type; |
| 284 | 281 |
| 285 // Constructor. Defaults to initializing with NULL. | 282 // Constructor. Defaults to initializing with NULL. |
| 286 // There is no way to create an uninitialized scoped_array. | 283 // There is no way to create an uninitialized scoped_array. |
| 287 // The input parameter must be allocated with new []. | 284 // The input parameter must be allocated with new []. |
| 288 explicit scoped_array(C* p = NULL) : array_(p) { } | 285 explicit scoped_array(C* p = NULL) : array_(p) { } |
| 289 | 286 |
| 290 // Constructor. Move constructor for C++03 move emulation of this type. | 287 // Constructor. Move constructor for C++03 move emulation of this type. |
| 291 scoped_array(RValue& other) | 288 scoped_array(RValue rvalue) |
| 292 // The type of the underlying object is scoped_array; we have to | 289 : array_(rvalue.object->release()) { |
| 293 // reinterpret_cast back to the original type for the call to release to | |
| 294 // be valid. (See C++11 5.2.10.7) | |
| 295 : array_(reinterpret_cast<scoped_array&>(other).release()) { | |
| 296 } | 290 } |
| 297 | 291 |
| 298 // Destructor. If there is a C object, delete it. | 292 // Destructor. If there is a C object, delete it. |
| 299 // We don't need to test ptr_ == NULL because C++ does that for us. | 293 // We don't need to test ptr_ == NULL because C++ does that for us. |
| 300 ~scoped_array() { | 294 ~scoped_array() { |
| 301 enum { type_must_be_complete = sizeof(C) }; | 295 enum { type_must_be_complete = sizeof(C) }; |
| 302 delete[] array_; | 296 delete[] array_; |
| 303 } | 297 } |
| 304 | 298 |
| 305 // operator=. Move operator= for C++03 move emulation of this type. | 299 // operator=. Move operator= for C++03 move emulation of this type. |
| 306 scoped_array& operator=(RValue& rhs) { | 300 scoped_array& operator=(RValue rhs) { |
| 307 swap(rhs); | 301 swap(*rhs.object); |
| 308 return *this; | 302 return *this; |
| 309 } | 303 } |
| 310 | 304 |
| 311 // Reset. Deletes the current owned object, if any. | 305 // Reset. Deletes the current owned object, if any. |
| 312 // Then takes ownership of a new object, if given. | 306 // Then takes ownership of a new object, if given. |
| 313 // this->reset(this->get()) works. | 307 // this->reset(this->get()) works. |
| 314 void reset(C* p = NULL) { | 308 void reset(C* p = NULL) { |
| 315 if (p != array_) { | 309 if (p != array_) { |
| 316 enum { type_must_be_complete = sizeof(C) }; | 310 enum { type_must_be_complete = sizeof(C) }; |
| 317 delete[] array_; | 311 delete[] array_; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 typedef C element_type; | 402 typedef C element_type; |
| 409 | 403 |
| 410 // Constructor. Defaults to initializing with NULL. | 404 // Constructor. Defaults to initializing with NULL. |
| 411 // There is no way to create an uninitialized scoped_ptr. | 405 // There is no way to create an uninitialized scoped_ptr. |
| 412 // The input parameter must be allocated with an allocator that matches the | 406 // The input parameter must be allocated with an allocator that matches the |
| 413 // Free functor. For the default Free functor, this is malloc, calloc, or | 407 // Free functor. For the default Free functor, this is malloc, calloc, or |
| 414 // realloc. | 408 // realloc. |
| 415 explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {} | 409 explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {} |
| 416 | 410 |
| 417 // Constructor. Move constructor for C++03 move emulation of this type. | 411 // Constructor. Move constructor for C++03 move emulation of this type. |
| 418 scoped_ptr_malloc(RValue& other) | 412 scoped_ptr_malloc(RValue rvalue) |
| 419 // The type of the underlying object is scoped_ptr_malloc; we have to | 413 : ptr_(rvalue.object->release()) { |
| 420 // reinterpret_cast back to the original type for the call to release to | |
| 421 // be valid. (See C++11 5.2.10.7) | |
| 422 : ptr_(reinterpret_cast<scoped_ptr_malloc&>(other).release()) { | |
| 423 } | 414 } |
| 424 | 415 |
| 425 // Destructor. If there is a C object, call the Free functor. | 416 // Destructor. If there is a C object, call the Free functor. |
| 426 ~scoped_ptr_malloc() { | 417 ~scoped_ptr_malloc() { |
| 427 reset(); | 418 reset(); |
| 428 } | 419 } |
| 429 | 420 |
| 430 // operator=. Move operator= for C++03 move emulation of this type. | 421 // operator=. Move operator= for C++03 move emulation of this type. |
| 431 scoped_ptr_malloc& operator=(RValue& rhs) { | 422 scoped_ptr_malloc& operator=(RValue rhs) { |
| 432 swap(rhs); | 423 swap(*rhs.object); |
| 433 return *this; | 424 return *this; |
| 434 } | 425 } |
| 435 | 426 |
| 436 // Reset. Calls the Free functor on the current owned object, if any. | 427 // Reset. Calls the Free functor on the current owned object, if any. |
| 437 // Then takes ownership of a new object, if given. | 428 // Then takes ownership of a new object, if given. |
| 438 // this->reset(this->get()) works. | 429 // this->reset(this->get()) works. |
| 439 void reset(C* p = NULL) { | 430 void reset(C* p = NULL) { |
| 440 if (ptr_ != p) { | 431 if (ptr_ != p) { |
| 441 FreeProc free_proc; | 432 FreeProc free_proc; |
| 442 free_proc(ptr_); | 433 free_proc(ptr_); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 | 515 |
| 525 // A function to convert T* into scoped_ptr<T> | 516 // A function to convert T* into scoped_ptr<T> |
| 526 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 517 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
| 527 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 518 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
| 528 template <typename T> | 519 template <typename T> |
| 529 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 520 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
| 530 return scoped_ptr<T>(ptr); | 521 return scoped_ptr<T>(ptr); |
| 531 } | 522 } |
| 532 | 523 |
| 533 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 524 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
| OLD | NEW |