diff options
author | Tom Sepez <tsepez@chromium.org> | 2015-09-15 10:49:41 -0700 |
---|---|---|
committer | Tom Sepez <tsepez@chromium.org> | 2015-09-15 10:49:41 -0700 |
commit | 5eb8ba2ef43bac9c8fb96f75357b21b0dbeefe80 (patch) | |
tree | 767463a0464c9d6b4c2bc60d94bc3ce72f67c24b /third_party/base | |
parent | f458362c167f29f37301f48b08eb8d916e69d40b (diff) | |
download | pdfium-5eb8ba2ef43bac9c8fb96f75357b21b0dbeefe80.tar.xz |
Merge to XFA: Add move ctor to nonstd::unique_ptr.
(cherry picked from commit ac8fda05418b7c96255d320fdec296935c552187)
(cherry picked from commit 72d51871ae152163eeb9b005fd0a74d5c1651cd2)
Original Review URL: https://codereview.chromium.org/1338383002 .
TBR=thestig@chromium.org
Review URL: https://codereview.chromium.org/1344873002 .
Diffstat (limited to 'third_party/base')
-rw-r--r-- | third_party/base/nonstd_unique_ptr.h | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/third_party/base/nonstd_unique_ptr.h b/third_party/base/nonstd_unique_ptr.h index c24bfff74c..1d1c43f42f 100644 --- a/third_party/base/nonstd_unique_ptr.h +++ b/third_party/base/nonstd_unique_ptr.h @@ -66,7 +66,7 @@ #ifndef NONSTD_UNIQUE_PTR_H_ #define NONSTD_UNIQUE_PTR_H_ -// This is an implementation designed to match the anticipated future TR2 +// This is an implementation designed to match the anticipated future C++11 // implementation of the unique_ptr class. #include <assert.h> @@ -75,6 +75,13 @@ namespace nonstd { +// Replacement for move, but doesn't allow things that are already +// rvalue references. +template <class T> +T&& move(T& t) { + return static_cast<T&&>(t); +} + // Common implementation for both pointers to elements and pointers to // arrays. These are differentiated below based on the need to invoke // delete vs. delete[] as appropriate. @@ -87,6 +94,12 @@ class unique_ptr_base { explicit unique_ptr_base(C* p) : ptr_(p) { } + // Move constructor. + unique_ptr_base(unique_ptr_base<C>&& that) { + ptr_ = that.ptr_; + that.ptr_ = nullptr; + } + // Accessors to get the owned object. // operator* and operator-> will assert() if there is no current object. C& operator*() const { @@ -124,7 +137,7 @@ class unique_ptr_base { } // Allow promotion to bool for conditional statements. - operator bool() const { return ptr_ != NULL; } + explicit operator bool() const { return ptr_ != NULL; } protected: C* ptr_; @@ -141,6 +154,9 @@ class unique_ptr : public unique_ptr_base<C> { // allocated with new (not new[] - see below). explicit unique_ptr(C* p = NULL) : unique_ptr_base<C>(p) { } + // Move constructor. + unique_ptr(unique_ptr<C>&& that) : unique_ptr_base<C>(nonstd::move(that)) {} + // Destructor. If there is a C object, delete it. // We don't need to test ptr_ == NULL because C++ does that for us. ~unique_ptr() { @@ -167,9 +183,10 @@ private: template <class C2> bool operator==(unique_ptr<C2> const& p2) const; template <class C2> bool operator!=(unique_ptr<C2> const& p2) const; - // Disallow evil constructors - unique_ptr(const unique_ptr&); - void operator=(const unique_ptr&); + // Disallow evil constructors. It doesn't make sense to make a copy of + // something that's allegedly unique. + unique_ptr(const unique_ptr&) = delete; + void operator=(const unique_ptr&) = delete; }; // Specialization for arrays using delete[]. @@ -183,6 +200,9 @@ class unique_ptr<C[]> : public unique_ptr_base<C> { // allocated with new[] (not new - see above). explicit unique_ptr(C* p = NULL) : unique_ptr_base<C>(p) { } + // Move constructor. + unique_ptr(unique_ptr<C>&& that) : unique_ptr_base<C>(nonstd::move(that)) {} + // Destructor. If there is a C object, delete it. // We don't need to test ptr_ == NULL because C++ does that for us. ~unique_ptr() { @@ -212,9 +232,10 @@ private: template <class C2> bool operator==(unique_ptr<C2> const& p2) const; template <class C2> bool operator!=(unique_ptr<C2> const& p2) const; - // Disallow evil constructors - unique_ptr(const unique_ptr&); - void operator=(const unique_ptr&); + // Disallow evil constructors. It doesn't make sense to make a copy of + // something that's allegedly unique. + unique_ptr(const unique_ptr&) = delete; + void operator=(const unique_ptr&) = delete; }; // Free functions |