| 1 | #ifndef THEADING_UTILS_ADOPTLOCKER_H |
| 2 | #define THEADING_UTILS_ADOPTLOCKER_H |
| 3 | |
| 4 | #include <QtGlobal> |
| 5 | |
| 6 | QT_FORWARD_DECLARE_CLASS(QMutex) |
| 7 | |
| 8 | namespace QtUtilities { |
| 9 | |
| 10 | /*! |
| 11 | * \brief Like QMutexLocker, but assumes that the mutex has already been locked. |
| 12 | */ |
| 13 | template <typename Mutex = QMutex> class AdoptLocker { |
| 14 | public: |
| 15 | /*! |
| 16 | * \brief Constructs the locker for the specified \a mutex. |
| 17 | */ |
| 18 | AdoptLocker(Mutex &mutex) |
| 19 | : m_mutex(mutex) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | /*! |
| 24 | * \brief Unlocks the mutex specified when constructing the instance. |
| 25 | */ |
| 26 | ~AdoptLocker() |
| 27 | { |
| 28 | m_mutex.unlock(); |
| 29 | } |
| 30 | |
| 31 | private: |
| 32 | Mutex &m_mutex; |
| 33 | }; |
| 34 | } // namespace QtUtilities |
| 35 | |
| 36 | #endif // THEADING_UTILS_ADOPTLOCKER_H |