QScoped(Array)Pointer: canonicalize swapping

This includes:
- have nothrow member-swap
- have ADL non-member swap
- not specialize qSwap or std::swap

Also prevent QScopedPointer <-> QScopedArrayPointer swaps by overloading
swap (both member and non-member) on QScopedArrayPointer. It's not 100%
safe, but it's what we're doing elsewhere (QMulti(Map,Hash), say).

That's technically a SiC change if users expected (qualified) std::swap
to invoke QScopedPointer::swap(), but those users were doing it wrong to
begin with, and they now get a compile-error instead of silent pessimization,
because generic std::swap() doesn't work on QScopedPointer, due to lack
of copy (and thus move) semantics.

Change-Id: I3ab5c1668722a2c8ccafc16f57310ce8d4bffbd6
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
bb10
Marc Mutz 2015-07-06 12:27:01 +02:00
parent f3af7fce4a
commit 5eae0ce800
1 changed files with 9 additions and 11 deletions

View File

@ -162,7 +162,7 @@ public:
return oldD;
}
inline void swap(QScopedPointer<T, Cleanup> &other)
void swap(QScopedPointer<T, Cleanup> &other) Q_DECL_NOTHROW
{
qSwap(d, other.d);
}
@ -189,18 +189,9 @@ inline bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPoint
}
template <class T, class Cleanup>
Q_INLINE_TEMPLATE void qSwap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2)
inline void swap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2) Q_DECL_NOTHROW
{ p1.swap(p2); }
QT_END_NAMESPACE
namespace std {
template <class T, class Cleanup>
Q_INLINE_TEMPLATE void swap(QT_PREPEND_NAMESPACE(QScopedPointer)<T, Cleanup> &p1, QT_PREPEND_NAMESPACE(QScopedPointer)<T, Cleanup> &p2)
{ p1.swap(p2); }
}
QT_BEGIN_NAMESPACE
namespace QtPrivate {
template <typename X, typename Y> struct QScopedArrayEnsureSameType;
@ -230,6 +221,9 @@ public:
return this->d[i];
}
void swap(QScopedArrayPointer &other) Q_DECL_NOTHROW // prevent QScopedPointer <->QScopedArrayPointer swaps
{ QScopedPointer<T, Cleanup>::swap(other); }
private:
explicit inline QScopedArrayPointer(void *) {
// Enforce the same type.
@ -245,6 +239,10 @@ private:
Q_DISABLE_COPY(QScopedArrayPointer)
};
template <typename T, typename Cleanup>
inline void swap(QScopedArrayPointer<T, Cleanup> &lhs, QScopedArrayPointer<T, Cleanup> &rhs) Q_DECL_NOTHROW
{ lhs.swap(rhs); }
QT_END_NAMESPACE
#endif // QSCOPEDPOINTER_H