Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qwindowscombase.h
Go to the documentation of this file.
1// Copyright (C) 2017 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 QWINDOWSCOMBASE_H
5#define QWINDOWSCOMBASE_H
6
7#include <qt_windows.h>
8
9#include <QtCore/qglobal.h>
10
11#include <unknwn.h>
12
14
15// The __uuidof operator of MinGW does not work for all interfaces (for example,
16// IAccessible2). Specializations of this function can be provides to work
17// around this.
18template <class DesiredInterface>
19static IID qUuidOf() { return __uuidof(DesiredInterface); }
20
21// Helper for implementing IUnknown::QueryInterface.
22template <class DesiredInterface, class Derived>
23bool qWindowsComQueryInterface(Derived *d, REFIID id, LPVOID *iface)
24{
25 if (id == qUuidOf<DesiredInterface>()) {
26 *iface = static_cast<DesiredInterface *>(d);
27 d->AddRef();
28 return true;
29 }
30 return false;
31}
32
33// Helper for implementing IUnknown::QueryInterface for IUnknown
34// in the case of multiple inheritance via the first inherited class.
35template <class FirstInheritedInterface, class Derived>
36bool qWindowsComQueryUnknownInterfaceMulti(Derived *d, REFIID id, LPVOID *iface)
37{
38 if (id == __uuidof(IUnknown)) {
39 *iface = static_cast<FirstInheritedInterface *>(d);
40 d->AddRef();
41 return true;
42 }
43 return false;
44}
45
46// Helper base class to provide IUnknown methods for COM classes (single inheritance)
47template <class ComInterface> class QWindowsComBase : public ComInterface
48{
49 Q_DISABLE_COPY_MOVE(QWindowsComBase)
50public:
51 explicit QWindowsComBase(ULONG initialRefCount = 1) : m_ref(initialRefCount) {}
52 virtual ~QWindowsComBase() = default;
53
54 HRESULT STDMETHODCALLTYPE QueryInterface(REFIID id, LPVOID *iface) override
55 {
56 *iface = nullptr;
57 return qWindowsComQueryInterface<IUnknown>(this, id, iface) || qWindowsComQueryInterface<ComInterface>(this, id, iface)
58 ? S_OK : E_NOINTERFACE;
59 }
60
61 ULONG STDMETHODCALLTYPE AddRef() override { return ++m_ref; }
62
63 ULONG STDMETHODCALLTYPE Release() override
64 {
65 if (!--m_ref) {
66 delete this;
67 return 0;
68 }
69 return m_ref;
70 }
71
72private:
73 ULONG m_ref;
74};
75
76// Clang does not consider __declspec(nothrow) as nothrow
77QT_WARNING_DISABLE_CLANG("-Wmicrosoft-exception-spec")
78QT_WARNING_DISABLE_CLANG("-Wmissing-exception-spec")
79
81
82#endif // QWINDOWSCOMBASE_H
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID id, LPVOID *iface) override
ULONG STDMETHODCALLTYPE Release() override
ULONG STDMETHODCALLTYPE AddRef() override
virtual ~QWindowsComBase()=default
QWindowsComBase(ULONG initialRefCount=1)
Combined button and popup list for selecting options.
#define QT_WARNING_DISABLE_CLANG(text)
long HRESULT
bool qWindowsComQueryUnknownInterfaceMulti(Derived *d, REFIID id, LPVOID *iface)
bool qWindowsComQueryInterface(Derived *d, REFIID id, LPVOID *iface)
static QT_BEGIN_NAMESPACE IID qUuidOf()