LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
directorylock.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_DIRECTORYLOCK_H
21 #define LIBREPCB_DIRECTORYLOCK_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../exceptions.h"
27 #include "filepath.h"
28 
29 #include <QtCore>
30 
31 /*******************************************************************************
32  * Namespace / Forward Declarations
33  ******************************************************************************/
34 namespace librepcb {
35 
36 /*******************************************************************************
37  * Class DirectoryLock
38  ******************************************************************************/
39 
162 class DirectoryLock final {
163  Q_DECLARE_TR_FUNCTIONS(DirectoryLock)
164 
165 public:
166  // Types
167 
171  enum class LockStatus {
172  Unlocked,
173  Locked,
174  StaleLock
175  };
176 
177  // Constructors / Destructor
178 
185  DirectoryLock() noexcept;
186 
192  DirectoryLock(const DirectoryLock& other) = delete;
193 
199  explicit DirectoryLock(const FilePath& dir) noexcept;
200 
207  ~DirectoryLock() noexcept;
208 
209  // Setters
210 
219  void setDirToLock(const FilePath& dir) noexcept;
220 
221  // Getters
222 
230  const FilePath& getDirToLock() const noexcept { return mDirToLock; }
231 
238  const FilePath& getLockFilepath() const noexcept { return mLockFilePath; }
239 
247  LockStatus getStatus() const;
248 
249  // General Methods
250 
266  void tryLock(bool* wasStale = nullptr);
267 
279  bool unlockIfLocked();
280 
291  void lock();
292 
302  void unlock();
303 
304  // Operator Overloadings
305  DirectoryLock& operator=(const DirectoryLock& rhs) = delete;
306 
307 private: // Data
312 
320 
335 };
336 
337 /*******************************************************************************
338  * End of File
339  ******************************************************************************/
340 
341 } // namespace librepcb
342 
343 #endif // LIBREPCB_DIRECTORYLOCK_H
void lock()
Lock the specified directory (create/update the lock file)
Definition: directorylock.cpp:164
const FilePath & getLockFilepath() const noexcept
Get the filepath of the lock file (NOT the directory to lock!)
Definition: directorylock.h:238
FilePath mLockFilePath
The filepath to the lock file.
Definition: directorylock.h:319
DirectoryLock() noexcept
The default constructor.
Definition: directorylock.cpp:39
bool mLockedByThisObject
This attribute defines if the lock is active by this object.
Definition: directorylock.h:334
LockStatus getStatus() const
Get the lock status of the specified directory.
Definition: directorylock.cpp:71
the directory is not locked (lock file does not exist)
the directory is locked by a crashed application instance
FilePath mDirToLock
The filepath to the directory to lock (passed by setDirToLock())
Definition: directorylock.h:311
bool unlockIfLocked()
Unlock the specified directory if it was locked by this object.
Definition: directorylock.cpp:155
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:130
void setDirToLock(const FilePath &dir) noexcept
Specify the directory for which you need the lock.
Definition: directorylock.cpp:61
DirectoryLock & operator=(const DirectoryLock &rhs)=delete
const FilePath & getDirToLock() const noexcept
Get the filepath of the directory to lock (passed by setDirToLock())
Definition: directorylock.h:230
LockStatus
The return type of getStatus()
Definition: directorylock.h:171
void unlock()
Unlock the specified directory (remove the lock file)
Definition: directorylock.cpp:192
the directory is locked by another application instance
void tryLock(bool *wasStale=nullptr)
Lock the specified directory if not already locked.
Definition: directorylock.cpp:133
This class can be used to implement file-based directory locks.
Definition: directorylock.h:162