diff options
Diffstat (limited to 'third_party/chromium/base/memory/weak_ptr.h')
-rw-r--r-- | third_party/chromium/base/memory/weak_ptr.h | 64 |
1 files changed, 34 insertions, 30 deletions
diff --git a/third_party/chromium/base/memory/weak_ptr.h b/third_party/chromium/base/memory/weak_ptr.h index 2efb024..509b380 100644 --- a/third_party/chromium/base/memory/weak_ptr.h +++ b/third_party/chromium/base/memory/weak_ptr.h @@ -107,10 +107,14 @@ class BASE_EXPORT WeakReference { }; WeakReference(); - WeakReference(const WeakReference& other); explicit WeakReference(const Flag* flag); ~WeakReference(); + WeakReference(WeakReference&& other); + WeakReference(const WeakReference& other); + WeakReference& operator=(WeakReference&& other) = default; + WeakReference& operator=(const WeakReference& other) = default; + bool is_valid() const; private: @@ -143,6 +147,11 @@ class BASE_EXPORT WeakPtrBase { WeakPtrBase(); ~WeakPtrBase(); + WeakPtrBase(const WeakPtrBase& other) = default; + WeakPtrBase(WeakPtrBase&& other) = default; + WeakPtrBase& operator=(const WeakPtrBase& other) = default; + WeakPtrBase& operator=(WeakPtrBase&& other) = default; + protected: explicit WeakPtrBase(const WeakReference& ref); @@ -203,10 +212,13 @@ class WeakPtr : public internal::WeakPtrBase { WeakPtr(std::nullptr_t) : ptr_(nullptr) {} // Allow conversion from U to T provided U "is a" T. Note that this - // is separate from the (implicit) copy constructor. + // is separate from the (implicit) copy and move constructors. template <typename U> WeakPtr(const WeakPtr<U>& other) : WeakPtrBase(other), ptr_(other.ptr_) { } + template <typename U> + WeakPtr(WeakPtr<U>&& other) + : WeakPtrBase(std::move(other)), ptr_(other.ptr_) {} T* get() const { return ref_.is_valid() ? ptr_ : nullptr; } @@ -224,36 +236,10 @@ class WeakPtr : public internal::WeakPtrBase { ptr_ = nullptr; } - // Implement "Safe Bool Idiom" - // https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool - // - // Allow WeakPtr<element_type> to be used in boolean expressions such as - // if (weak_ptr_instance) - // But do not become convertible to a real bool (which is dangerous). - // Implementation requires: - // typedef Testable - // operator Testable() const - // operator== - // operator!= - // - // == and != operators must be declared explicitly or dissallowed, as - // otherwise "ptr1 == ptr2" will compile but do the wrong thing (i.e., convert - // to Testable and then do the comparison). - // - // C++11 provides for "explicit operator bool()", however it is currently - // banned due to MSVS2013. https://chromium-cpp.appspot.com/#core-blacklist - private: - typedef T* WeakPtr::*Testable; - - public: - operator Testable() const { return get() ? &WeakPtr::ptr_ : nullptr; } + // Allow conditionals to test validity, e.g. if (weak_ptr) {...}; + explicit operator bool() const { return get() != nullptr; } private: - // Explicitly declare comparison operators as required by the "Safe Bool - // Idiom", but keep them private. - template <class U> bool operator==(WeakPtr<U> const&) const; - template <class U> bool operator!=(WeakPtr<U> const&) const; - friend class internal::SupportsWeakPtrBase; template <typename U> friend class WeakPtr; friend class SupportsWeakPtr<T>; @@ -269,6 +255,24 @@ class WeakPtr : public internal::WeakPtrBase { T* ptr_; }; +// Allow callers to compare WeakPtrs against nullptr to test validity. +template <class T> +bool operator!=(const WeakPtr<T>& weak_ptr, std::nullptr_t) { + return !(weak_ptr == nullptr); +} +template <class T> +bool operator!=(std::nullptr_t, const WeakPtr<T>& weak_ptr) { + return weak_ptr != nullptr; +} +template <class T> +bool operator==(const WeakPtr<T>& weak_ptr, std::nullptr_t) { + return weak_ptr.get() == nullptr; +} +template <class T> +bool operator==(std::nullptr_t, const WeakPtr<T>& weak_ptr) { + return weak_ptr == nullptr; +} + // A class may be composed of a WeakPtrFactory and thereby // control how it exposes weak pointers to itself. This is helpful if you only // need weak pointers within the implementation of a class. This class is also |