1 | // Copyright (C) 2025 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QCACHEDVALUE_P_H |
5 | #define QCACHEDVALUE_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QReadWriteLock> |
19 | |
20 | #include <optional> |
21 | #include <unordered_map> |
22 | |
23 | QT_BEGIN_NAMESPACE |
24 | |
25 | template <typename T> |
26 | class QCachedValue |
27 | { |
28 | public: |
29 | QCachedValue() = default; |
30 | |
31 | Q_DISABLE_COPY(QCachedValue) |
32 | |
33 | template <typename Creator> |
34 | T ensure(Creator &&creator) |
35 | { |
36 | { |
37 | QReadLocker locker(&m_lock); |
38 | if (m_cached) |
39 | return *m_cached; |
40 | } |
41 | |
42 | { |
43 | QWriteLocker locker(&m_lock); |
44 | if (!m_cached) |
45 | m_cached = creator(); |
46 | return *m_cached; |
47 | } |
48 | } |
49 | |
50 | bool update(T value) |
51 | { |
52 | QWriteLocker locker(&m_lock); |
53 | if (value == m_cached) |
54 | return false; |
55 | auto temp = std::exchange(m_cached, std::move(value)); |
56 | locker.unlock(); |
57 | return true; |
58 | } |
59 | |
60 | void reset() |
61 | { |
62 | QWriteLocker locker(&m_lock); |
63 | auto temp = std::exchange(m_cached, std::nullopt); |
64 | locker.unlock(); |
65 | } |
66 | |
67 | private: |
68 | QReadWriteLock m_lock; |
69 | std::optional<T> m_cached; |
70 | }; |
71 | |
72 | template <typename Key, typename Value> |
73 | class QCachedValueMap |
74 | { |
75 | public: |
76 | QCachedValueMap() = default; |
77 | |
78 | Q_DISABLE_COPY(QCachedValueMap) |
79 | |
80 | template <typename Creator> |
81 | Value ensure(const Key &key, Creator &&creator) |
82 | { |
83 | { |
84 | QReadLocker locker(&m_lock); |
85 | auto it = m_map.find(key); |
86 | if (it != m_map.end()) |
87 | return it->second; |
88 | } |
89 | |
90 | { |
91 | QWriteLocker locker(&m_lock); |
92 | auto emplaceRes = m_map.try_emplace(key, creator()); |
93 | return emplaceRes.first->second; |
94 | } |
95 | } |
96 | |
97 | private: |
98 | QReadWriteLock m_lock; |
99 | std::unordered_map<Key, Value> m_map; |
100 | }; |
101 | |
102 | QT_END_NAMESPACE |
103 | |
104 | #endif // QCACHEDVALUE_P_H |
105 | |