2016-07-04 23:26:48 +02:00
|
|
|
#ifndef THEADING_UTILS_ADOPTLOCKER_H
|
|
|
|
#define THEADING_UTILS_ADOPTLOCKER_H
|
2016-03-10 22:15:16 +01:00
|
|
|
|
|
|
|
#include <QtGlobal>
|
|
|
|
|
|
|
|
QT_FORWARD_DECLARE_CLASS(QMutex)
|
|
|
|
|
2019-06-10 21:57:46 +02:00
|
|
|
namespace QtUtilities {
|
2016-03-10 22:15:16 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Like QMutexLocker, but assumes that the mutex has already been locked.
|
|
|
|
*/
|
2017-05-01 03:16:25 +02:00
|
|
|
template <typename Mutex = QMutex> class AdoptLocker {
|
2016-03-10 22:15:16 +01:00
|
|
|
public:
|
|
|
|
/*!
|
2017-05-04 22:46:37 +02:00
|
|
|
* \brief Constructs the locker for the specified \a mutex.
|
|
|
|
*/
|
2017-05-01 03:16:25 +02:00
|
|
|
AdoptLocker(Mutex &mutex)
|
|
|
|
: m_mutex(mutex)
|
|
|
|
{
|
|
|
|
}
|
2016-03-10 22:15:16 +01:00
|
|
|
|
|
|
|
/*!
|
2017-05-04 22:46:37 +02:00
|
|
|
* \brief Unlocks the mutex specified when constructing the instance.
|
|
|
|
*/
|
2016-03-10 22:15:16 +01:00
|
|
|
~AdoptLocker()
|
|
|
|
{
|
|
|
|
m_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mutex &m_mutex;
|
|
|
|
};
|
2019-06-10 21:57:46 +02:00
|
|
|
} // namespace QtUtilities
|
2016-03-10 22:15:16 +01:00
|
|
|
|
2016-07-04 23:26:48 +02:00
|
|
|
#endif // THEADING_UTILS_ADOPTLOCKER_H
|