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// namespace QtPrivate {
53//
54// template <>
55// struct QComObjectTraits<IMFSampleGrabberSinkCallback>
56// {
57// static constexpr bool isGuidOf(REFIID riid) noexcept
58// {
59// return QComObjectTraits<IMFSampleGrabberSinkCallback, IMFClockStateSink>::isGuidOf(riid);
60// }
61// };
62//
63// }
64
65template <typename TFirstInterface, typename... TAdditionalInterfaces>
66class QComObject : public TFirstInterface, public TAdditionalInterfaces...
67{
68public:
69 STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject) override
70 {
71 if (!ppvObject)
72 return E_POINTER;
73
74 if (riid == __uuidof(IUnknown)) {
75 *ppvObject = static_cast<IUnknown *>(static_cast<TFirstInterface *>(this));
76 AddRef();
77
78 return S_OK;
79 }
80
81 return tryQueryInterface<TFirstInterface, TAdditionalInterfaces...>(riid, ppvObject);
82 }
83
84 STDMETHODIMP_(ULONG) AddRef() override
85 {
86 return m_referenceCount.fetch_add(1, std::memory_order_relaxed) + 1;
87 }
88
89 STDMETHODIMP_(ULONG) Release() override
90 {
91 const LONG referenceCount = m_referenceCount.fetch_sub(1, std::memory_order_release) - 1;
92 if (referenceCount == 0) {
93 // This acquire fence synchronizes with the release operation in other threads.
94 // It ensures that all memory writes made to this object by other threads
95 // are visible to this thread before we proceed to delete it.
96 std::atomic_thread_fence(std::memory_order_acquire);
97 delete this;
98 }
99
100 return referenceCount;
101 }
102
103protected:
104 QComObject() = default;
105
106 // Destructor is not public. Caller should call Release.
107 // Derived class should make its destructor private to force this behavior.
108 virtual ~QComObject() = default;
109
110 // allow derived classes to access the reference count
111 std::atomic<LONG> m_referenceCount = 1;
112
113private:
114 template <typename TInterface, typename... TRest>
115 HRESULT tryQueryInterface(REFIID riid, void **ppvObject)
116 {
117 if (QtPrivate::QComObjectTraits<TInterface>::isGuidOf(riid)) {
118 *ppvObject = static_cast<TInterface *>(this);
119 AddRef();
120
121 return S_OK;
122 }
123
124 if constexpr (sizeof...(TRest) > 0)
125 return tryQueryInterface<TRest...>(riid, ppvObject);
126
127 *ppvObject = nullptr;
128
129 return E_NOINTERFACE;
130 }
131};
132
133QT_END_NAMESPACE
134
135#endif // Q_OS_WIN
136
137#endif // QCOMOBJECT_P_H