Chromium Embedded Framework (CEF)  130.1.2+g48f3ef6+chromium-130.0.6723.44
scoped_refptr< T > Class Template Reference

A smart pointer class for reference counted objects. More...

#include "include/base/cef_scoped_refptr.h"

Public Types

typedef T element_type
 

Public Member Functions

constexpr scoped_refptr ()=default
 
constexpr scoped_refptr (std::nullptr_t)
 
 scoped_refptr (T *p)
 
 scoped_refptr (const scoped_refptr &r)
 
template<typename U , typename = typename std::enable_if< std::is_convertible<U*, T*>::value>::type>
 scoped_refptr (const scoped_refptr< U > &r)
 
 scoped_refptr (scoped_refptr &&r) noexcept
 
template<typename U , typename = typename std::enable_if< std::is_convertible<U*, T*>::value>::type>
 scoped_refptr (scoped_refptr< U > &&r) noexcept
 
 ~scoped_refptr ()
 
T * get () const
 
T & operator* () const
 
T * operator-> () const
 
scoped_refptroperator= (std::nullptr_t)
 
scoped_refptroperator= (T *p)
 
scoped_refptroperator= (scoped_refptr r) noexcept
 
void reset ()
 
T * release ()
 
void swap (scoped_refptr &r) noexcept
 
 operator bool () const
 
template<typename U >
bool operator== (const scoped_refptr< U > &rhs) const
 
template<typename U >
bool operator!= (const scoped_refptr< U > &rhs) const
 
template<typename U >
bool operator< (const scoped_refptr< U > &rhs) const
 

Protected Attributes

T * ptr_ = nullptr
 

Friends

class ::base::SequencedTaskRunner
 
class ::base::WrappedPromise
 
template<typename U >
class scoped_refptr
 
template<typename U >
scoped_refptr< U > base::AdoptRef (U *)
 

Detailed Description

template<class T>
class scoped_refptr< T >

A smart pointer class for reference counted objects.

Use this class instead of calling AddRef and Release manually on a reference counted object to avoid common memory leaks caused by forgetting to Release an object reference. Sample usage:

  class MyFoo : public RefCounted<MyFoo> {
   ...
   private:
    friend class RefCounted<MyFoo>;  // Allow destruction by RefCounted<>.
    ~MyFoo();                        // Destructor must be
    private/protected.
  };

  void some_function() {
    scoped_refptr<MyFoo> foo = MakeRefCounted<MyFoo>();
    foo->Method(param);
    // |foo| is released when this function returns
  }

  void some_other_function() {
    scoped_refptr<MyFoo> foo = MakeRefCounted<MyFoo>();
    ...
    foo.reset();  // explicitly releases |foo|
    ...
    if (foo)
      foo->Method(param);
  }

The above examples show how scoped_refptr<T> acts like a pointer to T. Given two scoped_refptr<T> classes, it is also possible to exchange references between the two objects, like so:

  {
    scoped_refptr<MyFoo> a = MakeRefCounted<MyFoo>();
    scoped_refptr<MyFoo> b;

    b.swap(a);
    // now, |b| references the MyFoo object, and |a| references nullptr.
  }

To make both |a| and |b| in the above example reference the same MyFoo object, simply use the assignment operator:

  {
    scoped_refptr<MyFoo> a = MakeRefCounted<MyFoo>();
    scoped_refptr<MyFoo> b;

    b = a;
    // now, |a| and |b| each own a reference to the same MyFoo object.
  }

Also see Chromium's ownership and calling conventions: https://chromium.googlesource.com/chromium/src/+/lkgr/styleguide/c++/c++.md#object-ownership-and-calling-conventions Specifically: If the function (at least sometimes) takes a ref on a refcounted object, declare the param as scoped_refptr<T>. The caller can decide whether it wishes to transfer ownership (by calling std::move(t) when passing t) or retain its ref (by simply passing t directly). In other words, use scoped_refptr like you would a std::unique_ptr except in the odd case where it's required to hold on to a ref while handing one to another component (if a component merely needs to use t on the stack without keeping a ref: pass t as a raw T*).

