diff --git a/src/core/utils_win.cpp b/src/core/utils_win.cpp index 8bcabb9..cb05a89 100644 --- a/src/core/utils_win.cpp +++ b/src/core/utils_win.cpp @@ -38,7 +38,6 @@ #include "framelessmanager.h" #include "framelesshelper_windows.h" #include "framelessconfig_p.h" -#include #include Q_DECLARE_METATYPE(QMargins) @@ -127,6 +126,61 @@ FRAMELESSHELPER_STRING_CONSTANT(TrackPopupMenu) FRAMELESSHELPER_STRING_CONSTANT(ClientToScreen) FRAMELESSHELPER_STRING_CONSTANT2(HKEY_CURRENT_USER, "HKEY_CURRENT_USER") +template +class HumbleComPtr +{ + Q_DISABLE_COPY_MOVE(HumbleComPtr) + +public: + HumbleComPtr() = default; + + HumbleComPtr(std::nullptr_t ptr) + { + Q_UNUSED(ptr); + } + + ~HumbleComPtr() + { + if (p) { + p->Release(); + p = nullptr; + } + } + + [[nodiscard]] operator T*() const + { + return p; + } + + [[nodiscard]] T &operator*() const + { + Q_ASSERT(p); + return *p; + } + + // The assert on operator& usually indicates a bug. If this is really + // what is needed, however, take the address of the p member explicitly. + [[nodiscard]] T **operator&() + { + Q_ASSERT(false); + return &p; + } + + [[nodiscard]] T *operator->() const + { + Q_ASSERT(p); + return p; + } + + [[nodiscard]] bool operator!() const + { + return (p == nullptr); + } + +private: + T *p = nullptr; +}; + [[nodiscard]] static inline bool doCompareWindowsVersion(const DWORD dwMajor, const DWORD dwMinor, const DWORD dwBuild) { @@ -728,7 +782,7 @@ quint32 Utils::getPrimaryScreenDpi(const bool horizontal) reinterpret_cast( QSystemLibrary::resolve(kd2d1, "D2D1CreateFactory")); if (pD2D1CreateFactory) { - CComPtr d2dFactory = nullptr; + HumbleComPtr d2dFactory = nullptr; HRESULT hr = pD2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory), nullptr, reinterpret_cast(&d2dFactory)); if (SUCCEEDED(hr)) {