Chromium Embedded Framework (CEF) 148.0.4+ga59e378+chromium-148.0.7778.40
base::WeakPtr< T > Class Template Reference

The WeakPtr class holds a weak reference to |T*|. More...

#include "include/base/cef_weak_ptr.h"

Public Member Functions

 WeakPtr ()=default
 
 WeakPtr (std::nullptr_t)
 
template<typename U >
requires (std::convertible_to<U*, T*>)
 WeakPtr (const WeakPtr< U > &other)
 Allow conversion from U to T provided U "is a" T.
 
template<typename U >
requires (std::convertible_to<U*, T*>)
WeakPtroperator= (const WeakPtr< U > &other)
 
template<typename U >
requires (std::convertible_to<U*, T*>)
 WeakPtr (WeakPtr< U > &&other)
 
template<typename U >
requires (std::convertible_to<U*, T*>)
WeakPtroperator= (WeakPtr< U > &&other)
 
T * get () const
 
T & operator* () const
 Provide access to the underlying T as a reference.
 
T * operator-> () const
 Used to call methods on the underlying T.
 
 operator bool () const
 Allow conditionals to test validity, e.g.
 
void reset ()
 Resets the WeakPtr to hold nothing.
 
bool MaybeValid () const
 Do not use this method.
 
bool WasInvalidated () const
 Returns whether the object |this| points to has been invalidated.
 

Friends

template<typename U >
class WeakPtr
 
class WeakPtrFactory< T >
 
class WeakPtrFactory< std::remove_const_t< T > >
 

Detailed Description

template<typename T>
class base::WeakPtr< T >

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:

  class Foo { ... };
  WeakPtr<Foo> foo;
  if (foo)
    foo->method();

WeakPtr intentionally doesn't implement operator== or operator<=>, because comparisons of weak references are inherently unstable. If the comparison takes validity into account, the result can change at any time as pointers are invalidated. If it depends only on the underlying pointer value, even after the pointer is invalidated, unrelated WeakPtrs can unexpectedly compare equal if the address is reused.

Constructor & Destructor Documentation

◆ WeakPtr() [1/4]

template<typename T >
base::WeakPtr< T >::WeakPtr ( )
default

◆ WeakPtr() [2/4]

template<typename T >
base::WeakPtr< T >::WeakPtr ( std::nullptr_t  )
inline

◆ WeakPtr() [3/4]

template<typename T >
template<typename U >
requires (std::convertible_to<U*, T*>)
base::WeakPtr< T >::WeakPtr ( const WeakPtr< U > &  other)
inline

Allow conversion from U to T provided U "is a" T.

Note that this is separate from the (implicit) copy and move constructors.

◆ WeakPtr() [4/4]

template<typename T >
template<typename U >
requires (std::convertible_to<U*, T*>)
base::WeakPtr< T >::WeakPtr ( WeakPtr< U > &&  other)
inline

Member Function Documentation

◆ get()

template<typename T >
T * base::WeakPtr< T >::get ( ) const
inline

◆ MaybeValid()

template<typename T >
bool base::WeakPtr< T >::MaybeValid ( ) const
inline

Do not use this method.

Almost all callers should instead use operator bool().

There are a few rare cases where the caller intentionally needs to check validity of a base::WeakPtr on a thread different from the bound thread as an unavoidable performance optimization.

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 IsValid() 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.

◆ operator bool()

template<typename T >
base::WeakPtr< T >::operator bool ( ) const
inlineexplicit

Allow conditionals to test validity, e.g.

if (weak_ptr) {...};

◆ operator*()

template<typename T >
T & base::WeakPtr< T >::operator* ( ) const
inline

Provide access to the underlying T as a reference.

Will CHECK() if the T pointee is no longer alive.

◆ operator->()

template<typename T >
T * base::WeakPtr< T >::operator-> ( ) const
inline

Used to call methods on the underlying T.

Will CHECK() if the T pointee is no longer alive.

◆ operator=() [1/2]

template<typename T >
template<typename U >
requires (std::convertible_to<U*, T*>)
WeakPtr & base::WeakPtr< T >::operator= ( const WeakPtr< U > &  other)
inline

◆ operator=() [2/2]

template<typename T >
template<typename U >
requires (std::convertible_to<U*, T*>)
WeakPtr & base::WeakPtr< T >::operator= ( WeakPtr< U > &&  other)
inline

◆ reset()

template<typename T >
void base::WeakPtr< T >::reset ( )
inline

Resets the WeakPtr to hold nothing.

The get() method will return nullptr thereafter, and MaybeValid() will be false.

◆ WasInvalidated()

template<typename T >
bool base::WeakPtr< T >::WasInvalidated ( ) const
inline

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.

Friends And Related Symbol Documentation

◆ WeakPtr

template<typename T >
template<typename U >
friend class WeakPtr
friend

◆ WeakPtrFactory< std::remove_const_t< T > >

template<typename T >
friend class WeakPtrFactory< std::remove_const_t< T > >
friend

◆ WeakPtrFactory< T >

template<typename T >
friend class WeakPtrFactory< T >
friend

The documentation for this class was generated from the following file: