template <typename T>

class WeakPtrFactory

Defined at line 146 of file ../../src/lib/fxl/memory/weak_ptr.h

Class that produces (valid) |WeakPtr

<T

>|s. Typically, this is used as a

member variable of |T| (preferably the last one -- see below), and |T|'s

methods control how weak pointers to it are vended. This class is not

thread-safe, and should only be used on a single thread.

Example:

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 |WeakPtr|s to |Controller| are invalidated before its member

// variables' destructors are executed.

WeakPtrFactory

<Controller

> weak_factory_;

};

class Worker {

public:

static void StartNew(const WeakPtr

<Controller

>

&

controller) {

Worker* worker = new Worker(controller);

// Kick off asynchronous processing....

}

private:

Worker(const WeakPtr

<Controller

>

&

controller) : controller_(controller) {}

void DidCompleteAsynchronousProcessing(const Result

&

result) {

if (controller_)

controller_->WorkComplete(result);

}

WeakPtr

<Controller

> controller_;

};

Public Methods

void WeakPtrFactory<T> (T * ptr)

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

void ~WeakPtrFactory<T> ()

Defined at line 149 of file ../../src/lib/fxl/memory/weak_ptr.h

WeakPtr<T> GetWeakPtr ()

Gets a new weak pointer, which will be valid until either

|InvalidateWeakPtrs()| is called or this object is destroyed.

Defined at line 156 of file ../../src/lib/fxl/memory/weak_ptr.h

void InvalidateWeakPtrs ()

Call this method to invalidate all existing weak pointers. (Note that

additional weak pointers can be produced even after this is called.)

Defined at line 165 of file ../../src/lib/fxl/memory/weak_ptr.h

bool HasWeakPtrs ()

Call this method to determine if any weak pointers exist. (Note that a

"false" result is definitive, but a "true" result may not be if weak

pointers are held/reset/destroyed/reassigned on other threads.)

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