Louvre  v2.13.0-1
C++ library for Wayland compositors
Public Types | Public Member Functions | List of all members
LWeak< T > Class Template Reference

Weak reference to an LObject. More...

Public Types

using OnDestroyCallback = std::function< void(T *)>
 

Public Member Functions

 LWeak () noexcept=default
 Creates an empty LWeak. More...
 
 LWeak (T *object) noexcept
 Creates a reference for the given LObject, or initializes an empty LWeak if nullptr is passed. More...
 
 ~LWeak () noexcept
 Destructor, removes the LWeak from the LObject references. More...
 
 LWeak (const LWeak< T > &other) noexcept
 Copy constructor, assigns the LObject of another LWeak. More...
 
LWeak< T > & operator= (const LWeak< T > &other) noexcept
 Assignment operator, assigns the LObject of another LWeak. More...
 
T * get () const noexcept
 Gets a pointer to the LObject or nullptr if not set or the object has been destroyed. More...
 
 operator T* () const noexcept
 Implicit conversion to raw pointer. More...
 
T * operator-> () const noexcept
 Access underlying object via pointer semantics. More...
 
UInt64 count () const noexcept
 Return the number of existing references to the current LObject. More...
 
void reset (T *object=nullptr) noexcept
 Replace the reference with another object. More...
 
void setOnDestroyCallback (const OnDestroyCallback &callback) noexcept
 Set the onDestroy callback function. More...
 

Detailed Description

template<class T>
class Louvre::LWeak< T >

Weak reference to an LObject.

The LWeak class template provides a mechanism for creating weak pointer references to subclasses of LObject.
It is conceptually similar to std::weak_ptr but is specifically tailored for LObject subclasses, avoiding the pointer indirection and associated performance overhead of the std::weak_ptr thread-safe mechanisms.

When the object being referenced is destroyed, an optional on destroy callback event is emitted, and get() returns nullptr.

Member Typedef Documentation

◆ OnDestroyCallback

using OnDestroyCallback = std::function<void(T*)>

Callback function type used to handle the OnDestroy() event.

Constructor & Destructor Documentation

◆ LWeak() [1/3]

LWeak ( )
defaultnoexcept

Creates an empty LWeak.

◆ LWeak() [2/3]

LWeak ( T *  object)
inlinenoexcept

Creates a reference for the given LObject, or initializes an empty LWeak if nullptr is passed.

Parameters
objectThe LObject to create a reference for, or nullptr to initialize an empty LWeak.

◆ ~LWeak()

~LWeak ( )
inlinenoexcept

Destructor, removes the LWeak from the LObject references.

◆ LWeak() [3/3]

LWeak ( const LWeak< T > &  other)
inlinenoexcept

Copy constructor, assigns the LObject of another LWeak.

Parameters
otherThe LWeak object to copy from.

Member Function Documentation

◆ operator=()

LWeak<T>& operator= ( const LWeak< T > &  other)
inlinenoexcept

Assignment operator, assigns the LObject of another LWeak.

Parameters
otherThe LWeak object to assign from.
Returns
Reference to the updated LWeak object.

◆ get()

T* get ( ) const
inlinenoexcept

Gets a pointer to the LObject or nullptr if not set or the object has been destroyed.

Returns
Raw pointer to the referenced LObject.

◆ operator T*()

operator T* ( ) const
inlinenoexcept

Implicit conversion to raw pointer.

Provides access to the underlying raw pointer through an implicit conversion.

Returns
A pointer to the underlying object.

◆ operator->()

T* operator-> ( ) const
inlinenoexcept

Access underlying object via pointer semantics.

Allows accessing members of the underlying object using pointer semantics.

Returns
Pointer to the underlying object.

◆ count()

UInt64 count ( ) const
inlinenoexcept

Return the number of existing references to the current LObject.

Returns
The number of existing references to the current LObject, if no object is set returns 0.

◆ reset()

void reset ( T *  object = nullptr)
inlinenoexcept

Replace the reference with another object.

Parameters
objectThe LObject to set as the new reference, or nullptr to unset the reference.

◆ setOnDestroyCallback()

void setOnDestroyCallback ( const OnDestroyCallback callback)
inlinenoexcept

Set the onDestroy callback function.

Note
callback functions are not copied across LWeak instances.
Parameters
callbackThe callback function to be called when the referenced object is destroyed. Passing nullptr disables the callback.