cf::WeakPtr¶
Non-owning weak reference for exclusively owned resources. More...
#include <weak_ptr.h>
Public Functions¶
| Name | |
|---|---|
| constexpr | WeakPtr() =default Default constructor: creates an empty weak reference. |
| constexpr | WeakPtr(std::nullptr_t ) Constructs an empty weak reference from nullptr. |
| WeakPtr(constWeakPtr & ) =default Copy constructor. |
|
| WeakPtr & | operator=(constWeakPtr & ) =default Copy assignment operator. |
| WeakPtr(WeakPtr && ) =default Move constructor. |
|
| WeakPtr & | operator=(WeakPtr && ) =default Move assignment operator. |
| template <typenameU ,typename =std::enable_if_t |
WeakPtr(constWeakPtr< U > & other) Covariant copy constructor from WeakPtr |
| template <typenameU ,typename =std::enable_if_t WeakPtr & |
operator=(constWeakPtr< U > & other) Covariant copy assignment from WeakPtr |
| ~WeakPtr() =default Destructor: does not affect Owner lifetime. |
|
| T * | Get() const Gets the raw pointer if the owner is still alive. |
| T * | operator->() const Dereferences the weak pointer. |
| T & | operator*() const Dereferences the weak pointer. |
| bool | IsValid() const Checks if the owner is still alive. |
| operator bool() const Boolean conversion operator for validity check. |
|
| void | Reset() Resets the weak pointer to empty state. |
| bool | operator==(constWeakPtr & other) const Equality comparison with another WeakPtr. |
| bool | operator!=(constWeakPtr & other) const Inequality comparison with another WeakPtr. |
| bool | operator==(std::nullptr_t ) const Equality comparison with nullptr. |
| bool | operator!=(std::nullptr_t ) const Inequality comparison with nullptr. |
| template <typenameSource > WeakPtr |
DynamicCast(constWeakPtr< Source > & other) Dynamically converts WeakPtr |
Friends¶
| Name | |
|---|---|
| class | WeakPtr(WeakPtr ) Friend declaration for covariance support. |
| class | WeakPtrFactory(WeakPtrFactory ) Friend declaration for factory access. |
Detailed Description¶
Non-owning weak reference for exclusively owned resources.
Template Parameters:
- T Type of the referenced object.
Note: Supports covariance: WeakPtr
Warning: WeakPtr should only be used on the same thread (sequence) where it was created. The "check + dereference" two-step operation is not atomic.
Unlike std::weak_ptr, WeakPtr does not participate in reference counting. The resource is exclusively owned by an owner (typically holding WeakPtrFactory), and WeakPtr acts as a "claim ticket" that becomes invalid when the owner disappears.
class ThemeManager {
public:
...
private:
WeakPtrFactory<ThemeManager> weak_factory_{this}; // Declare last
};
WeakPtr<ThemeManager> ref = tm.GetWeakPtr();
if (ref) { // or ref.IsValid()
ref->ApplyTheme(); // Safe access
}
Public Functions Documentation¶
function WeakPtr¶
Default constructor: creates an empty weak reference.
Exceptions:
- None
Since: N/A
Note: None
Warning: None
function WeakPtr¶
Constructs an empty weak reference from nullptr.
Parameters:
- nullptr_t The nullptr literal.
Exceptions:
- None
Since: N/A
Note: None
Warning: None
function WeakPtr¶
Copy constructor.
Exceptions:
- None
Since: N/A
Note: None
Warning: None
function operator=¶
Copy assignment operator.
Parameters:
- other Source weak pointer.
Exceptions:
- None
Return: Reference to this WeakPtr.
Since: N/A
Note: None
Warning: None
function WeakPtr¶
Move constructor.
Exceptions:
- None
Since: N/A
Note: None
Warning: None
function operator=¶
Move assignment operator.
Parameters:
- other Source weak pointer.
Exceptions:
- None
Return: Reference to this WeakPtr.
Since: N/A
Note: None
Warning: None
function WeakPtr¶
template <typenameU ,
typename =std::enable_if_t<std::is_convertible_v<U*, T*>>>
inline WeakPtr(
constWeakPtr< U > & other
)
Covariant copy constructor from WeakPtr
Parameters:
- other Source weak pointer.
Exceptions:
- None
Template Parameters:
- U Derived type that is convertible to T.
Since: N/A
Note: None
Warning: None
function operator=¶
template <typenameU ,
typename =std::enable_if_t<std::is_convertible_v<U*, T*>>>
inline WeakPtr & operator=(
constWeakPtr< U > & other
)
Covariant copy assignment from WeakPtr
Parameters:
- other Source weak pointer.
Exceptions:
- None
Template Parameters:
- U Derived type that is convertible to T.
Return: Reference to this WeakPtr.
Since: N/A
Note: None
Warning: None
function ~WeakPtr¶
Destructor: does not affect Owner lifetime.
Exceptions:
- None
Since: N/A
Note: None
Warning: None
function Get¶
Gets the raw pointer if the owner is still alive.
Exceptions:
- None
Return: Raw pointer to the owned object, or nullptr if owner is gone.
Since: N/A
Note: None
Warning: None
function operator->¶
Dereferences the weak pointer.
Exceptions:
- None (assertion failure if invalid).
Return: Raw pointer to the owned object.
Since: N/A
Note: Asserts that the WeakPtr is valid before dereferencing.
Warning: Undefined behavior if called on an invalid WeakPtr.
function operator*¶
Dereferences the weak pointer.
Exceptions:
- None (assertion failure if invalid).
Return: Reference to the owned object.
Since: N/A
Note: Asserts that the WeakPtr is valid before dereferencing.
Warning: Undefined behavior if called on an invalid WeakPtr.
function IsValid¶
Checks if the owner is still alive.
Exceptions:
- None
Return: true if the owner is alive, false otherwise.
Since: N/A
Note: None
Warning: None
function operator bool¶
Boolean conversion operator for validity check.
Exceptions:
- None
Return: true if the owner is alive, false otherwise.
Since: N/A
Note: None
Warning: None
function Reset¶
Resets the weak pointer to empty state.
Exceptions:
- None
Since: N/A
Note: After reset, IsValid() returns false.
Warning: None
function operator==¶
Equality comparison with another WeakPtr.
Parameters:
- other Other WeakPtr to compare with.
Exceptions:
- None
Return: true if both weak pointers point to the same object.
Since: N/A
Note: None
Warning: None
function operator!=¶
Inequality comparison with another WeakPtr.
Parameters:
- other Other WeakPtr to compare with.
Exceptions:
- None
Return: true if weak pointers point to different objects.
Since: N/A
Note: None
Warning: None
function operator==¶
Equality comparison with nullptr.
Exceptions:
- None
Return: true if the WeakPtr is invalid (empty).
Since: N/A
Note: None
Warning: None
function operator!=¶
Inequality comparison with nullptr.
Exceptions:
- None
Return: true if the WeakPtr is valid.
Since: N/A
Note: None
Warning: None
function DynamicCast¶
Dynamically converts WeakPtr
Parameters:
- other WeakPtr to the source object.
Exceptions:
- None
Template Parameters:
- Source Source type (base class).
Return: WeakPtr
Since: N/A
Note: The returned WeakPtr shares the same weak reference flag as the source.
Warning: Always check the returned WeakPtr's validity before use.
Performs a dynamic_cast on the underlying pointer. Returns a valid WeakPtr
WeakPtr<BaseClass> basePtr = ...;
auto derivedPtr = WeakPtr<DerivedClass>::DynamicCast(basePtr);
if (derivedPtr) {
derivedPtr->DerivedMethod();
}
Friends¶
friend WeakPtr¶
Friend declaration for covariance support.
Template Parameters:
- U Type parameter for the befriended WeakPtr.
Since: N/A
friend WeakPtrFactory¶
Friend declaration for factory access.
Template Parameters:
- U Type parameter for the befriended WeakPtrFactory.
Since: N/A
Updated on 2026-03-09 at 10:14:00 +0000