file_lock.h 754 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "base/basic_types.h"
  9. #include <QtCore/QFile>
  10. namespace base {
  11. class FileLock {
  12. public:
  13. FileLock();
  14. bool lock(QFile &file, QIODevice::OpenMode mode);
  15. [[nodiscard]] bool locked() const;
  16. void unlock();
  17. static constexpr auto kSkipBytes = size_type(4);
  18. ~FileLock();
  19. private:
  20. class Lock;
  21. struct Descriptor;
  22. struct LockingPid;
  23. static constexpr auto kLockOffset = index_type(0);
  24. static constexpr auto kLockLimit = kSkipBytes;
  25. std::unique_ptr<Lock> _lock;
  26. };
  27. } // namespace base