Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#ifndef QCOMOBJECT_P_H
5#define QCOMOBJECT_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 <QtCore/private/qglobal_p.h>
19
20#if defined(Q_OS_WIN) || defined(Q_QDOC)
21
22# include <QtCore/qt_windows.h>
23
25
26namespace QtPrivate {
27
28template <typename... TInterfaces>
29struct QComObjectTraits
30{
31 static constexpr bool isGuidOf(REFIID riid) noexcept
32 {
33 return ((riid == __uuidof(TInterfaces)) || ...);
34 }
35};
36
37} // namespace QtPrivate
38
39// NOTE: In order to be able to query the intermediate interface, i.e. the one you do not specify in
40// QComObject interface list (TFirstInterface, TInterfaces) but that is a base for any of them
41// (except IUnknown) you need to provide an explicit specialization of function
42// QComObjectTraits<...>::isGuidOf for that type. For example, if you want to inherit interface
43// IMFSampleGrabberSinkCallback which inherits IMFClockStateSink and you want to be able to query
44// the latter one you need to provide this explicit specialization:
45//
46// class SinkCallback : public QComObject<IMFSampleGrabberSinkCallback>
47// {
48// ...
49// };
50//
51// namespace QtPrivate {
52//
53// template <>
54// struct QComObjectTraits<IMFSampleGrabberSinkCallback>
55// {
56// static constexpr bool isGuidOf(REFIID riid) noexcept
57// {
58// return QComObjectTraits<IMFSampleGrabberSinkCallback, IMFClockStateSink>::isGuidOf(riid);
59// }
60// };
61//
62// }
63
64template <typename TFirstInterface, typename... TAdditionalInterfaces>
65class QComObject : public TFirstInterface, public TAdditionalInterfaces...
66{
67public:
68 STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject) override
69 {
70 if (!ppvObject)
71 return E_POINTER;
72
73 if (riid == __uuidof(IUnknown)) {
74 *ppvObject = static_cast<IUnknown *>(static_cast<TFirstInterface *>(this));
75 AddRef();
76
77 return S_OK;
78 }
79
80 return tryQueryInterface<TFirstInterface, TAdditionalInterfaces...>(riid, ppvObject);
81 }
82
83 STDMETHODIMP_(ULONG) AddRef() override { return ++m_referenceCount; }
84
85 STDMETHODIMP_(ULONG) Release() override
86 {
87 const LONG referenceCount = --m_referenceCount;
88 if (referenceCount == 0)
89 delete this;
90
91 return referenceCount;
92 }
93
94protected:
95 QComObject() = default;
96
97 // Destructor is not public. Caller should call Release.
98 // Derived class should make its destructor private to force this behavior.
99 virtual ~QComObject() = default;
100
101private:
102 template <typename TInterface, typename... TRest>
103 HRESULT tryQueryInterface(REFIID riid, void **ppvObject)
104 {
105 if (QtPrivate::QComObjectTraits<TInterface>::isGuidOf(riid)) {
106 *ppvObject = static_cast<TInterface *>(this);
107 AddRef();
108
109 return S_OK;
110 }
111
112 if constexpr (sizeof...(TRest) > 0)
113 return tryQueryInterface<TRest...>(riid, ppvObject);
114
115 *ppvObject = nullptr;
116
117 return E_NOINTERFACE;
118 }
119
120 std::atomic<LONG> m_referenceCount = 1;
121};
122
124
125#endif // Q_OS_WIN
126
127#endif // QCOMOBJECT_P_H
STDMETHODIMP_(ULONG) MFDecoderSourceReader
Combined button and popup list for selecting options.
\macro QT_NO_KEYWORDS >
IUIViewSettingsInterop __RPC__in REFIID riid
long HRESULT