QModelIndex: store quintptr instead of void*
Rationale: 1. Comparing pointers that don't point the same array is undefined behaviour, IIRC, and op== and op< did that. 2. The functions that cast to/from the storage type can't be constexpr. It makes more sense to have the quintptr functions be constexpr (they have a fighting chance to actually get passed something constant) than it is to have the void* functions constexpr. Thus, the storage type should be quintptr. Also prepare op< to be constexpr-compatible. Change-Id: I4b2d4a0ec8ca80d619d272bf07c57887cbd11c2f Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
This commit is contained in:
parent
44a0e972a0
commit
3291e0a77c
@ -59,12 +59,12 @@ class Q_CORE_EXPORT QModelIndex
|
||||
{
|
||||
friend class QAbstractItemModel;
|
||||
public:
|
||||
inline QModelIndex() : r(-1), c(-1), p(0), m(0) {}
|
||||
inline QModelIndex() : r(-1), c(-1), i(0), m(0) {}
|
||||
// compiler-generated copy/move ctors/assignment operators are fine!
|
||||
inline int row() const { return r; }
|
||||
inline int column() const { return c; }
|
||||
inline void *internalPointer() const { return p; }
|
||||
inline quintptr internalId() const { return quintptr(p); }
|
||||
inline quintptr internalId() const { return i; }
|
||||
inline void *internalPointer() const { return reinterpret_cast<void*>(i); }
|
||||
inline QModelIndex parent() const;
|
||||
inline QModelIndex sibling(int row, int column) const;
|
||||
inline QModelIndex child(int row, int column) const;
|
||||
@ -73,27 +73,23 @@ public:
|
||||
inline const QAbstractItemModel *model() const { return m; }
|
||||
inline bool isValid() const { return (r >= 0) && (c >= 0) && (m != 0); }
|
||||
inline bool operator==(const QModelIndex &other) const
|
||||
{ return (other.r == r) && (other.p == p) && (other.c == c) && (other.m == m); }
|
||||
{ return (other.r == r) && (other.i == i) && (other.c == c) && (other.m == m); }
|
||||
inline bool operator!=(const QModelIndex &other) const
|
||||
{ return !(*this == other); }
|
||||
inline bool operator<(const QModelIndex &other) const
|
||||
{
|
||||
if (r < other.r) return true;
|
||||
if (r == other.r) {
|
||||
if (c < other.c) return true;
|
||||
if (c == other.c) {
|
||||
if (p < other.p) return true;
|
||||
if (p == other.p) return m < other.m;
|
||||
}
|
||||
}
|
||||
return false; }
|
||||
return r < other.r
|
||||
|| (r == other.r && (c < other.c
|
||||
|| (c == other.c && (i < other.i
|
||||
|| (i == other.i && m < other.m )))));
|
||||
}
|
||||
private:
|
||||
inline QModelIndex(int arow, int acolumn, void *ptr, const QAbstractItemModel *amodel)
|
||||
: r(arow), c(acolumn), p(ptr), m(amodel) {}
|
||||
: r(arow), c(acolumn), i(reinterpret_cast<quintptr>(ptr)), m(amodel) {}
|
||||
inline QModelIndex(int arow, int acolumn, quintptr id, const QAbstractItemModel *amodel)
|
||||
: r(arow), c(acolumn), p(reinterpret_cast<void*>(id)), m(amodel) {}
|
||||
: r(arow), c(acolumn), i(id), m(amodel) {}
|
||||
int r, c;
|
||||
void *p;
|
||||
quintptr i;
|
||||
const QAbstractItemModel *m;
|
||||
};
|
||||
Q_DECLARE_TYPEINFO(QModelIndex, Q_MOVABLE_TYPE);
|
||||
|
Loading…
Reference in New Issue
Block a user