| 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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 #define BASE_MEMORY_SCOPED_PTR_H_ | 88 #define BASE_MEMORY_SCOPED_PTR_H_ |
| 89 | 89 |
| 90 // This is an implementation designed to match the anticipated future TR2 | 90 // This is an implementation designed to match the anticipated future TR2 |
| 91 // implementation of the scoped_ptr class, and its closely-related brethren, | 91 // implementation of the scoped_ptr class, and its closely-related brethren, |
| 92 // scoped_array, scoped_ptr_malloc. | 92 // scoped_array, scoped_ptr_malloc. |
| 93 | 93 |
| 94 #include <assert.h> | 94 #include <assert.h> |
| 95 #include <stddef.h> | 95 #include <stddef.h> |
| 96 #include <stdlib.h> | 96 #include <stdlib.h> |
| 97 | 97 |
| 98 #include <type_traits> |
| 99 |
| 100 |
| 98 #include "base/basictypes.h" | 101 #include "base/basictypes.h" |
| 99 #include "base/compiler_specific.h" | 102 #include "base/compiler_specific.h" |
| 100 #include "base/move.h" | 103 #include "base/move.h" |
| 104 #include "base/nullptr.h" |
| 101 #include "base/template_util.h" | 105 #include "base/template_util.h" |
| 102 | 106 |
| 103 namespace base { | 107 namespace base { |
| 104 | 108 |
| 105 namespace subtle { | 109 namespace subtle { |
| 106 class RefCountedBase; | 110 class RefCountedBase; |
| 107 class RefCountedThreadSafeBase; | 111 class RefCountedThreadSafeBase; |
| 108 } // namespace subtle | 112 } // namespace subtle |
| 109 | 113 |
| 110 namespace internal { | 114 namespace internal { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 134 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) | 138 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) |
| 135 | 139 |
| 136 COMPILE_ASSERT(base::internal::IsNotRefCounted<C>::value, | 140 COMPILE_ASSERT(base::internal::IsNotRefCounted<C>::value, |
| 137 C_is_refcounted_type_and_needs_scoped_refptr); | 141 C_is_refcounted_type_and_needs_scoped_refptr); |
| 138 | 142 |
| 139 public: | 143 public: |
| 140 | 144 |
| 141 // The element type | 145 // The element type |
| 142 typedef C element_type; | 146 typedef C element_type; |
| 143 | 147 |
| 148 scoped_ptr() : ptr_(NULL) {} |
| 149 scoped_ptr(base::nullptr_t) : ptr_(NULL) {} |
| 150 |
| 144 // Constructor. Defaults to initializing with NULL. | 151 // Constructor. Defaults to initializing with NULL. |
| 145 // There is no way to create an uninitialized scoped_ptr. | 152 // There is no way to create an uninitialized scoped_ptr. |
| 146 // The input parameter must be allocated with new. | 153 // The input parameter must be allocated with new. |
| 147 explicit scoped_ptr(C* p = NULL) : ptr_(p) { } | 154 template <typename V> |
| 155 scoped_ptr(V p, typename std::enable_if<!std::is_integral<V>::value && |
| 156 std::is_convertible<V, C*>::value, int
>::type = 0) : ptr_(p) { } |
| 148 | 157 |
| 149 // Constructor. Allows construction from a scoped_ptr rvalue for a | 158 // Constructor. Allows construction from a scoped_ptr rvalue for a |
| 150 // convertible type. | 159 // convertible type. |
| 151 template <typename U> | 160 template <typename U> |
| 152 scoped_ptr(scoped_ptr<U> other) : ptr_(other.release()) { } | 161 scoped_ptr(scoped_ptr<U> other) : ptr_(other.release()) { } |
| 153 | 162 |
| 154 // Constructor. Move constructor for C++03 move emulation of this type. | 163 // Constructor. Move constructor for C++03 move emulation of this type. |
| 155 scoped_ptr(RValue rvalue) | 164 scoped_ptr(RValue rvalue) |
| 156 : ptr_(rvalue.object->release()) { | 165 : ptr_(rvalue.object->release()) { |
| 157 } | 166 } |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 | 524 |
| 516 // A function to convert T* into scoped_ptr<T> | 525 // A function to convert T* into scoped_ptr<T> |
| 517 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 526 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
| 518 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 527 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
| 519 template <typename T> | 528 template <typename T> |
| 520 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 529 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
| 521 return scoped_ptr<T>(ptr); | 530 return scoped_ptr<T>(ptr); |
| 522 } | 531 } |
| 523 | 532 |
| 524 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 533 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
| OLD | NEW |