From 4bfd68ccbda50fa3336d6ee6875d87b1b308b870 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Thu, 28 Oct 2010 11:59:32 +0200 Subject: qSwap: use std::swap as implementation, and specialize std::swap for our container std::swap might be reimplemented for many types. That might speedup our Qt algorithms that uses qSwap. (std::swap also use std::move) Specialize std::swap for our shared type, so stl algorithls work faster Reviewed-by: Joao --- src/corelib/global/qglobal.h | 25 ++++++++++++++++++++++--- src/corelib/tools/qscopedpointer.h | 12 ++++++++++++ src/corelib/tools/qshareddata.h | 14 ++++++++++++++ src/corelib/tools/qsharedpointer_impl.h | 10 ++++++++++ tests/auto/qalgorithms/tst_qalgorithms.cpp | 23 +++++++++++++++++++++++ 5 files changed, 81 insertions(+), 3 deletions(-) diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 547ad8d..90dd43e 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -43,6 +43,9 @@ #define QGLOBAL_H #include +#ifndef QT_NO_STL +#include +#endif #define QT_VERSION_STR "4.8.0" /* @@ -2062,9 +2065,14 @@ Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) template inline void qSwap(T &value1, T &value2) { +#ifdef QT_NO_STL const T t = value1; value1 = value2; value2 = t; +#else + using std::swap; + swap(value1, value2); +#endif } /* @@ -2076,12 +2084,23 @@ inline void qSwap(T &value1, T &value2) types must declare a 'bool isDetached(void) const;' member for this to work. */ +#ifdef QT_NO_STL +#define Q_DECLARE_SHARED_STL(TYPE) +#else +#define Q_DECLARE_SHARED_STL(TYPE) \ +QT_END_NAMESPACE \ +namespace std { \ + template<> inline void swap(TYPE &value1, TYPE &value2) \ + { swap(value1.data_ptr(), value2.data_ptr()); } \ +} \ +QT_BEGIN_NAMESPACE +#endif + #define Q_DECLARE_SHARED(TYPE) \ template <> inline bool qIsDetached(TYPE &t) { return t.isDetached(); } \ template <> inline void qSwap(TYPE &value1, TYPE &value2) \ -{ \ - qSwap(value1.data_ptr(), value2.data_ptr()); \ -} +{ qSwap(value1.data_ptr(), value2.data_ptr()); } \ +Q_DECLARE_SHARED_STL(TYPE) /* QTypeInfo primitive specializations diff --git a/src/corelib/tools/qscopedpointer.h b/src/corelib/tools/qscopedpointer.h index 40d3851..a24cc71 100644 --- a/src/corelib/tools/qscopedpointer.h +++ b/src/corelib/tools/qscopedpointer.h @@ -186,6 +186,18 @@ template Q_INLINE_TEMPLATE void qSwap(QScopedPointer &p1, QScopedPointer &p2) { p1.swap(p2); } +#ifndef QT_NO_STL +QT_END_NAMESPACE +namespace std { + template + Q_INLINE_TEMPLATE void swap(QScopedPointer &p1, QScopedPointer &p2) + { p1.swap(p2); } +} +QT_BEGIN_NAMESPACE +#endif + + + namespace QtPrivate { template struct QScopedArrayEnsureSameType; template struct QScopedArrayEnsureSameType { typedef X* Type; }; diff --git a/src/corelib/tools/qshareddata.h b/src/corelib/tools/qshareddata.h index b646a9d..1079595 100644 --- a/src/corelib/tools/qshareddata.h +++ b/src/corelib/tools/qshareddata.h @@ -263,6 +263,20 @@ template Q_INLINE_TEMPLATE void qSwap(QExplicitlySharedDataPointer &p1, QExplicitlySharedDataPointer &p2) { p1.swap(p2); } +#ifndef QT_NO_STL +QT_END_NAMESPACE +namespace std { + template + Q_INLINE_TEMPLATE void swap(QSharedDataPointer &p1, QSharedDataPointer &p2) + { p1.swap(p2); } + + template + Q_INLINE_TEMPLATE void swap(QExplicitlySharedDataPointer &p1, QExplicitlySharedDataPointer &p2) + { p1.swap(p2); } +} +QT_BEGIN_NAMESPACE +#endif + QT_END_NAMESPACE QT_END_HEADER diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 4b47713..965ac14 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -784,6 +784,16 @@ inline void qSwap(QSharedPointer &p1, QSharedPointer &p2) p1.swap(p2); } +#ifndef QT_NO_STL +QT_END_NAMESPACE +namespace std { + template + inline void swap(QSharedPointer &p1, QSharedPointer &p2) + { p1.swap(p2); } +} +QT_BEGIN_NAMESPACE +#endif + namespace QtSharedPointer { // helper functions: template diff --git a/tests/auto/qalgorithms/tst_qalgorithms.cpp b/tests/auto/qalgorithms/tst_qalgorithms.cpp index 690bbbd..17e6c04 100644 --- a/tests/auto/qalgorithms/tst_qalgorithms.cpp +++ b/tests/auto/qalgorithms/tst_qalgorithms.cpp @@ -81,6 +81,7 @@ private slots: void test_qBinaryFind(); void qBinaryFindOneEntry(); void swap(); + void swap2(); void sortEmptyList(); void sortedList(); void sortAPItest(); @@ -521,6 +522,28 @@ void tst_QAlgorithms::swap() } } +namespace SwapTest { + struct ST { int i; int j; }; + void swap(ST &a, ST &b) { + a.i = b.j; + b.i = a.j; + } +} + +void tst_QAlgorithms::swap2() +{ + { +#ifndef QT_NO_SQL + //check the namespace lookup works correctly + SwapTest::ST a = { 45, 65 }; + SwapTest::ST b = { 48, 68 }; + qSwap(a, b); + QCOMPARE(a.i, 68); + QCOMPARE(b.i, 65); +#endif + } +} + void tst_QAlgorithms::sortEmptyList() { // Only test if it crashes -- cgit v0.12 From 41afd00317922946ca2539d0be565bd8f917d239 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 29 Oct 2010 16:00:41 +0200 Subject: Fix compilation in namespace Reviewed-by: Joao --- src/corelib/global/qglobal.h | 2 +- src/corelib/tools/qscopedpointer.h | 2 +- src/corelib/tools/qshareddata.h | 4 ++-- src/corelib/tools/qsharedpointer_impl.h | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 4e78ab1..50f1538 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -2092,7 +2092,7 @@ inline void qSwap(T &value1, T &value2) #define Q_DECLARE_SHARED_STL(TYPE) \ QT_END_NAMESPACE \ namespace std { \ - template<> inline void swap(TYPE &value1, TYPE &value2) \ + template<> inline void swap(QT_PREPEND_NAMESPACE(TYPE) &value1, QT_PREPEND_NAMESPACE(TYPE) &value2) \ { swap(value1.data_ptr(), value2.data_ptr()); } \ } \ QT_BEGIN_NAMESPACE diff --git a/src/corelib/tools/qscopedpointer.h b/src/corelib/tools/qscopedpointer.h index a24cc71..b3a6db6 100644 --- a/src/corelib/tools/qscopedpointer.h +++ b/src/corelib/tools/qscopedpointer.h @@ -190,7 +190,7 @@ Q_INLINE_TEMPLATE void qSwap(QScopedPointer &p1, QScopedPointer - Q_INLINE_TEMPLATE void swap(QScopedPointer &p1, QScopedPointer &p2) + Q_INLINE_TEMPLATE void swap(QT_PREPEND_NAMESPACE(QScopedPointer) &p1, QT_PREPEND_NAMESPACE(QScopedPointer) &p2) { p1.swap(p2); } } QT_BEGIN_NAMESPACE diff --git a/src/corelib/tools/qshareddata.h b/src/corelib/tools/qshareddata.h index 1079595..45456fe 100644 --- a/src/corelib/tools/qshareddata.h +++ b/src/corelib/tools/qshareddata.h @@ -267,11 +267,11 @@ Q_INLINE_TEMPLATE void qSwap(QExplicitlySharedDataPointer &p1, QExplicitlySha QT_END_NAMESPACE namespace std { template - Q_INLINE_TEMPLATE void swap(QSharedDataPointer &p1, QSharedDataPointer &p2) + Q_INLINE_TEMPLATE void swap(QT_PREPEND_NAMESPACE(QSharedDataPointer) &p1, QT_PREPEND_NAMESPACE(QSharedDataPointer) &p2) { p1.swap(p2); } template - Q_INLINE_TEMPLATE void swap(QExplicitlySharedDataPointer &p1, QExplicitlySharedDataPointer &p2) + Q_INLINE_TEMPLATE void swap(QT_PREPEND_NAMESPACE(QExplicitlySharedDataPointer) &p1, QT_PREPEND_NAMESPACE(QExplicitlySharedDataPointer) &p2) { p1.swap(p2); } } QT_BEGIN_NAMESPACE diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 965ac14..ef8c454 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -788,7 +788,7 @@ inline void qSwap(QSharedPointer &p1, QSharedPointer &p2) QT_END_NAMESPACE namespace std { template - inline void swap(QSharedPointer &p1, QSharedPointer &p2) + inline void swap(QT_PREPEND_NAMESPACE(QSharedPointer) &p1, QT_PREPEND_NAMESPACE(QSharedPointer) &p2) { p1.swap(p2); } } QT_BEGIN_NAMESPACE -- cgit v0.12 From d548fe325f8293a9bb444a317e5191a9b639d24e Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Sat, 30 Oct 2010 16:12:22 +0200 Subject: Fix compilation in qglobal.h when included from a .c file We cannot include c++ header from C files --- src/corelib/global/qglobal.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 50f1538..b75a3d8 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -43,9 +43,6 @@ #define QGLOBAL_H #include -#ifndef QT_NO_STL -#include -#endif #define QT_VERSION_STR "4.8.0" /* @@ -67,6 +64,10 @@ #ifdef __cplusplus +#ifndef QT_NO_STL +#include +#endif + #ifndef QT_NAMESPACE /* user namespace */ # define QT_PREPEND_NAMESPACE(name) ::name -- cgit v0.12