跳转至

cf::WeakPtrFactory

Factory for producing WeakPtr instances for an exclusive owner. More...

#include <weak_ptr_factory.h>

Public Functions

Name
WeakPtrFactory(T * owner)
Constructs a WeakPtrFactory for the given owner.
~WeakPtrFactory()
Destructor: automatically invalidates all issued WeakPtr instances.
WeakPtrFactory(constWeakPtrFactory & ) =delete
Copy constructor: deleted.
WeakPtrFactory & operator=(constWeakPtrFactory & ) =delete
Copy assignment: deleted.
WeakPtrFactory(WeakPtrFactory && ) =delete
Move constructor: deleted.
WeakPtrFactory & operator=(WeakPtrFactory && ) =delete
Move assignment: deleted.
WeakPtr< T > GetWeakPtr() const
Creates a WeakPtr pointing to the owner.
void InvalidateWeakPtrs()
Invalidates all previously issued WeakPtr instances.
bool HasWeakPtrs() const
Checks if any WeakPtr instances are currently held externally.

Detailed Description

template <typenameT >
class cf::WeakPtrFactory;

Factory for producing WeakPtr instances for an exclusive owner.

Template Parameters:

  • T Type of the owner class.

Note: Not copyable or movable. GetWeakPtr() should be called on the same thread where the WeakPtr is used.

Warning: Declare WeakPtrFactory as the LAST member of the owner class so it is destroyed first, invalidating all weak pointers before any other members are destroyed.

WeakPtrFactory should be declared as a member variable of the owner class, as the last member to ensure it is destroyed first (C++ destroys members in reverse declaration order).

class Foo {
public:
    void doSomething() {}

private:
    SomeResource   resource_;   // Destroyed first
    WeakPtrFactory<Foo> weak_factory_{this}; // Destroyed last
};

Foo foo;
WeakPtr<Foo> ref = foo.weak_factory_.GetWeakPtr();
if (ref) {
    ref->doSomething();
}

Public Functions Documentation

function WeakPtrFactory

inline explicit WeakPtrFactory(
    T * owner
)

Constructs a WeakPtrFactory for the given owner.

Parameters:

  • owner Raw pointer to the owner object. Must not be nullptr.

Exceptions:

  • None (assertion failure if owner is nullptr).

Since: N/A

Note: The factory does not take ownership; the owner must outlive the factory or explicitly manage lifetime.

Warning: None

function ~WeakPtrFactory

inline ~WeakPtrFactory()

Destructor: automatically invalidates all issued WeakPtr instances.

function WeakPtrFactory

WeakPtrFactory(
    constWeakPtrFactory & 
) =delete

Copy constructor: deleted.

function operator=

WeakPtrFactory & operator=(
    constWeakPtrFactory & 
) =delete

Copy assignment: deleted.

function WeakPtrFactory

WeakPtrFactory(
    WeakPtrFactory && 
) =delete

Move constructor: deleted.

function operator=

WeakPtrFactory & operator=(
    WeakPtrFactory && 
) =delete

Move assignment: deleted.

function GetWeakPtr

inline WeakPtr< T > GetWeakPtr() const

Creates a WeakPtr pointing to the owner.

Exceptions:

Return: WeakPtr that can be used to safely access the owner.

Since: N/A

Note: The returned WeakPtr does not extend the owner's lifetime.

Warning: Do not call after InvalidateWeakPtrs() has been called.

function InvalidateWeakPtrs

inline void InvalidateWeakPtrs()

Invalidates all previously issued WeakPtr instances.

Exceptions:

  • None

Since: N/A

Note: New WeakPtr instances can be created after invalidation.

Warning: None

After calling this method, all existing WeakPtr instances return nullptr from Get(). The owner remains alive and new WeakPtr instances can be created via GetWeakPtr().

function HasWeakPtrs

inline bool HasWeakPtrs() const

Checks if any WeakPtr instances are currently held externally.

Exceptions:

  • None

Return: true if external WeakPtr instances exist, false otherwise.

Since: N/A

Note: Uses the shared_ptr use_count to determine if any WeakPtr instances are holding the flag.

Warning: None


Updated on 2026-03-09 at 10:14:00 +0000