Skip to content

Commit

Permalink
Merge pull request #41 from KDABLabs/wip/dfaure/as_const
Browse files Browse the repository at this point in the history
qAsConst is deprecated, use std::as_const
  • Loading branch information
iamsergio authored Nov 28, 2024
2 parents 00926ec + 97c2f49 commit 0de6862
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
20 changes: 17 additions & 3 deletions qt/KDStlContainerAdaptor/test/tst_KDStlContainerAdaptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ void tst_KDStlContainerAdaptor::vectorAdaptorIterators()
QCOMPARE(result, 15);
}

// For Qt5 (without C++17) and Qt6/C++17 compatibility
template<typename T>
struct AddConst
{
typedef const T type;
};
template<typename T>
constexpr typename AddConst<T>::type &asConst(T &t) noexcept
{
return t;
}
template<typename T>
void asConst(const T &&) = delete; // prevent rvalue arguments

void tst_KDStlContainerAdaptor::vectorAdaptorDataAccess()
{
IntVec v{1, 2, 3, 4, 5};
Expand All @@ -80,7 +94,7 @@ void tst_KDStlContainerAdaptor::vectorAdaptorDataAccess()
QCOMPARE(v.constData()[i], expected);
QCOMPARE(v.at(i), expected);
QCOMPARE(v[i], expected);
QCOMPARE(qAsConst(v)[i], expected);
QCOMPARE(asConst(v)[i], expected);
QCOMPARE(v.value(i), expected);
}

Expand All @@ -93,14 +107,14 @@ void tst_KDStlContainerAdaptor::vectorAdaptorDataAccess()
QCOMPARE(v.first(), -1);
v.first() = 123;
QCOMPARE(v.first(), 123);
QCOMPARE(qAsConst(v).first(), 123);
QCOMPARE(asConst(v).first(), 123);
QCOMPARE(v.constFirst(), 123);

v[4] = -1;
QCOMPARE(v.last(), -1);
v.last() = 456;
QCOMPARE(v.last(), 456);
QCOMPARE(qAsConst(v).last(), 456);
QCOMPARE(asConst(v).last(), 456);
QCOMPARE(v.constLast(), 456);
}

Expand Down
4 changes: 4 additions & 0 deletions qt/model_view/updateableModel/UpdateableModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,11 @@ class UpdateableModel : public BaseModel
// reconstruct vector of changed roles
QVector<int> roles;
roles.reserve(m_changedRoles.count());
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
for (int role : std::as_const(m_changedRoles))
#else
for (int role : qAsConst(m_changedRoles))
#endif
{
roles.append(role);
}
Expand Down
4 changes: 4 additions & 0 deletions qt/tabWindow/src/tabwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ void TabWindowManager::removeWindow(TabWindow *window)

TabWindow *TabWindowManager::possibleWindow(TabWindow *currentWindow, QPoint globalPos)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
for (auto tabWindow : std::as_const(m_windows))
#else
for (auto tabWindow : qAsConst(m_windows))
#endif
{
if (tabWindow == currentWindow)
continue;
Expand Down

0 comments on commit 0de6862

Please sign in to comment.