File: main.cpp

package info (click to toggle)
clazy 1.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,248 kB
  • sloc: cpp: 23,552; python: 1,450; xml: 450; sh: 237; makefile: 46
file content (55 lines) | stat: -rw-r--r-- 1,814 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtCore/QMap>
#include <QtCore/QReadWriteLock>


class Test
{
    QReadWriteLock m_projectLock;
    QMap<QString, QString> m_fileToProjectParts;
    QMap<QString, QString> m_someOtherMap;

    void test(const QString &fileName)
    {
        m_someOtherMap.find(fileName); // OK, we did not pretend to lock anything here...
        QReadLocker locker(&m_projectLock);
        auto it = m_fileToProjectParts.find(fileName); // WARN, possible detach
        auto lookup = m_fileToProjectParts[fileName]; // WARN, possible detach
        Q_UNUSED(lookup);
        auto it3 = QMap<QString, QString>().find(fileName); // OK, not a member
    }

    void test2(const QString &fileName)
    {
        QWriteLocker locker(&m_projectLock);
        auto it = m_fileToProjectParts.find(fileName); // OK, we have a write lock
    }

    void test3(const QString &fileName)
    {
        {
            QReadLocker locker(&m_projectLock);
            auto it = m_fileToProjectParts.constFind(fileName); // OK - const version of this function
        }

        auto it = m_fileToProjectParts.find(fileName); // OK, outside of locker
    }

    void testUnlock(const QString &fileName)
    {
        QReadLocker locker(&m_projectLock);
        auto it = m_fileToProjectParts.find(fileName); // WARN
        locker.unlock();
        it = m_fileToProjectParts.find(fileName); // OK, we unlocked
    }

    void testLockUnlock(const QString &fileName)
    {
        auto it = m_fileToProjectParts.find(fileName); // OK, we did not lock yet
        m_projectLock.lockForRead();
        it = m_fileToProjectParts.find(fileName); // WARN, inside of read-only lock
        m_projectLock.unlock();
        it = m_fileToProjectParts.find(fileName); // OK, we unlocked
    }
};