Qx v0.5.8
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::ExclusiveAccess< AccessType, Mutex > Class Template Reference

The ExclusiveAccess template class is a convenience class that simplifies access to resources secured by a mutex. More...

#include <qx/core/qx-exclusiveaccess.h>

Public Member Functions

 ExclusiveAccess (AccessType *data, Mutex *mutex) noexcept
 
 ExclusiveAccess (ExclusiveAccess &&other) noexcept
 
 ~ExclusiveAccess ()
 
AccessType * access ()
 
const AccessType * access () const
 
bool isLocked () const noexcept
 
Mutex * mutex () const
 
AccessType & operator* ()
 
const AccessType & operator* () const
 
AccessType * operator-> ()
 
const AccessType * operator-> () const
 
ExclusiveAccessoperator= (ExclusiveAccess &&other) noexcept
 
void relock () noexcept
 
void swap (ExclusiveAccess &other) noexcept
 
void unlock () noexcept
 

Detailed Description

template<typename AccessType, typename Mutex>
requires any_of<Mutex, QMutex, QRecursiveMutex>
class Qx::ExclusiveAccess< AccessType, Mutex >

This class is essentially a slightly more capable QMutexLocker and can be used in the same ways; however, it couples access to the data protected by the mutex (in cases where there is, or data can easily be wrapped within, a single resource) along with the automatic locking/unlocking of QMutexLocker.

This can be useful when exclusive access to data needs to be provided by a class/function:

class Node;
class Manager
{
public:
Manager();
void registerNode(Node* node);
//...
};
class Node
{
{
// RAII
static Manager m;
static QMutex mtx;
return Qx::ExclusiveAccess(&m, &mtx); // Locks the mutex and stays locked since this is moved
}
public:
Node();
void registerSelf()
{
// Nodes could be in any thread, so this allows access to the manager to be synchronized
auto mAccess = manager();
mAccess->registerNode(this);
// Use access as needed...
// Mutex is unlocked when mAccess is destroyed
}
};
void someFunctionInAThread()
{
std::shared_ptr<Node> node;
node->registerSelf();
//...
}
The ExclusiveAccess template class is a convenience class that simplifies access to resources secured...
Definition qx-exclusiveaccess.h:16

Constructor & Destructor Documentation

◆ ExclusiveAccess() [1/2]

template<typename AccessType , typename Mutex >
Qx::ExclusiveAccess< AccessType, Mutex >::ExclusiveAccess ( AccessType * data,
Mutex * mutex )
inlineexplicitnoexcept

Constructs an ExclusiveAccess and locks mutex. The mutex will be unlocked when the ExclusiveAccess is destroyed. If mutex is nullptr, ExclusiveAccess only provides access to data.

See also
QMutexLocker.

◆ ExclusiveAccess() [2/2]

template<typename AccessType , typename Mutex >
Qx::ExclusiveAccess< AccessType, Mutex >::ExclusiveAccess ( ExclusiveAccess< AccessType, Mutex > && other)
inlinenoexcept

Move-constructs and ExclusiveAccess from other. The mutex, data pointer, and state of other is transferred to the newly constructed instance. After the move, other will no longer manage the mutex, nor have a valid data pointer.

See also
QMutexLocker.

◆ ~ExclusiveAccess()

template<typename AccessType , typename Mutex >
Qx::ExclusiveAccess< AccessType, Mutex >::~ExclusiveAccess ( )
inline

Destroys the ExclusiveAccess and unlocks the mutex provided by the constructor if it's still locked.

Member Function Documentation

◆ access() [1/2]

template<typename AccessType , typename Mutex >
AccessType * Qx::ExclusiveAccess< AccessType, Mutex >::access ( )
inline

Returns a pointer to the data the ExclusiveAccess is providing access to.

◆ access() [2/2]

template<typename AccessType , typename Mutex >
const AccessType * Qx::ExclusiveAccess< AccessType, Mutex >::access ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ isLocked()

template<typename AccessType , typename Mutex >
void Qx::ExclusiveAccess< AccessType, Mutex >::isLocked ( ) const
inlinenoexcept

Returns true if this ExclusiveAccess is currently locking its associated mutex; otherwise, returns false.

◆ mutex()

template<typename AccessType , typename Mutex >
void Qx::ExclusiveAccess< AccessType, Mutex >::mutex ( ) const
inline

Returns the mutex on which the ExclusiveAccess is operating.

◆ operator*() [1/2]

template<typename AccessType , typename Mutex >
AccessType & Qx::ExclusiveAccess< AccessType, Mutex >::operator* ( )
inline

Returns a reference to the data the ExclusiveAccess is providing access to.

◆ operator*() [2/2]

template<typename AccessType , typename Mutex >
const AccessType & Qx::ExclusiveAccess< AccessType, Mutex >::operator* ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ operator->() [1/2]

template<typename AccessType , typename Mutex >
AccessType * Qx::ExclusiveAccess< AccessType, Mutex >::operator-> ( )
inline

Provides convenient access to the members of DataType for the accessible data.

◆ operator->() [2/2]

template<typename AccessType , typename Mutex >
const AccessType * Qx::ExclusiveAccess< AccessType, Mutex >::operator-> ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ operator=()

template<typename AccessType , typename Mutex >
void Qx::ExclusiveAccess< AccessType, Mutex >::operator= ( ExclusiveAccess< AccessType, Mutex > && other)
inlinenoexcept

Move-assigns other onto this ExclusiveAccess. If this ExclusiveAccess was holding onto a locked mutex before the assignment, the mutex will be unlocked. The mutex, data pointer, and state of other is then transferred to this QMutexLocker. After the move, other will no longer manage the mutex, nor have a valid data pointer.

◆ relock()

template<typename AccessType , typename Mutex >
void Qx::ExclusiveAccess< AccessType, Mutex >::relock ( )
inlinenoexcept

Relocks an unlocked ExclusiveAccess.

See also
unlock().

◆ swap()

template<typename AccessType , typename Mutex >
void Qx::ExclusiveAccess< AccessType, Mutex >::swap ( ExclusiveAccess< AccessType, Mutex > & other)
inlinenoexcept

Swaps the mutex, data pointer, and state of this ExclusiveAccess with other. This operation is very fast and never fails.

◆ unlock()

template<typename AccessType , typename Mutex >
void Qx::ExclusiveAccess< AccessType, Mutex >::unlock ( )
inlinenoexcept

Unlocks this ExclusiveAccess. You can use relock() to lock it again. It does not need to be locked when destroyed.

See also
relock().

The documentation for this class was generated from the following files: