From 3291e0a77c10a92a63173d9ece4f83661f849ba9 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 24 Sep 2012 15:21:19 +0200 Subject: 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 Reviewed-by: Stephen Kelly --- src/corelib/itemmodels/qabstractitemmodel.h | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'src/corelib/itemmodels/qabstractitemmodel.h') diff --git a/src/corelib/itemmodels/qabstractitemmodel.h b/src/corelib/itemmodels/qabstractitemmodel.h index fdc4d6aa90..109ed32c30 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.h +++ b/src/corelib/itemmodels/qabstractitemmodel.h @@ -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(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(ptr)), m(amodel) {} inline QModelIndex(int arow, int acolumn, quintptr id, const QAbstractItemModel *amodel) - : r(arow), c(acolumn), p(reinterpret_cast(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); -- cgit v1.2.3