diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2012-09-24 15:21:19 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-09-24 18:01:37 +0200 |
commit | 3291e0a77c10a92a63173d9ece4f83661f849ba9 (patch) | |
tree | 6239ef4c0a84174888bd71df38b2348b74637f05 /src/corelib | |
parent | 44a0e972a0fc6787f1d275a457c2bb0a0d10aff8 (diff) |
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>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/itemmodels/qabstractitemmodel.h | 28 |
1 files changed, 12 insertions, 16 deletions
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<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); |