Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qrhivaluemapper_p.h
Go to the documentation of this file.
1
// Copyright (C) 2024 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
QRHIVALUEMAPPER_P_H
5
#
define
QRHIVALUEMAPPER_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
<
QtMultimedia
/
qtmultimediaglobal
.
h
>
19
#
include
<
QtMultimedia
/
private
/
qmultimedia_ranges_p
.
h
>
20
21
#
include
<
qreadwritelock
.
h
>
22
23
#
include
<
memory
>
24
#
include
<
map
>
25
26
QT_BEGIN_NAMESPACE
27
28
class
QRhi;
29
30
/**
31
* @brief The QRhiCallback class implements a thread-safe wrapper around QRhi
32
* cleanup callbacks. For adding a callback to QRhi, create a shared insance
33
* of the class and invoke addToRhi for the specific QRhi. To deactivate
34
* the callback delete the instance.
35
*/
36
class
Q_MULTIMEDIA_EXPORT
QRhiCallback
:
public
std::enable_shared_from_this<QRhiCallback>
37
{
38
public
:
39
class
Manager;
40
41
QRhiCallback();
42
virtual
~QRhiCallback();
43
44
void
registerCallback(QRhi &rhi);
45
46
protected
:
47
virtual
void
onRhiCleanup(QRhi &rhi) = 0;
48
49
private
:
50
std::shared_ptr<Manager> m_manager;
51
};
52
53
/**
54
* @brief The class associates values of the specified type with different QRhi.
55
* One instance of QRhiValueMapper associates one QRhi with one value.
56
* The mapped value is deleted when the matching QRhi is cleaned/deleted,
57
* when QRhiValueMapper::clear is invoked, or the QRhiValueMapper's
58
* instance is deleted.
59
*
60
* QRhiValueMapper's API is thread safe, whereas the objects, which pointers you
61
* obtain via QRhiValueMapper::get(), are not. Thus, their thread-safity
62
* has to be managed by the code using the mapper.
63
64
* Note, that QRhiValueMapper destructs the values under its mutex.
65
* Keep it in mind and aim to avoid callbacks and signals emissions fror
66
* the Value's destructor.
67
*/
68
template
<
typename
Value>
69
class
QRhiValueMapper
70
{
71
struct
Data : QRhiCallback
72
{
73
QReadWriteLock lock;
74
// In most cases, one or 2 elements will be here, so let's use map
75
// instead of unordered_map for better efficiency with few elements.
76
std::map<QRhi *, Value> storage;
77
78
void
onRhiCleanup(QRhi &rhi) override
79
{
80
QWriteLocker locker(&lock);
81
storage.erase(&rhi);
82
}
83
};
84
85
public
:
86
~
QRhiValueMapper
()
87
{
88
clear
(
)
;
// it must be cleared when on the destruction to synchronize with rhi cleanup
89
}
90
91
Q_DISABLE_COPY
(
QRhiValueMapper
);
92
93
QRhiValueMapper
(
QRhiValueMapper
&& )
noexcept
=
default
;
94
QRhiValueMapper
&
operator
= (
QRhiValueMapper
&&)
noexcept
=
default
;
95
96
QRhiValueMapper
() : m_data(
std
::make_shared<Data>()) { }
97
98
template
<
typename
V>
99
std
::pair<Value *,
bool
>
tryMap
(QRhi &rhi, V &&value)
100
{
101
QWriteLocker locker(&m_data->lock);
102
103
auto
[rhiIt, rhiAdded] = m_data->storage.try_emplace(&rhi,
std
::forward<V>(value));
104
105
if
(rhiAdded)
106
m_data->registerCallback(rhi);
107
108
return
{
std
::addressof(rhiIt->second), rhiAdded };
109
}
110
111
Value *
get
(QRhi *rhi)
const
112
{
113
QReadLocker locker(&m_data->lock);
114
auto
rhiIt = m_data->storage.find(rhi);
115
return
rhiIt == m_data->storage.end() ?
nullptr
:
std
::addressof(rhiIt->second);
116
}
117
118
template
<
typename
Functor>
119
Value &
getOrCreate
(QRhi &rhi, Functor &&factory)
120
{
121
if
(Value *value =
get
(
&rhi
)
)
122
return
*value;
123
return
*tryMap(rhi, factory()).first;
124
}
125
126
// To be added for thread-safe value read:
127
//
128
// template <typename Reader>
129
// void read(QRhi &rhi, Reader&& reader) const {
130
// QReadLocker locker(&m_data->lock);
131
// auto rhiIt = m_data->storage.find(&rhi);
132
// if (rhiIt != m_data->storage.end())
133
// reader(const_cast<const Value&>(rhiIt->second));
134
// }
135
136
void
clear
()
137
{
138
if
(!m_data)
139
return
;
// the object has been moved
140
QWriteLocker locker(&m_data->lock);
141
m_data->storage.clear();
142
}
143
144
template
<
typename
Predicate>
145
QRhi *
findRhi
(Predicate &&p)
const
146
{
147
QReadLocker locker(&m_data->lock);
148
auto
&storage = m_data->storage;
149
namespace
ranges = QtMultimediaPrivate::ranges;
150
151
auto
it = ranges::find_if(storage, [&](
auto
&rhiItem) {
152
return
p(*rhiItem.first);
153
});
154
return
it == storage.end() ?
nullptr
: it->first;
155
}
156
157
private
:
158
std
::shared_ptr<Data> m_data;
159
};
160
161
QT_END_NAMESPACE
162
163
#
endif
// QRHIVALUEMAPPER_P_H
QMutexLocker
\inmodule QtCore
Definition
qmutex.h:347
QRhiCallback::Manager
Definition
qrhivaluemapper.cpp:16
QRhiCallback::Manager::registerCallback
void registerCallback(QRhi &rhi, const std::weak_ptr< QRhiCallback > &cb)
Definition
qrhivaluemapper.cpp:40
QRhiCallback
The QRhiCallback class implements a thread-safe wrapper around QRhi cleanup callbacks....
Definition
qrhivaluemapper_p.h:37
QRhiValueMapper
The class associates values of the specified type with different QRhi. One instance of QRhiValueMappe...
Definition
qrhivaluemapper_p.h:70
QRhiValueMapper::operator=
QRhiValueMapper & operator=(QRhiValueMapper &&) noexcept=default
QRhiValueMapper::getOrCreate
Value & getOrCreate(QRhi &rhi, Functor &&factory)
Definition
qrhivaluemapper_p.h:119
QRhiValueMapper::QRhiValueMapper
QRhiValueMapper(QRhiValueMapper &&) noexcept=default
QRhiValueMapper::findRhi
QRhi * findRhi(Predicate &&p) const
Definition
qrhivaluemapper_p.h:145
QRhiValueMapper::clear
void clear()
Definition
qrhivaluemapper_p.h:136
QRhiValueMapper::tryMap
std::pair< Value *, bool > tryMap(QRhi &rhi, V &&value)
Definition
qrhivaluemapper_p.h:99
QRhiValueMapper::~QRhiValueMapper
~QRhiValueMapper()
Definition
qrhivaluemapper_p.h:86
QRhiValueMapper::QRhiValueMapper
QRhiValueMapper()
Definition
qrhivaluemapper_p.h:96
QRhiValueMapper::get
Value * get(QRhi *rhi) const
Definition
qrhivaluemapper_p.h:111
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
q20
Definition
qnumeric.cpp:467
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
QBasicMutex
QMutex QBasicMutex
Definition
qmutex.h:361
Q_GLOBAL_STATIC
Q_GLOBAL_STATIC(const QTzTimeZoneHash, tzZones, loadTzTimeZones())
qtmultimedia
src
multimedia
qrhivaluemapper_p.h
Generated on
for Qt by
1.18.0