qtutilities/misc/adoptlocker.h

37 lines
694 B
C
Raw Permalink Normal View History

#ifndef THEADING_UTILS_ADOPTLOCKER_H
#define THEADING_UTILS_ADOPTLOCKER_H
#include <QtGlobal>
QT_FORWARD_DECLARE_CLASS(QMutex)
namespace QtUtilities {
/*!
* \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 {
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)
{
}
/*!
2017-05-04 22:46:37 +02:00
* \brief Unlocks the mutex specified when constructing the instance.
*/
~AdoptLocker()
{
m_mutex.unlock();
}
private:
Mutex &m_mutex;
};
} // namespace QtUtilities
#endif // THEADING_UTILS_ADOPTLOCKER_H