mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
Update include/base headers for C++11/14 (see issue #3140)
See the issue for update guidelines.
This commit is contained in:
@ -29,7 +29,7 @@
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// Weak pointers are pointers to an object that do not affect its lifetime,
|
||||
// and which may be invalidated (i.e. reset to NULL) by the object, or its
|
||||
// and which 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
|
||||
@ -42,25 +42,24 @@
|
||||
//
|
||||
// class Controller {
|
||||
// public:
|
||||
// Controller() : weak_factory_(this) {}
|
||||
// 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_;
|
||||
// WeakPtrFactory<Controller> weak_factory_{this};
|
||||
// };
|
||||
//
|
||||
// class Worker {
|
||||
// public:
|
||||
// static void StartNew(const WeakPtr<Controller>& controller) {
|
||||
// Worker* worker = new Worker(controller);
|
||||
// static void StartNew(WeakPtr<Controller> controller) {
|
||||
// Worker* worker = new Worker(std::move(controller));
|
||||
// // Kick off asynchronous processing...
|
||||
// }
|
||||
// private:
|
||||
// Worker(const WeakPtr<Controller>& controller)
|
||||
// : controller_(controller) {}
|
||||
// Worker(WeakPtr<Controller> controller)
|
||||
// : controller_(std::move(controller)) {}
|
||||
// void DidCompleteAsynchronousProcessing(const Result& result) {
|
||||
// if (controller_)
|
||||
// controller_->WorkComplete(result);
|
||||
@ -75,18 +74,19 @@
|
||||
// ------------------------- IMPORTANT: Thread-safety -------------------------
|
||||
|
||||
// Weak pointers may be passed safely between threads, but must always be
|
||||
// dereferenced and invalidated on the same thread otherwise checking the
|
||||
// pointer would be racey.
|
||||
// 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, and cannot be dereferenced or invalidated on any other thread. Bound
|
||||
// WeakPtrs can still be handed off to other threads, e.g. to use to post tasks
|
||||
// back to object on the bound thread.
|
||||
// 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 SequencedTaskRunner/Thread. The WeakPtrFactory may then be
|
||||
// destroyed, or new WeakPtr objects may be used, from a different sequence.
|
||||
// unbound from the ThreaddTaskRunner/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
|
||||
@ -96,12 +96,7 @@
|
||||
#define CEF_INCLUDE_BASE_CEF_WEAK_PTR_H_
|
||||
#pragma once
|
||||
|
||||
#if defined(BASE_MEMORY_WEAK_PTR_H_)
|
||||
// Do nothing if the Chromium header has already been included.
|
||||
// This can happen in cases where Chromium code is used directly by the
|
||||
// client application. When using Chromium code directly always include
|
||||
// the Chromium header first to avoid type conflicts.
|
||||
#elif defined(USING_CHROMIUM_INCLUDES)
|
||||
#if defined(USING_CHROMIUM_INCLUDES)
|
||||
// When building CEF include the Chromium header directly.
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#else // !USING_CHROMIUM_INCLUDES
|
||||
@ -109,10 +104,13 @@
|
||||
// If the Chromium implementation diverges the below implementation should be
|
||||
// updated to match.
|
||||
|
||||
#include "include/base/cef_basictypes.h"
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
|
||||
#include "include/base/cef_atomic_flag.h"
|
||||
#include "include/base/cef_logging.h"
|
||||
#include "include/base/cef_macros.h"
|
||||
#include "include/base/cef_ref_counted.h"
|
||||
#include "include/base/cef_template_util.h"
|
||||
#include "include/base/cef_thread_checker.h"
|
||||
|
||||
namespace base {
|
||||
@ -122,14 +120,14 @@ class SupportsWeakPtr;
|
||||
template <typename T>
|
||||
class WeakPtr;
|
||||
|
||||
namespace cef_internal {
|
||||
namespace internal {
|
||||
// These classes are part of the WeakPtr implementation.
|
||||
// DO NOT USE THESE CLASSES DIRECTLY YOURSELF.
|
||||
|
||||
class WeakReference {
|
||||
public:
|
||||
// Although Flag is bound to a specific thread, it may be deleted from another
|
||||
// via base::WeakPtr::~WeakPtr().
|
||||
// Although Flag is bound to a specific ThreaddTaskRunner, it may be
|
||||
// deleted from another via base::WeakPtr::~WeakPtr().
|
||||
class Flag : public RefCountedThreadSafe<Flag> {
|
||||
public:
|
||||
Flag();
|
||||
@ -137,23 +135,30 @@ class WeakReference {
|
||||
void Invalidate();
|
||||
bool IsValid() const;
|
||||
|
||||
bool MaybeValid() const;
|
||||
|
||||
void DetachFromThread();
|
||||
|
||||
private:
|
||||
friend class base::RefCountedThreadSafe<Flag>;
|
||||
|
||||
~Flag();
|
||||
|
||||
// The current Chromium implementation uses SequenceChecker instead of
|
||||
// ThreadChecker to support SequencedWorkerPools. CEF does not yet expose
|
||||
// the concept of SequencedWorkerPools.
|
||||
ThreadChecker thread_checker_;
|
||||
bool is_valid_;
|
||||
base::ThreadChecker thread_checker_;
|
||||
AtomicFlag invalidated_;
|
||||
};
|
||||
|
||||
WeakReference();
|
||||
explicit WeakReference(const Flag* flag);
|
||||
explicit WeakReference(const scoped_refptr<Flag>& flag);
|
||||
~WeakReference();
|
||||
|
||||
bool is_valid() const;
|
||||
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;
|
||||
|
||||
private:
|
||||
scoped_refptr<const Flag> flag_;
|
||||
@ -166,12 +171,12 @@ class WeakReferenceOwner {
|
||||
|
||||
WeakReference GetRef() const;
|
||||
|
||||
bool HasRefs() const { return flag_.get() && !flag_->HasOneRef(); }
|
||||
bool HasRefs() const { return !flag_->HasOneRef(); }
|
||||
|
||||
void Invalidate();
|
||||
|
||||
private:
|
||||
mutable scoped_refptr<WeakReference::Flag> flag_;
|
||||
scoped_refptr<WeakReference::Flag> flag_;
|
||||
};
|
||||
|
||||
// This class simplifies the implementation of WeakPtr's type conversion
|
||||
@ -183,10 +188,24 @@ class WeakPtrBase {
|
||||
WeakPtrBase();
|
||||
~WeakPtrBase();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
protected:
|
||||
explicit WeakPtrBase(const WeakReference& ref);
|
||||
WeakPtrBase(const WeakReference& ref, uintptr_t ptr);
|
||||
|
||||
WeakReference ref_;
|
||||
|
||||
// This pointer is only valid when ref_.is_valid() is true. Otherwise, its
|
||||
// value is undefined (as opposed to nullptr).
|
||||
uintptr_t ptr_;
|
||||
};
|
||||
|
||||
// This class provides a common implementation of common functions that would
|
||||
@ -198,13 +217,14 @@ class SupportsWeakPtrBase {
|
||||
// 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.
|
||||
//
|
||||
// Precondition: t != nullptr
|
||||
template <typename Derived>
|
||||
static WeakPtr<Derived> StaticAsWeakPtr(Derived* t) {
|
||||
typedef is_convertible<Derived, cef_internal::SupportsWeakPtrBase&>
|
||||
convertible;
|
||||
COMPILE_ASSERT(convertible::value,
|
||||
AsWeakPtr_argument_inherits_from_SupportsWeakPtr);
|
||||
return AsWeakPtrImpl<Derived>(t, *t);
|
||||
static_assert(
|
||||
std::is_base_of<internal::SupportsWeakPtrBase, Derived>::value,
|
||||
"AsWeakPtr argument must inherit from SupportsWeakPtr");
|
||||
return AsWeakPtrImpl<Derived>(t);
|
||||
}
|
||||
|
||||
private:
|
||||
@ -212,14 +232,14 @@ class SupportsWeakPtrBase {
|
||||
// which is an instance of SupportsWeakPtr<Base>. We can then safely
|
||||
// static_cast the Base* to a Derived*.
|
||||
template <typename Derived, typename Base>
|
||||
static WeakPtr<Derived> AsWeakPtrImpl(Derived* t,
|
||||
const SupportsWeakPtr<Base>&) {
|
||||
WeakPtr<Base> ptr = t->Base::AsWeakPtr();
|
||||
return WeakPtr<Derived>(ptr.ref_, static_cast<Derived*>(ptr.ptr_));
|
||||
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_)));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace cef_internal
|
||||
} // namespace internal
|
||||
|
||||
template <typename T>
|
||||
class WeakPtrFactory;
|
||||
@ -238,81 +258,113 @@ class WeakPtrFactory;
|
||||
// foo->method();
|
||||
//
|
||||
template <typename T>
|
||||
class WeakPtr : public cef_internal::WeakPtrBase {
|
||||
class WeakPtr : public internal::WeakPtrBase {
|
||||
public:
|
||||
WeakPtr() : ptr_(NULL) {}
|
||||
WeakPtr() = default;
|
||||
WeakPtr(std::nullptr_t) {}
|
||||
|
||||
// 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_) {}
|
||||
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);
|
||||
}
|
||||
template <typename U>
|
||||
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);
|
||||
}
|
||||
|
||||
T* get() const { return ref_.is_valid() ? ptr_ : NULL; }
|
||||
T* get() const {
|
||||
return ref_.IsValid() ? reinterpret_cast<T*>(ptr_) : nullptr;
|
||||
}
|
||||
|
||||
T& operator*() const {
|
||||
CHECK(ref_.is_valid());
|
||||
CHECK(ref_.IsValid());
|
||||
return *get();
|
||||
}
|
||||
T* operator->() const {
|
||||
CHECK(ref_.is_valid());
|
||||
CHECK(ref_.IsValid());
|
||||
return get();
|
||||
}
|
||||
|
||||
// Allow WeakPtr<element_type> to be used in boolean expressions, but not
|
||||
// implicitly convertible to a real bool (which is dangerous).
|
||||
// Allow conditionals to test validity, e.g. if (weak_ptr) {...};
|
||||
explicit operator bool() const { return get() != nullptr; }
|
||||
|
||||
// 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.
|
||||
//
|
||||
// Note that this trick is only safe when the == and != operators
|
||||
// are declared explicitly, as otherwise "weak_ptr1 == weak_ptr2"
|
||||
// will compile but do the wrong thing (i.e., convert to Testable
|
||||
// and then do the comparison).
|
||||
private:
|
||||
typedef T* WeakPtr::*Testable;
|
||||
// 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.
|
||||
bool MaybeValid() const { return ref_.MaybeValid(); }
|
||||
|
||||
public:
|
||||
operator Testable() const { return get() ? &WeakPtr::ptr_ : NULL; }
|
||||
|
||||
void reset() {
|
||||
ref_ = cef_internal::WeakReference();
|
||||
ptr_ = NULL;
|
||||
}
|
||||
// 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.
|
||||
bool WasInvalidated() const { return ptr_ && !ref_.IsValid(); }
|
||||
|
||||
private:
|
||||
// Explicitly declare comparison operators as required by the bool
|
||||
// trick, but keep them private.
|
||||
template <class U>
|
||||
bool operator==(WeakPtr<U> const&) const;
|
||||
template <class U>
|
||||
bool operator!=(WeakPtr<U> const&) const;
|
||||
|
||||
friend class cef_internal::SupportsWeakPtrBase;
|
||||
friend class internal::SupportsWeakPtrBase;
|
||||
template <typename U>
|
||||
friend class WeakPtr;
|
||||
friend class SupportsWeakPtr<T>;
|
||||
friend class WeakPtrFactory<T>;
|
||||
|
||||
WeakPtr(const cef_internal::WeakReference& ref, T* ptr)
|
||||
: WeakPtrBase(ref), ptr_(ptr) {}
|
||||
|
||||
// This pointer is only valid when ref_.is_valid() is true. Otherwise, its
|
||||
// value is undefined (as opposed to NULL).
|
||||
T* ptr_;
|
||||
WeakPtr(const internal::WeakReference& ref, T* ptr)
|
||||
: WeakPtrBase(ref, reinterpret_cast<uintptr_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;
|
||||
}
|
||||
|
||||
namespace internal {
|
||||
class WeakPtrFactoryBase {
|
||||
protected:
|
||||
WeakPtrFactoryBase(uintptr_t ptr);
|
||||
~WeakPtrFactoryBase();
|
||||
internal::WeakReferenceOwner weak_reference_owner_;
|
||||
uintptr_t ptr_;
|
||||
};
|
||||
} // namespace internal
|
||||
|
||||
// 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.
|
||||
template <class T>
|
||||
class WeakPtrFactory {
|
||||
class WeakPtrFactory : public internal::WeakPtrFactoryBase {
|
||||
public:
|
||||
explicit WeakPtrFactory(T* ptr) : ptr_(ptr) {}
|
||||
explicit WeakPtrFactory(T* ptr)
|
||||
: WeakPtrFactoryBase(reinterpret_cast<uintptr_t>(ptr)) {}
|
||||
|
||||
~WeakPtrFactory() { ptr_ = NULL; }
|
||||
~WeakPtrFactory() = default;
|
||||
|
||||
WeakPtr<T> GetWeakPtr() {
|
||||
DCHECK(ptr_);
|
||||
return WeakPtr<T>(weak_reference_owner_.GetRef(), ptr_);
|
||||
WeakPtr<T> GetWeakPtr() const {
|
||||
return WeakPtr<T>(weak_reference_owner_.GetRef(),
|
||||
reinterpret_cast<T*>(ptr_));
|
||||
}
|
||||
|
||||
// Call this method to invalidate all existing weak pointers.
|
||||
@ -328,8 +380,6 @@ class WeakPtrFactory {
|
||||
}
|
||||
|
||||
private:
|
||||
cef_internal::WeakReferenceOwner weak_reference_owner_;
|
||||
T* ptr_;
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory);
|
||||
};
|
||||
|
||||
@ -339,19 +389,19 @@ class WeakPtrFactory {
|
||||
// weak pointers to the class until after the derived class' members have been
|
||||
// destroyed, its use can lead to subtle use-after-destroy issues.
|
||||
template <class T>
|
||||
class SupportsWeakPtr : public cef_internal::SupportsWeakPtrBase {
|
||||
class SupportsWeakPtr : public internal::SupportsWeakPtrBase {
|
||||
public:
|
||||
SupportsWeakPtr() {}
|
||||
SupportsWeakPtr() = default;
|
||||
|
||||
WeakPtr<T> AsWeakPtr() {
|
||||
return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this));
|
||||
}
|
||||
|
||||
protected:
|
||||
~SupportsWeakPtr() {}
|
||||
~SupportsWeakPtr() = default;
|
||||
|
||||
private:
|
||||
cef_internal::WeakReferenceOwner weak_reference_owner_;
|
||||
internal::WeakReferenceOwner weak_reference_owner_;
|
||||
DISALLOW_COPY_AND_ASSIGN(SupportsWeakPtr);
|
||||
};
|
||||
|
||||
@ -375,7 +425,7 @@ class SupportsWeakPtr : public cef_internal::SupportsWeakPtrBase {
|
||||
|
||||
template <typename Derived>
|
||||
WeakPtr<Derived> AsWeakPtr(Derived* t) {
|
||||
return cef_internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t);
|
||||
return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t);
|
||||
}
|
||||
|
||||
} // namespace base
|
||||
|
Reference in New Issue
Block a user