2014-07-15 00:18:51 +02:00
|
|
|
// Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2012
|
|
|
|
// Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the name Chromium Embedded
|
|
|
|
// Framework nor the names of its contributors may be used to endorse
|
|
|
|
// or promote products derived from this software without specific prior
|
|
|
|
// written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2022-09-05 22:17:57 +02:00
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// Weak pointers are pointers to an object that do not affect its lifetime.
|
|
|
|
/// They may be invalidated (i.e. reset to nullptr) by the object, or its
|
|
|
|
/// owner, at any time, most commonly when the object is about to be deleted.
|
|
|
|
///
|
|
|
|
/// Weak pointers are useful when an object needs to be accessed safely by one
|
|
|
|
/// or more objects other than its owner, and those callers can cope with the
|
|
|
|
/// object vanishing and e.g. tasks posted to it being silently dropped.
|
|
|
|
/// Reference-counting such an object would complicate the ownership graph and
|
|
|
|
/// make it harder to reason about the object's lifetime.
|
|
|
|
///
|
|
|
|
/// EXAMPLE:
|
|
|
|
///
|
|
|
|
/// <pre>
|
|
|
|
/// class Controller {
|
|
|
|
/// public:
|
|
|
|
/// void SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); }
|
|
|
|
/// void WorkComplete(const Result& result) { ... }
|
|
|
|
/// private:
|
|
|
|
/// // Member variables should appear before the WeakPtrFactory, to ensure
|
|
|
|
/// // that any WeakPtrs to Controller are invalidated before its members
|
|
|
|
/// // variable's destructors are executed, rendering them invalid.
|
|
|
|
/// WeakPtrFactory<Controller> weak_factory_{this};
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// class Worker {
|
|
|
|
/// public:
|
|
|
|
/// static void StartNew(WeakPtr<Controller> controller) {
|
|
|
|
/// Worker* worker = new Worker(std::move(controller));
|
|
|
|
/// // Kick off asynchronous processing...
|
|
|
|
/// }
|
|
|
|
/// private:
|
|
|
|
/// Worker(WeakPtr<Controller> controller)
|
|
|
|
/// : controller_(std::move(controller)) {}
|
|
|
|
/// void DidCompleteAsynchronousProcessing(const Result& result) {
|
|
|
|
/// if (controller_)
|
|
|
|
/// controller_->WorkComplete(result);
|
|
|
|
/// }
|
|
|
|
/// WeakPtr<Controller> controller_;
|
|
|
|
/// };
|
|
|
|
/// </pre>
|
|
|
|
///
|
|
|
|
/// With this implementation a caller may use SpawnWorker() to dispatch multiple
|
|
|
|
/// Workers and subsequently delete the Controller, without waiting for all
|
|
|
|
/// Workers to have completed.
|
|
|
|
///
|
|
|
|
/// <b>IMPORTANT: Thread-safety</b>
|
|
|
|
///
|
|
|
|
/// Weak pointers may be passed safely between threads, but must always be
|
|
|
|
/// dereferenced and invalidated on the same ThreaddTaskRunner otherwise
|
|
|
|
/// checking the pointer would be racey.
|
|
|
|
///
|
|
|
|
/// To ensure correct use, the first time a WeakPtr issued by a WeakPtrFactory
|
|
|
|
/// is dereferenced, the factory and its WeakPtrs become bound to the calling
|
|
|
|
/// thread or current ThreaddWorkerPool token, and cannot be dereferenced or
|
|
|
|
/// invalidated on any other task runner. Bound WeakPtrs can still be handed
|
|
|
|
/// off to other task runners, e.g. to use to post tasks back to object on the
|
|
|
|
/// bound thread.
|
|
|
|
///
|
|
|
|
/// If all WeakPtr objects are destroyed or invalidated then the factory is
|
|
|
|
/// unbound from the ThreadedTaskRunner/Thread. The WeakPtrFactory may then be
|
|
|
|
/// destroyed, or new WeakPtr objects may be used, from a different thread.
|
|
|
|
///
|
|
|
|
/// Thus, at least one WeakPtr object must exist and have been dereferenced on
|
|
|
|
/// the correct thread to enforce that other WeakPtr objects will enforce they
|
|
|
|
/// are used on the desired thread.
|
2014-07-15 00:18:51 +02:00
|
|
|
|
|
|
|
#ifndef CEF_INCLUDE_BASE_CEF_WEAK_PTR_H_
|
|
|
|
#define CEF_INCLUDE_BASE_CEF_WEAK_PTR_H_
|
|
|
|
#pragma once
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
#if defined(USING_CHROMIUM_INCLUDES)
|
2014-07-15 00:18:51 +02:00
|
|
|
// When building CEF include the Chromium header directly.
|
|
|
|
#include "base/memory/weak_ptr.h"
|
2016-03-16 03:55:59 +01:00
|
|
|
#else // !USING_CHROMIUM_INCLUDES
|
2014-07-15 00:18:51 +02:00
|
|
|
// The following is substantially similar to the Chromium implementation.
|
|
|
|
// If the Chromium implementation diverges the below implementation should be
|
|
|
|
// updated to match.
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
#include <cstddef>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#include "include/base/cef_atomic_flag.h"
|
2014-07-15 00:18:51 +02:00
|
|
|
#include "include/base/cef_logging.h"
|
|
|
|
#include "include/base/cef_ref_counted.h"
|
|
|
|
#include "include/base/cef_thread_checker.h"
|
|
|
|
|
|
|
|
namespace base {
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
template <typename T>
|
|
|
|
class SupportsWeakPtr;
|
|
|
|
template <typename T>
|
|
|
|
class WeakPtr;
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
namespace internal {
|
2014-07-15 00:18:51 +02:00
|
|
|
// These classes are part of the WeakPtr implementation.
|
|
|
|
// DO NOT USE THESE CLASSES DIRECTLY YOURSELF.
|
|
|
|
|
|
|
|
class WeakReference {
|
|
|
|
public:
|
2021-06-17 21:40:57 +02:00
|
|
|
// Although Flag is bound to a specific ThreaddTaskRunner, it may be
|
|
|
|
// deleted from another via base::WeakPtr::~WeakPtr().
|
2014-07-15 00:18:51 +02:00
|
|
|
class Flag : public RefCountedThreadSafe<Flag> {
|
|
|
|
public:
|
|
|
|
Flag();
|
|
|
|
|
|
|
|
void Invalidate();
|
|
|
|
bool IsValid() const;
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
bool MaybeValid() const;
|
|
|
|
|
|
|
|
void DetachFromThread();
|
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
private:
|
|
|
|
friend class base::RefCountedThreadSafe<Flag>;
|
|
|
|
|
|
|
|
~Flag();
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
base::ThreadChecker thread_checker_;
|
|
|
|
AtomicFlag invalidated_;
|
2014-07-15 00:18:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
WeakReference();
|
2021-06-17 21:40:57 +02:00
|
|
|
explicit WeakReference(const scoped_refptr<Flag>& flag);
|
2014-07-15 00:18:51 +02:00
|
|
|
~WeakReference();
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakReference(WeakReference&& other) noexcept;
|
|
|
|
WeakReference(const WeakReference& other);
|
|
|
|
WeakReference& operator=(WeakReference&& other) noexcept = default;
|
|
|
|
WeakReference& operator=(const WeakReference& other) = default;
|
|
|
|
|
|
|
|
bool IsValid() const;
|
|
|
|
bool MaybeValid() const;
|
2014-07-15 00:18:51 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
scoped_refptr<const Flag> flag_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class WeakReferenceOwner {
|
|
|
|
public:
|
|
|
|
WeakReferenceOwner();
|
|
|
|
~WeakReferenceOwner();
|
|
|
|
|
|
|
|
WeakReference GetRef() const;
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
bool HasRefs() const { return !flag_->HasOneRef(); }
|
2014-07-15 00:18:51 +02:00
|
|
|
|
|
|
|
void Invalidate();
|
|
|
|
|
|
|
|
private:
|
2021-06-17 21:40:57 +02:00
|
|
|
scoped_refptr<WeakReference::Flag> flag_;
|
2014-07-15 00:18:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// This class simplifies the implementation of WeakPtr's type conversion
|
|
|
|
// constructor by avoiding the need for a public accessor for ref_. A
|
|
|
|
// WeakPtr<T> cannot access the private members of WeakPtr<U>, so this
|
|
|
|
// base class gives us a way to access ref_ in a protected fashion.
|
|
|
|
class WeakPtrBase {
|
|
|
|
public:
|
|
|
|
WeakPtrBase();
|
|
|
|
~WeakPtrBase();
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakPtrBase(const WeakPtrBase& other) = default;
|
|
|
|
WeakPtrBase(WeakPtrBase&& other) noexcept = default;
|
|
|
|
WeakPtrBase& operator=(const WeakPtrBase& other) = default;
|
|
|
|
WeakPtrBase& operator=(WeakPtrBase&& other) noexcept = default;
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
ref_ = internal::WeakReference();
|
|
|
|
ptr_ = 0;
|
|
|
|
}
|
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
protected:
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakPtrBase(const WeakReference& ref, uintptr_t ptr);
|
2014-07-15 00:18:51 +02:00
|
|
|
|
|
|
|
WeakReference ref_;
|
2021-06-17 21:40:57 +02:00
|
|
|
|
|
|
|
// This pointer is only valid when ref_.is_valid() is true. Otherwise, its
|
|
|
|
// value is undefined (as opposed to nullptr).
|
|
|
|
uintptr_t ptr_;
|
2014-07-15 00:18:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// This class provides a common implementation of common functions that would
|
|
|
|
// otherwise get instantiated separately for each distinct instantiation of
|
|
|
|
// SupportsWeakPtr<>.
|
|
|
|
class SupportsWeakPtrBase {
|
|
|
|
public:
|
|
|
|
// A safe static downcast of a WeakPtr<Base> to WeakPtr<Derived>. This
|
|
|
|
// conversion will only compile if there is exists a Base which inherits
|
|
|
|
// from SupportsWeakPtr<Base>. See base::AsWeakPtr() below for a helper
|
|
|
|
// function that makes calling this easier.
|
2021-06-17 21:40:57 +02:00
|
|
|
//
|
|
|
|
// Precondition: t != nullptr
|
2017-05-17 11:29:28 +02:00
|
|
|
template <typename Derived>
|
2014-07-15 00:18:51 +02:00
|
|
|
static WeakPtr<Derived> StaticAsWeakPtr(Derived* t) {
|
2021-06-17 21:40:57 +02:00
|
|
|
static_assert(
|
|
|
|
std::is_base_of<internal::SupportsWeakPtrBase, Derived>::value,
|
|
|
|
"AsWeakPtr argument must inherit from SupportsWeakPtr");
|
|
|
|
return AsWeakPtrImpl<Derived>(t);
|
2014-07-15 00:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// This template function uses type inference to find a Base of Derived
|
|
|
|
// which is an instance of SupportsWeakPtr<Base>. We can then safely
|
|
|
|
// static_cast the Base* to a Derived*.
|
|
|
|
template <typename Derived, typename Base>
|
2021-06-17 21:40:57 +02:00
|
|
|
static WeakPtr<Derived> AsWeakPtrImpl(SupportsWeakPtr<Base>* t) {
|
|
|
|
WeakPtr<Base> ptr = t->AsWeakPtr();
|
|
|
|
return WeakPtr<Derived>(
|
|
|
|
ptr.ref_, static_cast<Derived*>(reinterpret_cast<Base*>(ptr.ptr_)));
|
2014-07-15 00:18:51 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
} // namespace internal
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
template <typename T>
|
|
|
|
class WeakPtrFactory;
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2022-09-05 22:17:57 +02:00
|
|
|
///
|
|
|
|
/// The WeakPtr class holds a weak reference to |T*|.
|
|
|
|
///
|
|
|
|
/// This class is designed to be used like a normal pointer. You should always
|
|
|
|
/// null-test an object of this class before using it or invoking a method that
|
|
|
|
/// may result in the underlying object being destroyed.
|
|
|
|
///
|
|
|
|
/// EXAMPLE:
|
|
|
|
///
|
|
|
|
/// <pre>
|
|
|
|
/// class Foo { ... };
|
|
|
|
/// WeakPtr<Foo> foo;
|
|
|
|
/// if (foo)
|
|
|
|
/// foo->method();
|
|
|
|
/// </pre>
|
|
|
|
///
|
2014-07-15 00:18:51 +02:00
|
|
|
template <typename T>
|
2021-06-17 21:40:57 +02:00
|
|
|
class WeakPtr : public internal::WeakPtrBase {
|
2014-07-15 00:18:51 +02:00
|
|
|
public:
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakPtr() = default;
|
|
|
|
WeakPtr(std::nullptr_t) {}
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2022-09-05 22:17:57 +02:00
|
|
|
///
|
|
|
|
/// Allow conversion from U to T provided U "is a" T. Note that this
|
|
|
|
/// is separate from the (implicit) copy and move constructors.
|
|
|
|
///
|
2021-06-17 21:40:57 +02:00
|
|
|
template <typename U>
|
|
|
|
WeakPtr(const WeakPtr<U>& other) : WeakPtrBase(other) {
|
|
|
|
// Need to cast from U* to T* to do pointer adjustment in case of multiple
|
|
|
|
// inheritance. This also enforces the "U is a T" rule.
|
|
|
|
T* t = reinterpret_cast<U*>(other.ptr_);
|
|
|
|
ptr_ = reinterpret_cast<uintptr_t>(t);
|
|
|
|
}
|
2014-07-15 00:18:51 +02:00
|
|
|
template <typename U>
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakPtr(WeakPtr<U>&& other) noexcept : WeakPtrBase(std::move(other)) {
|
|
|
|
// Need to cast from U* to T* to do pointer adjustment in case of multiple
|
|
|
|
// inheritance. This also enforces the "U is a T" rule.
|
|
|
|
T* t = reinterpret_cast<U*>(other.ptr_);
|
|
|
|
ptr_ = reinterpret_cast<uintptr_t>(t);
|
|
|
|
}
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
T* get() const {
|
|
|
|
return ref_.IsValid() ? reinterpret_cast<T*>(ptr_) : nullptr;
|
|
|
|
}
|
2014-07-15 00:18:51 +02:00
|
|
|
|
|
|
|
T& operator*() const {
|
2021-06-17 21:40:57 +02:00
|
|
|
CHECK(ref_.IsValid());
|
2014-07-15 00:18:51 +02:00
|
|
|
return *get();
|
|
|
|
}
|
|
|
|
T* operator->() const {
|
2021-06-17 21:40:57 +02:00
|
|
|
CHECK(ref_.IsValid());
|
2014-07-15 00:18:51 +02:00
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
2022-09-05 22:17:57 +02:00
|
|
|
///
|
|
|
|
/// Allow conditionals to test validity, e.g. `if (weak_ptr) {...}`;
|
|
|
|
///
|
2021-06-17 21:40:57 +02:00
|
|
|
explicit operator bool() const { return get() != nullptr; }
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2022-09-05 22:17:57 +02:00
|
|
|
///
|
|
|
|
/// Returns false if the WeakPtr is confirmed to be invalid. This call is safe
|
|
|
|
/// to make from any thread, e.g. to optimize away unnecessary work, but
|
|
|
|
/// operator bool() must always be called, on the correct thread, before
|
|
|
|
/// actually using the pointer.
|
|
|
|
///
|
|
|
|
/// Warning: as with any object, this call is only thread-safe if the WeakPtr
|
|
|
|
/// instance isn't being re-assigned or reset() racily with this call.
|
|
|
|
///
|
2021-06-17 21:40:57 +02:00
|
|
|
bool MaybeValid() const { return ref_.MaybeValid(); }
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2022-09-05 22:17:57 +02:00
|
|
|
///
|
|
|
|
/// Returns whether the object |this| points to has been invalidated. This can
|
|
|
|
/// be used to distinguish a WeakPtr to a destroyed object from one that has
|
|
|
|
/// been explicitly set to null.
|
|
|
|
///
|
2021-06-17 21:40:57 +02:00
|
|
|
bool WasInvalidated() const { return ptr_ && !ref_.IsValid(); }
|
2014-07-15 00:18:51 +02:00
|
|
|
|
|
|
|
private:
|
2021-06-17 21:40:57 +02:00
|
|
|
friend class internal::SupportsWeakPtrBase;
|
2017-05-17 11:29:28 +02:00
|
|
|
template <typename U>
|
|
|
|
friend class WeakPtr;
|
2014-07-15 00:18:51 +02:00
|
|
|
friend class SupportsWeakPtr<T>;
|
|
|
|
friend class WeakPtrFactory<T>;
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakPtr(const internal::WeakReference& ref, T* ptr)
|
|
|
|
: WeakPtrBase(ref, reinterpret_cast<uintptr_t>(ptr)) {}
|
|
|
|
};
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2022-09-05 22:17:57 +02:00
|
|
|
///
|
|
|
|
/// Allow callers to compare WeakPtrs against nullptr to test validity.
|
|
|
|
///
|
2021-06-17 21:40:57 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
class WeakPtrFactoryBase {
|
|
|
|
protected:
|
|
|
|
WeakPtrFactoryBase(uintptr_t ptr);
|
|
|
|
~WeakPtrFactoryBase();
|
|
|
|
internal::WeakReferenceOwner weak_reference_owner_;
|
|
|
|
uintptr_t ptr_;
|
2014-07-15 00:18:51 +02:00
|
|
|
};
|
2021-06-17 21:40:57 +02:00
|
|
|
} // namespace internal
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2022-09-05 22:17:57 +02:00
|
|
|
///
|
|
|
|
/// 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 useful
|
|
|
|
/// when working with primitive types. For example, you could have a
|
|
|
|
/// WeakPtrFactory<bool> that is used to pass around a weak reference to a
|
|
|
|
/// bool.
|
|
|
|
///
|
2014-07-15 00:18:51 +02:00
|
|
|
template <class T>
|
2021-06-17 21:40:57 +02:00
|
|
|
class WeakPtrFactory : public internal::WeakPtrFactoryBase {
|
2014-07-15 00:18:51 +02:00
|
|
|
public:
|
2021-12-06 23:26:35 +01:00
|
|
|
WeakPtrFactory() = delete;
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
explicit WeakPtrFactory(T* ptr)
|
|
|
|
: WeakPtrFactoryBase(reinterpret_cast<uintptr_t>(ptr)) {}
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-12-06 23:26:35 +01:00
|
|
|
WeakPtrFactory(const WeakPtrFactory&) = delete;
|
|
|
|
WeakPtrFactory& operator=(const WeakPtrFactory&) = delete;
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
~WeakPtrFactory() = default;
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakPtr<T> GetWeakPtr() const {
|
|
|
|
return WeakPtr<T>(weak_reference_owner_.GetRef(),
|
|
|
|
reinterpret_cast<T*>(ptr_));
|
2014-07-15 00:18:51 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 22:17:57 +02:00
|
|
|
///
|
|
|
|
/// Call this method to invalidate all existing weak pointers.
|
|
|
|
///
|
2014-07-15 00:18:51 +02:00
|
|
|
void InvalidateWeakPtrs() {
|
|
|
|
DCHECK(ptr_);
|
|
|
|
weak_reference_owner_.Invalidate();
|
|
|
|
}
|
|
|
|
|
2022-09-05 22:17:57 +02:00
|
|
|
///
|
|
|
|
/// Call this method to determine if any weak pointers exist.
|
|
|
|
///
|
2014-07-15 00:18:51 +02:00
|
|
|
bool HasWeakPtrs() const {
|
|
|
|
DCHECK(ptr_);
|
|
|
|
return weak_reference_owner_.HasRefs();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-05 22:17:57 +02:00
|
|
|
///
|
|
|
|
/// A class may extend from SupportsWeakPtr to let others take weak pointers to
|
|
|
|
/// it. This avoids the class itself implementing boilerplate to dispense weak
|
|
|
|
/// pointers. However, since SupportsWeakPtr's destructor won't invalidate
|
|
|
|
/// weak pointers to the class until after the derived class' members have been
|
|
|
|
/// destroyed, its use can lead to subtle use-after-destroy issues.
|
|
|
|
///
|
2014-07-15 00:18:51 +02:00
|
|
|
template <class T>
|
2021-06-17 21:40:57 +02:00
|
|
|
class SupportsWeakPtr : public internal::SupportsWeakPtrBase {
|
2014-07-15 00:18:51 +02:00
|
|
|
public:
|
2021-06-17 21:40:57 +02:00
|
|
|
SupportsWeakPtr() = default;
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-12-06 23:26:35 +01:00
|
|
|
SupportsWeakPtr(const SupportsWeakPtr&) = delete;
|
|
|
|
SupportsWeakPtr& operator=(const SupportsWeakPtr&) = delete;
|
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
WeakPtr<T> AsWeakPtr() {
|
|
|
|
return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2021-06-17 21:40:57 +02:00
|
|
|
~SupportsWeakPtr() = default;
|
2014-07-15 00:18:51 +02:00
|
|
|
|
|
|
|
private:
|
2021-06-17 21:40:57 +02:00
|
|
|
internal::WeakReferenceOwner weak_reference_owner_;
|
2014-07-15 00:18:51 +02:00
|
|
|
};
|
|
|
|
|
2022-09-05 22:17:57 +02:00
|
|
|
///
|
|
|
|
/// Helper function that uses type deduction to safely return a WeakPtr<Derived>
|
|
|
|
/// when Derived doesn't directly extend SupportsWeakPtr<Derived>, instead it
|
|
|
|
/// extends a Base that extends SupportsWeakPtr<Base>.
|
|
|
|
///
|
|
|
|
/// EXAMPLE:
|
|
|
|
/// <pre>
|
|
|
|
/// class Base : public base::SupportsWeakPtr<Producer> {};
|
|
|
|
/// class Derived : public Base {};
|
|
|
|
///
|
|
|
|
/// Derived derived;
|
|
|
|
/// base::WeakPtr<Derived> ptr = base::AsWeakPtr(&derived);
|
|
|
|
/// </pre>
|
|
|
|
///
|
|
|
|
/// Note that the following doesn't work (invalid type conversion) since
|
|
|
|
/// Derived::AsWeakPtr() is WeakPtr<Base> SupportsWeakPtr<Base>::AsWeakPtr(),
|
|
|
|
/// and there's no way to safely cast WeakPtr<Base> to WeakPtr<Derived> at
|
|
|
|
/// the caller.
|
|
|
|
///
|
|
|
|
/// <pre>
|
|
|
|
/// base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails.
|
|
|
|
/// </pre>
|
|
|
|
///
|
2014-07-15 00:18:51 +02:00
|
|
|
template <typename Derived>
|
|
|
|
WeakPtr<Derived> AsWeakPtr(Derived* t) {
|
2021-06-17 21:40:57 +02:00
|
|
|
return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t);
|
2014-07-15 00:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace base
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
#endif // !USING_CHROMIUM_INCLUDES
|
2014-07-15 00:18:51 +02:00
|
|
|
|
|
|
|
#endif // CEF_INCLUDE_BASE_CEF_WEAK_PTR_H_
|