summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOlivier Goffart <olivier.goffart@nokia.com>2010-10-28 09:59:32 (GMT)
committerOlivier Goffart <olivier.goffart@nokia.com>2010-10-29 09:48:04 (GMT)
commit4bfd68ccbda50fa3336d6ee6875d87b1b308b870 (patch)
tree0e57a67d80643dcc2caca3d3e3f3fe11bf065a8d /src
parent77819edac505823226c40033e76d2dda0e8b363a (diff)
downloadQt-4bfd68ccbda50fa3336d6ee6875d87b1b308b870.zip
Qt-4bfd68ccbda50fa3336d6ee6875d87b1b308b870.tar.gz
Qt-4bfd68ccbda50fa3336d6ee6875d87b1b308b870.tar.bz2
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
Diffstat (limited to 'src')
-rw-r--r--src/corelib/global/qglobal.h25
-rw-r--r--src/corelib/tools/qscopedpointer.h12
-rw-r--r--src/corelib/tools/qshareddata.h14
-rw-r--r--src/corelib/tools/qsharedpointer_impl.h10
4 files changed, 58 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 <stddef.h>
+#ifndef QT_NO_STL
+#include <algorithm>
+#endif
#define QT_VERSION_STR "4.8.0"
/*
@@ -2062,9 +2065,14 @@ Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS)
template <typename T>
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>(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>(TYPE &t) { return t.isDetached(); } \
template <> inline void qSwap<TYPE>(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 <class T, class Cleanup>
Q_INLINE_TEMPLATE void qSwap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2)
{ p1.swap(p2); }
+#ifndef QT_NO_STL
+QT_END_NAMESPACE
+namespace std {
+ template <class T, class Cleanup>
+ Q_INLINE_TEMPLATE void swap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2)
+ { p1.swap(p2); }
+}
+QT_BEGIN_NAMESPACE
+#endif
+
+
+
namespace QtPrivate {
template <typename X, typename Y> struct QScopedArrayEnsureSameType;
template <typename X> struct QScopedArrayEnsureSameType<X,X> { 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 <class T>
Q_INLINE_TEMPLATE void qSwap(QExplicitlySharedDataPointer<T> &p1, QExplicitlySharedDataPointer<T> &p2)
{ p1.swap(p2); }
+#ifndef QT_NO_STL
+QT_END_NAMESPACE
+namespace std {
+ template <class T>
+ Q_INLINE_TEMPLATE void swap(QSharedDataPointer<T> &p1, QSharedDataPointer<T> &p2)
+ { p1.swap(p2); }
+
+ template <class T>
+ Q_INLINE_TEMPLATE void swap(QExplicitlySharedDataPointer<T> &p1, QExplicitlySharedDataPointer<T> &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<T> &p1, QSharedPointer<T> &p2)
p1.swap(p2);
}
+#ifndef QT_NO_STL
+QT_END_NAMESPACE
+namespace std {
+ template <class T>
+ inline void swap(QSharedPointer<T> &p1, QSharedPointer<T> &p2)
+ { p1.swap(p2); }
+}
+QT_BEGIN_NAMESPACE
+#endif
+
namespace QtSharedPointer {
// helper functions:
template <class X, class T>