Use ComPtr smart pointer to make transfer of ownership explicit

With ComPtr, transfer of ownership is explicit in the code because we
see the 'Detach' function being used.

Task-number: QTBUG-126530
Change-Id: I815c22f21b4a9b174d0d49ceb014bbffe41b6e82
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
(cherry picked from commit d7edf2894c94271c3794a85ecf24a24ce92d2fcd)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Jøger Hansegård 2024-06-27 15:49:15 +02:00 committed by Qt Cherry-pick Bot
parent 89ba182737
commit f5ff1096ec
3 changed files with 29 additions and 27 deletions

View File

@ -271,31 +271,31 @@ HRESULT QWindowsUiaMainProvider::GetPatternProvider(PATTERNID idPattern, IUnknow
switch (idPattern) {
case UIA_WindowPatternId:
if (accessible->parent() && (accessible->parent()->role() == QAccessible::Application)) {
*pRetVal = new QWindowsUiaWindowProvider(id());
*pRetVal = makeComObject<QWindowsUiaWindowProvider>(id()).Detach();
}
break;
case UIA_TextPatternId:
case UIA_TextPattern2Id:
// All text controls.
if (accessible->textInterface()) {
*pRetVal = new QWindowsUiaTextProvider(id());
*pRetVal = makeComObject<QWindowsUiaTextProvider>(id()).Detach();
}
break;
case UIA_ValuePatternId:
// All non-static controls support the Value pattern.
if (accessible->role() != QAccessible::StaticText)
*pRetVal = new QWindowsUiaValueProvider(id());
*pRetVal = makeComObject<QWindowsUiaValueProvider>(id()).Detach();
break;
case UIA_RangeValuePatternId:
// Controls providing a numeric value within a range (e.g., sliders, scroll bars, dials).
if (accessible->valueInterface()) {
*pRetVal = new QWindowsUiaRangeValueProvider(id());
*pRetVal = makeComObject<QWindowsUiaRangeValueProvider>(id()).Detach();
}
break;
case UIA_TogglePatternId:
// Checkboxes and other checkable controls.
if (accessible->state().checkable)
*pRetVal = new QWindowsUiaToggleProvider(id());
*pRetVal = makeComObject<QWindowsUiaToggleProvider>(id()).Detach();
break;
case UIA_SelectionPatternId:
case UIA_SelectionPattern2Id:
@ -303,7 +303,7 @@ HRESULT QWindowsUiaMainProvider::GetPatternProvider(PATTERNID idPattern, IUnknow
if (accessible->selectionInterface()
|| accessible->role() == QAccessible::List
|| accessible->role() == QAccessible::PageTabList) {
*pRetVal = new QWindowsUiaSelectionProvider(id());
*pRetVal = makeComObject<QWindowsUiaSelectionProvider>(id()).Detach();
}
break;
case UIA_SelectionItemPatternId:
@ -312,41 +312,41 @@ HRESULT QWindowsUiaMainProvider::GetPatternProvider(PATTERNID idPattern, IUnknow
|| (accessible->role() == QAccessible::RadioButton)
|| (accessible->role() == QAccessible::ListItem)
|| (accessible->role() == QAccessible::PageTab)) {
*pRetVal = new QWindowsUiaSelectionItemProvider(id());
*pRetVal = makeComObject<QWindowsUiaSelectionItemProvider>(id()).Detach();
}
break;
case UIA_TablePatternId:
// Table/tree.
if (accessible->tableInterface()
&& ((accessible->role() == QAccessible::Table) || (accessible->role() == QAccessible::Tree))) {
*pRetVal = new QWindowsUiaTableProvider(id());
*pRetVal = makeComObject<QWindowsUiaTableProvider>(id()).Detach();
}
break;
case UIA_TableItemPatternId:
// Item within a table/tree.
if (accessible->tableCellInterface()
&& ((accessible->role() == QAccessible::Cell) || (accessible->role() == QAccessible::TreeItem))) {
*pRetVal = new QWindowsUiaTableItemProvider(id());
*pRetVal = makeComObject<QWindowsUiaTableItemProvider>(id()).Detach();
}
break;
case UIA_GridPatternId:
// Table/tree.
if (accessible->tableInterface()
&& ((accessible->role() == QAccessible::Table) || (accessible->role() == QAccessible::Tree))) {
*pRetVal = new QWindowsUiaGridProvider(id());
*pRetVal = makeComObject<QWindowsUiaGridProvider>(id()).Detach();
}
break;
case UIA_GridItemPatternId:
// Item within a table/tree.
if (accessible->tableCellInterface()
&& ((accessible->role() == QAccessible::Cell) || (accessible->role() == QAccessible::TreeItem))) {
*pRetVal = new QWindowsUiaGridItemProvider(id());
*pRetVal = makeComObject<QWindowsUiaGridItemProvider>(id()).Detach();
}
break;
case UIA_InvokePatternId:
// Things that have an invokable action (e.g., simple buttons).
if (accessible->actionInterface()) {
*pRetVal = new QWindowsUiaInvokeProvider(id());
*pRetVal = makeComObject<QWindowsUiaInvokeProvider>(id()).Detach();
}
break;
case UIA_ExpandCollapsePatternId:
@ -356,7 +356,7 @@ HRESULT QWindowsUiaMainProvider::GetPatternProvider(PATTERNID idPattern, IUnknow
&& accessible->child(0)->role() == QAccessible::PopupMenu)
|| accessible->role() == QAccessible::ComboBox
|| (accessible->role() == QAccessible::TreeItem && accessible->state().expandable)) {
*pRetVal = new QWindowsUiaExpandCollapseProvider(id());
*pRetVal = makeComObject<QWindowsUiaExpandCollapseProvider>(id()).Detach();
}
break;
default:

View File

@ -11,7 +11,7 @@
#include <QtGui/qaccessible.h>
#include <QtCore/qloggingcategory.h>
#include <QtCore/qstring.h>
#include <QtCore/private/qcomptr_p.h>
QT_BEGIN_NAMESPACE
using namespace QWindowsUiAutomation;
@ -50,9 +50,9 @@ HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::GetSelection(SAFEARRAY **pRet
for (LONG i = 0; i < selCount; ++i) {
int startOffset = 0, endOffset = 0;
textInterface->selection((int)i, &startOffset, &endOffset);
auto *textRangeProvider = new QWindowsUiaTextRangeProvider(id(), startOffset, endOffset);
SafeArrayPutElement(*pRetVal, &i, static_cast<IUnknown *>(textRangeProvider));
textRangeProvider->Release();
ComPtr<IUnknown> textRangeProvider =
makeComObject<QWindowsUiaTextRangeProvider>(id(), startOffset, endOffset);
SafeArrayPutElement(*pRetVal, &i, textRangeProvider.Get());
}
}
} else {
@ -60,9 +60,9 @@ HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::GetSelection(SAFEARRAY **pRet
if ((*pRetVal = SafeArrayCreateVector(VT_UNKNOWN, 0, 1))) {
LONG i = 0;
int cursorPosition = textInterface->cursorPosition();
auto *textRangeProvider = new QWindowsUiaTextRangeProvider(id(), cursorPosition, cursorPosition);
SafeArrayPutElement(*pRetVal, &i, static_cast<IUnknown *>(textRangeProvider));
textRangeProvider->Release();
ComPtr<IUnknown> textRangeProvider = makeComObject<QWindowsUiaTextRangeProvider>(
id(), cursorPosition, cursorPosition);
SafeArrayPutElement(*pRetVal, &i, textRangeProvider.Get());
}
}
return S_OK;
@ -88,9 +88,9 @@ HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::GetVisibleRanges(SAFEARRAY **
// Considering the entire text as visible.
if ((*pRetVal = SafeArrayCreateVector(VT_UNKNOWN, 0, 1))) {
LONG i = 0;
auto *textRangeProvider = new QWindowsUiaTextRangeProvider(id(), 0, textInterface->characterCount());
SafeArrayPutElement(*pRetVal, &i, static_cast<IUnknown *>(textRangeProvider));
textRangeProvider->Release();
ComPtr<IUnknown> textRangeProvider =
makeComObject<QWindowsUiaTextRangeProvider>(id(), 0, textInterface->characterCount());
SafeArrayPutElement(*pRetVal, &i, textRangeProvider.Get());
}
return S_OK;
}
@ -135,7 +135,7 @@ HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::RangeFromPoint(UiaPoint point
if (offset < 0 || offset >= textInterface->characterCount())
return UIA_E_ELEMENTNOTAVAILABLE;
*pRetVal = new QWindowsUiaTextRangeProvider(id(), offset, offset);
*pRetVal = makeComObject<QWindowsUiaTextRangeProvider>(id(), offset, offset).Detach();
return S_OK;
}
@ -156,7 +156,8 @@ HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::get_DocumentRange(ITextRangeP
if (!textInterface)
return UIA_E_ELEMENTNOTAVAILABLE;
*pRetVal = new QWindowsUiaTextRangeProvider(id(), 0, textInterface->characterCount());
*pRetVal = makeComObject<QWindowsUiaTextRangeProvider>(id(), 0, textInterface->characterCount())
.Detach();
return S_OK;
}
@ -202,7 +203,8 @@ HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::GetCaretRange(BOOL *isActive,
*isActive = accessible->state().focused;
int cursorPosition = textInterface->cursorPosition();
*pRetVal = new QWindowsUiaTextRangeProvider(id(), cursorPosition, cursorPosition);
*pRetVal = makeComObject<QWindowsUiaTextRangeProvider>(id(), cursorPosition, cursorPosition)
.Detach();
return S_OK;
}

View File

@ -45,7 +45,7 @@ HRESULT QWindowsUiaTextRangeProvider::Clone(ITextRangeProvider **pRetVal)
if (!pRetVal)
return E_INVALIDARG;
*pRetVal = new QWindowsUiaTextRangeProvider(id(), m_startOffset, m_endOffset);
*pRetVal = makeComObject<QWindowsUiaTextRangeProvider>(id(), m_startOffset, m_endOffset).Detach();
return S_OK;
}