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 a | 5 // Scopers help you manage ownership of a pointer, helping you easily manage a |
6 // pointer within a scope, and automatically destroying the pointer at the end | 6 // pointer within a scope, and automatically destroying the pointer at the end |
7 // of a scope. There are two main classes you will use, which correspond to the | 7 // of a scope. There are two main classes you will use, which correspond to the |
8 // operators new/delete and new[]/delete[]. | 8 // operators new/delete and new[]/delete[]. |
9 // | 9 // |
10 // Example usage (scoped_ptr<T>): | 10 // Example usage (scoped_ptr<T>): |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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() |
68 // is different though because we are constructing a temporary on the return | 68 // is different though because we are constructing a temporary on the return |
69 // line and thus can avoid needing to call Pass(). | 69 // line and thus can avoid needing to call Pass(). |
70 // | 70 // |
71 // Pass() properly handles upcast in initialization, i.e. you can use a | 71 // Pass() properly handles upcast in initialization, i.e. you can use a |
72 // scoped_ptr<Child> to initialize a scoped_ptr<Parent>: | 72 // scoped_ptr<Child> to initialize a scoped_ptr<Parent>: |
73 // | 73 // |
74 // scoped_ptr<Foo> foo(new Foo()); | 74 // scoped_ptr<Foo> foo(new Foo()); |
75 // scoped_ptr<FooParent> parent(foo.Pass()); | 75 // scoped_ptr<FooParent> parent(foo.Pass()); |
76 // | |
77 // PassAs<>() should be used to upcast return value in return statement: | |
78 // | |
79 // scoped_ptr<Foo> CreateFoo() { | |
80 // scoped_ptr<FooChild> result(new FooChild()); | |
81 // return result.PassAs<Foo>(); | |
82 // } | |
83 // | |
84 // Note that PassAs<>() is implemented only for scoped_ptr<T>, but not for | |
85 // scoped_ptr<T[]>. This is because casting array pointers may not be safe. | |
86 | 76 |
87 #ifndef BASE_MEMORY_SCOPED_PTR_H_ | 77 #ifndef BASE_MEMORY_SCOPED_PTR_H_ |
88 #define BASE_MEMORY_SCOPED_PTR_H_ | 78 #define BASE_MEMORY_SCOPED_PTR_H_ |
89 | 79 |
90 // This is an implementation designed to match the anticipated future TR2 | 80 // This is an implementation designed to match the anticipated future TR2 |
91 // implementation of the scoped_ptr class. | 81 // implementation of the scoped_ptr class. |
92 | 82 |
93 #include <assert.h> | 83 #include <assert.h> |
94 #include <stddef.h> | 84 #include <stddef.h> |
95 #include <stdlib.h> | 85 #include <stdlib.h> |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 } | 419 } |
430 | 420 |
431 // Release a pointer. | 421 // Release a pointer. |
432 // The return value is the current pointer held by this object. If this object | 422 // The return value is the current pointer held by this object. If this object |
433 // holds a nullptr, the return value is nullptr. After this operation, this | 423 // holds a nullptr, the return value is nullptr. After this operation, this |
434 // object will hold a nullptr, and will not own the object any more. | 424 // object will hold a nullptr, and will not own the object any more. |
435 element_type* release() WARN_UNUSED_RESULT { | 425 element_type* release() WARN_UNUSED_RESULT { |
436 return impl_.release(); | 426 return impl_.release(); |
437 } | 427 } |
438 | 428 |
439 // C++98 doesn't support functions templates with default parameters which | |
440 // makes it hard to write a PassAs() that understands converting the deleter | |
441 // while preserving simple calling semantics. | |
442 // | |
443 // Until there is a use case for PassAs() with custom deleters, just ignore | |
444 // the custom deleter. | |
445 template <typename PassAsType> | |
446 scoped_ptr<PassAsType> PassAs() { | |
447 return scoped_ptr<PassAsType>(Pass()); | |
448 } | |
449 | |
450 private: | 429 private: |
451 // Needed to reach into |impl_| in the constructor. | 430 // Needed to reach into |impl_| in the constructor. |
452 template <typename U, typename V> friend class scoped_ptr; | 431 template <typename U, typename V> friend class scoped_ptr; |
453 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; | 432 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; |
454 | 433 |
455 // Forbidden for API compatibility with std::unique_ptr. | 434 // Forbidden for API compatibility with std::unique_ptr. |
456 explicit scoped_ptr(int disallow_construction_from_null); | 435 explicit scoped_ptr(int disallow_construction_from_null); |
457 | 436 |
458 // Forbid comparison of scoped_ptr types. If U != T, it totally | 437 // Forbid comparison of scoped_ptr types. If U != T, it totally |
459 // doesn't make sense, and if U == T, it still doesn't make sense | 438 // doesn't make sense, and if U == T, it still doesn't make sense |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 | 579 |
601 // A function to convert T* into scoped_ptr<T> | 580 // A function to convert T* into scoped_ptr<T> |
602 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 581 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
603 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 582 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
604 template <typename T> | 583 template <typename T> |
605 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 584 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
606 return scoped_ptr<T>(ptr); | 585 return scoped_ptr<T>(ptr); |
607 } | 586 } |
608 | 587 |
609 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 588 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
OLD | NEW |