| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. | |
| 2 // | |
| 3 // Redistribution and use in source and binary forms, with or without | |
| 4 // modification, are permitted provided that the following conditions are | |
| 5 // met: | |
| 6 // | |
| 7 // * Redistributions of source code must retain the above copyright | |
| 8 // notice, this list of conditions and the following disclaimer. | |
| 9 // * Redistributions in binary form must reproduce the above | |
| 10 // copyright notice, this list of conditions and the following disclaimer | |
| 11 // in the documentation and/or other materials provided with the | |
| 12 // distribution. | |
| 13 // * Neither the name of Google Inc. nor the names of its | |
| 14 // contributors may be used to endorse or promote products derived from | |
| 15 // this software without specific prior written permission. | |
| 16 // | |
| 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | |
| 29 // This is a copy of breakpad's standalone scoped_ptr, which has been | |
| 30 // renamed to nonstd::unique_ptr, and from which more complicated classes | |
| 31 // have been removed. The reset() method has also been tweaked to more | |
| 32 // closely match c++11, and an implicit conversion to bool has been added. | |
| 33 | |
| 34 // Scopers help you manage ownership of a pointer, helping you easily manage the | |
| 35 // a pointer within a scope, and automatically destroying the pointer at the | |
| 36 // end of a scope. | |
| 37 // | |
| 38 // A unique_ptr<T> is like a T*, except that the destructor of unique_ptr<T> | |
| 39 // automatically deletes the pointer it holds (if any). | |
| 40 // That is, unique_ptr<T> owns the T object that it points to. | |
| 41 // Like a T*, a unique_ptr<T> may hold either NULL or a pointer to a T object. | |
| 42 // Also like T*, unique_ptr<T> is thread-compatible, and once you | |
| 43 // dereference it, you get the thread safety guarantees of T. | |
| 44 // | |
| 45 // Example usage (unique_ptr): | |
| 46 // { | |
| 47 // unique_ptr<Foo> foo(new Foo("wee")); | |
| 48 // } // foo goes out of scope, releasing the pointer with it. | |
| 49 // | |
| 50 // { | |
| 51 // unique_ptr<Foo> foo; // No pointer managed. | |
| 52 // foo.reset(new Foo("wee")); // Now a pointer is managed. | |
| 53 // foo.reset(new Foo("wee2")); // Foo("wee") was destroyed. | |
| 54 // foo.reset(new Foo("wee3")); // Foo("wee2") was destroyed. | |
| 55 // foo->Method(); // Foo::Method() called. | |
| 56 // foo.get()->Method(); // Foo::Method() called. | |
| 57 // SomeFunc(foo.release()); // SomeFunc takes ownership, foo no longer | |
| 58 // // manages a pointer. | |
| 59 // foo.reset(new Foo("wee4")); // foo manages a pointer again. | |
| 60 // foo.reset(); // Foo("wee4") destroyed, foo no longer | |
| 61 // // manages a pointer. | |
| 62 // } // foo wasn't managing a pointer, so nothing was destroyed. | |
| 63 // | |
| 64 // The size of a unique_ptr is small: sizeof(unique_ptr<C>) == sizeof(C*) | |
| 65 | |
| 66 #ifndef NONSTD_UNIQUE_PTR_H_ | |
| 67 #define NONSTD_UNIQUE_PTR_H_ | |
| 68 | |
| 69 // This is an implementation designed to match the anticipated future C++11 | |
| 70 // implementation of the unique_ptr class. | |
| 71 | |
| 72 #include <assert.h> | |
| 73 #include <stddef.h> | |
| 74 #include <stdlib.h> | |
| 75 | |
| 76 #include <ostream> | |
| 77 #include <utility> | |
| 78 | |
| 79 #include "template_util.h" | |
| 80 | |
| 81 namespace nonstd { | |
| 82 | |
| 83 // Function object which deletes its parameter, which must be a pointer. | |
| 84 // If C is an array type, invokes 'delete[]' on the parameter; otherwise, | |
| 85 // invokes 'delete'. The default deleter for unique_ptr<T>. | |
| 86 template <class T> | |
| 87 struct DefaultDeleter { | |
| 88 DefaultDeleter() {} | |
| 89 template <typename U> | |
| 90 DefaultDeleter(const DefaultDeleter<U>& other) { | |
| 91 // IMPLEMENTATION NOTE: C++11 20.7.1.1.2p2 only provides this constructor | |
| 92 // if U* is implicitly convertible to T* and U is not an array type. | |
| 93 // | |
| 94 // Correct implementation should use SFINAE to disable this | |
| 95 // constructor. However, since there are no other 1-argument constructors, | |
| 96 // using a static_assert() based on is_convertible<> and requiring | |
| 97 // complete types is simpler and will cause compile failures for equivalent | |
| 98 // misuses. | |
| 99 // | |
| 100 // Note, the is_convertible<U*, T*> check also ensures that U is not an | |
| 101 // array. T is guaranteed to be a non-array, so any U* where U is an array | |
| 102 // cannot convert to T*. | |
| 103 enum { T_must_be_complete = sizeof(T) }; | |
| 104 enum { U_must_be_complete = sizeof(U) }; | |
| 105 static_assert((pdfium::base::is_convertible<U*, T*>::value), | |
| 106 "U_ptr_must_implicitly_convert_to_T_ptr"); | |
| 107 } | |
| 108 inline void operator()(T* ptr) const { | |
| 109 enum { type_must_be_complete = sizeof(T) }; | |
| 110 delete ptr; | |
| 111 } | |
| 112 }; | |
| 113 | |
| 114 // Specialization of DefaultDeleter for array types. | |
| 115 template <class T> | |
| 116 struct DefaultDeleter<T[]> { | |
| 117 inline void operator()(T* ptr) const { | |
| 118 enum { type_must_be_complete = sizeof(T) }; | |
| 119 delete[] ptr; | |
| 120 } | |
| 121 | |
| 122 private: | |
| 123 // Disable this operator for any U != T because it is undefined to execute | |
| 124 // an array delete when the static type of the array mismatches the dynamic | |
| 125 // type. | |
| 126 // | |
| 127 // References: | |
| 128 // C++98 [expr.delete]p3 | |
| 129 // http://cplusplus.github.com/LWG/lwg-defects.html#938 | |
| 130 template <typename U> | |
| 131 void operator()(U* array) const; | |
| 132 }; | |
| 133 | |
| 134 template <class T, int n> | |
| 135 struct DefaultDeleter<T[n]> { | |
| 136 // Never allow someone to declare something like unique_ptr<int[10]>. | |
| 137 static_assert(sizeof(T) == -1, "do_not_use_array_with_size_as_type"); | |
| 138 }; | |
| 139 | |
| 140 namespace internal { | |
| 141 | |
| 142 // Common implementation for both pointers to elements and pointers to | |
| 143 // arrays. These are differentiated below based on the need to invoke | |
| 144 // delete vs. delete[] as appropriate. | |
| 145 template <class C, class D> | |
| 146 class unique_ptr_base { | |
| 147 public: | |
| 148 // The element type | |
| 149 typedef C element_type; | |
| 150 | |
| 151 explicit unique_ptr_base(C* p) : data_(p) {} | |
| 152 | |
| 153 // Initializer for deleters that have data parameters. | |
| 154 unique_ptr_base(C* p, const D& d) : data_(p, d) {} | |
| 155 | |
| 156 // Move constructor. | |
| 157 unique_ptr_base(unique_ptr_base<C, D>&& that) | |
| 158 : data_(that.release(), that.get_deleter()) {} | |
| 159 | |
| 160 ~unique_ptr_base() { | |
| 161 enum { type_must_be_complete = sizeof(C) }; | |
| 162 if (data_.ptr != nullptr) { | |
| 163 // Not using get_deleter() saves one function call in non-optimized | |
| 164 // builds. | |
| 165 static_cast<D&>(data_)(data_.ptr); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 void reset(C* p = nullptr) { | |
| 170 C* old = data_.ptr; | |
| 171 data_.ptr = p; | |
| 172 if (old != nullptr) | |
| 173 static_cast<D&>(data_)(old); | |
| 174 } | |
| 175 | |
| 176 C* get() const { return data_.ptr; } | |
| 177 D& get_deleter() { return data_; } | |
| 178 const D& get_deleter() const { return data_; } | |
| 179 | |
| 180 // Comparison operators. | |
| 181 // These return whether two unique_ptr refer to the same object, not just to | |
| 182 // two different but equal objects. | |
| 183 bool operator==(C* p) const { return data_.ptr == p; } | |
| 184 bool operator!=(C* p) const { return data_.ptr != p; } | |
| 185 | |
| 186 // Swap two unique pointers. | |
| 187 void swap(unique_ptr_base& p2) { | |
| 188 Data tmp = data_; | |
| 189 data_ = p2.data_; | |
| 190 p2.data_ = tmp; | |
| 191 } | |
| 192 | |
| 193 // Release a pointer. | |
| 194 // The return value is the current pointer held by this object. | |
| 195 // If this object holds a NULL pointer, the return value is NULL. | |
| 196 // After this operation, this object will hold a NULL pointer, | |
| 197 // and will not own the object any more. | |
| 198 C* release() { | |
| 199 C* ptr = data_.ptr; | |
| 200 data_.ptr = nullptr; | |
| 201 return ptr; | |
| 202 } | |
| 203 | |
| 204 // Allow promotion to bool for conditional statements. | |
| 205 explicit operator bool() const { return data_.ptr != nullptr; } | |
| 206 | |
| 207 protected: | |
| 208 // Use the empty base class optimization to allow us to have a D | |
| 209 // member, while avoiding any space overhead for it when D is an | |
| 210 // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good | |
| 211 // discussion of this technique. | |
| 212 struct Data : public D { | |
| 213 explicit Data(C* ptr_in) : ptr(ptr_in) {} | |
| 214 Data(C* ptr_in, const D& other) : D(other), ptr(ptr_in) {} | |
| 215 C* ptr; | |
| 216 }; | |
| 217 | |
| 218 Data data_; | |
| 219 }; | |
| 220 | |
| 221 } // namespace internal | |
| 222 | |
| 223 // Implementation for ordinary pointers using delete. | |
| 224 template <class C, class D = DefaultDeleter<C>> | |
| 225 class unique_ptr : public internal::unique_ptr_base<C, D> { | |
| 226 public: | |
| 227 // Constructor. Defaults to initializing with nullptr. | |
| 228 unique_ptr() : internal::unique_ptr_base<C, D>(nullptr) {} | |
| 229 | |
| 230 // Constructor. Takes ownership of p. | |
| 231 explicit unique_ptr(C* p) : internal::unique_ptr_base<C, D>(p) {} | |
| 232 | |
| 233 // Constructor. Allows initialization of a stateful deleter. | |
| 234 unique_ptr(C* p, const D& d) : internal::unique_ptr_base<C, D>(p, d) {} | |
| 235 | |
| 236 // Constructor. Allows construction from a nullptr. | |
| 237 unique_ptr(decltype(nullptr)) : internal::unique_ptr_base<C, D>(nullptr) {} | |
| 238 | |
| 239 // Move constructor. | |
| 240 unique_ptr(unique_ptr&& that) | |
| 241 : internal::unique_ptr_base<C, D>(std::move(that)) {} | |
| 242 | |
| 243 // operator=. Allows assignment from a nullptr. Deletes the currently owned | |
| 244 // object, if any. | |
| 245 unique_ptr& operator=(decltype(nullptr)) { | |
| 246 this->reset(); | |
| 247 return *this; | |
| 248 } | |
| 249 | |
| 250 // Move assignment. | |
| 251 unique_ptr<C>& operator=(unique_ptr<C>&& that) { | |
| 252 this->reset(that.release()); | |
| 253 return *this; | |
| 254 } | |
| 255 | |
| 256 // Accessors to get the owned object. | |
| 257 // operator* and operator-> will assert() if there is no current object. | |
| 258 C& operator*() const { | |
| 259 assert(this->data_.ptr != nullptr); | |
| 260 return *this->data_.ptr; | |
| 261 } | |
| 262 C* operator->() const { | |
| 263 assert(this->data_.ptr != nullptr); | |
| 264 return this->data_.ptr; | |
| 265 } | |
| 266 | |
| 267 // Comparison operators. | |
| 268 // These return whether two unique_ptr refer to the same object, not just to | |
| 269 // two different but equal objects. | |
| 270 bool operator==(const C* p) const { return this->get() == p; } | |
| 271 bool operator!=(const C* p) const { return this->get() != p; } | |
| 272 | |
| 273 private: | |
| 274 // Disallow evil constructors. It doesn't make sense to make a copy of | |
| 275 // something that's allegedly unique. | |
| 276 unique_ptr(const unique_ptr&) = delete; | |
| 277 void operator=(const unique_ptr&) = delete; | |
| 278 | |
| 279 // Forbid comparison of unique_ptr types. If U != C, it totally | |
| 280 // doesn't make sense, and if U == C, it still doesn't make sense | |
| 281 // because you should never have the same object owned by two different | |
| 282 // unique_ptrs. | |
| 283 template <class U> | |
| 284 bool operator==(unique_ptr<U> const& p2) const; | |
| 285 template <class U> | |
| 286 bool operator!=(unique_ptr<U> const& p2) const; | |
| 287 }; | |
| 288 | |
| 289 // Specialization for arrays using delete[]. | |
| 290 template <class C, class D> | |
| 291 class unique_ptr<C[], D> : public internal::unique_ptr_base<C, D> { | |
| 292 public: | |
| 293 // Constructor. Defaults to initializing with nullptr. | |
| 294 unique_ptr() : internal::unique_ptr_base<C, D>(nullptr) {} | |
| 295 | |
| 296 // Constructor. Stores the given array. Note that the argument's type | |
| 297 // must exactly match T*. In particular: | |
| 298 // - it cannot be a pointer to a type derived from T, because it is | |
| 299 // inherently unsafe in the general case to access an array through a | |
| 300 // pointer whose dynamic type does not match its static type (eg., if | |
| 301 // T and the derived types had different sizes access would be | |
| 302 // incorrectly calculated). Deletion is also always undefined | |
| 303 // (C++98 [expr.delete]p3). If you're doing this, fix your code. | |
| 304 // - it cannot be const-qualified differently from T per unique_ptr spec | |
| 305 // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting | |
| 306 // to work around this may use const_cast<const T*>(). | |
| 307 explicit unique_ptr(C* p) : internal::unique_ptr_base<C, D>(p) {} | |
| 308 | |
| 309 // Constructor. Allows construction from a nullptr. | |
| 310 unique_ptr(decltype(nullptr)) : internal::unique_ptr_base<C, D>(nullptr) {} | |
| 311 | |
| 312 // Move constructor. | |
| 313 unique_ptr(unique_ptr&& that) | |
| 314 : internal::unique_ptr_base<C, D>(std::move(that)) {} | |
| 315 | |
| 316 // operator=. Allows assignment from a nullptr. Deletes the currently owned | |
| 317 // array, if any. | |
| 318 unique_ptr& operator=(decltype(nullptr)) { | |
| 319 this->reset(); | |
| 320 return *this; | |
| 321 } | |
| 322 | |
| 323 // Move assignment. | |
| 324 unique_ptr<C>& operator=(unique_ptr<C>&& that) { | |
| 325 this->reset(that.release()); | |
| 326 return *this; | |
| 327 } | |
| 328 | |
| 329 // Reset. Deletes the currently owned array, if any. | |
| 330 // Then takes ownership of a new object, if given. | |
| 331 void reset(C* array = nullptr) { | |
| 332 static_cast<internal::unique_ptr_base<C, D>*>(this)->reset(array); | |
| 333 } | |
| 334 | |
| 335 // Support indexing since it is holding array. | |
| 336 C& operator[](size_t i) { return this->data_.ptr[i]; } | |
| 337 | |
| 338 // Comparison operators. | |
| 339 // These return whether two unique_ptr refer to the same object, not just to | |
| 340 // two different but equal objects. | |
| 341 bool operator==(C* array) const { return this->get() == array; } | |
| 342 bool operator!=(C* array) const { return this->get() != array; } | |
| 343 | |
| 344 private: | |
| 345 // Disable initialization from any type other than element_type*, by | |
| 346 // providing a constructor that matches such an initialization, but is | |
| 347 // private and has no definition. This is disabled because it is not safe to | |
| 348 // call delete[] on an array whose static type does not match its dynamic | |
| 349 // type. | |
| 350 template <typename U> | |
| 351 explicit unique_ptr(U* array); | |
| 352 explicit unique_ptr(int disallow_construction_from_null); | |
| 353 | |
| 354 // Disable reset() from any type other than element_type*, for the same | |
| 355 // reasons as the constructor above. | |
| 356 template <typename U> | |
| 357 void reset(U* array); | |
| 358 void reset(int disallow_reset_from_null); | |
| 359 | |
| 360 // Disallow evil constructors. It doesn't make sense to make a copy of | |
| 361 // something that's allegedly unique. | |
| 362 unique_ptr(const unique_ptr&) = delete; | |
| 363 void operator=(const unique_ptr&) = delete; | |
| 364 | |
| 365 // Forbid comparison of unique_ptr types. If U != C, it totally | |
| 366 // doesn't make sense, and if U == C, it still doesn't make sense | |
| 367 // because you should never have the same object owned by two different | |
| 368 // unique_ptrs. | |
| 369 template <class U> | |
| 370 bool operator==(unique_ptr<U> const& p2) const; | |
| 371 template <class U> | |
| 372 bool operator!=(unique_ptr<U> const& p2) const; | |
| 373 }; | |
| 374 | |
| 375 // Free functions | |
| 376 template <class C> | |
| 377 void swap(unique_ptr<C>& p1, unique_ptr<C>& p2) { | |
| 378 p1.swap(p2); | |
| 379 } | |
| 380 | |
| 381 template <class C> | |
| 382 bool operator==(C* p1, const unique_ptr<C>& p2) { | |
| 383 return p1 == p2.get(); | |
| 384 } | |
| 385 | |
| 386 template <class C> | |
| 387 bool operator!=(C* p1, const unique_ptr<C>& p2) { | |
| 388 return p1 != p2.get(); | |
| 389 } | |
| 390 | |
| 391 template <typename T> | |
| 392 std::ostream& operator<<(std::ostream& out, const unique_ptr<T>& p) { | |
| 393 return out << p.get(); | |
| 394 } | |
| 395 | |
| 396 } // namespace nonstd | |
| 397 | |
| 398 #endif // NONSTD_UNIQUE_PTR_H_ | |
| OLD | NEW |