QTaggedPointer: some API cleanups
- don't refer to the class with template arguments in the body of the class (incl. one where we got the template args wrong) - make namespace-level swap a non-member friend - make default ctor constexpr and non-explicit (default ctors should never be explicit) - make ctor from std::nullptr_t constexpr - remove op= from std::nullptr (will be handled by op=(T*) anyway) - pass QTaggedPointer by value (it's a Trivial Type, so can be passed in registers) - fix missing QTypeInfo for non-default Tag types - remove unused include limits.h - make qHash-able (why is the tag ignored?) Change-Id: Idee1d685ac365c988698a8637fd5df3accfc1396 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
e5683c5e5f
commit
5ffa5808f8
@ -43,8 +43,7 @@
|
|||||||
#include <QtCore/qglobal.h>
|
#include <QtCore/qglobal.h>
|
||||||
#include <QtCore/qalgorithms.h>
|
#include <QtCore/qalgorithms.h>
|
||||||
#include <QtCore/qmath.h>
|
#include <QtCore/qmath.h>
|
||||||
|
#include <QtCore/qtypeinfo.h>
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
@ -80,10 +79,13 @@ public:
|
|||||||
static constexpr quintptr tagMask() { return QtPrivate::TagInfo<T>::alignment - 1; }
|
static constexpr quintptr tagMask() { return QtPrivate::TagInfo<T>::alignment - 1; }
|
||||||
static constexpr quintptr pointerMask() { return ~tagMask(); }
|
static constexpr quintptr pointerMask() { return ~tagMask(); }
|
||||||
|
|
||||||
explicit QTaggedPointer(T *pointer = nullptr, Tag tag = Tag()) noexcept
|
constexpr QTaggedPointer() noexcept : d(0) {}
|
||||||
|
constexpr QTaggedPointer(std::nullptr_t) noexcept : QTaggedPointer() {}
|
||||||
|
|
||||||
|
explicit QTaggedPointer(T *pointer, Tag tag = Tag()) noexcept
|
||||||
: d(quintptr(pointer))
|
: d(quintptr(pointer))
|
||||||
{
|
{
|
||||||
Q_STATIC_ASSERT(sizeof(Type*) == sizeof(QTaggedPointer<Type>));
|
Q_STATIC_ASSERT(sizeof(Type*) == sizeof(QTaggedPointer));
|
||||||
|
|
||||||
Q_ASSERT_X((quintptr(pointer) & tagMask()) == 0,
|
Q_ASSERT_X((quintptr(pointer) & tagMask()) == 0,
|
||||||
"QTaggedPointer<T, Tag>", "Pointer is not aligned");
|
"QTaggedPointer<T, Tag>", "Pointer is not aligned");
|
||||||
@ -107,18 +109,12 @@ public:
|
|||||||
return !isNull();
|
return !isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
QTaggedPointer<T, Tag> &operator=(T *other) noexcept
|
QTaggedPointer &operator=(T *other) noexcept
|
||||||
{
|
{
|
||||||
d = reinterpret_cast<quintptr>(other) | (d & tagMask());
|
d = reinterpret_cast<quintptr>(other) | (d & tagMask());
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
QTaggedPointer<T, Tag> &operator=(std::nullptr_t) noexcept
|
|
||||||
{
|
|
||||||
d &= tagMask();
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr Tag maximumTag() noexcept
|
static constexpr Tag maximumTag() noexcept
|
||||||
{
|
{
|
||||||
return TagType(typename QtPrivate::TagInfo<T>::TagType(tagMask()));
|
return TagType(typename QtPrivate::TagInfo<T>::TagType(tagMask()));
|
||||||
@ -147,58 +143,62 @@ public:
|
|||||||
return !data();
|
return !data();
|
||||||
}
|
}
|
||||||
|
|
||||||
void swap(QTaggedPointer<T, Tag> &other) noexcept
|
void swap(QTaggedPointer &other) noexcept
|
||||||
{
|
{
|
||||||
qSwap(d, other.d);
|
qSwap(d, other.d);
|
||||||
}
|
}
|
||||||
|
|
||||||
friend inline bool operator==(const QTaggedPointer<T, Tag> &lhs, const QTaggedPointer<T, Tag> &rhs) noexcept
|
friend inline bool operator==(QTaggedPointer lhs, QTaggedPointer rhs) noexcept
|
||||||
{
|
{
|
||||||
return lhs.data() == rhs.data();
|
return lhs.data() == rhs.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
friend inline bool operator!=(const QTaggedPointer<T, Tag> &lhs, const QTaggedPointer<T, Tag> &rhs) noexcept
|
friend inline bool operator!=(QTaggedPointer lhs, QTaggedPointer rhs) noexcept
|
||||||
{
|
{
|
||||||
return lhs.data() != rhs.data();
|
return lhs.data() != rhs.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
friend inline bool operator==(const QTaggedPointer<T, Tag> &lhs, std::nullptr_t) noexcept
|
friend inline bool operator==(QTaggedPointer lhs, std::nullptr_t) noexcept
|
||||||
{
|
{
|
||||||
return lhs.isNull();
|
return lhs.isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
friend inline bool operator==(std::nullptr_t, const QTaggedPointer<T, Tag> &rhs) noexcept
|
friend inline bool operator==(std::nullptr_t, QTaggedPointer rhs) noexcept
|
||||||
{
|
{
|
||||||
return rhs.isNull();
|
return rhs.isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
friend inline bool operator!=(const QTaggedPointer<T, Tag> &lhs, std::nullptr_t) noexcept
|
friend inline bool operator!=(QTaggedPointer lhs, std::nullptr_t) noexcept
|
||||||
{
|
{
|
||||||
return !lhs.isNull();
|
return !lhs.isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
friend inline bool operator!=(std::nullptr_t, const QTaggedPointer<T, Tag> &rhs) noexcept
|
friend inline bool operator!=(std::nullptr_t, QTaggedPointer rhs) noexcept
|
||||||
{
|
{
|
||||||
return !rhs.isNull();
|
return !rhs.isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
friend inline bool operator!(const QTaggedPointer<T, Tag> &ptr) noexcept
|
friend inline bool operator!(QTaggedPointer ptr) noexcept
|
||||||
{
|
{
|
||||||
return !ptr.data();
|
return !ptr.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
friend inline void swap(QTaggedPointer &p1, QTaggedPointer &p2) noexcept
|
||||||
|
{
|
||||||
|
p1.swap(p2);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
quintptr d;
|
quintptr d;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T, typename Tag>
|
||||||
Q_DECLARE_TYPEINFO_BODY(QTaggedPointer<T>, Q_MOVABLE_TYPE);
|
constexpr inline std::size_t qHash(QTaggedPointer<T, Tag> p, std::size_t seed = 0) noexcept
|
||||||
|
{ return qHash(p.data(), seed); }
|
||||||
|
|
||||||
template <typename T, typename Tag>
|
template <typename T, typename Tag>
|
||||||
inline void swap(QTaggedPointer<T, Tag> &p1, QTaggedPointer<T, Tag> &p2) noexcept
|
class QTypeInfo<QTaggedPointer<T, Tag>>
|
||||||
{
|
: public QTypeInfoMerger<QTaggedPointer<T, Tag>, quintptr> {};
|
||||||
p1.swap(p2);
|
|
||||||
}
|
|
||||||
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
|
@ -92,6 +92,18 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\fn template <typename T, typename Tag> QTaggedPointer<T, Tag>::QTaggedPointer()
|
||||||
|
|
||||||
|
Creates a tagged pointer that contains nullptr and stores no tag.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\fn template <typename T, typename Tag> QTaggedPointer<T, Tag>::QTaggedPointer(std::nullptr_t)
|
||||||
|
|
||||||
|
Creates a tagged pointer that contains nullptr and stores no tag.
|
||||||
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn template <typename T, typename Tag> explicit QTaggedPointer<T, Tag>::QTaggedPointer(T *pointer = nullptr, Tag tag = Tag()) noexcept
|
\fn template <typename T, typename Tag> explicit QTaggedPointer<T, Tag>::QTaggedPointer(T *pointer = nullptr, Tag tag = Tag()) noexcept
|
||||||
|
|
||||||
@ -159,7 +171,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn template <typename T, typename Tag> inline bool operator==(const QTaggedPointer<T, Tag> &lhs, const QTaggedPointer<T, Tag> &rhs) noexcept
|
\fn template <typename T, typename Tag> inline bool operator==(QTaggedPointer lhs, QTaggedPointer rhs) noexcept
|
||||||
\relates QTaggedPointer
|
\relates QTaggedPointer
|
||||||
|
|
||||||
Returns \c true if \a lhs is equal to \a rhs; otherwise returns \c false.
|
Returns \c true if \a lhs is equal to \a rhs; otherwise returns \c false.
|
||||||
@ -168,7 +180,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn template <typename T, typename Tag> inline bool operator!=(const QTaggedPointer<T, Tag> &lhs, const QTaggedPointer<T, Tag> &rhs) noexcept
|
\fn template <typename T, typename Tag> inline bool operator!=(QTaggedPointer lhs, QTaggedPointer rhs) noexcept
|
||||||
\relates QTaggedPointer
|
\relates QTaggedPointer
|
||||||
|
|
||||||
Returns \c true if \a lhs is not equal to \a rhs; otherwise returns \c false.
|
Returns \c true if \a lhs is not equal to \a rhs; otherwise returns \c false.
|
||||||
@ -177,30 +189,36 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn template <typename T, typename Tag> inline bool operator==(const QTaggedPointer<T, Tag> &lhs, std::nullptr_t) noexcept
|
\fn template <typename T, typename Tag> inline bool operator==(QTaggedPointer lhs, std::nullptr_t) noexcept
|
||||||
\relates QTaggedPointer
|
\relates QTaggedPointer
|
||||||
|
|
||||||
Returns \c true if \a lhs refers to \c nullptr.
|
Returns \c true if \a lhs refers to \c nullptr.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn template <typename T, typename Tag> inline bool operator==(std::nullptr_t, const QTaggedPointer<T, Tag> &rhs) noexcept
|
\fn template <typename T, typename Tag> inline bool operator==(std::nullptr_t, QTaggedPointer rhs) noexcept
|
||||||
\relates QTaggedPointer
|
\relates QTaggedPointer
|
||||||
|
|
||||||
Returns \c true if \a rhs refers to \c nullptr.
|
Returns \c true if \a rhs refers to \c nullptr.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn template <typename T, typename Tag> inline bool operator!=(const QTaggedPointer<T, Tag> &lhs, std::nullptr_t) noexcept
|
\fn template <typename T, typename Tag> inline bool operator!=(QTaggedPointerlhs, std::nullptr_t) noexcept
|
||||||
\relates QTaggedPointer
|
\relates QTaggedPointer
|
||||||
|
|
||||||
Returns \c true if \a lhs refers to a valid (i.e. non-null) pointer.
|
Returns \c true if \a lhs refers to a valid (i.e. non-null) pointer.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn template <typename T, typename Tag> inline bool operator!=(std::nullptr_t, const QTaggedPointer<T, Tag> &rhs) noexcept
|
\fn template <typename T, typename Tag> inline bool operator!=(std::nullptr_t, QTaggedPointer rhs) noexcept
|
||||||
\relates QTaggedPointer
|
\relates QTaggedPointer
|
||||||
|
|
||||||
Returns \c true if \a rhs refers to a valid (i.e. non-null) pointer.
|
Returns \c true if \a rhs refers to a valid (i.e. non-null) pointer.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\fn template <typename T, typename Tag> qHash(QTaggedPointer<T, Tag> key, std::size_t seed)
|
||||||
|
\relates QTaggedPointer
|
||||||
|
|
||||||
|
Returns the hash value for the \a key, using \a seed to seed the calculation.
|
||||||
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user