template <typename T>

class RefPtr

Defined at line 66 of file ../../src/lib/fxl/memory/ref_ptr.h

A smart pointer class for intrusively reference-counted objects (e.g., those

subclassing |RefCountedThreadSafe| -- see ref_counted.h).

Such objects require *adoption* to obtain the first |RefPtr|, which is

accomplished using |AdoptRef| (see below). (This is due to such objects being

constructed with a reference count of 1. The adoption requirement is

enforced, at least in Debug builds, by assertions.)

E.g., if |Foo| is an intrusively reference-counted class:

// The |AdoptRef| may be put in a static factory method (e.g., if |Foo|'s

// constructor is private).

RefPtr

<Foo

> my_foo_ptr(AdoptRef(new Foo()));

// Now OK, since "my Foo" has been adopted ...

RefPtr

<Foo

> another_ptr_to_my_foo(my_foo_ptr.get());

// ... though this would preferable in this situation.

RefPtr

<Foo

> yet_another_ptr_to_my_foo(my_foo_ptr);

Unlike Chromium's |scoped_refptr|, |RefPtr| is only explicitly constructible

from a plain pointer (and not assignable). It is however implicitly

constructible from |nullptr|. So:

RefPtr

<Foo

> foo(plain_ptr_to_adopted_foo); // OK.

foo = plain_ptr_to_adopted_foo; // Not OK (doesn't compile).

foo = RefPtr

<Foo

>(plain_ptr_to_adopted_foo); // OK.

foo = nullptr; // OK.

And if we have |void MyFunction(RefPtr

<Foo

> foo)|, calling it using

|MyFunction(nullptr)| is also valid.

Implementation note: For copy/move constructors/operator=s, we often have

templated versions, so that the operation can be done on a |RefPtr

<U

>|, where

|U| is a subclass of |T|. However, we also have non-templated versions with

|U = T|, since the templated versions don't count as copy/move

constructors/operator=s for the purposes of causing the default copy

constructor/operator= to be deleted. E.g., if we didn't declare any

non-templated versions, we'd get the default copy constructor/operator= (we'd

only not get the default move constructor/operator= by virtue of having a

destructor)! (In fact, it'd suffice to only declare a non-templated move

constructor or move operator=, which would cause the copy

constructor/operator= to be deleted, but for clarity we include explicit

non-templated versions of everything.)

Public Methods

void RefPtr<T> ()

Defined at line 70 of file ../../src/lib/fxl/memory/ref_ptr.h

void RefPtr<T> (std::nullptr_t )

Defined at line 71 of file ../../src/lib/fxl/memory/ref_ptr.h

template <typename U>
void RefPtr<T> (U * p)

Explicit constructor from a plain pointer (to an object that must have

already been adopted). (Note that in |T::T()|, references to |this| cannot

be taken, since the object being constructed will not have been adopted

yet.)

Defined at line 78 of file ../../src/lib/fxl/memory/ref_ptr.h

void RefPtr<T> (const RefPtr<T> & r)

Copy constructor.

Defined at line 84 of file ../../src/lib/fxl/memory/ref_ptr.h

template <typename U>
void RefPtr<T> (const RefPtr<U> & r)

Defined at line 90 of file ../../src/lib/fxl/memory/ref_ptr.h

void RefPtr<T> (RefPtr<T> && r)

Move constructor.

Defined at line 96 of file ../../src/lib/fxl/memory/ref_ptr.h

template <typename U>
void RefPtr<T> (RefPtr<U> && r)

Defined at line 99 of file ../../src/lib/fxl/memory/ref_ptr.h

void ~RefPtr<T> ()

Destructor.

Defined at line 104 of file ../../src/lib/fxl/memory/ref_ptr.h

T * get ()

Defined at line 109 of file ../../src/lib/fxl/memory/ref_ptr.h

T & operator* ()

Defined at line 111 of file ../../src/lib/fxl/memory/ref_ptr.h

T * operator-> ()

Defined at line 116 of file ../../src/lib/fxl/memory/ref_ptr.h

RefPtr<T> & operator= (const RefPtr<T> & r)

Copy assignment.

Defined at line 122 of file ../../src/lib/fxl/memory/ref_ptr.h

template <typename U>
RefPtr<T> & operator= (const RefPtr<U> & r)

Defined at line 134 of file ../../src/lib/fxl/memory/ref_ptr.h

RefPtr<T> & operator= (RefPtr<T> && r)

Move assignment.

Note: Like |std::shared_ptr|, we support self-move and move assignment is

equivalent to |RefPtr

<T

>(std::move(r)).swap(*this)|.

Defined at line 148 of file ../../src/lib/fxl/memory/ref_ptr.h

template <typename U>
RefPtr<T> & operator= (RefPtr<U> && r)

Defined at line 154 of file ../../src/lib/fxl/memory/ref_ptr.h

void reset ()

Releases any existing reference. We do not have a variant that takes a

pointer to reset to because there would be a temptation to do:

ptr.reset(new Foo);

which would fail the adoption check (the Foo object would have two refs

and this would assert in debug mode as a result). It seems simpler to

force such usage through the constructor and use assignment which people

are more familiar with.

Defined at line 166 of file ../../src/lib/fxl/memory/ref_ptr.h

void swap (RefPtr<T> & r)

Defined at line 175 of file ../../src/lib/fxl/memory/ref_ptr.h

RefPtr<T> Clone ()

Returns a new |RefPtr

<T

>| with the same contents as this pointer. Useful

when a function takes a |RefPtr

<T

>

&

&

| argument and the caller wants to

retain its reference (rather than moving it).

Defined at line 184 of file ../../src/lib/fxl/memory/ref_ptr.h

bool operator bool ()

Defined at line 186 of file ../../src/lib/fxl/memory/ref_ptr.h

template <typename U>
bool operator== (const RefPtr<U> & rhs)

Defined at line 189 of file ../../src/lib/fxl/memory/ref_ptr.h

template <typename U>
bool operator!= (const RefPtr<U> & rhs)

Defined at line 194 of file ../../src/lib/fxl/memory/ref_ptr.h

template <typename U>
bool operator< (const RefPtr<U> & rhs)

Defined at line 199 of file ../../src/lib/fxl/memory/ref_ptr.h

Friends

template <typename T>
RefPtr RefPtr (T * )
template <typename U>
class RefPtr