| OLD | NEW |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. | 1 // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Redistribution and use in source and binary forms, with or without | 3 // Redistribution and use in source and binary forms, with or without |
| 4 // modification, are permitted provided that the following conditions are | 4 // modification, are permitted provided that the following conditions are |
| 5 // met: | 5 // met: |
| 6 // | 6 // |
| 7 // * Redistributions of source code must retain the above copyright | 7 // * Redistributions of source code must retain the above copyright |
| 8 // notice, this list of conditions and the following disclaimer. | 8 // notice, this list of conditions and the following disclaimer. |
| 9 // * Redistributions in binary form must reproduce the above | 9 // * Redistributions in binary form must reproduce the above |
| 10 // copyright notice, this list of conditions and the following disclaimer | 10 // copyright notice, this list of conditions and the following disclaimer |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 #define NONSTD_UNIQUE_PTR_H_ | 67 #define NONSTD_UNIQUE_PTR_H_ |
| 68 | 68 |
| 69 // This is an implementation designed to match the anticipated future C++11 | 69 // This is an implementation designed to match the anticipated future C++11 |
| 70 // implementation of the unique_ptr class. | 70 // implementation of the unique_ptr class. |
| 71 | 71 |
| 72 #include <assert.h> | 72 #include <assert.h> |
| 73 #include <stddef.h> | 73 #include <stddef.h> |
| 74 #include <stdlib.h> | 74 #include <stdlib.h> |
| 75 | 75 |
| 76 #include <ostream> | 76 #include <ostream> |
| 77 #include <utility> |
| 77 | 78 |
| 78 #include "template_util.h" | 79 #include "template_util.h" |
| 79 | 80 |
| 80 namespace nonstd { | 81 namespace nonstd { |
| 81 | 82 |
| 82 // Replacement for move, but doesn't allow things that are already | |
| 83 // rvalue references. | |
| 84 template <class T> | |
| 85 T&& move(T& t) { | |
| 86 return static_cast<T&&>(t); | |
| 87 } | |
| 88 | |
| 89 // Function object which deletes its parameter, which must be a pointer. | 83 // Function object which deletes its parameter, which must be a pointer. |
| 90 // If C is an array type, invokes 'delete[]' on the parameter; otherwise, | 84 // If C is an array type, invokes 'delete[]' on the parameter; otherwise, |
| 91 // invokes 'delete'. The default deleter for unique_ptr<T>. | 85 // invokes 'delete'. The default deleter for unique_ptr<T>. |
| 92 template <class T> | 86 template <class T> |
| 93 struct DefaultDeleter { | 87 struct DefaultDeleter { |
| 94 DefaultDeleter() {} | 88 DefaultDeleter() {} |
| 95 template <typename U> | 89 template <typename U> |
| 96 DefaultDeleter(const DefaultDeleter<U>& other) { | 90 DefaultDeleter(const DefaultDeleter<U>& other) { |
| 97 // IMPLEMENTATION NOTE: C++11 20.7.1.1.2p2 only provides this constructor | 91 // IMPLEMENTATION NOTE: C++11 20.7.1.1.2p2 only provides this constructor |
| 98 // if U* is implicitly convertible to T* and U is not an array type. | 92 // if U* is implicitly convertible to T* and U is not an array type. |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 explicit unique_ptr(C* p) : internal::unique_ptr_base<C, D>(p) {} | 231 explicit unique_ptr(C* p) : internal::unique_ptr_base<C, D>(p) {} |
| 238 | 232 |
| 239 // Constructor. Allows initialization of a stateful deleter. | 233 // Constructor. Allows initialization of a stateful deleter. |
| 240 unique_ptr(C* p, const D& d) : internal::unique_ptr_base<C, D>(p, d) {} | 234 unique_ptr(C* p, const D& d) : internal::unique_ptr_base<C, D>(p, d) {} |
| 241 | 235 |
| 242 // Constructor. Allows construction from a nullptr. | 236 // Constructor. Allows construction from a nullptr. |
| 243 unique_ptr(decltype(nullptr)) : internal::unique_ptr_base<C, D>(nullptr) {} | 237 unique_ptr(decltype(nullptr)) : internal::unique_ptr_base<C, D>(nullptr) {} |
| 244 | 238 |
| 245 // Move constructor. | 239 // Move constructor. |
| 246 unique_ptr(unique_ptr&& that) | 240 unique_ptr(unique_ptr&& that) |
| 247 : internal::unique_ptr_base<C, D>(nonstd::move(that)) {} | 241 : internal::unique_ptr_base<C, D>(std::move(that)) {} |
| 248 | 242 |
| 249 // operator=. Allows assignment from a nullptr. Deletes the currently owned | 243 // operator=. Allows assignment from a nullptr. Deletes the currently owned |
| 250 // object, if any. | 244 // object, if any. |
| 251 unique_ptr& operator=(decltype(nullptr)) { | 245 unique_ptr& operator=(decltype(nullptr)) { |
| 252 this->reset(); | 246 this->reset(); |
| 253 return *this; | 247 return *this; |
| 254 } | 248 } |
| 255 | 249 |
| 256 // Move assignment. | 250 // Move assignment. |
| 257 unique_ptr<C>& operator=(unique_ptr<C>&& that) { | 251 unique_ptr<C>& operator=(unique_ptr<C>&& that) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 // - it cannot be const-qualified differently from T per unique_ptr spec | 304 // - it cannot be const-qualified differently from T per unique_ptr spec |
| 311 // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting | 305 // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting |
| 312 // to work around this may use const_cast<const T*>(). | 306 // to work around this may use const_cast<const T*>(). |
| 313 explicit unique_ptr(C* p) : internal::unique_ptr_base<C, D>(p) {} | 307 explicit unique_ptr(C* p) : internal::unique_ptr_base<C, D>(p) {} |
| 314 | 308 |
| 315 // Constructor. Allows construction from a nullptr. | 309 // Constructor. Allows construction from a nullptr. |
| 316 unique_ptr(decltype(nullptr)) : internal::unique_ptr_base<C, D>(nullptr) {} | 310 unique_ptr(decltype(nullptr)) : internal::unique_ptr_base<C, D>(nullptr) {} |
| 317 | 311 |
| 318 // Move constructor. | 312 // Move constructor. |
| 319 unique_ptr(unique_ptr&& that) | 313 unique_ptr(unique_ptr&& that) |
| 320 : internal::unique_ptr_base<C, D>(nonstd::move(that)) {} | 314 : internal::unique_ptr_base<C, D>(std::move(that)) {} |
| 321 | 315 |
| 322 // operator=. Allows assignment from a nullptr. Deletes the currently owned | 316 // operator=. Allows assignment from a nullptr. Deletes the currently owned |
| 323 // array, if any. | 317 // array, if any. |
| 324 unique_ptr& operator=(decltype(nullptr)) { | 318 unique_ptr& operator=(decltype(nullptr)) { |
| 325 this->reset(); | 319 this->reset(); |
| 326 return *this; | 320 return *this; |
| 327 } | 321 } |
| 328 | 322 |
| 329 // Move assignment. | 323 // Move assignment. |
| 330 unique_ptr<C>& operator=(unique_ptr<C>&& that) { | 324 unique_ptr<C>& operator=(unique_ptr<C>&& that) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 } | 389 } |
| 396 | 390 |
| 397 template <typename T> | 391 template <typename T> |
| 398 std::ostream& operator<<(std::ostream& out, const unique_ptr<T>& p) { | 392 std::ostream& operator<<(std::ostream& out, const unique_ptr<T>& p) { |
| 399 return out << p.get(); | 393 return out << p.get(); |
| 400 } | 394 } |
| 401 | 395 |
| 402 } // namespace nonstd | 396 } // namespace nonstd |
| 403 | 397 |
| 404 #endif // NONSTD_UNIQUE_PTR_H_ | 398 #endif // NONSTD_UNIQUE_PTR_H_ |
| OLD | NEW |