From 9b072cc5e91e45dbef4effabefc091461eccccc3 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 19 Mar 2010 15:11:27 +0100 Subject: Add some constants for C++0x features Reviewed-by: Olivier Goffart Reviewed-by: Joao --- src/corelib/global/qglobal.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index ede6dcd..f9879cd 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -474,6 +474,25 @@ namespace QT_NAMESPACE {} # define QT_NO_ARM_EABI # endif # endif +# if defined(__GXX_EXPERIMENTAL_CXX0X__) +# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 403 + /* C++0x features supported in GCC 4.3: */ +# define Q_COMPILER_RVALUE_REFS +# endif +# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 404 + /* C++0x features supported in GCC 4.4: */ +# define Q_COMPILER_VARIADIC_TEMPLATES +# define Q_COMPILER_AUTO_TYPE +# define Q_COMPILER_EXTERN_TEMPLATES +# define Q_COMPILER_DEFAULT_DELETE_MEMBERS +# define Q_COMPILER_CLASS_ENUM +# endif +# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 405 + /* C++0x features supported in GCC 4.5: */ +# define Q_COMPILER_LAMBDA +# define Q_COMPILER_UNICODE_STRINGS +# endif +# endif /* IBM compiler versions are a bit messy. There are actually two products: the C product, and the C++ product. The C++ compiler is always packaged -- cgit v0.12 From 605aa0cf1889110620dc0d5d84066be8177fe9c2 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 3 Sep 2010 10:44:16 +0200 Subject: Add define for c++0x initilizer lists Reviewed-by: Joao --- src/corelib/global/qglobal.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index f9879cd..02196e4 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -486,6 +486,7 @@ namespace QT_NAMESPACE {} # define Q_COMPILER_EXTERN_TEMPLATES # define Q_COMPILER_DEFAULT_DELETE_MEMBERS # define Q_COMPILER_CLASS_ENUM +# define Q_COMPILER_INITIALIZER_LISTS # endif # if (__GNUC__ * 100 + __GNUC_MINOR__) >= 405 /* C++0x features supported in GCC 4.5: */ -- cgit v0.12 From da0e1a682362144b9f13b4c564f86e09efb681bb Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Sun, 19 Apr 2009 21:12:21 +0200 Subject: C++0x move operators This makes assignement faster from temporaries, as the operator is fully inline, and doesn't do any atomic operations. Reviewed-by: joao --- src/corelib/io/qdir.h | 4 ++++ src/corelib/io/qfileinfo.h | 4 ++++ src/corelib/io/qurl.h | 4 ++++ src/corelib/kernel/qvariant.h | 4 ++++ src/corelib/tools/qbitarray.h | 4 ++++ src/corelib/tools/qbytearray.h | 4 ++++ src/corelib/tools/qcontiguouscache.h | 4 ++++ src/corelib/tools/qhash.h | 4 ++++ src/corelib/tools/qlinkedlist.h | 4 ++++ src/corelib/tools/qlist.h | 4 ++++ src/corelib/tools/qmap.h | 4 ++++ src/corelib/tools/qregexp.h | 4 ++++ src/corelib/tools/qset.h | 4 ++++ src/corelib/tools/qshareddata.h | 8 ++++++++ src/corelib/tools/qsharedpointer_impl.h | 7 +++++++ src/corelib/tools/qstring.h | 5 ++++- src/corelib/tools/qvector.h | 6 +++++- src/gui/image/qicon.h | 4 ++++ src/gui/image/qimage.h | 5 +++++ src/gui/image/qpicture.h | 4 ++++ src/gui/image/qpixmap.h | 4 ++++ src/gui/kernel/qcursor.h | 4 ++++ src/gui/kernel/qkeysequence.h | 4 ++++ src/gui/kernel/qpalette.h | 8 ++++++++ src/gui/painting/qbrush.h | 4 ++++ src/gui/painting/qpainterpath.h | 4 ++++ src/gui/painting/qpen.h | 4 ++++ src/gui/painting/qregion.h | 5 ++++- src/gui/text/qfont.h | 5 ++++- src/gui/text/qfontmetrics.h | 9 ++++++++- 30 files changed, 137 insertions(+), 5 deletions(-) diff --git a/src/corelib/io/qdir.h b/src/corelib/io/qdir.h index 28da271..0a56304 100644 --- a/src/corelib/io/qdir.h +++ b/src/corelib/io/qdir.h @@ -129,6 +129,10 @@ public: QDir &operator=(const QDir &); QDir &operator=(const QString &path); +#ifdef Q_COMPILER_RVALUE_REFS + inline QDir &operator=(QDir &&other) + { qSwap(d_ptr, other.d_ptr); return *this; } +#endif void setPath(const QString &path); QString path() const; diff --git a/src/corelib/io/qfileinfo.h b/src/corelib/io/qfileinfo.h index 7e82aed..f0c6d60 100644 --- a/src/corelib/io/qfileinfo.h +++ b/src/corelib/io/qfileinfo.h @@ -67,6 +67,10 @@ public: ~QFileInfo(); QFileInfo &operator=(const QFileInfo &fileinfo); +#ifdef Q_COMPILER_RVALUE_REFS + inline QFileInfo&operator=(QFileInfo &&other) + { qSwap(d_ptr, other.d_ptr); return *this; } +#endif bool operator==(const QFileInfo &fileinfo); // 5.0 - remove me bool operator==(const QFileInfo &fileinfo) const; inline bool operator!=(const QFileInfo &fileinfo) { return !(operator==(fileinfo)); } // 5.0 - remove me diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h index 162aa7c..563be5f 100644 --- a/src/corelib/io/qurl.h +++ b/src/corelib/io/qurl.h @@ -94,6 +94,10 @@ public: #ifndef QT_NO_URL_CAST_FROM_STRING QUrl &operator =(const QString &url); #endif +#ifdef Q_COMPILER_RVALUE_REFS + inline QUrl &operator=(QUrl &&other) + { qSwap(d, other.d); return *this; } +#endif ~QUrl(); void setUrl(const QString &url); diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index a4d45f6..b267954 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -219,6 +219,10 @@ class Q_CORE_EXPORT QVariant QVariant(Qt::GlobalColor color); QVariant& operator=(const QVariant &other); +#ifdef Q_COMPILER_RVALUE_REFS + inline QVariant &operator=(QVariant &&other) + { qSwap(d, other.d); return *this; } +#endif Type type() const; int userType() const; diff --git a/src/corelib/tools/qbitarray.h b/src/corelib/tools/qbitarray.h index 60cdc9c..bd79904 100644 --- a/src/corelib/tools/qbitarray.h +++ b/src/corelib/tools/qbitarray.h @@ -63,6 +63,10 @@ public: explicit QBitArray(int size, bool val = false); QBitArray(const QBitArray &other) : d(other.d) {} inline QBitArray &operator=(const QBitArray &other) { d = other.d; return *this; } +#ifdef Q_COMPILER_RVALUE_REFS + inline QBitArray &operator=(QBitArray &&other) + { qSwap(d, other.d); return *this; } +#endif inline int size() const { return (d.size() << 3) - *d.constData(); } inline int count() const { return (d.size() << 3) - *d.constData(); } diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index a3fe3f5..3cdcaab 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -144,6 +144,10 @@ public: QByteArray &operator=(const QByteArray &); QByteArray &operator=(const char *str); +#ifdef Q_COMPILER_RVALUE_REFS + inline QByteArray &operator=(QByteArray &&other) + { qSwap(d, other.d); return *this; } +#endif inline int size() const; bool isEmpty() const; diff --git a/src/corelib/tools/qcontiguouscache.h b/src/corelib/tools/qcontiguouscache.h index f767962..4c1a846 100644 --- a/src/corelib/tools/qcontiguouscache.h +++ b/src/corelib/tools/qcontiguouscache.h @@ -110,6 +110,10 @@ public: inline void setSharable(bool sharable) { if (!sharable) detach(); d->sharable = sharable; } QContiguousCache &operator=(const QContiguousCache &other); +#ifdef Q_COMPILER_RVALUE_REFS + inline QContiguousCache &operator=(QContiguousCache &&other) + { qSwap(d, other.d); return *this; } +#endif bool operator==(const QContiguousCache &other) const; inline bool operator!=(const QContiguousCache &other) const { return !(*this == other); } diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 14ed514..992ff33 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -283,6 +283,10 @@ public: inline ~QHash() { if (!d->ref.deref()) freeData(d); } QHash &operator=(const QHash &other); +#ifdef Q_COMPILER_RVALUE_REFS + inline QHash &operator=(QHash &&other) + { qSwap(d, other.d); return *this; } +#endif bool operator==(const QHash &other) const; inline bool operator!=(const QHash &other) const { return !(*this == other); } diff --git a/src/corelib/tools/qlinkedlist.h b/src/corelib/tools/qlinkedlist.h index 9b3efa3..c087944 100644 --- a/src/corelib/tools/qlinkedlist.h +++ b/src/corelib/tools/qlinkedlist.h @@ -85,6 +85,10 @@ public: inline QLinkedList(const QLinkedList &l) : d(l.d) { d->ref.ref(); if (!d->sharable) detach(); } ~QLinkedList(); QLinkedList &operator=(const QLinkedList &); +#ifdef Q_COMPILER_RVALUE_REFS + inline QLinkedList &operator=(QLinkedList &&other) + { qSwap(d, other.d); return *this; } +#endif bool operator==(const QLinkedList &l) const; inline bool operator!=(const QLinkedList &l) const { return !(*this == l); } diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 1282bca..b1d07bd 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -118,6 +118,10 @@ public: inline QList(const QList &l) : d(l.d) { d->ref.ref(); if (!d->sharable) detach_helper(); } ~QList(); QList &operator=(const QList &l); +#ifdef Q_COMPILER_RVALUE_REFS + inline QList &operator=(QList &&other) + { qSwap(d, other.d); return *this; } +#endif bool operator==(const QList &l) const; inline bool operator!=(const QList &l) const { return !(*this == l); } diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h index 08f5a35..ce8fd75 100644 --- a/src/corelib/tools/qmap.h +++ b/src/corelib/tools/qmap.h @@ -185,6 +185,10 @@ public: inline ~QMap() { if (!d) return; if (!d->ref.deref()) freeData(d); } QMap &operator=(const QMap &other); +#ifdef Q_COMPILER_RVALUE_REFS + inline QMap &operator=(QMap &&other) + { qSwap(d, other.d); return *this; } +#endif #ifndef QT_NO_STL explicit QMap(const typename std::map &other); std::map toStdMap() const; diff --git a/src/corelib/tools/qregexp.h b/src/corelib/tools/qregexp.h index e19c130..0b4a702 100644 --- a/src/corelib/tools/qregexp.h +++ b/src/corelib/tools/qregexp.h @@ -76,6 +76,10 @@ public: QRegExp(const QRegExp &rx); ~QRegExp(); QRegExp &operator=(const QRegExp &rx); +#ifdef Q_COMPILER_RVALUE_REFS + inline QRegExp &operator=(QRegExp &&other) + { qSwap(priv,other.priv); return *this; } +#endif bool operator==(const QRegExp &rx) const; inline bool operator!=(const QRegExp &rx) const { return !operator==(rx); } diff --git a/src/corelib/tools/qset.h b/src/corelib/tools/qset.h index fe50d4d..dc3c45a 100644 --- a/src/corelib/tools/qset.h +++ b/src/corelib/tools/qset.h @@ -61,6 +61,10 @@ public: inline QSet &operator=(const QSet &other) { q_hash = other.q_hash; return *this; } +#ifdef Q_COMPILER_RVALUE_REFS + inline QSet &operator=(QSet &&other) + { qSwap(q_hash, other.q_hash); return *this; } +#endif inline bool operator==(const QSet &other) const { return q_hash == other.q_hash; } diff --git a/src/corelib/tools/qshareddata.h b/src/corelib/tools/qshareddata.h index 1ae2fa9..b646a9d 100644 --- a/src/corelib/tools/qshareddata.h +++ b/src/corelib/tools/qshareddata.h @@ -113,6 +113,10 @@ public: } return *this; } +#ifdef Q_COMPILER_RVALUE_REFS + inline QSharedDataPointer &operator=(QSharedDataPointer &&other) + { qSwap(d, other.d); return *this; } +#endif inline bool operator!() const { return !d; } @@ -192,6 +196,10 @@ public: } return *this; } +#ifdef Q_COMPILER_RVALUE_REFS + inline QSharedDataPointer &operator=(QSharedDataPointer &&other) + { qSwap(d, other.d); return *this; } +#endif inline bool operator!() const { return !d; } diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index bb06f3a..20dda12 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -465,6 +465,13 @@ public: BaseClass::internalCopy(other); return *this; } +#ifdef Q_COMPILER_RVALUE_REFS + inline QSharedPointer &operator=(QSharedPointer &&other) + { + QSharedPointer::internalSwap(other); + return *this; + } +#endif template inline QSharedPointer(const QSharedPointer &other) : BaseClass(other) diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 2633e78..589fdc2 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -104,7 +104,10 @@ public: QString &operator=(QChar c); QString &operator=(const QString &); inline QString &operator=(const QLatin1String &); - +#ifdef Q_COMPILER_RVALUE_REFS + inline QString &operator=(QString &&other) + { qSwap(d, other.d); return *this; } +#endif inline int size() const { return d->size; } inline int count() const { return d->size; } inline int length() const; diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 2f0ad42..c001699 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -118,6 +118,11 @@ public: inline QVector(const QVector &v) : d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); } inline ~QVector() { if (!d) return; if (!d->ref.deref()) free(p); } QVector &operator=(const QVector &v); +#ifdef Q_COMPILER_RVALUE_REFS + inline QVector operator=(QVector &&other) + { qSwap(p, other.p); return *this; } +#endif + bool operator==(const QVector &v) const; inline bool operator!=(const QVector &v) const { return !(*this == v); } @@ -297,7 +302,6 @@ public: inline std::vector toStdVector() const { std::vector tmp; qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; } #endif - private: friend class QRegion; // Optimization for QRegion::rects() diff --git a/src/gui/image/qicon.h b/src/gui/image/qicon.h index faef07b..fd591e6 100644 --- a/src/gui/image/qicon.h +++ b/src/gui/image/qicon.h @@ -71,6 +71,10 @@ public: explicit QIcon(QIconEngineV2 *engine); ~QIcon(); QIcon &operator=(const QIcon &other); +#ifdef Q_COMPILER_RVALUE_REFS + inline QIcon &operator=(QIcon &&other) + { qSwap(d, other.d); return *this; } +#endif operator QVariant() const; QPixmap pixmap(const QSize &size, Mode mode = Normal, State state = Off) const; diff --git a/src/gui/image/qimage.h b/src/gui/image/qimage.h index f37dcf2..4fe7da2 100644 --- a/src/gui/image/qimage.h +++ b/src/gui/image/qimage.h @@ -140,6 +140,11 @@ public: ~QImage(); QImage &operator=(const QImage &); +#ifdef Q_COMPILER_RVALUE_REFS + inline QImage &operator=(QImage &&other) + { qSwap(d, other.d); return *this; } +#endif + bool isNull() const; int devType() const; diff --git a/src/gui/image/qpicture.h b/src/gui/image/qpicture.h index 63ef42e..49b0fd6 100644 --- a/src/gui/image/qpicture.h +++ b/src/gui/image/qpicture.h @@ -81,6 +81,10 @@ public: void setBoundingRect(const QRect &r); QPicture& operator=(const QPicture &p); +#ifdef Q_COMPILER_RVALUE_REFS + inline QPicture &operator=(QPicture &&other) + { qSwap(d_ptr, other.d_ptr); return *this; } +#endif void detach(); bool isDetached() const; diff --git a/src/gui/image/qpixmap.h b/src/gui/image/qpixmap.h index 64ca8a3..e9f9365 100644 --- a/src/gui/image/qpixmap.h +++ b/src/gui/image/qpixmap.h @@ -83,6 +83,10 @@ public: ~QPixmap(); QPixmap &operator=(const QPixmap &); +#ifdef Q_COMPILER_RVALUE_REFS + inline QPixmap &operator=(QPixmap &&other) + { qSwap(data, other.data); return *this; } +#endif operator QVariant() const; bool isNull() const; // ### Qt 5: make inline diff --git a/src/gui/kernel/qcursor.h b/src/gui/kernel/qcursor.h index 2490d20..ed6b254 100644 --- a/src/gui/kernel/qcursor.h +++ b/src/gui/kernel/qcursor.h @@ -96,6 +96,10 @@ public: QCursor(const QCursor &cursor); ~QCursor(); QCursor &operator=(const QCursor &cursor); +#ifdef Q_COMPILER_RVALUE_REFS + inline QCursor &operator=(QCursor &&other) + { qSwap(d, other.d); return *this; } +#endif operator QVariant() const; Qt::CursorShape shape() const; diff --git a/src/gui/kernel/qkeysequence.h b/src/gui/kernel/qkeysequence.h index 5a973e3..b47873d 100644 --- a/src/gui/kernel/qkeysequence.h +++ b/src/gui/kernel/qkeysequence.h @@ -179,6 +179,10 @@ public: operator int() const; int operator[](uint i) const; QKeySequence &operator=(const QKeySequence &other); +#ifdef Q_COMPILER_RVALUE_REFS + inline QKeySequence &operator=(QKeySequence &&other) + { qSwap(d, other.d); return *this; } +#endif bool operator==(const QKeySequence &other) const; inline bool operator!= (const QKeySequence &other) const { return !(*this == other); } diff --git a/src/gui/kernel/qpalette.h b/src/gui/kernel/qpalette.h index f797f86..7b7b41d 100644 --- a/src/gui/kernel/qpalette.h +++ b/src/gui/kernel/qpalette.h @@ -78,6 +78,14 @@ public: QPalette(const QPalette &palette); ~QPalette(); QPalette &operator=(const QPalette &palette); +#ifdef Q_COMPILER_RVALUE_REFS + inline QPalette &operator=(QPalette &&other) + { + resolve_mask = other.resolve_mask; + current_group = other.current_group; + qSwap(d, other.d); return *this; + } +#endif operator QVariant() const; // Do not change the order, the serialization format depends on it diff --git a/src/gui/painting/qbrush.h b/src/gui/painting/qbrush.h index b657301..efc720b 100644 --- a/src/gui/painting/qbrush.h +++ b/src/gui/painting/qbrush.h @@ -92,6 +92,10 @@ public: ~QBrush(); QBrush &operator=(const QBrush &brush); +#ifdef Q_COMPILER_RVALUE_REFS + inline QBrush &operator=(QBrush &&other) + { qSwap(d, other.d); return *this; } +#endif operator QVariant() const; inline Qt::BrushStyle style() const; diff --git a/src/gui/painting/qpainterpath.h b/src/gui/painting/qpainterpath.h index 15d83b8..9a7b60a 100644 --- a/src/gui/painting/qpainterpath.h +++ b/src/gui/painting/qpainterpath.h @@ -95,6 +95,10 @@ public: explicit QPainterPath(const QPointF &startPoint); QPainterPath(const QPainterPath &other); QPainterPath &operator=(const QPainterPath &other); +#ifdef Q_COMPILER_RVALUE_REFS + inline QPainterPath &operator=(QPainterPath &&other) + { qSwap(d_ptr, other.d_ptr); return *this; } +#endif ~QPainterPath(); void closeSubpath(); diff --git a/src/gui/painting/qpen.h b/src/gui/painting/qpen.h index aff7071..4006112 100644 --- a/src/gui/painting/qpen.h +++ b/src/gui/painting/qpen.h @@ -74,6 +74,10 @@ public: ~QPen(); QPen &operator=(const QPen &pen); +#ifdef Q_COMPILER_RVALUE_REFS + inline QPen &operator=(QPen &&other) + { qSwap(d, other.d); return *this; } +#endif Qt::PenStyle style() const; void setStyle(Qt::PenStyle); diff --git a/src/gui/painting/qregion.h b/src/gui/painting/qregion.h index bc4da28..2e42844 100644 --- a/src/gui/painting/qregion.h +++ b/src/gui/painting/qregion.h @@ -81,7 +81,10 @@ public: QRegion(const QBitmap &bitmap); ~QRegion(); QRegion &operator=(const QRegion &); - +#ifdef Q_COMPILER_RVALUE_REFS + inline QRegion &operator=(QRegion &&other) + { qSwap(d, other.d); return *this; } +#endif #ifdef QT3_SUPPORT inline QT3_SUPPORT bool isNull() const { return isEmpty(); } #endif diff --git a/src/gui/text/qfont.h b/src/gui/text/qfont.h index f941d1e..d6f4dc5 100644 --- a/src/gui/text/qfont.h +++ b/src/gui/text/qfont.h @@ -226,7 +226,10 @@ public: bool operator<(const QFont &) const; operator QVariant() const; bool isCopyOf(const QFont &) const; - +#ifdef Q_COMPILER_RVALUE_REFS + inline QFont &operator=(QFont &&other) + { qSwap(d, other.d); return *this; } +#endif #ifdef Q_WS_WIN HFONT handle() const; diff --git a/src/gui/text/qfontmetrics.h b/src/gui/text/qfontmetrics.h index 9911ad2..5a79429 100644 --- a/src/gui/text/qfontmetrics.h +++ b/src/gui/text/qfontmetrics.h @@ -71,6 +71,10 @@ public: ~QFontMetrics(); QFontMetrics &operator=(const QFontMetrics &); +#ifdef Q_COMPILER_RVALUE_REFS + inline QFontMetrics &operator=(QFontMetrics &&other) + { qSwap(d, other.d); return *this; } +#endif int ascent() const; int descent() const; @@ -149,7 +153,10 @@ public: QFontMetricsF &operator=(const QFontMetricsF &); QFontMetricsF &operator=(const QFontMetrics &); - +#ifdef Q_COMPILER_RVALUE_REFS + inline QFontMetricsF &operator=(QFontMetricsF &&other) + { qSwap(d, other.d); return *this; } +#endif qreal ascent() const; qreal descent() const; qreal height() const; -- cgit v0.12 From c22d43237363463e3409286470392a3227f49961 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Sun, 19 Apr 2009 22:45:06 +0200 Subject: C++0x: being able to create a list with the {,,,} notation Reviewed-by: Joao --- src/corelib/tools/qlist.h | 8 ++++++++ src/corelib/tools/qvector.h | 23 ++++++++++++++++++++++- tests/auto/qlist/tst_qlist.cpp | 16 ++++++++++++++++ tests/auto/qvector/tst_qvector.cpp | 15 +++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index b1d07bd..8f988d6 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -50,6 +50,10 @@ #include #include #endif +#ifdef Q_COMPILER_INITIALIZER_LISTS +#include +#include +#endif #include #include @@ -122,6 +126,10 @@ public: inline QList &operator=(QList &&other) { qSwap(d, other.d); return *this; } #endif +#ifdef Q_COMPILER_INITIALIZER_LISTS + inline QList(std::initializer_list args) : d(&QListData::shared_null) + { d->ref.ref(); qCopy(args.begin(), args.end(), std::back_inserter(*this)); } +#endif bool operator==(const QList &l) const; inline bool operator!=(const QList &l) const { return !(*this == l); } diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index c001699..535f43d 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -53,6 +53,9 @@ #endif #include #include +#ifdef Q_COMPILER_INITIALIZER_LISTS +#include +#endif QT_BEGIN_HEADER @@ -122,7 +125,9 @@ public: inline QVector operator=(QVector &&other) { qSwap(p, other.p); return *this; } #endif - +#ifdef Q_COMPILER_INITIALIZER_LISTS + inline QVector(std::initializer_list args); +#endif bool operator==(const QVector &v) const; inline bool operator!=(const QVector &v) const { return !(*this == v); } @@ -430,6 +435,22 @@ QVector::QVector(int asize, const T &t) new (--i) T(t); } +#ifdef Q_COMPILER_INITIALIZER_LISTS +template +QVector::QVector(std::initializer_list args) +{ + p = malloc(args.size()); + d->ref = 1; + d->alloc = d->size = args.size(); + d->sharable = true; + d->capacity = false; + T* i = p->array + d->size; + auto it = args.end(); + while (i != p->array) + new (--i) T(*(--it)); +} +#endif + template void QVector::free(Data *x) { diff --git a/tests/auto/qlist/tst_qlist.cpp b/tests/auto/qlist/tst_qlist.cpp index ba8aefa..9f6b4a5 100644 --- a/tests/auto/qlist/tst_qlist.cpp +++ b/tests/auto/qlist/tst_qlist.cpp @@ -89,6 +89,8 @@ private slots: void testSTLIterators() const; void testOperators() const; + + void initializeList() const; }; void tst_QList::length() const @@ -662,5 +664,19 @@ void tst_QList::testSTLIterators() const QCOMPARE(*it, QLatin1String("foo")); } +void tst_QList::initializeList() const +{ +#ifdef QT_CXX0X_INITIALIZERLIST + QList v1{2,3,4}; + QCOMPARE(v1, QList() << 2 << 3 << 4); + QCOMPARE(v1, (QList{2,3,4})); + + QList> v2{ v1, {1}, QList(), {2,3,4} }; + QList> v3; + v3 << v1 << (QList() << 1) << QList() << v1; + QCOMPARE(v3, v2); +#endif +} + QTEST_APPLESS_MAIN(tst_QList) #include "tst_qlist.moc" diff --git a/tests/auto/qvector/tst_qvector.cpp b/tests/auto/qvector/tst_qvector.cpp index 2bc8d15..d8dfacf 100644 --- a/tests/auto/qvector/tst_qvector.cpp +++ b/tests/auto/qvector/tst_qvector.cpp @@ -88,6 +88,7 @@ private slots: void outOfMemory(); void QTBUG6416_reserve(); + void initializeList(); }; void tst_QVector::constructors() const @@ -834,5 +835,19 @@ void tst_QVector::QTBUG6416_reserve() QCOMPARE(fooCtor, fooDtor); } +void tst_QVector::initializeList() +{ +#ifdef QT_CXX0X_INITIALIZERLIST + QVector v1{2,3,4}; + QCOMPARE(v1, QVector() << 2 << 3 << 4); + QCOMPARE(v1, (QVector{2,3,4})); + + QVector> v2{ v1, {1}, QVector(), {2,3,4} }; + QVector> v3; + v3 << v1 << (QVector() << 1) << QVector() << v1; + QCOMPARE(v3, v2); +#endif +} + QTEST_APPLESS_MAIN(tst_QVector) #include "tst_qvector.moc" -- cgit v0.12