DuxReiNummariae 1.1.0-alpha.19
Simple and powerful budgeting application
Loading...
Searching...
No Matches
drn::foundation::QtPtr< ObjectType > Class Template Reference

A smart pointer class ensuring the Qt reference counting for automatic memory management. QtPtr is a template-based smart pointer class that ensures Qt's reference counting manages the lifetime of a dynamically allocated object. When the parent owning an object is destroyed or reset, the managed object is automatically deleted. Additionally, when the observed object is destructed, the object observing will turn null. More...

#include <QtMemory.hpp>

Inheritance diagram for drn::foundation::QtPtr< ObjectType >:
Collaboration diagram for drn::foundation::QtPtr< ObjectType >:

Public Member Functions

 QtPtr (const QtPtr &other)
 Copy constructor that fully initiaises the object using the supplied object as its data source.
 QtPtr (const UniqueQtPtr< ObjectType > &observe)
 Constructs a QtPtr that observes a given UniqueQtPtr object.
 QtPtr (ObjectType *const observe=nullptr)
 Constructs a QtPtr that observes a given Qt object.
 QtPtr (QtPtr &&other) noexcept
 Move constructor that fully initiaises the object by taking the supplied object as its data source.
 ~QtPtr ()
 Disconnects the Qt connection associated with the pointer if the observed object is derived from QObject.
QtPtroperator= (const QtPtr &other)
 Copy assignment for the current instance using the supplied object as its data source.
QtPtroperator= (QtPtr &&other) noexcept
 Move assignment for the current instance taking the supplied object as its data source.
UniqueQtPtr< ObjectType > releaseUnique ()
 Releases the ownership of the Qt object.
Public Member Functions inherited from drn::foundation::ObserverPtr< ObjectType >
 ObserverPtr (ObjectType *const observe=nullptr)
 Full initialisation constructor for observing a naked pointer.
 ~ObserverPtr ()=default
ObjectType * get () const noexcept
 An accessor to the underlying pointer being observed.
 operator bool () const noexcept
 Determines if this object is observing a pointer.
void operator delete (void *)=delete
 operator ObjectType * () const noexcept
 Conversion operator to the underlying pointer being observed.
bool operator!= (const std::nullptr_t) const
 Inequality operator for testing if this object points to null.
ObjectType & operator* ()
 Dereference pointer to the observed pointer. The behaviour is undefined if the observed pointer is null.
ObjectType * operator-> ()
 Dereference pointer to the observed pointer. The behaviour is undefined if the observed pointer is null.
bool operator< (const ObjectType *observed) const
 Less-than operator for testing if the address observed by this object appears before another specified naked pointer's address.
ObserverPtroperator= (const ObserverPtr &)=default
bool operator== (const std::nullptr_t) const
 Equality operator for testing if this object points to null.

Additional Inherited Members

Public Types inherited from drn::foundation::ObserverPtr< ObjectType >
using element_type
 The type the smart pointer is observing.
using pointer
 The pointer type the smart pointer is observing.

Detailed Description

template<typename ObjectType>
class drn::foundation::QtPtr< ObjectType >

A smart pointer class ensuring the Qt reference counting for automatic memory management. QtPtr is a template-based smart pointer class that ensures Qt's reference counting manages the lifetime of a dynamically allocated object. When the parent owning an object is destroyed or reset, the managed object is automatically deleted. Additionally, when the observed object is destructed, the object observing will turn null.

This is similar in nature to the QPointer, but with the addition of allowing non-QObject derived Qt objects to be stored in the pointer.

Template Parameters
ObjectTypeThe type of the object stored.

Constructor & Destructor Documentation

◆ QtPtr() [1/4]

template<typename ObjectType>
drn::foundation::QtPtr< ObjectType >::QtPtr ( ObjectType *const observe = nullptr)

Constructs a QtPtr that observes a given Qt object.

Parameters
observeThe Qt object to be observed.
Here is the caller graph for this function:

◆ QtPtr() [2/4]

template<typename ObjectType>
drn::foundation::QtPtr< ObjectType >::QtPtr ( const UniqueQtPtr< ObjectType > & observe)

Constructs a QtPtr that observes a given UniqueQtPtr object.

Parameters
observeThe unique Qt object to be observed.
Here is the call graph for this function:

◆ QtPtr() [3/4]

template<typename ObjectType>
drn::foundation::QtPtr< ObjectType >::QtPtr ( const QtPtr< ObjectType > & other)

Copy constructor that fully initiaises the object using the supplied object as its data source.

Parameters
otherThe pointer to copy.
Here is the call graph for this function:

◆ QtPtr() [4/4]

template<typename ObjectType>
drn::foundation::QtPtr< ObjectType >::QtPtr ( QtPtr< ObjectType > && other)
noexcept

Move constructor that fully initiaises the object by taking the supplied object as its data source.

Parameters
otherThe pointer to move its data.
Here is the call graph for this function:

◆ ~QtPtr()

template<typename ObjectType>
drn::foundation::QtPtr< ObjectType >::~QtPtr ( )

Disconnects the Qt connection associated with the pointer if the observed object is derived from QObject.

Here is the call graph for this function:

Member Function Documentation

◆ operator=() [1/2]

template<typename ObjectType>
QtPtr & drn::foundation::QtPtr< ObjectType >::operator= ( const QtPtr< ObjectType > & other)

Copy assignment for the current instance using the supplied object as its data source.

Parameters
otherThe pointer to copy.
Returns
The current instance after modification.
Here is the call graph for this function:

◆ operator=() [2/2]

template<typename ObjectType>
QtPtr & drn::foundation::QtPtr< ObjectType >::operator= ( QtPtr< ObjectType > && other)
noexcept

Move assignment for the current instance taking the supplied object as its data source.

Parameters
otherThe pointer to move.
Returns
The current instance after modification.
Here is the call graph for this function:

◆ releaseUnique()

template<typename ObjectType>
UniqueQtPtr< ObjectType > drn::foundation::QtPtr< ObjectType >::releaseUnique ( )
nodiscard

Releases the ownership of the Qt object.

The method removes the Qt object’s association with its parent, preventing Qt’s internal memory management from automatically deleting the object when the parent is destroyed. The caller is responsible for managing the memory.

Postcondition
The object is disassociated from its parent in the case of Qt objects.
Returns
The object that was released.
Warning
The caller is responsible for managing the memory of the object that is returned.
Here is the call graph for this function:
Here is the caller graph for this function: