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 
159 class DirectoryLock final {
160  Q_DECLARE_TR_FUNCTIONS(DirectoryLock)
161 
162 public:
163  // Types
164 
168  enum class LockStatus {
169  Unlocked,
170  Locked,
171  StaleLock
172  };
173 
174  // Constructors / Destructor
175 
182  DirectoryLock() noexcept;
183 
189  DirectoryLock(const DirectoryLock& other) = delete;
190 
196  explicit DirectoryLock(const FilePath& dir) noexcept;
197 
204  ~DirectoryLock() noexcept;
205 
206  // Setters
207 
216  void setDirToLock(const FilePath& dir) noexcept;
217 
218  // Getters
219 
227  const FilePath& getDirToLock() const noexcept { return mDirToLock; }
228 
235  const FilePath& getLockFilepath() const noexcept { return mLockFilePath; }
236 
244  LockStatus getStatus() const;
245 
246  // General Methods
247 
263  void tryLock(bool* wasStale = nullptr);
264 
276  bool unlockIfLocked();
277 
288  void lock();
289 
299  void unlock();
300 
301  // Operator Overloadings
302  DirectoryLock& operator=(const DirectoryLock& rhs) = delete;
303 
304 private: // Data
309 
317 
332 };
333 
334 /*******************************************************************************
335  * End of File
336  ******************************************************************************/
337 
338 } // namespace librepcb
339 
340 #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:235
FilePath mLockFilePath
The filepath to the lock file.
Definition: directorylock.h:316
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:331
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:308
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:127
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:227
LockStatus
The return type of getStatus()
Definition: directorylock.h:168
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:159