1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/memory/scoped_ptr.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,709 @@ 1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +// Scopers help you manage ownership of a pointer, helping you easily manage the 1.9 +// a pointer within a scope, and automatically destroying the pointer at the 1.10 +// end of a scope. There are two main classes you will use, which correspond 1.11 +// to the operators new/delete and new[]/delete[]. 1.12 +// 1.13 +// Example usage (scoped_ptr<T>): 1.14 +// { 1.15 +// scoped_ptr<Foo> foo(new Foo("wee")); 1.16 +// } // foo goes out of scope, releasing the pointer with it. 1.17 +// 1.18 +// { 1.19 +// scoped_ptr<Foo> foo; // No pointer managed. 1.20 +// foo.reset(new Foo("wee")); // Now a pointer is managed. 1.21 +// foo.reset(new Foo("wee2")); // Foo("wee") was destroyed. 1.22 +// foo.reset(new Foo("wee3")); // Foo("wee2") was destroyed. 1.23 +// foo->Method(); // Foo::Method() called. 1.24 +// foo.get()->Method(); // Foo::Method() called. 1.25 +// SomeFunc(foo.release()); // SomeFunc takes ownership, foo no longer 1.26 +// // manages a pointer. 1.27 +// foo.reset(new Foo("wee4")); // foo manages a pointer again. 1.28 +// foo.reset(); // Foo("wee4") destroyed, foo no longer 1.29 +// // manages a pointer. 1.30 +// } // foo wasn't managing a pointer, so nothing was destroyed. 1.31 +// 1.32 +// Example usage (scoped_ptr<T[]>): 1.33 +// { 1.34 +// scoped_ptr<Foo[]> foo(new Foo[100]); 1.35 +// foo.get()->Method(); // Foo::Method on the 0th element. 1.36 +// foo[10].Method(); // Foo::Method on the 10th element. 1.37 +// } 1.38 +// 1.39 +// These scopers also implement part of the functionality of C++11 unique_ptr 1.40 +// in that they are "movable but not copyable." You can use the scopers in 1.41 +// the parameter and return types of functions to signify ownership transfer 1.42 +// in to and out of a function. When calling a function that has a scoper 1.43 +// as the argument type, it must be called with the result of an analogous 1.44 +// scoper's Pass() function or another function that generates a temporary; 1.45 +// passing by copy will NOT work. Here is an example using scoped_ptr: 1.46 +// 1.47 +// void TakesOwnership(scoped_ptr<Foo> arg) { 1.48 +// // Do something with arg 1.49 +// } 1.50 +// scoped_ptr<Foo> CreateFoo() { 1.51 +// // No need for calling Pass() because we are constructing a temporary 1.52 +// // for the return value. 1.53 +// return scoped_ptr<Foo>(new Foo("new")); 1.54 +// } 1.55 +// scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) { 1.56 +// return arg.Pass(); 1.57 +// } 1.58 +// 1.59 +// { 1.60 +// scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay"). 1.61 +// TakesOwnership(ptr.Pass()); // ptr no longer owns Foo("yay"). 1.62 +// scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo. 1.63 +// scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2. 1.64 +// PassThru(ptr2.Pass()); // ptr2 is correspondingly NULL. 1.65 +// } 1.66 +// 1.67 +// Notice that if you do not call Pass() when returning from PassThru(), or 1.68 +// when invoking TakesOwnership(), the code will not compile because scopers 1.69 +// are not copyable; they only implement move semantics which require calling 1.70 +// the Pass() function to signify a destructive transfer of state. CreateFoo() 1.71 +// is different though because we are constructing a temporary on the return 1.72 +// line and thus can avoid needing to call Pass(). 1.73 +// 1.74 +// Pass() properly handles upcast in assignment, i.e. you can assign 1.75 +// scoped_ptr<Child> to scoped_ptr<Parent>: 1.76 +// 1.77 +// scoped_ptr<Foo> foo(new Foo()); 1.78 +// scoped_ptr<FooParent> parent = foo.Pass(); 1.79 +// 1.80 +// PassAs<>() should be used to upcast return value in return statement: 1.81 +// 1.82 +// scoped_ptr<Foo> CreateFoo() { 1.83 +// scoped_ptr<FooChild> result(new FooChild()); 1.84 +// return result.PassAs<Foo>(); 1.85 +// } 1.86 +// 1.87 +// Note that PassAs<>() is implemented only for scoped_ptr<T>, but not for 1.88 +// scoped_ptr<T[]>. This is because casting array pointers may not be safe. 1.89 + 1.90 +#ifndef BASE_MEMORY_SCOPED_PTR_H_ 1.91 +#define BASE_MEMORY_SCOPED_PTR_H_ 1.92 + 1.93 +// This is an implementation designed to match the anticipated future TR2 1.94 +// implementation of the scoped_ptr class and scoped_ptr_malloc (deprecated). 1.95 + 1.96 +#include <assert.h> 1.97 +#include <stddef.h> 1.98 +#include <stdlib.h> 1.99 + 1.100 +#include <algorithm> // For std::swap(). 1.101 + 1.102 +#include "base/basictypes.h" 1.103 +#include "base/compiler_specific.h" 1.104 +#include "base/move.h" 1.105 +#include "base/template_util.h" 1.106 + 1.107 +namespace base { 1.108 + 1.109 +namespace subtle { 1.110 +class RefCountedBase; 1.111 +class RefCountedThreadSafeBase; 1.112 +} // namespace subtle 1.113 + 1.114 +// Function object which deletes its parameter, which must be a pointer. 1.115 +// If C is an array type, invokes 'delete[]' on the parameter; otherwise, 1.116 +// invokes 'delete'. The default deleter for scoped_ptr<T>. 1.117 +template <class T> 1.118 +struct DefaultDeleter { 1.119 + DefaultDeleter() {} 1.120 + template <typename U> DefaultDeleter(const DefaultDeleter<U>& other) { 1.121 + // IMPLEMENTATION NOTE: C++11 20.7.1.1.2p2 only provides this constructor 1.122 + // if U* is implicitly convertible to T* and U is not an array type. 1.123 + // 1.124 + // Correct implementation should use SFINAE to disable this 1.125 + // constructor. However, since there are no other 1-argument constructors, 1.126 + // using a COMPILE_ASSERT() based on is_convertible<> and requiring 1.127 + // complete types is simpler and will cause compile failures for equivalent 1.128 + // misuses. 1.129 + // 1.130 + // Note, the is_convertible<U*, T*> check also ensures that U is not an 1.131 + // array. T is guaranteed to be a non-array, so any U* where U is an array 1.132 + // cannot convert to T*. 1.133 + enum { T_must_be_complete = sizeof(T) }; 1.134 + enum { U_must_be_complete = sizeof(U) }; 1.135 + COMPILE_ASSERT((base::is_convertible<U*, T*>::value), 1.136 + U_ptr_must_implicitly_convert_to_T_ptr); 1.137 + } 1.138 + inline void operator()(T* ptr) const { 1.139 + enum { type_must_be_complete = sizeof(T) }; 1.140 + delete ptr; 1.141 + } 1.142 +}; 1.143 + 1.144 +// Specialization of DefaultDeleter for array types. 1.145 +template <class T> 1.146 +struct DefaultDeleter<T[]> { 1.147 + inline void operator()(T* ptr) const { 1.148 + enum { type_must_be_complete = sizeof(T) }; 1.149 + delete[] ptr; 1.150 + } 1.151 + 1.152 + private: 1.153 + // Disable this operator for any U != T because it is undefined to execute 1.154 + // an array delete when the static type of the array mismatches the dynamic 1.155 + // type. 1.156 + // 1.157 + // References: 1.158 + // C++98 [expr.delete]p3 1.159 + // http://cplusplus.github.com/LWG/lwg-defects.html#938 1.160 + template <typename U> void operator()(U* array) const; 1.161 +}; 1.162 + 1.163 +template <class T, int n> 1.164 +struct DefaultDeleter<T[n]> { 1.165 + // Never allow someone to declare something like scoped_ptr<int[10]>. 1.166 + COMPILE_ASSERT(sizeof(T) == -1, do_not_use_array_with_size_as_type); 1.167 +}; 1.168 + 1.169 +// Function object which invokes 'free' on its parameter, which must be 1.170 +// a pointer. Can be used to store malloc-allocated pointers in scoped_ptr: 1.171 +// 1.172 +// scoped_ptr<int, base::FreeDeleter> foo_ptr( 1.173 +// static_cast<int*>(malloc(sizeof(int)))); 1.174 +struct FreeDeleter { 1.175 + inline void operator()(void* ptr) const { 1.176 + free(ptr); 1.177 + } 1.178 +}; 1.179 + 1.180 +namespace internal { 1.181 + 1.182 +template <typename T> struct IsNotRefCounted { 1.183 + enum { 1.184 + value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value && 1.185 + !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>:: 1.186 + value 1.187 + }; 1.188 +}; 1.189 + 1.190 +// Minimal implementation of the core logic of scoped_ptr, suitable for 1.191 +// reuse in both scoped_ptr and its specializations. 1.192 +template <class T, class D> 1.193 +class scoped_ptr_impl { 1.194 + public: 1.195 + explicit scoped_ptr_impl(T* p) : data_(p) { } 1.196 + 1.197 + // Initializer for deleters that have data parameters. 1.198 + scoped_ptr_impl(T* p, const D& d) : data_(p, d) {} 1.199 + 1.200 + // Templated constructor that destructively takes the value from another 1.201 + // scoped_ptr_impl. 1.202 + template <typename U, typename V> 1.203 + scoped_ptr_impl(scoped_ptr_impl<U, V>* other) 1.204 + : data_(other->release(), other->get_deleter()) { 1.205 + // We do not support move-only deleters. We could modify our move 1.206 + // emulation to have base::subtle::move() and base::subtle::forward() 1.207 + // functions that are imperfect emulations of their C++11 equivalents, 1.208 + // but until there's a requirement, just assume deleters are copyable. 1.209 + } 1.210 + 1.211 + template <typename U, typename V> 1.212 + void TakeState(scoped_ptr_impl<U, V>* other) { 1.213 + // See comment in templated constructor above regarding lack of support 1.214 + // for move-only deleters. 1.215 + reset(other->release()); 1.216 + get_deleter() = other->get_deleter(); 1.217 + } 1.218 + 1.219 + ~scoped_ptr_impl() { 1.220 + if (data_.ptr != NULL) { 1.221 + // Not using get_deleter() saves one function call in non-optimized 1.222 + // builds. 1.223 + static_cast<D&>(data_)(data_.ptr); 1.224 + } 1.225 + } 1.226 + 1.227 + void reset(T* p) { 1.228 + // This is a self-reset, which is no longer allowed: http://crbug.com/162971 1.229 + if (p != NULL && p == data_.ptr) 1.230 + abort(); 1.231 + 1.232 + // Note that running data_.ptr = p can lead to undefined behavior if 1.233 + // get_deleter()(get()) deletes this. In order to pevent this, reset() 1.234 + // should update the stored pointer before deleting its old value. 1.235 + // 1.236 + // However, changing reset() to use that behavior may cause current code to 1.237 + // break in unexpected ways. If the destruction of the owned object 1.238 + // dereferences the scoped_ptr when it is destroyed by a call to reset(), 1.239 + // then it will incorrectly dispatch calls to |p| rather than the original 1.240 + // value of |data_.ptr|. 1.241 + // 1.242 + // During the transition period, set the stored pointer to NULL while 1.243 + // deleting the object. Eventually, this safety check will be removed to 1.244 + // prevent the scenario initially described from occuring and 1.245 + // http://crbug.com/176091 can be closed. 1.246 + T* old = data_.ptr; 1.247 + data_.ptr = NULL; 1.248 + if (old != NULL) 1.249 + static_cast<D&>(data_)(old); 1.250 + data_.ptr = p; 1.251 + } 1.252 + 1.253 + T* get() const { return data_.ptr; } 1.254 + 1.255 + D& get_deleter() { return data_; } 1.256 + const D& get_deleter() const { return data_; } 1.257 + 1.258 + void swap(scoped_ptr_impl& p2) { 1.259 + // Standard swap idiom: 'using std::swap' ensures that std::swap is 1.260 + // present in the overload set, but we call swap unqualified so that 1.261 + // any more-specific overloads can be used, if available. 1.262 + using std::swap; 1.263 + swap(static_cast<D&>(data_), static_cast<D&>(p2.data_)); 1.264 + swap(data_.ptr, p2.data_.ptr); 1.265 + } 1.266 + 1.267 + T* release() { 1.268 + T* old_ptr = data_.ptr; 1.269 + data_.ptr = NULL; 1.270 + return old_ptr; 1.271 + } 1.272 + 1.273 + private: 1.274 + // Needed to allow type-converting constructor. 1.275 + template <typename U, typename V> friend class scoped_ptr_impl; 1.276 + 1.277 + // Use the empty base class optimization to allow us to have a D 1.278 + // member, while avoiding any space overhead for it when D is an 1.279 + // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good 1.280 + // discussion of this technique. 1.281 + struct Data : public D { 1.282 + explicit Data(T* ptr_in) : ptr(ptr_in) {} 1.283 + Data(T* ptr_in, const D& other) : D(other), ptr(ptr_in) {} 1.284 + T* ptr; 1.285 + }; 1.286 + 1.287 + Data data_; 1.288 + 1.289 + DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl); 1.290 +}; 1.291 + 1.292 +} // namespace internal 1.293 + 1.294 +} // namespace base 1.295 + 1.296 +// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> 1.297 +// automatically deletes the pointer it holds (if any). 1.298 +// That is, scoped_ptr<T> owns the T object that it points to. 1.299 +// Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object. 1.300 +// Also like T*, scoped_ptr<T> is thread-compatible, and once you 1.301 +// dereference it, you get the thread safety guarantees of T. 1.302 +// 1.303 +// The size of scoped_ptr is small. On most compilers, when using the 1.304 +// DefaultDeleter, sizeof(scoped_ptr<T>) == sizeof(T*). Custom deleters will 1.305 +// increase the size proportional to whatever state they need to have. See 1.306 +// comments inside scoped_ptr_impl<> for details. 1.307 +// 1.308 +// Current implementation targets having a strict subset of C++11's 1.309 +// unique_ptr<> features. Known deficiencies include not supporting move-only 1.310 +// deleteres, function pointers as deleters, and deleters with reference 1.311 +// types. 1.312 +template <class T, class D = base::DefaultDeleter<T> > 1.313 +class scoped_ptr { 1.314 + MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) 1.315 + 1.316 + COMPILE_ASSERT(base::internal::IsNotRefCounted<T>::value, 1.317 + T_is_refcounted_type_and_needs_scoped_refptr); 1.318 + 1.319 + public: 1.320 + // The element and deleter types. 1.321 + typedef T element_type; 1.322 + typedef D deleter_type; 1.323 + 1.324 + // Constructor. Defaults to initializing with NULL. 1.325 + scoped_ptr() : impl_(NULL) { } 1.326 + 1.327 + // Constructor. Takes ownership of p. 1.328 + explicit scoped_ptr(element_type* p) : impl_(p) { } 1.329 + 1.330 + // Constructor. Allows initialization of a stateful deleter. 1.331 + scoped_ptr(element_type* p, const D& d) : impl_(p, d) { } 1.332 + 1.333 + // Constructor. Allows construction from a scoped_ptr rvalue for a 1.334 + // convertible type and deleter. 1.335 + // 1.336 + // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this constructor distinct 1.337 + // from the normal move constructor. By C++11 20.7.1.2.1.21, this constructor 1.338 + // has different post-conditions if D is a reference type. Since this 1.339 + // implementation does not support deleters with reference type, 1.340 + // we do not need a separate move constructor allowing us to avoid one 1.341 + // use of SFINAE. You only need to care about this if you modify the 1.342 + // implementation of scoped_ptr. 1.343 + template <typename U, typename V> 1.344 + scoped_ptr(scoped_ptr<U, V> other) : impl_(&other.impl_) { 1.345 + COMPILE_ASSERT(!base::is_array<U>::value, U_cannot_be_an_array); 1.346 + } 1.347 + 1.348 + // Constructor. Move constructor for C++03 move emulation of this type. 1.349 + scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { } 1.350 + 1.351 + // operator=. Allows assignment from a scoped_ptr rvalue for a convertible 1.352 + // type and deleter. 1.353 + // 1.354 + // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this operator= distinct from 1.355 + // the normal move assignment operator. By C++11 20.7.1.2.3.4, this templated 1.356 + // form has different requirements on for move-only Deleters. Since this 1.357 + // implementation does not support move-only Deleters, we do not need a 1.358 + // separate move assignment operator allowing us to avoid one use of SFINAE. 1.359 + // You only need to care about this if you modify the implementation of 1.360 + // scoped_ptr. 1.361 + template <typename U, typename V> 1.362 + scoped_ptr& operator=(scoped_ptr<U, V> rhs) { 1.363 + COMPILE_ASSERT(!base::is_array<U>::value, U_cannot_be_an_array); 1.364 + impl_.TakeState(&rhs.impl_); 1.365 + return *this; 1.366 + } 1.367 + 1.368 + // Reset. Deletes the currently owned object, if any. 1.369 + // Then takes ownership of a new object, if given. 1.370 + void reset(element_type* p = NULL) { impl_.reset(p); } 1.371 + 1.372 + // Accessors to get the owned object. 1.373 + // operator* and operator-> will assert() if there is no current object. 1.374 + element_type& operator*() const { 1.375 + assert(impl_.get() != NULL); 1.376 + return *impl_.get(); 1.377 + } 1.378 + element_type* operator->() const { 1.379 + assert(impl_.get() != NULL); 1.380 + return impl_.get(); 1.381 + } 1.382 + element_type* get() const { return impl_.get(); } 1.383 + 1.384 + // Access to the deleter. 1.385 + deleter_type& get_deleter() { return impl_.get_deleter(); } 1.386 + const deleter_type& get_deleter() const { return impl_.get_deleter(); } 1.387 + 1.388 + // Allow scoped_ptr<element_type> to be used in boolean expressions, but not 1.389 + // implicitly convertible to a real bool (which is dangerous). 1.390 + // 1.391 + // Note that this trick is only safe when the == and != operators 1.392 + // are declared explicitly, as otherwise "scoped_ptr1 == 1.393 + // scoped_ptr2" will compile but do the wrong thing (i.e., convert 1.394 + // to Testable and then do the comparison). 1.395 + private: 1.396 + typedef base::internal::scoped_ptr_impl<element_type, deleter_type> 1.397 + scoped_ptr::*Testable; 1.398 + 1.399 + public: 1.400 + operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; } 1.401 + 1.402 + // Comparison operators. 1.403 + // These return whether two scoped_ptr refer to the same object, not just to 1.404 + // two different but equal objects. 1.405 + bool operator==(const element_type* p) const { return impl_.get() == p; } 1.406 + bool operator!=(const element_type* p) const { return impl_.get() != p; } 1.407 + 1.408 + // Swap two scoped pointers. 1.409 + void swap(scoped_ptr& p2) { 1.410 + impl_.swap(p2.impl_); 1.411 + } 1.412 + 1.413 + // Release a pointer. 1.414 + // The return value is the current pointer held by this object. 1.415 + // If this object holds a NULL pointer, the return value is NULL. 1.416 + // After this operation, this object will hold a NULL pointer, 1.417 + // and will not own the object any more. 1.418 + element_type* release() WARN_UNUSED_RESULT { 1.419 + return impl_.release(); 1.420 + } 1.421 + 1.422 + // C++98 doesn't support functions templates with default parameters which 1.423 + // makes it hard to write a PassAs() that understands converting the deleter 1.424 + // while preserving simple calling semantics. 1.425 + // 1.426 + // Until there is a use case for PassAs() with custom deleters, just ignore 1.427 + // the custom deleter. 1.428 + template <typename PassAsType> 1.429 + scoped_ptr<PassAsType> PassAs() { 1.430 + return scoped_ptr<PassAsType>(Pass()); 1.431 + } 1.432 + 1.433 + private: 1.434 + // Needed to reach into |impl_| in the constructor. 1.435 + template <typename U, typename V> friend class scoped_ptr; 1.436 + base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; 1.437 + 1.438 + // Forbidden for API compatibility with std::unique_ptr. 1.439 + explicit scoped_ptr(int disallow_construction_from_null); 1.440 + 1.441 + // Forbid comparison of scoped_ptr types. If U != T, it totally 1.442 + // doesn't make sense, and if U == T, it still doesn't make sense 1.443 + // because you should never have the same object owned by two different 1.444 + // scoped_ptrs. 1.445 + template <class U> bool operator==(scoped_ptr<U> const& p2) const; 1.446 + template <class U> bool operator!=(scoped_ptr<U> const& p2) const; 1.447 +}; 1.448 + 1.449 +template <class T, class D> 1.450 +class scoped_ptr<T[], D> { 1.451 + MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) 1.452 + 1.453 + public: 1.454 + // The element and deleter types. 1.455 + typedef T element_type; 1.456 + typedef D deleter_type; 1.457 + 1.458 + // Constructor. Defaults to initializing with NULL. 1.459 + scoped_ptr() : impl_(NULL) { } 1.460 + 1.461 + // Constructor. Stores the given array. Note that the argument's type 1.462 + // must exactly match T*. In particular: 1.463 + // - it cannot be a pointer to a type derived from T, because it is 1.464 + // inherently unsafe in the general case to access an array through a 1.465 + // pointer whose dynamic type does not match its static type (eg., if 1.466 + // T and the derived types had different sizes access would be 1.467 + // incorrectly calculated). Deletion is also always undefined 1.468 + // (C++98 [expr.delete]p3). If you're doing this, fix your code. 1.469 + // - it cannot be NULL, because NULL is an integral expression, not a 1.470 + // pointer to T. Use the no-argument version instead of explicitly 1.471 + // passing NULL. 1.472 + // - it cannot be const-qualified differently from T per unique_ptr spec 1.473 + // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting 1.474 + // to work around this may use implicit_cast<const T*>(). 1.475 + // However, because of the first bullet in this comment, users MUST 1.476 + // NOT use implicit_cast<Base*>() to upcast the static type of the array. 1.477 + explicit scoped_ptr(element_type* array) : impl_(array) { } 1.478 + 1.479 + // Constructor. Move constructor for C++03 move emulation of this type. 1.480 + scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { } 1.481 + 1.482 + // operator=. Move operator= for C++03 move emulation of this type. 1.483 + scoped_ptr& operator=(RValue rhs) { 1.484 + impl_.TakeState(&rhs.object->impl_); 1.485 + return *this; 1.486 + } 1.487 + 1.488 + // Reset. Deletes the currently owned array, if any. 1.489 + // Then takes ownership of a new object, if given. 1.490 + void reset(element_type* array = NULL) { impl_.reset(array); } 1.491 + 1.492 + // Accessors to get the owned array. 1.493 + element_type& operator[](size_t i) const { 1.494 + assert(impl_.get() != NULL); 1.495 + return impl_.get()[i]; 1.496 + } 1.497 + element_type* get() const { return impl_.get(); } 1.498 + 1.499 + // Access to the deleter. 1.500 + deleter_type& get_deleter() { return impl_.get_deleter(); } 1.501 + const deleter_type& get_deleter() const { return impl_.get_deleter(); } 1.502 + 1.503 + // Allow scoped_ptr<element_type> to be used in boolean expressions, but not 1.504 + // implicitly convertible to a real bool (which is dangerous). 1.505 + private: 1.506 + typedef base::internal::scoped_ptr_impl<element_type, deleter_type> 1.507 + scoped_ptr::*Testable; 1.508 + 1.509 + public: 1.510 + operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; } 1.511 + 1.512 + // Comparison operators. 1.513 + // These return whether two scoped_ptr refer to the same object, not just to 1.514 + // two different but equal objects. 1.515 + bool operator==(element_type* array) const { return impl_.get() == array; } 1.516 + bool operator!=(element_type* array) const { return impl_.get() != array; } 1.517 + 1.518 + // Swap two scoped pointers. 1.519 + void swap(scoped_ptr& p2) { 1.520 + impl_.swap(p2.impl_); 1.521 + } 1.522 + 1.523 + // Release a pointer. 1.524 + // The return value is the current pointer held by this object. 1.525 + // If this object holds a NULL pointer, the return value is NULL. 1.526 + // After this operation, this object will hold a NULL pointer, 1.527 + // and will not own the object any more. 1.528 + element_type* release() WARN_UNUSED_RESULT { 1.529 + return impl_.release(); 1.530 + } 1.531 + 1.532 + private: 1.533 + // Force element_type to be a complete type. 1.534 + enum { type_must_be_complete = sizeof(element_type) }; 1.535 + 1.536 + // Actually hold the data. 1.537 + base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; 1.538 + 1.539 + // Disable initialization from any type other than element_type*, by 1.540 + // providing a constructor that matches such an initialization, but is 1.541 + // private and has no definition. This is disabled because it is not safe to 1.542 + // call delete[] on an array whose static type does not match its dynamic 1.543 + // type. 1.544 + template <typename U> explicit scoped_ptr(U* array); 1.545 + explicit scoped_ptr(int disallow_construction_from_null); 1.546 + 1.547 + // Disable reset() from any type other than element_type*, for the same 1.548 + // reasons as the constructor above. 1.549 + template <typename U> void reset(U* array); 1.550 + void reset(int disallow_reset_from_null); 1.551 + 1.552 + // Forbid comparison of scoped_ptr types. If U != T, it totally 1.553 + // doesn't make sense, and if U == T, it still doesn't make sense 1.554 + // because you should never have the same object owned by two different 1.555 + // scoped_ptrs. 1.556 + template <class U> bool operator==(scoped_ptr<U> const& p2) const; 1.557 + template <class U> bool operator!=(scoped_ptr<U> const& p2) const; 1.558 +}; 1.559 + 1.560 +// Free functions 1.561 +template <class T, class D> 1.562 +void swap(scoped_ptr<T, D>& p1, scoped_ptr<T, D>& p2) { 1.563 + p1.swap(p2); 1.564 +} 1.565 + 1.566 +template <class T, class D> 1.567 +bool operator==(T* p1, const scoped_ptr<T, D>& p2) { 1.568 + return p1 == p2.get(); 1.569 +} 1.570 + 1.571 +template <class T, class D> 1.572 +bool operator!=(T* p1, const scoped_ptr<T, D>& p2) { 1.573 + return p1 != p2.get(); 1.574 +} 1.575 + 1.576 +// DEPRECATED: Use scoped_ptr<C, base::FreeDeleter> instead. 1.577 +// 1.578 +// scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a 1.579 +// second template argument, the functor used to free the object. 1.580 + 1.581 +template<class C, class FreeProc = base::FreeDeleter> 1.582 +class scoped_ptr_malloc { 1.583 + MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr_malloc, RValue) 1.584 + 1.585 + public: 1.586 + 1.587 + // The element type 1.588 + typedef C element_type; 1.589 + 1.590 + // Constructor. Defaults to initializing with NULL. 1.591 + // There is no way to create an uninitialized scoped_ptr. 1.592 + // The input parameter must be allocated with an allocator that matches the 1.593 + // Free functor. For the default Free functor, this is malloc, calloc, or 1.594 + // realloc. 1.595 + explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {} 1.596 + 1.597 + // Constructor. Move constructor for C++03 move emulation of this type. 1.598 + scoped_ptr_malloc(RValue rvalue) 1.599 + : ptr_(rvalue.object->release()) { 1.600 + } 1.601 + 1.602 + // Destructor. If there is a C object, call the Free functor. 1.603 + ~scoped_ptr_malloc() { 1.604 + reset(); 1.605 + } 1.606 + 1.607 + // operator=. Move operator= for C++03 move emulation of this type. 1.608 + scoped_ptr_malloc& operator=(RValue rhs) { 1.609 + reset(rhs.object->release()); 1.610 + return *this; 1.611 + } 1.612 + 1.613 + // Reset. Calls the Free functor on the current owned object, if any. 1.614 + // Then takes ownership of a new object, if given. 1.615 + // this->reset(this->get()) works. 1.616 + void reset(C* p = NULL) { 1.617 + if (ptr_ != p) { 1.618 + if (ptr_ != NULL) { 1.619 + FreeProc free_proc; 1.620 + free_proc(ptr_); 1.621 + } 1.622 + ptr_ = p; 1.623 + } 1.624 + } 1.625 + 1.626 + // Get the current object. 1.627 + // operator* and operator-> will cause an assert() failure if there is 1.628 + // no current object. 1.629 + C& operator*() const { 1.630 + assert(ptr_ != NULL); 1.631 + return *ptr_; 1.632 + } 1.633 + 1.634 + C* operator->() const { 1.635 + assert(ptr_ != NULL); 1.636 + return ptr_; 1.637 + } 1.638 + 1.639 + C* get() const { 1.640 + return ptr_; 1.641 + } 1.642 + 1.643 + // Allow scoped_ptr_malloc<C> to be used in boolean expressions, but not 1.644 + // implicitly convertible to a real bool (which is dangerous). 1.645 + typedef C* scoped_ptr_malloc::*Testable; 1.646 + operator Testable() const { return ptr_ ? &scoped_ptr_malloc::ptr_ : NULL; } 1.647 + 1.648 + // Comparison operators. 1.649 + // These return whether a scoped_ptr_malloc and a plain pointer refer 1.650 + // to the same object, not just to two different but equal objects. 1.651 + // For compatibility with the boost-derived implementation, these 1.652 + // take non-const arguments. 1.653 + bool operator==(C* p) const { 1.654 + return ptr_ == p; 1.655 + } 1.656 + 1.657 + bool operator!=(C* p) const { 1.658 + return ptr_ != p; 1.659 + } 1.660 + 1.661 + // Swap two scoped pointers. 1.662 + void swap(scoped_ptr_malloc & b) { 1.663 + C* tmp = b.ptr_; 1.664 + b.ptr_ = ptr_; 1.665 + ptr_ = tmp; 1.666 + } 1.667 + 1.668 + // Release a pointer. 1.669 + // The return value is the current pointer held by this object. 1.670 + // If this object holds a NULL pointer, the return value is NULL. 1.671 + // After this operation, this object will hold a NULL pointer, 1.672 + // and will not own the object any more. 1.673 + C* release() WARN_UNUSED_RESULT { 1.674 + C* tmp = ptr_; 1.675 + ptr_ = NULL; 1.676 + return tmp; 1.677 + } 1.678 + 1.679 + private: 1.680 + C* ptr_; 1.681 + 1.682 + // no reason to use these: each scoped_ptr_malloc should have its own object 1.683 + template <class C2, class GP> 1.684 + bool operator==(scoped_ptr_malloc<C2, GP> const& p) const; 1.685 + template <class C2, class GP> 1.686 + bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const; 1.687 +}; 1.688 + 1.689 +template<class C, class FP> inline 1.690 +void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) { 1.691 + a.swap(b); 1.692 +} 1.693 + 1.694 +template<class C, class FP> inline 1.695 +bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { 1.696 + return p == b.get(); 1.697 +} 1.698 + 1.699 +template<class C, class FP> inline 1.700 +bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { 1.701 + return p != b.get(); 1.702 +} 1.703 + 1.704 +// A function to convert T* into scoped_ptr<T> 1.705 +// Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation 1.706 +// for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) 1.707 +template <typename T> 1.708 +scoped_ptr<T> make_scoped_ptr(T* ptr) { 1.709 + return scoped_ptr<T>(ptr); 1.710 +} 1.711 + 1.712 +#endif // BASE_MEMORY_SCOPED_PTR_H_