Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcomobject_p.h
Go to the documentation of this file.
1// Copyright (C) 2023 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// Qt-Security score:significant reason:default
4
5#ifndef QCOMOBJECT_P_H
6#define QCOMOBJECT_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/private/qglobal_p.h>
20
21#if defined(Q_OS_WIN) || defined(Q_QDOC)
22
23# include <QtCore/qt_windows.h>
24
25QT_BEGIN_NAMESPACE
26
27namespace QtPrivate {
28
29template <typename... TInterfaces>
30struct QComObjectTraits
31{
32 static constexpr bool isGuidOf(REFIID riid) noexcept
33 {
34 return ((riid == __uuidof(TInterfaces)) || ...);
35 }
36};
37
38} // namespace QtPrivate
39
40// NOTE: In order to be able to query the intermediate interface, i.e. the one you do not specify in
41// QComObject interface list (TFirstInterface, TInterfaces) but that is a base for any of them
42// (except IUnknown) you need to provide an explicit specialization of function
43// QComObjectTraits<...>::isGuidOf for that type. For example, if you want to inherit interface
44// IMFSampleGrabberSinkCallback which inherits IMFClockStateSink and you want to be able to query
45// the latter one you need to provide this explicit specialization:
46//
47// class SinkCallback : public QComObject<IMFSampleGrabberSinkCallback>
48// {
49// ...
50// };
51//
52// If the object is a QObject that needs to go through deleteLater(), inherit from
53// QComObjectWithDeleteLater instead of QComObject:
54// class SinkCallback : public QObject,
55// public QComObjectWithDeleteLater<SinkCallback, IMFSampleGrabberSinkCallback>
56// {
57// ...
58// };
59//
60//
61// namespace QtPrivate {
62//
63// template <>
64// struct QComObjectTraits<IMFSampleGrabberSinkCallback>
65// {
66// static constexpr bool isGuidOf(REFIID riid) noexcept
67// {
68// return QComObjectTraits<IMFSampleGrabberSinkCallback, IMFClockStateSink>::isGuidOf(riid);
69// }
70// };
71//
72// }
73
74template <typename TFirstInterface, typename... TAdditionalInterfaces>
75class QComObject : public TFirstInterface, public TAdditionalInterfaces...
76{
77public:
78 STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject) override
79 {
80 if (!ppvObject)
81 return E_POINTER;
82
83 if (riid == __uuidof(IUnknown)) {
84 *ppvObject = static_cast<IUnknown *>(static_cast<TFirstInterface *>(this));
85 AddRef();
86
87 return S_OK;
88 }
89
90 return tryQueryInterface<TFirstInterface, TAdditionalInterfaces...>(riid, ppvObject);
91 }
92
93 STDMETHODIMP_(ULONG) AddRef() override
94 {
95 return m_referenceCount.fetch_add(1, std::memory_order_relaxed) + 1;
96 }
97
98 STDMETHODIMP_(ULONG) Release() override
99 {
100 const LONG referenceCount = unref();
101 if (referenceCount == 0)
102 delete this;
103
104 return referenceCount;
105 }
106
107protected:
108 QComObject() = default;
109
110 // Destructor is not public. Caller should call Release.
111 // Derived class should make its destructor private to force this behavior.
112 virtual ~QComObject() = default;
113
114 // allow derived classes to access the reference count
115 std::atomic<LONG> m_referenceCount = 1;
116
117 [[nodiscard]] LONG unref()
118 {
119 const LONG referenceCount = m_referenceCount.fetch_sub(1, std::memory_order_release) - 1;
120 if (referenceCount == 0) {
121 // This acquire fence synchronizes with the release operation in other threads.
122 // It ensures that all memory writes made to this object by other threads
123 // are visible to this thread before we proceed to delete it.
124 std::atomic_thread_fence(std::memory_order_acquire);
125 }
126 return referenceCount;
127 }
128
129private:
130 template <typename TInterface, typename... TRest>
131 HRESULT tryQueryInterface(REFIID riid, void **ppvObject)
132 {
133 if (QtPrivate::QComObjectTraits<TInterface>::isGuidOf(riid)) {
134 *ppvObject = static_cast<TInterface *>(this);
135 AddRef();
136
137 return S_OK;
138 }
139
140 if constexpr (sizeof...(TRest) > 0)
141 return tryQueryInterface<TRest...>(riid, ppvObject);
142
143 *ppvObject = nullptr;
144
145 return E_NOINTERFACE;
146 }
147};
148
149template <typename Self, typename... Interfaces>
150class QComObjectWithDeleteLater : public QComObject<Interfaces...>
151{
152public:
153 STDMETHODIMP_(ULONG) Release() override
154 {
155 const LONG referenceCount = QComObject<Interfaces...>::unref();
156 if (referenceCount == 0)
157 static_cast<Self *>(this)->deleteLater();
158
159 return referenceCount;
160 }
161};
162
163QT_END_NAMESPACE
164
165#endif // Q_OS_WIN
166
167#endif // QCOMOBJECT_P_H