summaryrefslogtreecommitdiffstats
path: root/src/render/backend/stringtoint.cpp
blob: 60ab0e57c9f30dadf99fd9af63f38fb3ede50d3b (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "stringtoint_p.h"
#include <QHash>
#include <mutex>
#include <shared_mutex>
#include <vector>

QT_BEGIN_NAMESPACE

namespace Qt3DRender {

namespace Render {

namespace {

struct StringToIntCache
{
    std::shared_mutex lock;
    QHash<QString, int> map;
    std::vector<QString> reverseMap;

    static StringToIntCache& instance()
    {
        static StringToIntCache c;
        return c;
    }
};

} // anonymous

int StringToInt::lookupId(QLatin1String str)
{
    // ### optimize me
    return lookupId(QString(str));
}

int StringToInt::lookupId(const QString &str)
{
    auto& cache = StringToIntCache::instance();
    int idx;
    {
        std::shared_lock readLocker(cache.lock);
        idx = cache.map.value(str, -1);
    }

    if (Q_UNLIKELY(idx < 0)) {
        std::unique_lock writeLocker(cache.lock);
        idx = cache.map.value(str, -1);
        if (idx < 0) {
            idx = int(cache.reverseMap.size());
            Q_ASSERT(size_t(cache.map.size()) == cache.reverseMap.size());
            cache.map.insert(str, idx);
            cache.reverseMap.push_back(str);
        }
    }
    return idx;
}

QString StringToInt::lookupString(int idx)
{
    auto& cache = StringToIntCache::instance();
    std::shared_lock readLocker(cache.lock);
    if (Q_LIKELY(cache.reverseMap.size() > size_t(idx)))
        return cache.reverseMap[idx];

    return QString();
}

} // Render

} // Qt3DRender

QT_END_NAMESPACE