Chromium Code Reviews| 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 <algorithm> // For std::swap(). | |
| 99 | |
| 98 #include "base/basictypes.h" | 100 #include "base/basictypes.h" |
| 99 #include "base/compiler_specific.h" | 101 #include "base/compiler_specific.h" |
| 100 #include "base/move.h" | 102 #include "base/move.h" |
| 101 #include "base/template_util.h" | 103 #include "base/template_util.h" |
| 102 | 104 |
| 103 namespace base { | 105 namespace base { |
| 104 | 106 |
| 105 namespace subtle { | 107 namespace subtle { |
| 106 class RefCountedBase; | 108 class RefCountedBase; |
| 107 class RefCountedThreadSafeBase; | 109 class RefCountedThreadSafeBase; |
| 108 } // namespace subtle | 110 } // namespace subtle |
| 109 | 111 |
| 112 // Function object which deletes its parameter, which must be a pointer. | |
| 113 // If C is an array type, invokes 'delete[]' on the parameter; otherwise, | |
| 114 // invokes 'delete'. The default deleter for scoped_ptr<T>. | |
| 115 template <class T> | |
| 116 struct DefaultDeleter { | |
| 117 DefaultDeleter() {} | |
| 118 template <typename U> DefaultDeleter(const DefaultDeleter<U>& other) { | |
| 119 // All default single-object deleters can trivially convert to one another. | |
| 120 } | |
| 121 inline void operator()(T* ptr) const { | |
| 122 enum { type_must_be_complete = sizeof(T) }; | |
| 123 delete ptr; | |
| 124 } | |
| 125 }; | |
| 126 | |
| 127 // Specialization of DefaultDeleter for array types. | |
| 128 template <class T> | |
| 129 struct DefaultDeleter<T[]> { | |
| 130 inline void operator()(T* ptr) const { | |
| 131 enum { type_must_be_complete = sizeof(T) }; | |
| 132 delete[] ptr; | |
| 133 } | |
| 134 | |
| 135 private: | |
| 136 // Disable this operator for any U != T because it is undefined to execute | |
| 137 // an array delete when the static type of the array mismatches the dynamic | |
| 138 // type. | |
| 139 // | |
| 140 // References: | |
| 141 // C++98 [expr.delete]p3 | |
| 142 // http://cplusplus.github.com/LWG/lwg-defects.html#938 | |
| 143 template <typename U> void operator()(U* array) const; | |
| 144 }; | |
| 145 | |
| 146 // Function object which invokes 'free' on its parameter, which must be | |
| 147 // a pointer. Can be used to store malloc-allocated pointers in scoped_ptr: | |
| 148 // | |
| 149 // scoped_ptr<int, base::FreeDeleter> foo_ptr( | |
| 150 // static_cast<int*>(malloc(sizeof(int)))); | |
| 151 struct FreeDeleter { | |
| 152 inline void operator()(void* ptr) const { | |
| 153 free(ptr); | |
| 154 } | |
| 155 }; | |
| 156 | |
| 110 namespace internal { | 157 namespace internal { |
| 111 | 158 |
| 112 template <typename T> struct IsNotRefCounted { | 159 template <typename T> struct IsNotRefCounted { |
| 113 enum { | 160 enum { |
| 114 value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value && | 161 value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value && |
| 115 !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>:: | 162 !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>:: |
| 116 value | 163 value |
| 117 }; | 164 }; |
| 118 }; | 165 }; |
| 119 | 166 |
| 167 // Minimal implementation of the core logic of scoped_ptr, suitable for | |
| 168 // reuse in both scoped_ptr and its specializations. | |
| 169 template <class T, class D> | |
| 170 class scoped_ptr_impl { | |
|
Ryan Sleevi
2012/11/29 02:42:27
Have you confirmed what the compile time implicati
awong
2012/12/12 02:17:03
I'm not quite sure how to go about testing this.
Ryan Sleevi
2012/12/12 03:37:47
Usually just 3-5 timed clobber builds, run by scri
awong
2012/12/12 21:00:14
Grrr...can I just test on the build bots? :D
(I d
| |
| 171 public: | |
| 172 explicit scoped_ptr_impl(T* p) : data_(p) { } | |
| 173 | |
| 174 // Initializer for deleters that have data parameters. | |
| 175 scoped_ptr_impl(T* p, const D& d) : data_(p, d) {} | |
| 176 | |
| 177 // Templated constructor that destructively takes the value from another | |
| 178 // scoped_ptr_impl. | |
| 179 template <typename U, typename V> | |
| 180 scoped_ptr_impl(scoped_ptr_impl<U, V>* other) | |
| 181 : data_(other->release(), other->get_deleter()) { | |
| 182 // We do not support move-only deleters. We could modify our move | |
| 183 // emulation to have base::subtle::move() and base::subtle::forward() | |
| 184 // functions that are imperfect emulations of their C++11 equivalents, | |
| 185 // but until there's a requirement, just assume deleters are copyable. | |
| 186 } | |
| 187 | |
| 188 template <typename U, typename V> | |
| 189 void TakeState(scoped_ptr_impl<U, V>* other) { | |
| 190 // See comment in templated constructor above regarding lack of support | |
| 191 // for move-only deleters. | |
| 192 reset(other->release()); | |
| 193 get_deleter() = other->get_deleter(); | |
| 194 } | |
| 195 | |
| 196 ~scoped_ptr_impl() { | |
| 197 if (data_.ptr != NULL) { | |
| 198 // Not using get_deleter() saves one function call in non-optimized | |
| 199 // builds. | |
| 200 static_cast<D&>(data_)(data_.ptr); | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 void reset(T* p) { | |
| 205 // This self-reset check is deprecated. | |
| 206 // this->reset(this->get()) currently works, but it is DEPRECATED, and | |
| 207 // will be removed once we verify that no one depends on it. | |
| 208 // | |
| 209 // TODO(ajwong): Change this behavior to match unique_ptr<>. | |
| 210 // http://crbug.com/162971 | |
| 211 if (p != data_.ptr) { | |
| 212 if (data_.ptr != NULL) { | |
| 213 // Note that this can lead to undefined behavior and memory leaks | |
| 214 // in the unlikely but possible case that get_deleter()(get()) | |
| 215 // indirectly deletes this. The fix is to reset ptr_ before deleting | |
| 216 // its old value, but first we need to clean up the code that relies | |
| 217 // on the current sequencing. | |
|
gromer
2012/12/04 19:41:18
Maybe add a tracking bug for this too?
awong
2012/12/12 02:17:03
I think http://crbug.com/162971 cited earlier cove
Ryan Sleevi
2012/12/12 03:37:47
Ah, I just fail at reading comprehension then.
| |
| 218 static_cast<D&>(data_)(data_.ptr); | |
| 219 } | |
| 220 data_.ptr = p; | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 T* get() const { return data_.ptr; } | |
| 225 | |
| 226 D& get_deleter() { return data_; } | |
| 227 const D& get_deleter() const { return data_; } | |
| 228 | |
| 229 void swap(scoped_ptr_impl& p2) { | |
| 230 // Standard swap idiom: 'using std::swap' ensures that std::swap is | |
| 231 // present in the overload set, but we call swap unqualified so that | |
| 232 // any more-specific overloads can be used, if available. | |
| 233 using std::swap; | |
| 234 swap(static_cast<D&>(data_), static_cast<D&>(p2.data_)); | |
| 235 swap(data_.ptr, p2.data_.ptr); | |
| 236 } | |
| 237 | |
| 238 T* release() { | |
| 239 T* old_ptr = data_.ptr; | |
| 240 data_.ptr = NULL; | |
| 241 return old_ptr; | |
| 242 } | |
| 243 | |
| 244 private: | |
| 245 // Needed to allow type-converting constructor. | |
| 246 template <typename U, typename V> friend class scoped_ptr_impl; | |
| 247 | |
| 248 // Use the empty base class optimization to allow us to have a D | |
| 249 // member, while avoiding any space overhead for it when D is an | |
| 250 // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good | |
| 251 // discussion of this technique. | |
| 252 struct Data : public D { | |
| 253 explicit Data(T* ptr_in) : ptr(ptr_in) {} | |
| 254 Data(T* ptr_in, const D& other) : D(other), ptr(ptr_in) {} | |
| 255 T* ptr; | |
| 256 }; | |
| 257 | |
| 258 Data data_; | |
| 259 | |
| 260 DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl); | |
| 261 }; | |
| 262 | |
| 120 } // namespace internal | 263 } // namespace internal |
| 264 | |
| 121 } // namespace base | 265 } // namespace base |
| 122 | 266 |
| 123 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> | 267 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> |
| 124 // automatically deletes the pointer it holds (if any). | 268 // automatically deletes the pointer it holds (if any). |
| 125 // That is, scoped_ptr<T> owns the T object that it points to. | 269 // That is, scoped_ptr<T> owns the T object that it points to. |
| 126 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object. | 270 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object. |
| 127 // Also like T*, scoped_ptr<T> is thread-compatible, and once you | 271 // Also like T*, scoped_ptr<T> is thread-compatible, and once you |
| 128 // dereference it, you get the thread safety guarantees of T. | 272 // dereference it, you get the thread safety guarantees of T. |
| 129 // | 273 // |
| 130 // The size of a scoped_ptr is small: | 274 // The size of a scoped_ptr is small: |
| 131 // sizeof(scoped_ptr<C>) == sizeof(C*) | 275 // sizeof(scoped_ptr<C>) == sizeof(C*) |
| 132 template <class C> | 276 template <class T, class D = base::DefaultDeleter<T> > |
| 133 class scoped_ptr { | 277 class scoped_ptr { |
| 134 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) | 278 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) |
| 135 | 279 |
| 136 COMPILE_ASSERT(base::internal::IsNotRefCounted<C>::value, | 280 COMPILE_ASSERT(base::internal::IsNotRefCounted<T>::value, |
| 137 C_is_refcounted_type_and_needs_scoped_refptr); | 281 T_is_refcounted_type_and_needs_scoped_refptr); |
| 138 | 282 |
| 139 public: | 283 public: |
| 140 | 284 // The element and deleter types. |
| 141 // The element type | 285 typedef T element_type; |
| 142 typedef C element_type; | 286 typedef D deleter_type; |
| 143 | 287 |
| 144 // Constructor. Defaults to initializing with NULL. | 288 // Constructor. Defaults to initializing with NULL. |
| 145 // There is no way to create an uninitialized scoped_ptr. | 289 scoped_ptr() : impl_(NULL) { } |
| 146 // The input parameter must be allocated with new. | 290 |
| 147 explicit scoped_ptr(C* p = NULL) : ptr_(p) { } | 291 // Constructor. Takes ownership of p. |
| 292 explicit scoped_ptr(element_type* p) : impl_(p) { } | |
| 293 | |
| 294 // Constructor. Allows initialization of a stateful deleter. | |
| 295 scoped_ptr(element_type* p, const D& d) : impl_(p, d) { } | |
| 148 | 296 |
| 149 // Constructor. Allows construction from a scoped_ptr rvalue for a | 297 // Constructor. Allows construction from a scoped_ptr rvalue for a |
| 150 // convertible type. | 298 // convertible type and deleter. |
| 151 template <typename U> | 299 template <typename U, typename V> |
| 152 scoped_ptr(scoped_ptr<U> other) : ptr_(other.release()) { } | 300 scoped_ptr(scoped_ptr<U, V> other) : impl_(&other.impl_) { } |
| 153 | 301 |
| 154 // Constructor. Move constructor for C++03 move emulation of this type. | 302 // Constructor. Move constructor for C++03 move emulation of this type. |
| 155 scoped_ptr(RValue rvalue) | 303 scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { } |
| 156 : ptr_(rvalue.object->release()) { | |
| 157 } | |
| 158 | |
| 159 // Destructor. If there is a C object, delete it. | |
| 160 // We don't need to test ptr_ == NULL because C++ does that for us. | |
| 161 ~scoped_ptr() { | |
| 162 enum { type_must_be_complete = sizeof(C) }; | |
| 163 delete ptr_; | |
| 164 } | |
| 165 | 304 |
| 166 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible | 305 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible |
| 167 // type. | 306 // type and deleter. |
| 168 template <typename U> | 307 template <typename U, typename V> |
| 169 scoped_ptr& operator=(scoped_ptr<U> rhs) { | 308 scoped_ptr& operator=(scoped_ptr<U, V> rhs) { |
| 170 reset(rhs.release()); | 309 impl_.TakeState(&rhs.impl_); |
| 171 return *this; | 310 return *this; |
| 172 } | 311 } |
| 173 | 312 |
| 174 // operator=. Move operator= for C++03 move emulation of this type. | 313 // Reset. Deletes the currently owned object, if any. |
| 175 scoped_ptr& operator=(RValue rhs) { | |
| 176 swap(*rhs->object); | |
| 177 return *this; | |
| 178 } | |
| 179 | |
| 180 // Reset. Deletes the current owned object, if any. | |
| 181 // Then takes ownership of a new object, if given. | 314 // Then takes ownership of a new object, if given. |
| 182 // this->reset(this->get()) works. | 315 void reset(element_type* p = NULL) { impl_.reset(p); } |
| 183 void reset(C* p = NULL) { | |
| 184 if (p != ptr_) { | |
| 185 enum { type_must_be_complete = sizeof(C) }; | |
| 186 delete ptr_; | |
| 187 ptr_ = p; | |
| 188 } | |
| 189 } | |
| 190 | 316 |
| 191 // Accessors to get the owned object. | 317 // Accessors to get the owned object. |
| 192 // operator* and operator-> will assert() if there is no current object. | 318 // operator* and operator-> will assert() if there is no current object. |
| 193 C& operator*() const { | 319 element_type& operator*() const { |
| 194 assert(ptr_ != NULL); | 320 assert(impl_.get() != NULL); |
| 195 return *ptr_; | 321 return *impl_.get(); |
| 196 } | 322 } |
| 197 C* operator->() const { | 323 element_type* operator->() const { |
| 198 assert(ptr_ != NULL); | 324 assert(impl_.get() != NULL); |
| 199 return ptr_; | 325 return impl_.get(); |
| 200 } | 326 } |
| 201 C* get() const { return ptr_; } | 327 element_type* get() const { return impl_.get(); } |
| 202 | 328 |
| 203 // Allow scoped_ptr<C> to be used in boolean expressions, but not | 329 // Access to the deleter. |
| 330 deleter_type& get_deleter() { return impl_.get_deleter(); } | |
| 331 const deleter_type& get_deleter() const { return impl_.get_deleter(); } | |
| 332 | |
| 333 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not | |
| 204 // implicitly convertible to a real bool (which is dangerous). | 334 // implicitly convertible to a real bool (which is dangerous). |
| 205 typedef C* scoped_ptr::*Testable; | 335 private: |
|
gromer
2012/12/04 19:41:18
This violates the style guide by interleaving priv
awong
2012/12/12 02:17:03
Yeah...I could move the typedef into a private sec
| |
| 206 operator Testable() const { return ptr_ ? &scoped_ptr::ptr_ : NULL; } | 336 typedef base::internal::scoped_ptr_impl<element_type, deleter_type> |
| 337 scoped_ptr::*Testable; | |
| 338 | |
| 339 public: | |
| 340 operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; } | |
| 207 | 341 |
| 208 // Comparison operators. | 342 // Comparison operators. |
| 209 // These return whether two scoped_ptr refer to the same object, not just to | 343 // These return whether two scoped_ptr refer to the same object, not just to |
| 210 // two different but equal objects. | 344 // two different but equal objects. |
| 211 bool operator==(C* p) const { return ptr_ == p; } | 345 bool operator==(element_type* p) const { return impl_.get() == p; } |
| 212 bool operator!=(C* p) const { return ptr_ != p; } | 346 bool operator!=(element_type* p) const { return impl_.get() != p; } |
| 213 | 347 |
| 214 // Swap two scoped pointers. | 348 // Swap two scoped pointers. |
| 215 void swap(scoped_ptr& p2) { | 349 void swap(scoped_ptr& p2) { |
| 216 C* tmp = ptr_; | 350 impl_.swap(p2.impl_); |
| 217 ptr_ = p2.ptr_; | |
| 218 p2.ptr_ = tmp; | |
| 219 } | 351 } |
| 220 | 352 |
| 221 // Release a pointer. | 353 // Release a pointer. |
| 222 // The return value is the current pointer held by this object. | 354 // The return value is the current pointer held by this object. |
| 223 // If this object holds a NULL pointer, the return value is NULL. | 355 // If this object holds a NULL pointer, the return value is NULL. |
| 224 // After this operation, this object will hold a NULL pointer, | 356 // After this operation, this object will hold a NULL pointer, |
| 225 // and will not own the object any more. | 357 // and will not own the object any more. |
| 226 C* release() WARN_UNUSED_RESULT { | 358 element_type* release() WARN_UNUSED_RESULT { |
| 227 C* retVal = ptr_; | 359 return impl_.release(); |
| 228 ptr_ = NULL; | |
| 229 return retVal; | |
| 230 } | 360 } |
| 231 | 361 |
| 362 // C++98 doesn't support functions templates with default parameters which | |
| 363 // makes it hard to write a PassAs() that understands converting the deleter | |
| 364 // while preserving simple calling semantics. | |
| 365 // | |
| 366 // Until there is a use case for PassAs() with custom deleters, just ignore | |
| 367 // the custom deleter. | |
| 232 template <typename PassAsType> | 368 template <typename PassAsType> |
| 233 scoped_ptr<PassAsType> PassAs() { | 369 scoped_ptr<PassAsType> PassAs() { |
| 234 return scoped_ptr<PassAsType>(release()); | 370 return scoped_ptr<PassAsType>(Pass()); |
| 235 } | 371 } |
| 236 | 372 |
| 237 private: | 373 private: |
| 238 C* ptr_; | 374 // Needed to reach into |impl_| in the constructor. |
| 375 template <typename U, typename V> friend class scoped_ptr; | |
| 376 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; | |
| 239 | 377 |
| 240 // Forbid comparison of scoped_ptr types. If C2 != C, it totally doesn't | 378 // Forbid comparison of scoped_ptr types. If U != T, it totally |
| 241 // make sense, and if C2 == C, it still doesn't make sense because you should | 379 // doesn't make sense, and if U == T, it still doesn't make sense |
| 242 // never have the same object owned by two different scoped_ptrs. | 380 // because you should never have the same object owned by two different |
| 243 template <class C2> bool operator==(scoped_ptr<C2> const& p2) const; | 381 // scoped_ptrs. |
| 244 template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const; | 382 template <class U> bool operator==(scoped_ptr<U> const& p2) const; |
| 383 template <class U> bool operator!=(scoped_ptr<U> const& p2) const; | |
| 384 }; | |
| 245 | 385 |
| 386 template <class T, class D> | |
| 387 class scoped_ptr<T[], D> { | |
| 388 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) | |
| 389 | |
| 390 public: | |
| 391 // The element and deleter types. | |
| 392 typedef T element_type; | |
| 393 typedef D deleter_type; | |
| 394 | |
| 395 // Constructor. Defaults to initializing with NULL. | |
| 396 scoped_ptr() : impl_(NULL) { } | |
| 397 | |
| 398 // Constructor. Stores the given array. Note that the argument's type | |
| 399 // must exactly match T*. In particular: | |
| 400 // - it cannot be a pointer to a type derived from T, because it is | |
| 401 // inherently unsafe in the general case to access an array through a | |
| 402 // pointer whose dynamic type does not match its static type (eg., if | |
| 403 // T and the derived types had different sizes access would be | |
| 404 // incorrectly calculated). Deletion is also always undefined | |
| 405 // (C++98 [expr.delete]p3). If you're doing this, fix your code. | |
| 406 // - it cannot be NULL, because NULL is an integral expression, not a | |
| 407 // pointer to T. Use the no-argument version instead of explicitly | |
| 408 // passing NULL. | |
| 409 // - it cannot be const-qualified differently from T per unique_ptr spec | |
| 410 // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting | |
| 411 // to work around this may use the implicit_cast<const T*>() idiom. | |
| 412 // However, because of the first bullet in this comment, users MUST | |
| 413 // NOT use implicit_cast<Base*>() to upcast the static type of the array. | |
| 414 explicit scoped_ptr(element_type* array) : impl_(array) { } | |
| 415 | |
| 416 // Constructor. Move constructor for C++03 move emulation of this type. | |
| 417 scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { } | |
| 418 | |
| 419 // operator=. Move operator= for C++03 move emulation of this type. | |
| 420 scoped_ptr& operator=(RValue rhs) { | |
| 421 impl_.TakeState(&rhs.object->impl_); | |
| 422 return *this; | |
| 423 } | |
| 424 | |
| 425 // Reset. Deletes the currently owned array, if any. | |
| 426 // Then takes ownership of a new object, if given. | |
| 427 void reset(element_type* array = NULL) { impl_.reset(array); } | |
| 428 | |
| 429 // Accessors to get the owned array. | |
| 430 element_type& operator[](size_t i) const { | |
| 431 assert(impl_.get() != NULL); | |
| 432 return impl_.get()[i]; | |
| 433 } | |
| 434 element_type* get() const { return impl_.get(); } | |
| 435 | |
| 436 // Access to the deleter. | |
| 437 deleter_type& get_deleter() { return impl_.get_deleter(); } | |
| 438 const deleter_type& get_deleter() const { return impl_.get_deleter(); } | |
| 439 | |
| 440 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not | |
| 441 // implicitly convertible to a real bool (which is dangerous). | |
| 442 private: | |
| 443 typedef base::internal::scoped_ptr_impl<element_type, deleter_type> | |
| 444 scoped_ptr::*Testable; | |
| 445 | |
| 446 public: | |
| 447 operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; } | |
| 448 | |
| 449 // Comparison operators. | |
| 450 // These return whether two scoped_ptr refer to the same object, not just to | |
| 451 // two different but equal objects. | |
| 452 bool operator==(element_type* array) const { return impl_.get() == array; } | |
| 453 bool operator!=(element_type* array) const { return impl_.get() != array; } | |
| 454 | |
| 455 // Swap two scoped pointers. | |
| 456 void swap(scoped_ptr& p2) { | |
| 457 impl_.swap(p2.impl_); | |
| 458 } | |
| 459 | |
| 460 // Release a pointer. | |
| 461 // The return value is the current pointer held by this object. | |
| 462 // If this object holds a NULL pointer, the return value is NULL. | |
| 463 // After this operation, this object will hold a NULL pointer, | |
| 464 // and will not own the object any more. | |
| 465 element_type* release() WARN_UNUSED_RESULT { | |
| 466 return impl_.release(); | |
| 467 } | |
| 468 | |
| 469 private: | |
| 470 // Force element_type to be a complete type. | |
| 471 enum { type_must_be_complete = sizeof(element_type) }; | |
| 472 | |
| 473 // Actually hold the data. | |
| 474 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; | |
| 475 | |
| 476 // Disable initialization from any type other than element_type*, by | |
| 477 // providing a constructor that matches such an initialization, but is | |
| 478 // private and has no definition. This is disabled because it is not safe to | |
| 479 // call delete[] on an array whose static type does not match its dynamic | |
| 480 // type. | |
| 481 template <typename U> | |
| 482 explicit scoped_ptr(U* array); | |
| 483 | |
| 484 // Disable reset() from any type other than element_type*, for the same | |
| 485 // reasons as the constructor above. | |
| 486 template <typename U> | |
| 487 void reset(U* array); | |
| 488 | |
| 489 // Forbid comparison of scoped_ptr types. If U != T, it totally | |
| 490 // doesn't make sense, and if U == T, it still doesn't make sense | |
| 491 // because you should never have the same object owned by two different | |
| 492 // scoped_ptrs. | |
| 493 template <class U> bool operator==(scoped_ptr<U> const& p2) const; | |
| 494 template <class U> bool operator!=(scoped_ptr<U> const& p2) const; | |
| 246 }; | 495 }; |
| 247 | 496 |
| 248 // Free functions | 497 // Free functions |
| 249 template <class C> | 498 template <class T, class D> |
| 250 void swap(scoped_ptr<C>& p1, scoped_ptr<C>& p2) { | 499 void swap(scoped_ptr<T, D>& p1, scoped_ptr<T, D>& p2) { |
| 251 p1.swap(p2); | 500 p1.swap(p2); |
| 252 } | 501 } |
| 253 | 502 |
| 254 template <class C> | 503 template <class T, class D> |
| 255 bool operator==(C* p1, const scoped_ptr<C>& p2) { | 504 bool operator==(T* p1, const scoped_ptr<T, D>& p2) { |
| 256 return p1 == p2.get(); | 505 return p1 == p2.get(); |
| 257 } | 506 } |
| 258 | 507 |
| 259 template <class C> | 508 template <class T, class D> |
| 260 bool operator!=(C* p1, const scoped_ptr<C>& p2) { | 509 bool operator!=(T* p1, const scoped_ptr<T, D>& p2) { |
| 261 return p1 != p2.get(); | 510 return p1 != p2.get(); |
| 262 } | 511 } |
| 263 | 512 |
| 513 // DEPRECATED: Use scoped_ptr<C[]> instead. | |
| 514 // | |
| 264 // scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate | 515 // scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate |
| 265 // with new [] and the destructor deletes objects with delete []. | 516 // with new [] and the destructor deletes objects with delete []. |
| 266 // | 517 // |
| 267 // As with scoped_ptr<C>, a scoped_array<C> either points to an object | 518 // As with scoped_ptr<C>, a scoped_array<C> either points to an object |
| 268 // or is NULL. A scoped_array<C> owns the object that it points to. | 519 // or is NULL. A scoped_array<C> owns the object that it points to. |
| 269 // scoped_array<T> is thread-compatible, and once you index into it, | 520 // scoped_array<T> is thread-compatible, and once you index into it, |
| 270 // the returned objects have only the thread safety guarantees of T. | 521 // the returned objects have only the thread safety guarantees of T. |
| 271 // | 522 // |
| 272 // Size: sizeof(scoped_array<C>) == sizeof(C*) | 523 // Size: sizeof(scoped_array<C>) == sizeof(C*) |
| 273 template <class C> | 524 template <class C> |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 291 | 542 |
| 292 // Destructor. If there is a C object, delete it. | 543 // Destructor. If there is a C object, delete it. |
| 293 // We don't need to test ptr_ == NULL because C++ does that for us. | 544 // We don't need to test ptr_ == NULL because C++ does that for us. |
| 294 ~scoped_array() { | 545 ~scoped_array() { |
| 295 enum { type_must_be_complete = sizeof(C) }; | 546 enum { type_must_be_complete = sizeof(C) }; |
| 296 delete[] array_; | 547 delete[] array_; |
| 297 } | 548 } |
| 298 | 549 |
| 299 // operator=. Move operator= for C++03 move emulation of this type. | 550 // operator=. Move operator= for C++03 move emulation of this type. |
| 300 scoped_array& operator=(RValue rhs) { | 551 scoped_array& operator=(RValue rhs) { |
| 301 swap(*rhs.object); | 552 reset(rhs.object->release()); |
| 302 return *this; | 553 return *this; |
| 303 } | 554 } |
| 304 | 555 |
| 305 // Reset. Deletes the current owned object, if any. | 556 // Reset. Deletes the current owned object, if any. |
| 306 // Then takes ownership of a new object, if given. | 557 // Then takes ownership of a new object, if given. |
| 307 // this->reset(this->get()) works. | 558 // this->reset(this->get()) works. |
| 308 void reset(C* p = NULL) { | 559 void reset(C* p = NULL) { |
| 309 if (p != array_) { | 560 if (p != array_) { |
| 310 enum { type_must_be_complete = sizeof(C) }; | 561 enum { type_must_be_complete = sizeof(C) }; |
| 311 delete[] array_; | 562 delete[] array_; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 373 template <class C> | 624 template <class C> |
| 374 bool operator==(C* p1, const scoped_array<C>& p2) { | 625 bool operator==(C* p1, const scoped_array<C>& p2) { |
| 375 return p1 == p2.get(); | 626 return p1 == p2.get(); |
| 376 } | 627 } |
| 377 | 628 |
| 378 template <class C> | 629 template <class C> |
| 379 bool operator!=(C* p1, const scoped_array<C>& p2) { | 630 bool operator!=(C* p1, const scoped_array<C>& p2) { |
| 380 return p1 != p2.get(); | 631 return p1 != p2.get(); |
| 381 } | 632 } |
| 382 | 633 |
| 383 // This class wraps the c library function free() in a class that can be | 634 // DEPRECATED: Use scoped_ptr<C, base::FreeDeleter> instead. |
| 384 // passed as a template argument to scoped_ptr_malloc below. | 635 // |
| 385 class ScopedPtrMallocFree { | |
| 386 public: | |
| 387 inline void operator()(void* x) const { | |
| 388 free(x); | |
| 389 } | |
| 390 }; | |
| 391 | |
| 392 // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a | 636 // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a |
| 393 // second template argument, the functor used to free the object. | 637 // second template argument, the functor used to free the object. |
| 394 | 638 |
| 395 template<class C, class FreeProc = ScopedPtrMallocFree> | 639 template<class C, class FreeProc = base::FreeDeleter> |
| 396 class scoped_ptr_malloc { | 640 class scoped_ptr_malloc { |
| 397 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr_malloc, RValue) | 641 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr_malloc, RValue) |
| 398 | 642 |
| 399 public: | 643 public: |
| 400 | 644 |
| 401 // The element type | 645 // The element type |
| 402 typedef C element_type; | 646 typedef C element_type; |
| 403 | 647 |
| 404 // Constructor. Defaults to initializing with NULL. | 648 // Constructor. Defaults to initializing with NULL. |
| 405 // There is no way to create an uninitialized scoped_ptr. | 649 // There is no way to create an uninitialized scoped_ptr. |
| 406 // The input parameter must be allocated with an allocator that matches the | 650 // The input parameter must be allocated with an allocator that matches the |
| 407 // Free functor. For the default Free functor, this is malloc, calloc, or | 651 // Free functor. For the default Free functor, this is malloc, calloc, or |
| 408 // realloc. | 652 // realloc. |
| 409 explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {} | 653 explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {} |
| 410 | 654 |
| 411 // Constructor. Move constructor for C++03 move emulation of this type. | 655 // Constructor. Move constructor for C++03 move emulation of this type. |
| 412 scoped_ptr_malloc(RValue rvalue) | 656 scoped_ptr_malloc(RValue rvalue) |
| 413 : ptr_(rvalue.object->release()) { | 657 : ptr_(rvalue.object->release()) { |
| 414 } | 658 } |
| 415 | 659 |
| 416 // Destructor. If there is a C object, call the Free functor. | 660 // Destructor. If there is a C object, call the Free functor. |
| 417 ~scoped_ptr_malloc() { | 661 ~scoped_ptr_malloc() { |
| 418 reset(); | 662 reset(); |
| 419 } | 663 } |
| 420 | 664 |
| 421 // operator=. Move operator= for C++03 move emulation of this type. | 665 // operator=. Move operator= for C++03 move emulation of this type. |
| 422 scoped_ptr_malloc& operator=(RValue rhs) { | 666 scoped_ptr_malloc& operator=(RValue rhs) { |
| 423 swap(*rhs.object); | 667 reset(rhs.object->release()); |
| 424 return *this; | 668 return *this; |
| 425 } | 669 } |
| 426 | 670 |
| 427 // Reset. Calls the Free functor on the current owned object, if any. | 671 // Reset. Calls the Free functor on the current owned object, if any. |
| 428 // Then takes ownership of a new object, if given. | 672 // Then takes ownership of a new object, if given. |
| 429 // this->reset(this->get()) works. | 673 // this->reset(this->get()) works. |
| 430 void reset(C* p = NULL) { | 674 void reset(C* p = NULL) { |
| 431 if (ptr_ != p) { | 675 if (ptr_ != p) { |
| 432 FreeProc free_proc; | 676 if (ptr_ != NULL) { |
| 433 free_proc(ptr_); | 677 FreeProc free_proc; |
| 678 free_proc(ptr_); | |
| 679 } | |
| 434 ptr_ = p; | 680 ptr_ = p; |
| 435 } | 681 } |
| 436 } | 682 } |
| 437 | 683 |
| 438 // Get the current object. | 684 // Get the current object. |
| 439 // operator* and operator-> will cause an assert() failure if there is | 685 // operator* and operator-> will cause an assert() failure if there is |
| 440 // no current object. | 686 // no current object. |
| 441 C& operator*() const { | 687 C& operator*() const { |
| 442 assert(ptr_ != NULL); | 688 assert(ptr_ != NULL); |
| 443 return *ptr_; | 689 return *ptr_; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 515 | 761 |
| 516 // A function to convert T* into scoped_ptr<T> | 762 // A function to convert T* into scoped_ptr<T> |
| 517 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 763 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
| 518 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 764 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
| 519 template <typename T> | 765 template <typename T> |
| 520 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 766 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
| 521 return scoped_ptr<T>(ptr); | 767 return scoped_ptr<T>(ptr); |
| 522 } | 768 } |
| 523 | 769 |
| 524 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 770 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
| OLD | NEW |