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
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
24QT_BEGIN_NAMESPACE
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
84 {
85 return m_referenceCount.fetch_add(1, std::memory_order_relaxed) + 1;
86 }
87
88 STDMETHODIMP_(ULONG) Release() override
89 {
90 const LONG referenceCount = m_referenceCount.fetch_sub(1, std::memory_order_release) - 1;
91 if (referenceCount == 0) {
92 // This acquire fence synchronizes with the release operation in other threads.
93 // It ensures that all memory writes made to this object by other threads
94 // are visible to this thread before we proceed to delete it.
95 std::atomic_thread_fence(std::memory_order_acquire);
96 delete this;
97 }
98
99 return referenceCount;
100 }
101
102protected:
103 QComObject() = default;
104
105 // Destructor is not public. Caller should call Release.
106 // Derived class should make its destructor private to force this behavior.
107 virtual ~QComObject() = default;
108
109 // allow derived classes to access the reference count
110 std::atomic<LONG> m_referenceCount = 1;
111
112private:
113 template <typename TInterface, typename... TRest>
114 HRESULT tryQueryInterface(REFIID riid, void **ppvObject)
115 {
116 if (QtPrivate::QComObjectTraits<TInterface>::isGuidOf(riid)) {
117 *ppvObject = static_cast<TInterface *>(this);
118 AddRef();
119
120 return S_OK;
121 }
122
123 if constexpr (sizeof...(TRest) > 0)
124 return tryQueryInterface<TRest...>(riid, ppvObject);
125
126 *ppvObject = nullptr;
127
128 return E_NOINTERFACE;
129 }
130};
131
132QT_END_NAMESPACE
133
134#endif // Q_OS_WIN
135
136#endif // QCOMOBJECT_P_H