QQuickPalette: replace a QFlatMap with std::array<.,3>

Using a QFlatMap for an enum-to-object mapping when the valid enum
values are {0, 1, 2} is complete overkill.

A std::array<T,3> has both a more convenient API, and much more
efficiency.

Add some assertions.

Pick-to: 6.3
Change-Id: I6958302d1a7d0fd591e38efeea8291b3e9cc7212
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Marc Mutz 2022-01-09 16:58:06 +01:00
parent ce443cd2c3
commit 2133e5bc35
2 changed files with 27 additions and 13 deletions

View File

@ -43,6 +43,22 @@
QT_BEGIN_NAMESPACE
static constexpr bool is_valid(QPalette::ColorGroup cg) noexcept
{
// use a switch to enable "unhandled enum" warnings:
switch (cg) {
case QPalette::Active:
case QPalette::Disabled:
case QPalette::Inactive:
return true;
case QPalette::NColorGroups:
case QPalette::Current:
case QPalette::All:
return false;
}
Q_UNREACHABLE();
}
/*!
\internal
@ -284,20 +300,19 @@ QQuickColorGroup::GroupPtr QQuickPalette::colorGroup(QPalette::ColorGroup groupT
QQuickColorGroup::GroupPtr QQuickPalette::findColorGroup(QPalette::ColorGroup groupTag) const
{
if (auto it = m_colorGroups.find(groupTag); it != m_colorGroups.end()) {
return it->second;
}
return nullptr;
Q_ASSERT(is_valid(groupTag));
return m_colorGroups[groupTag];
}
void QQuickPalette::registerColorGroup(QQuickColorGroup *group, QPalette::ColorGroup groupTag)
{
if (auto it = m_colorGroups.find(groupTag); it != m_colorGroups.end() && it->second) {
it->second->deleteLater();
Q_ASSERT(is_valid(groupTag));
auto &g = m_colorGroups[groupTag];
if (g) {
Q_ASSERT(g != group);
g->deleteLater();
}
m_colorGroups[groupTag] = group;
g = group;
group->setGroupTag(groupTag);

View File

@ -50,11 +50,10 @@
// We mean it.
//
#include <vector> // Workaround: I think we should include vector to qflatmap_p.h
#include <QtCore/private/qflatmap_p.h>
#include <QtQuick/private/qquickcolorgroup_p.h>
#include <array>
QT_BEGIN_NAMESPACE
class QQuickAbstractPaletteProvider;
@ -119,7 +118,7 @@ private:
static constexpr QPalette::ColorGroup defaultCurrentGroup() { return QPalette::Active; }
private:
QFlatMap<QPalette::ColorGroup, QQuickColorGroup::GroupPtr> m_colorGroups;
std::array<QQuickColorGroup::GroupPtr, QPalette::NColorGroups> m_colorGroups = {};
QPalette::ColorGroup m_currentGroup;
};