跳转至

cf::internal::WeakReferenceFlag

Shared flag tracking WeakPtr liveness state. More...

#include <weak_ptr_internals.h>

Public Functions

Name
WeakReferenceFlag() =default
Default constructor: creates a flag in the alive state.
WeakReferenceFlag(constWeakReferenceFlag & ) =delete
Copy constructor: deleted. Flag is shared via shared_ptr.
WeakReferenceFlag & operator=(constWeakReferenceFlag & ) =delete
Copy assignment: deleted.
WeakReferenceFlag(WeakReferenceFlag && ) =delete
Move constructor: deleted.
WeakReferenceFlag & operator=(WeakReferenceFlag && ) =delete
Move assignment: deleted.
bool IsAlive() const
Checks if the owner is still alive.
void Invalidate()
Marks the flag as invalid.

Detailed Description

class cf::internal::WeakReferenceFlag;

Shared flag tracking WeakPtr liveness state.

Note: The alive_ flag uses std::atomic for thread-safety. Invalidate() and IsAlive() are thread-safe operations.

Warning: WeakPtr should only be used on the same thread where it was created. The "check + dereference" two-step operation between IsAlive() and actual access is not atomic.

The Owner (WeakPtrFactory) and all WeakPtr instances share the same WeakReferenceFlag instance. When the owner is destroyed (or explicitly calls InvalidateWeakPtrs), the alive_ flag is set to false, causing all WeakPtr::Get() calls to return nullptr.

// Internal use only - created by WeakPtrFactory
auto flag = std::make_shared<WeakReferenceFlag>();
if (flag->IsAlive()) {
    // Safe to access
}
flag->Invalidate();  // All WeakPtrs now return nullptr

Public Functions Documentation

function WeakReferenceFlag

WeakReferenceFlag() =default

Default constructor: creates a flag in the alive state.

function WeakReferenceFlag

WeakReferenceFlag(
    constWeakReferenceFlag & 
) =delete

Copy constructor: deleted. Flag is shared via shared_ptr.

function operator=

WeakReferenceFlag & operator=(
    constWeakReferenceFlag & 
) =delete

Copy assignment: deleted.

function WeakReferenceFlag

WeakReferenceFlag(
    WeakReferenceFlag && 
) =delete

Move constructor: deleted.

function operator=

WeakReferenceFlag & operator=(
    WeakReferenceFlag && 
) =delete

Move assignment: deleted.

function IsAlive

inline bool IsAlive() const

Checks if the owner is still alive.

Exceptions:

  • None

Return: true if the owner is alive, false if invalidated.

Since: N/A

Note: Uses memory_order_acquire for proper synchronization.

Warning: None

function Invalidate

inline void Invalidate()

Marks the flag as invalid.

Exceptions:

  • None

Since: N/A

Note: Uses memory_order_release for proper synchronization.

Warning: None

After calling this method, all existing and future IsAlive() calls returns false.


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