File indexing completed on 2024-05-19 04:38:45

0001 /****************************************************************************
0002 **
0003 ** Copyright (C) 2016 The Qt Company Ltd.
0004 ** Contact: https://www.qt.io/licensing/
0005 **
0006 ** This file is part of Qt Creator.
0007 **
0008 ** Commercial License Usage
0009 ** Licensees holding valid commercial Qt licenses may use this file in
0010 ** accordance with the commercial license agreement provided with the
0011 ** Software or, alternatively, in accordance with the terms contained in
0012 ** a written agreement between you and The Qt Company. For licensing terms
0013 ** and conditions see https://www.qt.io/terms-conditions. For further
0014 ** information use the contact form at https://www.qt.io/contact-us.
0015 **
0016 ** GNU General Public License Usage
0017 ** Alternatively, this file may be used under the terms of the GNU
0018 ** General Public License version 3 as published by the Free Software
0019 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
0020 ** included in the packaging of this file. Please review the following
0021 ** information to ensure the GNU General Public License requirements will
0022 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
0023 **
0024 ****************************************************************************/
0025 
0026 #pragma once
0027 
0028 #include <QFile>
0029 
0030 #if defined(Q_OS_WIN)
0031 #  if !defined(QT_QTLOCKEDFILE_EXPORT) && !defined(QT_QTLOCKEDFILE_IMPORT)
0032 #    define QT_QTLOCKEDFILE_EXPORT
0033 #  elif defined(QT_QTLOCKEDFILE_IMPORT)
0034 #    if defined(QT_QTLOCKEDFILE_EXPORT)
0035 #      undef QT_QTLOCKEDFILE_EXPORT
0036 #    endif
0037 #    define QT_QTLOCKEDFILE_EXPORT __declspec(dllimport)
0038 #  elif defined(QT_QTLOCKEDFILE_EXPORT)
0039 #    undef QT_QTLOCKEDFILE_EXPORT
0040 #    define QT_QTLOCKEDFILE_EXPORT __declspec(dllexport)
0041 #  endif
0042 #else
0043 #  define QT_QTLOCKEDFILE_EXPORT
0044 #endif
0045 
0046 namespace SharedTools {
0047 
0048 class QT_QTLOCKEDFILE_EXPORT QtLockedFile : public QFile
0049 {
0050 public:
0051     enum LockMode { NoLock = 0, ReadLock, WriteLock };
0052 
0053     QtLockedFile();
0054     QtLockedFile(const QString &name);
0055     ~QtLockedFile();
0056 
0057     bool lock(LockMode mode, bool block = true);
0058     bool unlock();
0059     bool isLocked() const;
0060     LockMode lockMode() const;
0061 
0062 private:
0063 #ifdef Q_OS_WIN
0064     Qt::HANDLE m_semaphore_hnd;
0065     Qt::HANDLE m_mutex_hnd;
0066 #endif
0067     LockMode m_lock_mode;
0068 };
0069 
0070 } // namespace SharedTools