QModelIndex/QAIM: improve const correctness

QAIM::createIndex() takes a non-const void*. It's
typically called from inside QAIM::index(), which is a
const member function. If the data storage wrapped
by the model is const correct, it means that we have
to drop constness somewhere before calling createIndex().

To support this: change createIndex() to take a
const void * instead. This is painless.

Accessing the pointer is a bit more troubling, because
the accessor (QModelIndex::internalPointer()) returns
void *. (Effectively, now it does a const_cast...).
To avoid a massive source break, I've left it alone,
and instead added another function to retrieve a
const void *.

Read-only models can now be fully (deep) const correct.

[ChangeLog][QtCore][QAbstractItemModel] The
createIndex() function now takes a const void *, rather
than a void *.

[ChangeLog][QtCore][QModelIndex] Added the
constInternalPointer() function, to retrieve the
internal pointer as a pointer-to-const.

Change-Id: I108912b6814fcd5fe0c5cb7db6c721ba51e83de0
Reviewed-by: Samuel Gaist <samuel.gaist@idiap.ch>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
bb10
Giuseppe D'Angelo 2020-05-05 21:43:42 +02:00
parent 0de4f0dcc3
commit 8c889bf110
2 changed files with 31 additions and 4 deletions

View File

@ -351,6 +351,22 @@ void *QPersistentModelIndex::internalPointer() const
return nullptr;
}
/*!
\fn const void *QPersistentModelIndex::constInternalPointer() const
\since 6.0
\internal
Returns a \c{const void} \c{*} pointer used by the model to
associate the index with the internal data structure.
*/
const void *QPersistentModelIndex::constInternalPointer() const
{
if (d)
return d->index.constInternalPointer();
return nullptr;
}
/*!
\fn quintptr QPersistentModelIndex::internalId() const
@ -1064,6 +1080,15 @@ void QAbstractItemModel::resetInternalData()
\sa QAbstractItemModel::createIndex()
*/
/*!
\fn const void *QModelIndex::constInternalPointer() const
Returns a \c{const void} \c{*} pointer used by the model to associate
the index with the internal data structure.
\sa QAbstractItemModel::createIndex()
*/
/*!
\fn quintptr QModelIndex::internalId() const
@ -2589,7 +2614,7 @@ bool QAbstractItemModel::setHeaderData(int section, Qt::Orientation orientation,
}
/*!
\fn QModelIndex QAbstractItemModel::createIndex(int row, int column, void *ptr) const
\fn QModelIndex QAbstractItemModel::createIndex(int row, int column, const void *ptr) const
Creates a model index for the given \a row and \a column with the internal
pointer \a ptr.

View File

@ -63,6 +63,7 @@ public:
Q_DECL_CONSTEXPR inline int column() const noexcept { return c; }
Q_DECL_CONSTEXPR inline quintptr internalId() const noexcept { return i; }
inline void *internalPointer() const noexcept { return reinterpret_cast<void*>(i); }
inline const void *constInternalPointer() const noexcept { return reinterpret_cast<const void *>(i); }
inline QModelIndex parent() const;
inline QModelIndex sibling(int row, int column) const;
inline QModelIndex siblingAtColumn(int column) const;
@ -86,7 +87,7 @@ public:
|| (i == other.i && std::less<const QAbstractItemModel *>()(m, other.m))))));
}
private:
inline QModelIndex(int arow, int acolumn, void *ptr, const QAbstractItemModel *amodel) noexcept
inline QModelIndex(int arow, int acolumn, const void *ptr, const QAbstractItemModel *amodel) noexcept
: r(arow), c(acolumn), i(reinterpret_cast<quintptr>(ptr)), m(amodel) {}
Q_DECL_CONSTEXPR inline QModelIndex(int arow, int acolumn, quintptr id, const QAbstractItemModel *amodel) noexcept
: r(arow), c(acolumn), i(id), m(amodel) {}
@ -129,6 +130,7 @@ public:
int row() const;
int column() const;
void *internalPointer() const;
const void *constInternalPointer() const;
quintptr internalId() const;
QModelIndex parent() const;
QModelIndex sibling(int row, int column) const;
@ -307,7 +309,7 @@ protected Q_SLOTS:
protected:
QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent = nullptr);
inline QModelIndex createIndex(int row, int column, void *data = nullptr) const;
inline QModelIndex createIndex(int row, int column, const void *data = nullptr) const;
inline QModelIndex createIndex(int row, int column, quintptr id) const;
void encodeData(const QModelIndexList &indexes, QDataStream &stream) const;
@ -378,7 +380,7 @@ inline bool QAbstractItemModel::moveRow(const QModelIndex &sourceParent, int sou
inline bool QAbstractItemModel::moveColumn(const QModelIndex &sourceParent, int sourceColumn,
const QModelIndex &destinationParent, int destinationChild)
{ return moveColumns(sourceParent, sourceColumn, 1, destinationParent, destinationChild); }
inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, void *adata) const
inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, const void *adata) const
{ return QModelIndex(arow, acolumn, adata, this); }
inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, quintptr aid) const
{ return QModelIndex(arow, acolumn, aid, this); }