Member Typedef Documentation

◆ element_type

template<class T >
typedef T scoped_refptr< T >::element_type

Constructor & Destructor Documentation

◆ scoped_refptr() [1/7]

template<class T >
constexpr scoped_refptr< T >::scoped_refptr ( )
constexprdefault

◆ scoped_refptr() [2/7]

template<class T >
constexpr scoped_refptr< T >::scoped_refptr ( std::nullptr_t  )
inlineconstexpr

◆ scoped_refptr() [3/7]

template<class T >
scoped_refptr< T >::scoped_refptr ( T *  p)
inline

◆ scoped_refptr() [4/7]

template<class T >
scoped_refptr< T >::scoped_refptr ( const scoped_refptr< T > &  r)
inline

◆ scoped_refptr() [5/7]

template<class T >
template<typename U , typename = typename std::enable_if< std::is_convertible<U*, T*>::value>::type>
scoped_refptr< T >::scoped_refptr ( const scoped_refptr< U > &  r)
inline

◆ scoped_refptr() [6/7]

template<class T >
scoped_refptr< T >::scoped_refptr ( scoped_refptr< T > &&  r)
inlinenoexcept

◆ scoped_refptr() [7/7]

template<class T >
template<typename U , typename = typename std::enable_if< std::is_convertible<U*, T*>::value>::type>
scoped_refptr< T >::scoped_refptr ( scoped_refptr< U > &&  r)
inlinenoexcept

◆ ~scoped_refptr()

template<class T >
scoped_refptr< T >::~scoped_refptr ( )
inline

Member Function Documentation

◆ get()

template<class T >
T* scoped_refptr< T >::get ( ) const
inline

◆ operator bool()

template<class T >
scoped_refptr< T >::operator bool ( ) const
inlineexplicit

◆ operator!=()

template<class T >
template<typename U >
bool scoped_refptr< T >::operator!= ( const scoped_refptr< U > &  rhs) const
inline

◆ operator*()

template<class T >
T& scoped_refptr< T >::operator* ( ) const
inline

◆ operator->()

template<class T >
T* scoped_refptr< T >::operator-> ( ) const
inline

◆ operator<()

template<class T >
template<typename U >
bool scoped_refptr< T >::operator< ( const scoped_refptr< U > &  rhs) const
inline

◆ operator=() [1/3]

template<class T >
scoped_refptr& scoped_refptr< T >::operator= ( scoped_refptr< T >  r)
inlinenoexcept

◆ operator=() [2/3]

template<class T >
scoped_refptr& scoped_refptr< T >::operator= ( std::nullptr_t  )
inline

◆ operator=() [3/3]

template<class T >
scoped_refptr& scoped_refptr< T >::operator= ( T *  p)
inline

◆ operator==()

template<class T >
template<typename U >
bool scoped_refptr< T >::operator== ( const scoped_refptr< U > &  rhs) const
inline

◆ release()

template<typename T >
T * scoped_refptr< T >::release

◆ reset()

template<class T >
void scoped_refptr< T >::reset ( )
inline

◆ swap()

template<class T >
void scoped_refptr< T >::swap ( scoped_refptr< T > &  r)
inlinenoexcept

Friends And Related Function Documentation

◆ ::base::SequencedTaskRunner

template<class T >
friend class ::base::SequencedTaskRunner
friend

◆ ::base::WrappedPromise

template<class T >
friend class ::base::WrappedPromise
friend

◆ base::AdoptRef

template<class T >
template<typename U >
scoped_refptr<U> base::AdoptRef ( U *  )
friend

◆ scoped_refptr

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

Member Data Documentation

◆ ptr_

template<class T >
T* scoped_refptr< T >::ptr_ = nullptr
protected

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