Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | // Scopers help you manage ownership of a pointer, helping you easily manage the |
michael@0 | 6 | // a pointer within a scope, and automatically destroying the pointer at the |
michael@0 | 7 | // end of a scope. There are two main classes you will use, which correspond |
michael@0 | 8 | // to the operators new/delete and new[]/delete[]. |
michael@0 | 9 | // |
michael@0 | 10 | // Example usage (scoped_ptr<T>): |
michael@0 | 11 | // { |
michael@0 | 12 | // scoped_ptr<Foo> foo(new Foo("wee")); |
michael@0 | 13 | // } // foo goes out of scope, releasing the pointer with it. |
michael@0 | 14 | // |
michael@0 | 15 | // { |
michael@0 | 16 | // scoped_ptr<Foo> foo; // No pointer managed. |
michael@0 | 17 | // foo.reset(new Foo("wee")); // Now a pointer is managed. |
michael@0 | 18 | // foo.reset(new Foo("wee2")); // Foo("wee") was destroyed. |
michael@0 | 19 | // foo.reset(new Foo("wee3")); // Foo("wee2") was destroyed. |
michael@0 | 20 | // foo->Method(); // Foo::Method() called. |
michael@0 | 21 | // foo.get()->Method(); // Foo::Method() called. |
michael@0 | 22 | // SomeFunc(foo.release()); // SomeFunc takes ownership, foo no longer |
michael@0 | 23 | // // manages a pointer. |
michael@0 | 24 | // foo.reset(new Foo("wee4")); // foo manages a pointer again. |
michael@0 | 25 | // foo.reset(); // Foo("wee4") destroyed, foo no longer |
michael@0 | 26 | // // manages a pointer. |
michael@0 | 27 | // } // foo wasn't managing a pointer, so nothing was destroyed. |
michael@0 | 28 | // |
michael@0 | 29 | // Example usage (scoped_ptr<T[]>): |
michael@0 | 30 | // { |
michael@0 | 31 | // scoped_ptr<Foo[]> foo(new Foo[100]); |
michael@0 | 32 | // foo.get()->Method(); // Foo::Method on the 0th element. |
michael@0 | 33 | // foo[10].Method(); // Foo::Method on the 10th element. |
michael@0 | 34 | // } |
michael@0 | 35 | // |
michael@0 | 36 | // These scopers also implement part of the functionality of C++11 unique_ptr |
michael@0 | 37 | // in that they are "movable but not copyable." You can use the scopers in |
michael@0 | 38 | // the parameter and return types of functions to signify ownership transfer |
michael@0 | 39 | // in to and out of a function. When calling a function that has a scoper |
michael@0 | 40 | // as the argument type, it must be called with the result of an analogous |
michael@0 | 41 | // scoper's Pass() function or another function that generates a temporary; |
michael@0 | 42 | // passing by copy will NOT work. Here is an example using scoped_ptr: |
michael@0 | 43 | // |
michael@0 | 44 | // void TakesOwnership(scoped_ptr<Foo> arg) { |
michael@0 | 45 | // // Do something with arg |
michael@0 | 46 | // } |
michael@0 | 47 | // scoped_ptr<Foo> CreateFoo() { |
michael@0 | 48 | // // No need for calling Pass() because we are constructing a temporary |
michael@0 | 49 | // // for the return value. |
michael@0 | 50 | // return scoped_ptr<Foo>(new Foo("new")); |
michael@0 | 51 | // } |
michael@0 | 52 | // scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) { |
michael@0 | 53 | // return arg.Pass(); |
michael@0 | 54 | // } |
michael@0 | 55 | // |
michael@0 | 56 | // { |
michael@0 | 57 | // scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay"). |
michael@0 | 58 | // TakesOwnership(ptr.Pass()); // ptr no longer owns Foo("yay"). |
michael@0 | 59 | // scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo. |
michael@0 | 60 | // scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2. |
michael@0 | 61 | // PassThru(ptr2.Pass()); // ptr2 is correspondingly NULL. |
michael@0 | 62 | // } |
michael@0 | 63 | // |
michael@0 | 64 | // Notice that if you do not call Pass() when returning from PassThru(), or |
michael@0 | 65 | // when invoking TakesOwnership(), the code will not compile because scopers |
michael@0 | 66 | // are not copyable; they only implement move semantics which require calling |
michael@0 | 67 | // the Pass() function to signify a destructive transfer of state. CreateFoo() |
michael@0 | 68 | // is different though because we are constructing a temporary on the return |
michael@0 | 69 | // line and thus can avoid needing to call Pass(). |
michael@0 | 70 | // |
michael@0 | 71 | // Pass() properly handles upcast in assignment, i.e. you can assign |
michael@0 | 72 | // scoped_ptr<Child> to scoped_ptr<Parent>: |
michael@0 | 73 | // |
michael@0 | 74 | // scoped_ptr<Foo> foo(new Foo()); |
michael@0 | 75 | // scoped_ptr<FooParent> parent = foo.Pass(); |
michael@0 | 76 | // |
michael@0 | 77 | // PassAs<>() should be used to upcast return value in return statement: |
michael@0 | 78 | // |
michael@0 | 79 | // scoped_ptr<Foo> CreateFoo() { |
michael@0 | 80 | // scoped_ptr<FooChild> result(new FooChild()); |
michael@0 | 81 | // return result.PassAs<Foo>(); |
michael@0 | 82 | // } |
michael@0 | 83 | // |
michael@0 | 84 | // Note that PassAs<>() is implemented only for scoped_ptr<T>, but not for |
michael@0 | 85 | // scoped_ptr<T[]>. This is because casting array pointers may not be safe. |
michael@0 | 86 | |
michael@0 | 87 | #ifndef BASE_MEMORY_SCOPED_PTR_H_ |
michael@0 | 88 | #define BASE_MEMORY_SCOPED_PTR_H_ |
michael@0 | 89 | |
michael@0 | 90 | // This is an implementation designed to match the anticipated future TR2 |
michael@0 | 91 | // implementation of the scoped_ptr class and scoped_ptr_malloc (deprecated). |
michael@0 | 92 | |
michael@0 | 93 | #include <assert.h> |
michael@0 | 94 | #include <stddef.h> |
michael@0 | 95 | #include <stdlib.h> |
michael@0 | 96 | |
michael@0 | 97 | #include <algorithm> // For std::swap(). |
michael@0 | 98 | |
michael@0 | 99 | #include "base/basictypes.h" |
michael@0 | 100 | #include "base/compiler_specific.h" |
michael@0 | 101 | #include "base/move.h" |
michael@0 | 102 | #include "base/template_util.h" |
michael@0 | 103 | |
michael@0 | 104 | namespace base { |
michael@0 | 105 | |
michael@0 | 106 | namespace subtle { |
michael@0 | 107 | class RefCountedBase; |
michael@0 | 108 | class RefCountedThreadSafeBase; |
michael@0 | 109 | } // namespace subtle |
michael@0 | 110 | |
michael@0 | 111 | // Function object which deletes its parameter, which must be a pointer. |
michael@0 | 112 | // If C is an array type, invokes 'delete[]' on the parameter; otherwise, |
michael@0 | 113 | // invokes 'delete'. The default deleter for scoped_ptr<T>. |
michael@0 | 114 | template <class T> |
michael@0 | 115 | struct DefaultDeleter { |
michael@0 | 116 | DefaultDeleter() {} |
michael@0 | 117 | template <typename U> DefaultDeleter(const DefaultDeleter<U>& other) { |
michael@0 | 118 | // IMPLEMENTATION NOTE: C++11 20.7.1.1.2p2 only provides this constructor |
michael@0 | 119 | // if U* is implicitly convertible to T* and U is not an array type. |
michael@0 | 120 | // |
michael@0 | 121 | // Correct implementation should use SFINAE to disable this |
michael@0 | 122 | // constructor. However, since there are no other 1-argument constructors, |
michael@0 | 123 | // using a COMPILE_ASSERT() based on is_convertible<> and requiring |
michael@0 | 124 | // complete types is simpler and will cause compile failures for equivalent |
michael@0 | 125 | // misuses. |
michael@0 | 126 | // |
michael@0 | 127 | // Note, the is_convertible<U*, T*> check also ensures that U is not an |
michael@0 | 128 | // array. T is guaranteed to be a non-array, so any U* where U is an array |
michael@0 | 129 | // cannot convert to T*. |
michael@0 | 130 | enum { T_must_be_complete = sizeof(T) }; |
michael@0 | 131 | enum { U_must_be_complete = sizeof(U) }; |
michael@0 | 132 | COMPILE_ASSERT((base::is_convertible<U*, T*>::value), |
michael@0 | 133 | U_ptr_must_implicitly_convert_to_T_ptr); |
michael@0 | 134 | } |
michael@0 | 135 | inline void operator()(T* ptr) const { |
michael@0 | 136 | enum { type_must_be_complete = sizeof(T) }; |
michael@0 | 137 | delete ptr; |
michael@0 | 138 | } |
michael@0 | 139 | }; |
michael@0 | 140 | |
michael@0 | 141 | // Specialization of DefaultDeleter for array types. |
michael@0 | 142 | template <class T> |
michael@0 | 143 | struct DefaultDeleter<T[]> { |
michael@0 | 144 | inline void operator()(T* ptr) const { |
michael@0 | 145 | enum { type_must_be_complete = sizeof(T) }; |
michael@0 | 146 | delete[] ptr; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | private: |
michael@0 | 150 | // Disable this operator for any U != T because it is undefined to execute |
michael@0 | 151 | // an array delete when the static type of the array mismatches the dynamic |
michael@0 | 152 | // type. |
michael@0 | 153 | // |
michael@0 | 154 | // References: |
michael@0 | 155 | // C++98 [expr.delete]p3 |
michael@0 | 156 | // http://cplusplus.github.com/LWG/lwg-defects.html#938 |
michael@0 | 157 | template <typename U> void operator()(U* array) const; |
michael@0 | 158 | }; |
michael@0 | 159 | |
michael@0 | 160 | template <class T, int n> |
michael@0 | 161 | struct DefaultDeleter<T[n]> { |
michael@0 | 162 | // Never allow someone to declare something like scoped_ptr<int[10]>. |
michael@0 | 163 | COMPILE_ASSERT(sizeof(T) == -1, do_not_use_array_with_size_as_type); |
michael@0 | 164 | }; |
michael@0 | 165 | |
michael@0 | 166 | // Function object which invokes 'free' on its parameter, which must be |
michael@0 | 167 | // a pointer. Can be used to store malloc-allocated pointers in scoped_ptr: |
michael@0 | 168 | // |
michael@0 | 169 | // scoped_ptr<int, base::FreeDeleter> foo_ptr( |
michael@0 | 170 | // static_cast<int*>(malloc(sizeof(int)))); |
michael@0 | 171 | struct FreeDeleter { |
michael@0 | 172 | inline void operator()(void* ptr) const { |
michael@0 | 173 | free(ptr); |
michael@0 | 174 | } |
michael@0 | 175 | }; |
michael@0 | 176 | |
michael@0 | 177 | namespace internal { |
michael@0 | 178 | |
michael@0 | 179 | template <typename T> struct IsNotRefCounted { |
michael@0 | 180 | enum { |
michael@0 | 181 | value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value && |
michael@0 | 182 | !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>:: |
michael@0 | 183 | value |
michael@0 | 184 | }; |
michael@0 | 185 | }; |
michael@0 | 186 | |
michael@0 | 187 | // Minimal implementation of the core logic of scoped_ptr, suitable for |
michael@0 | 188 | // reuse in both scoped_ptr and its specializations. |
michael@0 | 189 | template <class T, class D> |
michael@0 | 190 | class scoped_ptr_impl { |
michael@0 | 191 | public: |
michael@0 | 192 | explicit scoped_ptr_impl(T* p) : data_(p) { } |
michael@0 | 193 | |
michael@0 | 194 | // Initializer for deleters that have data parameters. |
michael@0 | 195 | scoped_ptr_impl(T* p, const D& d) : data_(p, d) {} |
michael@0 | 196 | |
michael@0 | 197 | // Templated constructor that destructively takes the value from another |
michael@0 | 198 | // scoped_ptr_impl. |
michael@0 | 199 | template <typename U, typename V> |
michael@0 | 200 | scoped_ptr_impl(scoped_ptr_impl<U, V>* other) |
michael@0 | 201 | : data_(other->release(), other->get_deleter()) { |
michael@0 | 202 | // We do not support move-only deleters. We could modify our move |
michael@0 | 203 | // emulation to have base::subtle::move() and base::subtle::forward() |
michael@0 | 204 | // functions that are imperfect emulations of their C++11 equivalents, |
michael@0 | 205 | // but until there's a requirement, just assume deleters are copyable. |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | template <typename U, typename V> |
michael@0 | 209 | void TakeState(scoped_ptr_impl<U, V>* other) { |
michael@0 | 210 | // See comment in templated constructor above regarding lack of support |
michael@0 | 211 | // for move-only deleters. |
michael@0 | 212 | reset(other->release()); |
michael@0 | 213 | get_deleter() = other->get_deleter(); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | ~scoped_ptr_impl() { |
michael@0 | 217 | if (data_.ptr != NULL) { |
michael@0 | 218 | // Not using get_deleter() saves one function call in non-optimized |
michael@0 | 219 | // builds. |
michael@0 | 220 | static_cast<D&>(data_)(data_.ptr); |
michael@0 | 221 | } |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | void reset(T* p) { |
michael@0 | 225 | // This is a self-reset, which is no longer allowed: http://crbug.com/162971 |
michael@0 | 226 | if (p != NULL && p == data_.ptr) |
michael@0 | 227 | abort(); |
michael@0 | 228 | |
michael@0 | 229 | // Note that running data_.ptr = p can lead to undefined behavior if |
michael@0 | 230 | // get_deleter()(get()) deletes this. In order to pevent this, reset() |
michael@0 | 231 | // should update the stored pointer before deleting its old value. |
michael@0 | 232 | // |
michael@0 | 233 | // However, changing reset() to use that behavior may cause current code to |
michael@0 | 234 | // break in unexpected ways. If the destruction of the owned object |
michael@0 | 235 | // dereferences the scoped_ptr when it is destroyed by a call to reset(), |
michael@0 | 236 | // then it will incorrectly dispatch calls to |p| rather than the original |
michael@0 | 237 | // value of |data_.ptr|. |
michael@0 | 238 | // |
michael@0 | 239 | // During the transition period, set the stored pointer to NULL while |
michael@0 | 240 | // deleting the object. Eventually, this safety check will be removed to |
michael@0 | 241 | // prevent the scenario initially described from occuring and |
michael@0 | 242 | // http://crbug.com/176091 can be closed. |
michael@0 | 243 | T* old = data_.ptr; |
michael@0 | 244 | data_.ptr = NULL; |
michael@0 | 245 | if (old != NULL) |
michael@0 | 246 | static_cast<D&>(data_)(old); |
michael@0 | 247 | data_.ptr = p; |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | T* get() const { return data_.ptr; } |
michael@0 | 251 | |
michael@0 | 252 | D& get_deleter() { return data_; } |
michael@0 | 253 | const D& get_deleter() const { return data_; } |
michael@0 | 254 | |
michael@0 | 255 | void swap(scoped_ptr_impl& p2) { |
michael@0 | 256 | // Standard swap idiom: 'using std::swap' ensures that std::swap is |
michael@0 | 257 | // present in the overload set, but we call swap unqualified so that |
michael@0 | 258 | // any more-specific overloads can be used, if available. |
michael@0 | 259 | using std::swap; |
michael@0 | 260 | swap(static_cast<D&>(data_), static_cast<D&>(p2.data_)); |
michael@0 | 261 | swap(data_.ptr, p2.data_.ptr); |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | T* release() { |
michael@0 | 265 | T* old_ptr = data_.ptr; |
michael@0 | 266 | data_.ptr = NULL; |
michael@0 | 267 | return old_ptr; |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | private: |
michael@0 | 271 | // Needed to allow type-converting constructor. |
michael@0 | 272 | template <typename U, typename V> friend class scoped_ptr_impl; |
michael@0 | 273 | |
michael@0 | 274 | // Use the empty base class optimization to allow us to have a D |
michael@0 | 275 | // member, while avoiding any space overhead for it when D is an |
michael@0 | 276 | // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good |
michael@0 | 277 | // discussion of this technique. |
michael@0 | 278 | struct Data : public D { |
michael@0 | 279 | explicit Data(T* ptr_in) : ptr(ptr_in) {} |
michael@0 | 280 | Data(T* ptr_in, const D& other) : D(other), ptr(ptr_in) {} |
michael@0 | 281 | T* ptr; |
michael@0 | 282 | }; |
michael@0 | 283 | |
michael@0 | 284 | Data data_; |
michael@0 | 285 | |
michael@0 | 286 | DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl); |
michael@0 | 287 | }; |
michael@0 | 288 | |
michael@0 | 289 | } // namespace internal |
michael@0 | 290 | |
michael@0 | 291 | } // namespace base |
michael@0 | 292 | |
michael@0 | 293 | // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> |
michael@0 | 294 | // automatically deletes the pointer it holds (if any). |
michael@0 | 295 | // That is, scoped_ptr<T> owns the T object that it points to. |
michael@0 | 296 | // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object. |
michael@0 | 297 | // Also like T*, scoped_ptr<T> is thread-compatible, and once you |
michael@0 | 298 | // dereference it, you get the thread safety guarantees of T. |
michael@0 | 299 | // |
michael@0 | 300 | // The size of scoped_ptr is small. On most compilers, when using the |
michael@0 | 301 | // DefaultDeleter, sizeof(scoped_ptr<T>) == sizeof(T*). Custom deleters will |
michael@0 | 302 | // increase the size proportional to whatever state they need to have. See |
michael@0 | 303 | // comments inside scoped_ptr_impl<> for details. |
michael@0 | 304 | // |
michael@0 | 305 | // Current implementation targets having a strict subset of C++11's |
michael@0 | 306 | // unique_ptr<> features. Known deficiencies include not supporting move-only |
michael@0 | 307 | // deleteres, function pointers as deleters, and deleters with reference |
michael@0 | 308 | // types. |
michael@0 | 309 | template <class T, class D = base::DefaultDeleter<T> > |
michael@0 | 310 | class scoped_ptr { |
michael@0 | 311 | MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) |
michael@0 | 312 | |
michael@0 | 313 | COMPILE_ASSERT(base::internal::IsNotRefCounted<T>::value, |
michael@0 | 314 | T_is_refcounted_type_and_needs_scoped_refptr); |
michael@0 | 315 | |
michael@0 | 316 | public: |
michael@0 | 317 | // The element and deleter types. |
michael@0 | 318 | typedef T element_type; |
michael@0 | 319 | typedef D deleter_type; |
michael@0 | 320 | |
michael@0 | 321 | // Constructor. Defaults to initializing with NULL. |
michael@0 | 322 | scoped_ptr() : impl_(NULL) { } |
michael@0 | 323 | |
michael@0 | 324 | // Constructor. Takes ownership of p. |
michael@0 | 325 | explicit scoped_ptr(element_type* p) : impl_(p) { } |
michael@0 | 326 | |
michael@0 | 327 | // Constructor. Allows initialization of a stateful deleter. |
michael@0 | 328 | scoped_ptr(element_type* p, const D& d) : impl_(p, d) { } |
michael@0 | 329 | |
michael@0 | 330 | // Constructor. Allows construction from a scoped_ptr rvalue for a |
michael@0 | 331 | // convertible type and deleter. |
michael@0 | 332 | // |
michael@0 | 333 | // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this constructor distinct |
michael@0 | 334 | // from the normal move constructor. By C++11 20.7.1.2.1.21, this constructor |
michael@0 | 335 | // has different post-conditions if D is a reference type. Since this |
michael@0 | 336 | // implementation does not support deleters with reference type, |
michael@0 | 337 | // we do not need a separate move constructor allowing us to avoid one |
michael@0 | 338 | // use of SFINAE. You only need to care about this if you modify the |
michael@0 | 339 | // implementation of scoped_ptr. |
michael@0 | 340 | template <typename U, typename V> |
michael@0 | 341 | scoped_ptr(scoped_ptr<U, V> other) : impl_(&other.impl_) { |
michael@0 | 342 | COMPILE_ASSERT(!base::is_array<U>::value, U_cannot_be_an_array); |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | // Constructor. Move constructor for C++03 move emulation of this type. |
michael@0 | 346 | scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { } |
michael@0 | 347 | |
michael@0 | 348 | // operator=. Allows assignment from a scoped_ptr rvalue for a convertible |
michael@0 | 349 | // type and deleter. |
michael@0 | 350 | // |
michael@0 | 351 | // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this operator= distinct from |
michael@0 | 352 | // the normal move assignment operator. By C++11 20.7.1.2.3.4, this templated |
michael@0 | 353 | // form has different requirements on for move-only Deleters. Since this |
michael@0 | 354 | // implementation does not support move-only Deleters, we do not need a |
michael@0 | 355 | // separate move assignment operator allowing us to avoid one use of SFINAE. |
michael@0 | 356 | // You only need to care about this if you modify the implementation of |
michael@0 | 357 | // scoped_ptr. |
michael@0 | 358 | template <typename U, typename V> |
michael@0 | 359 | scoped_ptr& operator=(scoped_ptr<U, V> rhs) { |
michael@0 | 360 | COMPILE_ASSERT(!base::is_array<U>::value, U_cannot_be_an_array); |
michael@0 | 361 | impl_.TakeState(&rhs.impl_); |
michael@0 | 362 | return *this; |
michael@0 | 363 | } |
michael@0 | 364 | |
michael@0 | 365 | // Reset. Deletes the currently owned object, if any. |
michael@0 | 366 | // Then takes ownership of a new object, if given. |
michael@0 | 367 | void reset(element_type* p = NULL) { impl_.reset(p); } |
michael@0 | 368 | |
michael@0 | 369 | // Accessors to get the owned object. |
michael@0 | 370 | // operator* and operator-> will assert() if there is no current object. |
michael@0 | 371 | element_type& operator*() const { |
michael@0 | 372 | assert(impl_.get() != NULL); |
michael@0 | 373 | return *impl_.get(); |
michael@0 | 374 | } |
michael@0 | 375 | element_type* operator->() const { |
michael@0 | 376 | assert(impl_.get() != NULL); |
michael@0 | 377 | return impl_.get(); |
michael@0 | 378 | } |
michael@0 | 379 | element_type* get() const { return impl_.get(); } |
michael@0 | 380 | |
michael@0 | 381 | // Access to the deleter. |
michael@0 | 382 | deleter_type& get_deleter() { return impl_.get_deleter(); } |
michael@0 | 383 | const deleter_type& get_deleter() const { return impl_.get_deleter(); } |
michael@0 | 384 | |
michael@0 | 385 | // Allow scoped_ptr<element_type> to be used in boolean expressions, but not |
michael@0 | 386 | // implicitly convertible to a real bool (which is dangerous). |
michael@0 | 387 | // |
michael@0 | 388 | // Note that this trick is only safe when the == and != operators |
michael@0 | 389 | // are declared explicitly, as otherwise "scoped_ptr1 == |
michael@0 | 390 | // scoped_ptr2" will compile but do the wrong thing (i.e., convert |
michael@0 | 391 | // to Testable and then do the comparison). |
michael@0 | 392 | private: |
michael@0 | 393 | typedef base::internal::scoped_ptr_impl<element_type, deleter_type> |
michael@0 | 394 | scoped_ptr::*Testable; |
michael@0 | 395 | |
michael@0 | 396 | public: |
michael@0 | 397 | operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; } |
michael@0 | 398 | |
michael@0 | 399 | // Comparison operators. |
michael@0 | 400 | // These return whether two scoped_ptr refer to the same object, not just to |
michael@0 | 401 | // two different but equal objects. |
michael@0 | 402 | bool operator==(const element_type* p) const { return impl_.get() == p; } |
michael@0 | 403 | bool operator!=(const element_type* p) const { return impl_.get() != p; } |
michael@0 | 404 | |
michael@0 | 405 | // Swap two scoped pointers. |
michael@0 | 406 | void swap(scoped_ptr& p2) { |
michael@0 | 407 | impl_.swap(p2.impl_); |
michael@0 | 408 | } |
michael@0 | 409 | |
michael@0 | 410 | // Release a pointer. |
michael@0 | 411 | // The return value is the current pointer held by this object. |
michael@0 | 412 | // If this object holds a NULL pointer, the return value is NULL. |
michael@0 | 413 | // After this operation, this object will hold a NULL pointer, |
michael@0 | 414 | // and will not own the object any more. |
michael@0 | 415 | element_type* release() WARN_UNUSED_RESULT { |
michael@0 | 416 | return impl_.release(); |
michael@0 | 417 | } |
michael@0 | 418 | |
michael@0 | 419 | // C++98 doesn't support functions templates with default parameters which |
michael@0 | 420 | // makes it hard to write a PassAs() that understands converting the deleter |
michael@0 | 421 | // while preserving simple calling semantics. |
michael@0 | 422 | // |
michael@0 | 423 | // Until there is a use case for PassAs() with custom deleters, just ignore |
michael@0 | 424 | // the custom deleter. |
michael@0 | 425 | template <typename PassAsType> |
michael@0 | 426 | scoped_ptr<PassAsType> PassAs() { |
michael@0 | 427 | return scoped_ptr<PassAsType>(Pass()); |
michael@0 | 428 | } |
michael@0 | 429 | |
michael@0 | 430 | private: |
michael@0 | 431 | // Needed to reach into |impl_| in the constructor. |
michael@0 | 432 | template <typename U, typename V> friend class scoped_ptr; |
michael@0 | 433 | base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; |
michael@0 | 434 | |
michael@0 | 435 | // Forbidden for API compatibility with std::unique_ptr. |
michael@0 | 436 | explicit scoped_ptr(int disallow_construction_from_null); |
michael@0 | 437 | |
michael@0 | 438 | // Forbid comparison of scoped_ptr types. If U != T, it totally |
michael@0 | 439 | // doesn't make sense, and if U == T, it still doesn't make sense |
michael@0 | 440 | // because you should never have the same object owned by two different |
michael@0 | 441 | // scoped_ptrs. |
michael@0 | 442 | template <class U> bool operator==(scoped_ptr<U> const& p2) const; |
michael@0 | 443 | template <class U> bool operator!=(scoped_ptr<U> const& p2) const; |
michael@0 | 444 | }; |
michael@0 | 445 | |
michael@0 | 446 | template <class T, class D> |
michael@0 | 447 | class scoped_ptr<T[], D> { |
michael@0 | 448 | MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) |
michael@0 | 449 | |
michael@0 | 450 | public: |
michael@0 | 451 | // The element and deleter types. |
michael@0 | 452 | typedef T element_type; |
michael@0 | 453 | typedef D deleter_type; |
michael@0 | 454 | |
michael@0 | 455 | // Constructor. Defaults to initializing with NULL. |
michael@0 | 456 | scoped_ptr() : impl_(NULL) { } |
michael@0 | 457 | |
michael@0 | 458 | // Constructor. Stores the given array. Note that the argument's type |
michael@0 | 459 | // must exactly match T*. In particular: |
michael@0 | 460 | // - it cannot be a pointer to a type derived from T, because it is |
michael@0 | 461 | // inherently unsafe in the general case to access an array through a |
michael@0 | 462 | // pointer whose dynamic type does not match its static type (eg., if |
michael@0 | 463 | // T and the derived types had different sizes access would be |
michael@0 | 464 | // incorrectly calculated). Deletion is also always undefined |
michael@0 | 465 | // (C++98 [expr.delete]p3). If you're doing this, fix your code. |
michael@0 | 466 | // - it cannot be NULL, because NULL is an integral expression, not a |
michael@0 | 467 | // pointer to T. Use the no-argument version instead of explicitly |
michael@0 | 468 | // passing NULL. |
michael@0 | 469 | // - it cannot be const-qualified differently from T per unique_ptr spec |
michael@0 | 470 | // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting |
michael@0 | 471 | // to work around this may use implicit_cast<const T*>(). |
michael@0 | 472 | // However, because of the first bullet in this comment, users MUST |
michael@0 | 473 | // NOT use implicit_cast<Base*>() to upcast the static type of the array. |
michael@0 | 474 | explicit scoped_ptr(element_type* array) : impl_(array) { } |
michael@0 | 475 | |
michael@0 | 476 | // Constructor. Move constructor for C++03 move emulation of this type. |
michael@0 | 477 | scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { } |
michael@0 | 478 | |
michael@0 | 479 | // operator=. Move operator= for C++03 move emulation of this type. |
michael@0 | 480 | scoped_ptr& operator=(RValue rhs) { |
michael@0 | 481 | impl_.TakeState(&rhs.object->impl_); |
michael@0 | 482 | return *this; |
michael@0 | 483 | } |
michael@0 | 484 | |
michael@0 | 485 | // Reset. Deletes the currently owned array, if any. |
michael@0 | 486 | // Then takes ownership of a new object, if given. |
michael@0 | 487 | void reset(element_type* array = NULL) { impl_.reset(array); } |
michael@0 | 488 | |
michael@0 | 489 | // Accessors to get the owned array. |
michael@0 | 490 | element_type& operator[](size_t i) const { |
michael@0 | 491 | assert(impl_.get() != NULL); |
michael@0 | 492 | return impl_.get()[i]; |
michael@0 | 493 | } |
michael@0 | 494 | element_type* get() const { return impl_.get(); } |
michael@0 | 495 | |
michael@0 | 496 | // Access to the deleter. |
michael@0 | 497 | deleter_type& get_deleter() { return impl_.get_deleter(); } |
michael@0 | 498 | const deleter_type& get_deleter() const { return impl_.get_deleter(); } |
michael@0 | 499 | |
michael@0 | 500 | // Allow scoped_ptr<element_type> to be used in boolean expressions, but not |
michael@0 | 501 | // implicitly convertible to a real bool (which is dangerous). |
michael@0 | 502 | private: |
michael@0 | 503 | typedef base::internal::scoped_ptr_impl<element_type, deleter_type> |
michael@0 | 504 | scoped_ptr::*Testable; |
michael@0 | 505 | |
michael@0 | 506 | public: |
michael@0 | 507 | operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; } |
michael@0 | 508 | |
michael@0 | 509 | // Comparison operators. |
michael@0 | 510 | // These return whether two scoped_ptr refer to the same object, not just to |
michael@0 | 511 | // two different but equal objects. |
michael@0 | 512 | bool operator==(element_type* array) const { return impl_.get() == array; } |
michael@0 | 513 | bool operator!=(element_type* array) const { return impl_.get() != array; } |
michael@0 | 514 | |
michael@0 | 515 | // Swap two scoped pointers. |
michael@0 | 516 | void swap(scoped_ptr& p2) { |
michael@0 | 517 | impl_.swap(p2.impl_); |
michael@0 | 518 | } |
michael@0 | 519 | |
michael@0 | 520 | // Release a pointer. |
michael@0 | 521 | // The return value is the current pointer held by this object. |
michael@0 | 522 | // If this object holds a NULL pointer, the return value is NULL. |
michael@0 | 523 | // After this operation, this object will hold a NULL pointer, |
michael@0 | 524 | // and will not own the object any more. |
michael@0 | 525 | element_type* release() WARN_UNUSED_RESULT { |
michael@0 | 526 | return impl_.release(); |
michael@0 | 527 | } |
michael@0 | 528 | |
michael@0 | 529 | private: |
michael@0 | 530 | // Force element_type to be a complete type. |
michael@0 | 531 | enum { type_must_be_complete = sizeof(element_type) }; |
michael@0 | 532 | |
michael@0 | 533 | // Actually hold the data. |
michael@0 | 534 | base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; |
michael@0 | 535 | |
michael@0 | 536 | // Disable initialization from any type other than element_type*, by |
michael@0 | 537 | // providing a constructor that matches such an initialization, but is |
michael@0 | 538 | // private and has no definition. This is disabled because it is not safe to |
michael@0 | 539 | // call delete[] on an array whose static type does not match its dynamic |
michael@0 | 540 | // type. |
michael@0 | 541 | template <typename U> explicit scoped_ptr(U* array); |
michael@0 | 542 | explicit scoped_ptr(int disallow_construction_from_null); |
michael@0 | 543 | |
michael@0 | 544 | // Disable reset() from any type other than element_type*, for the same |
michael@0 | 545 | // reasons as the constructor above. |
michael@0 | 546 | template <typename U> void reset(U* array); |
michael@0 | 547 | void reset(int disallow_reset_from_null); |
michael@0 | 548 | |
michael@0 | 549 | // Forbid comparison of scoped_ptr types. If U != T, it totally |
michael@0 | 550 | // doesn't make sense, and if U == T, it still doesn't make sense |
michael@0 | 551 | // because you should never have the same object owned by two different |
michael@0 | 552 | // scoped_ptrs. |
michael@0 | 553 | template <class U> bool operator==(scoped_ptr<U> const& p2) const; |
michael@0 | 554 | template <class U> bool operator!=(scoped_ptr<U> const& p2) const; |
michael@0 | 555 | }; |
michael@0 | 556 | |
michael@0 | 557 | // Free functions |
michael@0 | 558 | template <class T, class D> |
michael@0 | 559 | void swap(scoped_ptr<T, D>& p1, scoped_ptr<T, D>& p2) { |
michael@0 | 560 | p1.swap(p2); |
michael@0 | 561 | } |
michael@0 | 562 | |
michael@0 | 563 | template <class T, class D> |
michael@0 | 564 | bool operator==(T* p1, const scoped_ptr<T, D>& p2) { |
michael@0 | 565 | return p1 == p2.get(); |
michael@0 | 566 | } |
michael@0 | 567 | |
michael@0 | 568 | template <class T, class D> |
michael@0 | 569 | bool operator!=(T* p1, const scoped_ptr<T, D>& p2) { |
michael@0 | 570 | return p1 != p2.get(); |
michael@0 | 571 | } |
michael@0 | 572 | |
michael@0 | 573 | // DEPRECATED: Use scoped_ptr<C, base::FreeDeleter> instead. |
michael@0 | 574 | // |
michael@0 | 575 | // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a |
michael@0 | 576 | // second template argument, the functor used to free the object. |
michael@0 | 577 | |
michael@0 | 578 | template<class C, class FreeProc = base::FreeDeleter> |
michael@0 | 579 | class scoped_ptr_malloc { |
michael@0 | 580 | MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr_malloc, RValue) |
michael@0 | 581 | |
michael@0 | 582 | public: |
michael@0 | 583 | |
michael@0 | 584 | // The element type |
michael@0 | 585 | typedef C element_type; |
michael@0 | 586 | |
michael@0 | 587 | // Constructor. Defaults to initializing with NULL. |
michael@0 | 588 | // There is no way to create an uninitialized scoped_ptr. |
michael@0 | 589 | // The input parameter must be allocated with an allocator that matches the |
michael@0 | 590 | // Free functor. For the default Free functor, this is malloc, calloc, or |
michael@0 | 591 | // realloc. |
michael@0 | 592 | explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {} |
michael@0 | 593 | |
michael@0 | 594 | // Constructor. Move constructor for C++03 move emulation of this type. |
michael@0 | 595 | scoped_ptr_malloc(RValue rvalue) |
michael@0 | 596 | : ptr_(rvalue.object->release()) { |
michael@0 | 597 | } |
michael@0 | 598 | |
michael@0 | 599 | // Destructor. If there is a C object, call the Free functor. |
michael@0 | 600 | ~scoped_ptr_malloc() { |
michael@0 | 601 | reset(); |
michael@0 | 602 | } |
michael@0 | 603 | |
michael@0 | 604 | // operator=. Move operator= for C++03 move emulation of this type. |
michael@0 | 605 | scoped_ptr_malloc& operator=(RValue rhs) { |
michael@0 | 606 | reset(rhs.object->release()); |
michael@0 | 607 | return *this; |
michael@0 | 608 | } |
michael@0 | 609 | |
michael@0 | 610 | // Reset. Calls the Free functor on the current owned object, if any. |
michael@0 | 611 | // Then takes ownership of a new object, if given. |
michael@0 | 612 | // this->reset(this->get()) works. |
michael@0 | 613 | void reset(C* p = NULL) { |
michael@0 | 614 | if (ptr_ != p) { |
michael@0 | 615 | if (ptr_ != NULL) { |
michael@0 | 616 | FreeProc free_proc; |
michael@0 | 617 | free_proc(ptr_); |
michael@0 | 618 | } |
michael@0 | 619 | ptr_ = p; |
michael@0 | 620 | } |
michael@0 | 621 | } |
michael@0 | 622 | |
michael@0 | 623 | // Get the current object. |
michael@0 | 624 | // operator* and operator-> will cause an assert() failure if there is |
michael@0 | 625 | // no current object. |
michael@0 | 626 | C& operator*() const { |
michael@0 | 627 | assert(ptr_ != NULL); |
michael@0 | 628 | return *ptr_; |
michael@0 | 629 | } |
michael@0 | 630 | |
michael@0 | 631 | C* operator->() const { |
michael@0 | 632 | assert(ptr_ != NULL); |
michael@0 | 633 | return ptr_; |
michael@0 | 634 | } |
michael@0 | 635 | |
michael@0 | 636 | C* get() const { |
michael@0 | 637 | return ptr_; |
michael@0 | 638 | } |
michael@0 | 639 | |
michael@0 | 640 | // Allow scoped_ptr_malloc<C> to be used in boolean expressions, but not |
michael@0 | 641 | // implicitly convertible to a real bool (which is dangerous). |
michael@0 | 642 | typedef C* scoped_ptr_malloc::*Testable; |
michael@0 | 643 | operator Testable() const { return ptr_ ? &scoped_ptr_malloc::ptr_ : NULL; } |
michael@0 | 644 | |
michael@0 | 645 | // Comparison operators. |
michael@0 | 646 | // These return whether a scoped_ptr_malloc and a plain pointer refer |
michael@0 | 647 | // to the same object, not just to two different but equal objects. |
michael@0 | 648 | // For compatibility with the boost-derived implementation, these |
michael@0 | 649 | // take non-const arguments. |
michael@0 | 650 | bool operator==(C* p) const { |
michael@0 | 651 | return ptr_ == p; |
michael@0 | 652 | } |
michael@0 | 653 | |
michael@0 | 654 | bool operator!=(C* p) const { |
michael@0 | 655 | return ptr_ != p; |
michael@0 | 656 | } |
michael@0 | 657 | |
michael@0 | 658 | // Swap two scoped pointers. |
michael@0 | 659 | void swap(scoped_ptr_malloc & b) { |
michael@0 | 660 | C* tmp = b.ptr_; |
michael@0 | 661 | b.ptr_ = ptr_; |
michael@0 | 662 | ptr_ = tmp; |
michael@0 | 663 | } |
michael@0 | 664 | |
michael@0 | 665 | // Release a pointer. |
michael@0 | 666 | // The return value is the current pointer held by this object. |
michael@0 | 667 | // If this object holds a NULL pointer, the return value is NULL. |
michael@0 | 668 | // After this operation, this object will hold a NULL pointer, |
michael@0 | 669 | // and will not own the object any more. |
michael@0 | 670 | C* release() WARN_UNUSED_RESULT { |
michael@0 | 671 | C* tmp = ptr_; |
michael@0 | 672 | ptr_ = NULL; |
michael@0 | 673 | return tmp; |
michael@0 | 674 | } |
michael@0 | 675 | |
michael@0 | 676 | private: |
michael@0 | 677 | C* ptr_; |
michael@0 | 678 | |
michael@0 | 679 | // no reason to use these: each scoped_ptr_malloc should have its own object |
michael@0 | 680 | template <class C2, class GP> |
michael@0 | 681 | bool operator==(scoped_ptr_malloc<C2, GP> const& p) const; |
michael@0 | 682 | template <class C2, class GP> |
michael@0 | 683 | bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const; |
michael@0 | 684 | }; |
michael@0 | 685 | |
michael@0 | 686 | template<class C, class FP> inline |
michael@0 | 687 | void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) { |
michael@0 | 688 | a.swap(b); |
michael@0 | 689 | } |
michael@0 | 690 | |
michael@0 | 691 | template<class C, class FP> inline |
michael@0 | 692 | bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { |
michael@0 | 693 | return p == b.get(); |
michael@0 | 694 | } |
michael@0 | 695 | |
michael@0 | 696 | template<class C, class FP> inline |
michael@0 | 697 | bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { |
michael@0 | 698 | return p != b.get(); |
michael@0 | 699 | } |
michael@0 | 700 | |
michael@0 | 701 | // A function to convert T* into scoped_ptr<T> |
michael@0 | 702 | // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
michael@0 | 703 | // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
michael@0 | 704 | template <typename T> |
michael@0 | 705 | scoped_ptr<T> make_scoped_ptr(T* ptr) { |
michael@0 | 706 | return scoped_ptr<T>(ptr); |
michael@0 | 707 | } |
michael@0 | 708 | |
michael@0 | 709 | #endif // BASE_MEMORY_SCOPED_PTR_H_ |