diff options
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qarraydata.cpp | 4 | ||||
-rw-r--r-- | src/corelib/tools/qarraydata.h | 4 | ||||
-rw-r--r-- | src/corelib/tools/qbytearray.cpp | 2 | ||||
-rw-r--r-- | src/corelib/tools/qchar.h | 2 | ||||
-rw-r--r-- | src/corelib/tools/qcommandlineparser.cpp | 22 | ||||
-rw-r--r-- | src/corelib/tools/qcommandlineparser.h | 1 | ||||
-rw-r--r-- | src/corelib/tools/qhash.cpp | 26 | ||||
-rw-r--r-- | src/corelib/tools/qhash.h | 64 | ||||
-rw-r--r-- | src/corelib/tools/qline.cpp | 14 | ||||
-rw-r--r-- | src/corelib/tools/qpoint.cpp | 13 | ||||
-rw-r--r-- | src/corelib/tools/qrect.cpp | 12 | ||||
-rw-r--r-- | src/corelib/tools/qset.h | 2 | ||||
-rw-r--r-- | src/corelib/tools/qsharedpointer.cpp | 29 | ||||
-rw-r--r-- | src/corelib/tools/qsharedpointer.h | 3 | ||||
-rw-r--r-- | src/corelib/tools/qsharedpointer_impl.h | 65 | ||||
-rw-r--r-- | src/corelib/tools/qsize.cpp | 12 | ||||
-rw-r--r-- | src/corelib/tools/qstringlist.cpp | 41 | ||||
-rw-r--r-- | src/corelib/tools/qstringlist.h | 3 | ||||
-rw-r--r-- | src/corelib/tools/qtools_p.h | 2 | ||||
-rw-r--r-- | src/corelib/tools/qvector.cpp | 22 | ||||
-rw-r--r-- | src/corelib/tools/qvector.h | 19 |
21 files changed, 257 insertions, 105 deletions
diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index 825b3289c7..45587b1214 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -68,7 +68,7 @@ static const QArrayData &qt_array_empty = qt_array[0]; static const QArrayData &qt_array_unsharable_empty = qt_array[1]; QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, - size_t capacity, AllocationOptions options) + size_t capacity, AllocationOptions options) Q_DECL_NOTHROW { // Alignment is a power of two Q_ASSERT(alignment >= Q_ALIGNOF(QArrayData) @@ -111,7 +111,7 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, } void QArrayData::deallocate(QArrayData *data, size_t objectSize, - size_t alignment) + size_t alignment) Q_DECL_NOTHROW { // Alignment is a power of two Q_ASSERT(alignment >= Q_ALIGNOF(QArrayData) diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index ffb2b8765e..534f310d36 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -116,9 +116,9 @@ struct Q_CORE_EXPORT QArrayData static QArrayData *allocate(size_t objectSize, size_t alignment, size_t capacity, AllocationOptions options = Default) - Q_REQUIRED_RESULT; + Q_DECL_NOTHROW Q_REQUIRED_RESULT; static void deallocate(QArrayData *data, size_t objectSize, - size_t alignment); + size_t alignment) Q_DECL_NOTHROW; static const QArrayData shared_null[2]; static QArrayData *sharedNull() { return const_cast<QArrayData*>(shared_null); } diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index fb92d82f07..9c2a242e8e 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -67,7 +67,7 @@ int qFindByteArray( const char *needle0, int needleLen); -int qAllocMore(int alloc, int extra) +int qAllocMore(int alloc, int extra) Q_DECL_NOTHROW { Q_ASSERT(alloc >= 0 && extra >= 0); Q_ASSERT_X(alloc < (1 << 30) - extra, "qAllocMore", "Requested size is too large!"); diff --git a/src/corelib/tools/qchar.h b/src/corelib/tools/qchar.h index 266effb66a..88758bdd6d 100644 --- a/src/corelib/tools/qchar.h +++ b/src/corelib/tools/qchar.h @@ -384,7 +384,7 @@ public: QT_DEPRECATED inline char toAscii() const { return toLatin1(); } #endif inline char toLatin1() const; - inline ushort unicode() const { return ucs; } + Q_DECL_CONSTEXPR inline ushort unicode() const { return ucs; } inline ushort &unicode() { return ucs; } #if QT_DEPRECATED_SINCE(5, 0) diff --git a/src/corelib/tools/qcommandlineparser.cpp b/src/corelib/tools/qcommandlineparser.cpp index 505ab5f46d..168701d13c 100644 --- a/src/corelib/tools/qcommandlineparser.cpp +++ b/src/corelib/tools/qcommandlineparser.cpp @@ -495,10 +495,8 @@ void QCommandLineParser::process(const QStringList &arguments) ::exit(EXIT_FAILURE); } - if (d->builtinVersionOption && isSet(QStringLiteral("version"))) { - printf("%s %s\n", qPrintable(QCoreApplication::applicationName()), qPrintable(QCoreApplication::applicationVersion())); - ::exit(EXIT_SUCCESS); - } + if (d->builtinVersionOption && isSet(QStringLiteral("version"))) + showVersion(); if (d->builtinHelpOption && isSet(QStringLiteral("help"))) showHelp(EXIT_SUCCESS); @@ -888,6 +886,22 @@ QStringList QCommandLineParser::unknownOptionNames() const } /*! + Displays the version information from QCoreApplication::applicationVersion(), + and exits the application. + This is automatically triggered by the --version option, but can also + be used to display the version when not using process(). + The exit code is set to EXIT_SUCCESS (0). + + \sa addVersionOption() + \since 5.4 +*/ +Q_NORETURN void QCommandLineParser::showVersion() +{ + fprintf(stdout, "%s %s\n", qPrintable(QCoreApplication::applicationName()), qPrintable(QCoreApplication::applicationVersion())); + ::exit(EXIT_SUCCESS); +} + +/*! Displays the help information, and exits the application. This is automatically triggered by the --help option, but can also be used to display the help when the user is not invoking the diff --git a/src/corelib/tools/qcommandlineparser.h b/src/corelib/tools/qcommandlineparser.h index 5a7061f031..946d4dd238 100644 --- a/src/corelib/tools/qcommandlineparser.h +++ b/src/corelib/tools/qcommandlineparser.h @@ -92,6 +92,7 @@ public: QStringList optionNames() const; QStringList unknownOptionNames() const; + Q_NORETURN void showVersion(); Q_NORETURN void showHelp(int exitCode = 0); QString helpText() const; diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index a5d14a3535..928707660b 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -163,6 +163,11 @@ static inline uint hash(const uchar *p, int len, uint seed) Q_DECL_NOTHROW return h; } +uint qHashBits(const void *p, size_t len, uint seed) Q_DECL_NOTHROW +{ + return hash(static_cast<const uchar*>(p), int(len), seed); +} + static inline uint hash(const QChar *p, int len, uint seed) Q_DECL_NOTHROW { uint h = seed; @@ -669,6 +674,25 @@ void QHashData::checkSanity() Types \c T1 and \c T2 must be supported by qHash(). */ +/*! \fn uint qHashBits(const void *p, size_t len, uint seed = 0) + \relates QHash + \since 5.4 + + Returns the hash value for the memory block of size \a len pointed + to by \a p, using \a seed to seed the calculation. + + Use this function only to implement qHash() for your own custom + types. E.g., here's how you could implement a qHash() overload for + std::vector<int>: + + \snippet code/src_corelib_tools_qhash.cpp qhashbits + + It bears repeating that the implementation of qHashBits() - like + the qHash() overloads offered by Qt - may change at any time. You + \b{must not} rely on the fact that qHashBits() will give the same + results (for the same inputs) across different Qt versions. +*/ + /*! \fn uint qHash(char key, uint seed = 0) \relates QHash \since 5.0 @@ -778,7 +802,7 @@ uint qHash(long double key, uint seed) Q_DECL_NOTHROW } #endif -/*! \fn uint qHash(QChar key, uint seed = 0) +/*! \fn uint qHash(const QChar key, uint seed = 0) \relates QHash \since 5.0 diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 40e501355c..67924c0888 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -66,44 +66,40 @@ class QString; class QStringRef; class QLatin1String; -inline uint qHash(char key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; } -inline uint qHash(uchar key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; } -inline uint qHash(signed char key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; } -inline uint qHash(ushort key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; } -inline uint qHash(short key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; } -inline uint qHash(uint key, uint seed = 0) Q_DECL_NOTHROW { return key ^ seed; } -inline uint qHash(int key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; } -inline uint qHash(ulong key, uint seed = 0) Q_DECL_NOTHROW +Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHashBits(const void *p, size_t size, uint seed = 0) Q_DECL_NOTHROW; + +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(char key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; } +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(uchar key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; } +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(signed char key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; } +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(ushort key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; } +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(short key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; } +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(uint key, uint seed = 0) Q_DECL_NOTHROW { return key ^ seed; } +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(int key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; } +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(ulong key, uint seed = 0) Q_DECL_NOTHROW { - if (sizeof(ulong) > sizeof(uint)) { - return uint(((key >> (8 * sizeof(uint) - 1)) ^ key) & (~0U)) ^ seed; - } else { - return uint(key & (~0U)) ^ seed; - } + return (sizeof(ulong) > sizeof(uint)) + ? (uint(((key >> (8 * sizeof(uint) - 1)) ^ key) & (~0U)) ^ seed) + : (uint(key & (~0U)) ^ seed); } -inline uint qHash(long key, uint seed = 0) Q_DECL_NOTHROW { return qHash(ulong(key), seed); } -inline uint qHash(quint64 key, uint seed = 0) Q_DECL_NOTHROW +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(long key, uint seed = 0) Q_DECL_NOTHROW { return qHash(ulong(key), seed); } +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(quint64 key, uint seed = 0) Q_DECL_NOTHROW { - if (sizeof(quint64) > sizeof(uint)) { - return uint(((key >> (8 * sizeof(uint) - 1)) ^ key) & (~0U)) ^ seed; - } else { - return uint(key & (~0U)) ^ seed; - } + return uint(((key >> (8 * sizeof(uint) - 1)) ^ key) & (~0U)) ^ seed; } -inline uint qHash(qint64 key, uint seed = 0) Q_DECL_NOTHROW { return qHash(quint64(key), seed); } -Q_CORE_EXPORT uint qHash(float key, uint seed = 0) Q_DECL_NOTHROW; -Q_CORE_EXPORT uint qHash(double key, uint seed = 0) Q_DECL_NOTHROW; +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(qint64 key, uint seed = 0) Q_DECL_NOTHROW { return qHash(quint64(key), seed); } +Q_CORE_EXPORT Q_DECL_CONST_FUNCTION uint qHash(float key, uint seed = 0) Q_DECL_NOTHROW; +Q_CORE_EXPORT Q_DECL_CONST_FUNCTION uint qHash(double key, uint seed = 0) Q_DECL_NOTHROW; #ifndef Q_OS_DARWIN -Q_CORE_EXPORT uint qHash(long double key, uint seed = 0) Q_DECL_NOTHROW; +Q_CORE_EXPORT Q_DECL_CONST_FUNCTION uint qHash(long double key, uint seed = 0) Q_DECL_NOTHROW; #endif -inline uint qHash(QChar key, uint seed = 0) Q_DECL_NOTHROW { return qHash(key.unicode(), seed); } -Q_CORE_EXPORT uint qHash(const QByteArray &key, uint seed = 0) Q_DECL_NOTHROW; -Q_CORE_EXPORT uint qHash(const QString &key, uint seed = 0) Q_DECL_NOTHROW; -Q_CORE_EXPORT uint qHash(const QStringRef &key, uint seed = 0) Q_DECL_NOTHROW; -Q_CORE_EXPORT uint qHash(const QBitArray &key, uint seed = 0) Q_DECL_NOTHROW; -Q_CORE_EXPORT uint qHash(QLatin1String key, uint seed = 0) Q_DECL_NOTHROW; -Q_CORE_EXPORT uint qt_hash(const QString &key) Q_DECL_NOTHROW; -Q_CORE_EXPORT uint qt_hash(const QStringRef &key) Q_DECL_NOTHROW; +Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(const QChar key, uint seed = 0) Q_DECL_NOTHROW { return qHash(key.unicode(), seed); } +Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QByteArray &key, uint seed = 0) Q_DECL_NOTHROW; +Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QString &key, uint seed = 0) Q_DECL_NOTHROW; +Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QStringRef &key, uint seed = 0) Q_DECL_NOTHROW; +Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QBitArray &key, uint seed = 0) Q_DECL_NOTHROW; +Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(QLatin1String key, uint seed = 0) Q_DECL_NOTHROW; +Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(const QString &key) Q_DECL_NOTHROW; +Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(const QStringRef &key) Q_DECL_NOTHROW; template <class T> inline uint qHash(const T *key, uint seed = 0) Q_DECL_NOTHROW { @@ -301,7 +297,7 @@ public: inline QHash(std::initializer_list<std::pair<Key,T> > list) : d(const_cast<QHashData *>(&QHashData::shared_null)) { - reserve(list.size()); + reserve(int(list.size())); for (typename std::initializer_list<std::pair<Key,T> >::const_iterator it = list.begin(); it != list.end(); ++it) insert(it->first, it->second); } @@ -977,7 +973,7 @@ public: #ifdef Q_COMPILER_INITIALIZER_LISTS inline QMultiHash(std::initializer_list<std::pair<Key,T> > list) { - this->reserve(list.size()); + this->reserve(int(list.size())); for (typename std::initializer_list<std::pair<Key,T> >::const_iterator it = list.begin(); it != list.end(); ++it) insert(it->first, it->second); } diff --git a/src/corelib/tools/qline.cpp b/src/corelib/tools/qline.cpp index 990e6bef09..61c7899ef4 100644 --- a/src/corelib/tools/qline.cpp +++ b/src/corelib/tools/qline.cpp @@ -259,10 +259,11 @@ QT_BEGIN_NAMESPACE #ifndef QT_NO_DEBUG_STREAM -QDebug operator<<(QDebug d, const QLine &p) +QDebug operator<<(QDebug dbg, const QLine &p) { - d << "QLine(" << p.p1() << ',' << p.p2() << ')'; - return d; + QDebugStateSaver saver(dbg); + dbg.nospace() << "QLine(" << p.p1() << ',' << p.p2() << ')'; + return dbg; } #endif @@ -819,10 +820,11 @@ qreal QLineF::angle(const QLineF &l) const #ifndef QT_NO_DEBUG_STREAM -QDebug operator<<(QDebug d, const QLineF &p) +QDebug operator<<(QDebug dbg, const QLineF &p) { - d << "QLineF(" << p.p1() << ',' << p.p2() << ')'; - return d; + QDebugStateSaver saver(dbg); + dbg.nospace() << "QLineF(" << p.p1() << ',' << p.p2() << ')'; + return dbg; } #endif diff --git a/src/corelib/tools/qpoint.cpp b/src/corelib/tools/qpoint.cpp index 1063cda52b..090e8d0ff2 100644 --- a/src/corelib/tools/qpoint.cpp +++ b/src/corelib/tools/qpoint.cpp @@ -452,15 +452,18 @@ QDataStream &operator>>(QDataStream &s, QPoint &p) */ #ifndef QT_NO_DEBUG_STREAM -QDebug operator<<(QDebug dbg, const QPoint &p) { +QDebug operator<<(QDebug dbg, const QPoint &p) +{ + QDebugStateSaver saver(dbg); dbg.nospace() << "QPoint(" << p.x() << ',' << p.y() << ')'; - return dbg.space(); + return dbg; } -QDebug operator<<(QDebug d, const QPointF &p) +QDebug operator<<(QDebug dbg, const QPointF &p) { - d.nospace() << "QPointF(" << p.x() << ", " << p.y() << ')'; - return d.space(); + QDebugStateSaver saver(dbg); + dbg.nospace() << "QPointF(" << p.x() << ',' << p.y() << ')'; + return dbg; } #endif diff --git a/src/corelib/tools/qrect.cpp b/src/corelib/tools/qrect.cpp index 382793f175..04269e485b 100644 --- a/src/corelib/tools/qrect.cpp +++ b/src/corelib/tools/qrect.cpp @@ -1286,10 +1286,12 @@ QDataStream &operator>>(QDataStream &s, QRect &r) #ifndef QT_NO_DEBUG_STREAM -QDebug operator<<(QDebug dbg, const QRect &r) { +QDebug operator<<(QDebug dbg, const QRect &r) +{ + QDebugStateSaver saver(dbg); dbg.nospace() << "QRect(" << r.x() << ',' << r.y() << ' ' << r.width() << 'x' << r.height() << ')'; - return dbg.space(); + return dbg; } #endif @@ -2493,10 +2495,12 @@ QDataStream &operator>>(QDataStream &s, QRectF &r) #ifndef QT_NO_DEBUG_STREAM -QDebug operator<<(QDebug dbg, const QRectF &r) { +QDebug operator<<(QDebug dbg, const QRectF &r) +{ + QDebugStateSaver saver(dbg); dbg.nospace() << "QRectF(" << r.x() << ',' << r.y() << ' ' << r.width() << 'x' << r.height() << ')'; - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/tools/qset.h b/src/corelib/tools/qset.h index ad2f91b983..291c8fe4f9 100644 --- a/src/corelib/tools/qset.h +++ b/src/corelib/tools/qset.h @@ -60,7 +60,7 @@ public: #ifdef Q_COMPILER_INITIALIZER_LISTS inline QSet(std::initializer_list<T> list) { - reserve(list.size()); + reserve(int(list.size())); for (typename std::initializer_list<T>::const_iterator it = list.begin(); it != list.end(); ++it) insert(*it); } diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp index a610fc46e5..58a9a021d0 100644 --- a/src/corelib/tools/qsharedpointer.cpp +++ b/src/corelib/tools/qsharedpointer.cpp @@ -630,9 +630,15 @@ This function will attempt to call a constructor for type \tt T that can accept all the arguments passed. Arguments will be perfectly-forwarded. - \note This function is only available with a C++11 compiler that supports - perfect forwarding of an arbitrary number of arguments. If the compiler - does not support the necessary C++11 features, you must use the overload + \note This function is only fully available with a C++11 compiler that + supports perfect forwarding of an arbitrary number of arguments. + + If the compiler does not support the necessary C++11 features, + then a restricted version is available since Qt 5.4: you may pass + one (but just one) argument, and it will always be passed by const + reference. + + If you target Qt before version 5.4, you must use the overload that calls the default constructor. */ @@ -771,6 +777,14 @@ */ /*! + \fn void QWeakPointer::swap(QWeakPointer<T> &other) + \since 5.4 + + Swaps this weak pointer instance with \a other. This function is + very fast and never fails. +*/ + +/*! \fn bool QWeakPointer::isNull() const Returns \c true if this object is holding a reference to a null @@ -894,6 +908,15 @@ */ /*! + \fn QSharedPointer<T> QWeakPointer::lock() const + \since 5.4 + + Same as toStrongRef(). + + This function is provided for API compatibility with std::weak_ptr. +*/ + +/*! \fn void QWeakPointer::clear() Clears this QWeakPointer object, dropping the reference that it diff --git a/src/corelib/tools/qsharedpointer.h b/src/corelib/tools/qsharedpointer.h index 34b4bfbb12..d9de48b7f4 100644 --- a/src/corelib/tools/qsharedpointer.h +++ b/src/corelib/tools/qsharedpointer.h @@ -122,10 +122,13 @@ public: QWeakPointer(const QObject *other); QWeakPointer<T> operator=(const QObject *other); + void swap(QWeakPointer<T> &other); + T *data() const; void clear(); QSharedPointer<T> toStrongRef() const; + QSharedPointer<T> lock() const; }; template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2); diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 1423449a69..f3df32469f 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -318,16 +318,22 @@ public: inline QSharedPointer(T *ptr, Deleter deleter) : value(ptr) // throws { internalConstruct(ptr, deleter); } - inline QSharedPointer(const QSharedPointer<T> &other) : value(other.value), d(other.d) + inline QSharedPointer(const QSharedPointer &other) : value(other.value), d(other.d) { if (d) ref(); } - inline QSharedPointer<T> &operator=(const QSharedPointer<T> &other) + inline QSharedPointer &operator=(const QSharedPointer &other) { QSharedPointer copy(other); swap(copy); return *this; } #ifdef Q_COMPILER_RVALUE_REFS - inline QSharedPointer<T> &operator=(QSharedPointer<T> &&other) + inline QSharedPointer(QSharedPointer &&other) + : value(other.value), d(other.d) + { + other.d = 0; + other.value = 0; + } + inline QSharedPointer &operator=(QSharedPointer &&other) { swap(other); return *this; @@ -339,7 +345,7 @@ public: { if (d) ref(); } template <class X> - inline QSharedPointer<T> &operator=(const QSharedPointer<X> &other) + inline QSharedPointer &operator=(const QSharedPointer<X> &other) { QSHAREDPOINTER_VERIFY_AUTO_CAST(T, X); // if you get an error in this line, the cast is invalid internalCopy(other); @@ -355,7 +361,7 @@ public: { internalSet(other.d, other.value); return *this; } inline void swap(QSharedPointer &other) - { QSharedPointer<T>::internalSwap(other); } + { this->internalSwap(other); } inline void reset() { clear(); } inline void reset(T *t) @@ -396,7 +402,7 @@ public: #if defined(Q_COMPILER_RVALUE_REFS) && defined(Q_COMPILER_VARIADIC_TEMPLATES) template <typename... Args> - static QSharedPointer<T> create(Args && ...arguments) + static QSharedPointer create(Args && ...arguments) { typedef QtSharedPointer::ExternalRefCountWithContiguousData<T> Private; # ifdef QT_SHAREDPOINTER_TRACK_POINTERS @@ -404,7 +410,7 @@ public: # else typename Private::DestroyerFn destroy = &Private::deleter; # endif - QSharedPointer<T> result(Qt::Uninitialized); + QSharedPointer result(Qt::Uninitialized); result.d = Private::create(&result.value, destroy); // now initialize the data @@ -416,7 +422,7 @@ public: return result; } #else - static inline QSharedPointer<T> create() + static inline QSharedPointer create() { typedef QtSharedPointer::ExternalRefCountWithContiguousData<T> Private; # ifdef QT_SHAREDPOINTER_TRACK_POINTERS @@ -424,7 +430,7 @@ public: # else typename Private::DestroyerFn destroy = &Private::deleter; # endif - QSharedPointer<T> result(Qt::Uninitialized); + QSharedPointer result(Qt::Uninitialized); result.d = Private::create(&result.value, destroy); // now initialize the data @@ -435,6 +441,27 @@ public: result.d->setQObjectShared(result.value, true); return result; } + + template <typename Arg> + static inline QSharedPointer create(const Arg &arg) + { + typedef QtSharedPointer::ExternalRefCountWithContiguousData<T> Private; +# ifdef QT_SHAREDPOINTER_TRACK_POINTERS + typename Private::DestroyerFn destroy = &Private::safetyCheckDeleter; +# else + typename Private::DestroyerFn destroy = &Private::deleter; +# endif + QSharedPointer result(Qt::Uninitialized); + result.d = Private::create(&result.value, destroy); + + // now initialize the data + new (result.data()) T(arg); +# ifdef QT_SHAREDPOINTER_TRACK_POINTERS + internalSafetyCheckAdd(result.d, result.value); +# endif + result.d->setQObjectShared(result.value, true); + return result; + } #endif private: @@ -573,17 +600,23 @@ public: { return *this = QWeakPointer(ptr); } #endif - inline QWeakPointer(const QWeakPointer<T> &o) : d(o.d), value(o.value) + inline QWeakPointer(const QWeakPointer &o) : d(o.d), value(o.value) { if (d) d->weakref.ref(); } - inline QWeakPointer<T> &operator=(const QWeakPointer<T> &o) + inline QWeakPointer &operator=(const QWeakPointer &o) { internalSet(o.d, o.value); return *this; } + inline void swap(QWeakPointer &other) + { + qSwap(this->d, other.d); + qSwap(this->value, other.value); + } + inline QWeakPointer(const QSharedPointer<T> &o) : d(o.d), value(o.data()) { if (d) d->weakref.ref();} - inline QWeakPointer<T> &operator=(const QSharedPointer<T> &o) + inline QWeakPointer &operator=(const QSharedPointer<T> &o) { internalSet(o.d, o.value); return *this; @@ -594,7 +627,7 @@ public: { *this = o; } template <class X> - inline QWeakPointer<T> &operator=(const QWeakPointer<X> &o) + inline QWeakPointer &operator=(const QWeakPointer<X> &o) { // conversion between X and T could require access to the virtual table // so force the operation to go through QSharedPointer @@ -615,7 +648,7 @@ public: { *this = o; } template <class X> - inline QWeakPointer<T> &operator=(const QSharedPointer<X> &o) + inline QWeakPointer &operator=(const QSharedPointer<X> &o) { QSHAREDPOINTER_VERIFY_AUTO_CAST(T, X); // if you get an error in this line, the cast is invalid internalSet(o.d, o.data()); @@ -630,9 +663,11 @@ public: inline bool operator!=(const QSharedPointer<X> &o) const { return !(*this == o); } - inline void clear() { *this = QWeakPointer<T>(); } + inline void clear() { *this = QWeakPointer(); } inline QSharedPointer<T> toStrongRef() const { return QSharedPointer<T>(*this); } + // std::weak_ptr compatibility: + inline QSharedPointer<T> lock() const { return toStrongRef(); } #if defined(QWEAKPOINTER_ENABLE_ARROW) inline T *operator->() const { return data(); } diff --git a/src/corelib/tools/qsize.cpp b/src/corelib/tools/qsize.cpp index b53eced298..da447157ff 100644 --- a/src/corelib/tools/qsize.cpp +++ b/src/corelib/tools/qsize.cpp @@ -445,9 +445,11 @@ QDataStream &operator>>(QDataStream &s, QSize &sz) #endif // QT_NO_DATASTREAM #ifndef QT_NO_DEBUG_STREAM -QDebug operator<<(QDebug dbg, const QSize &s) { +QDebug operator<<(QDebug dbg, const QSize &s) +{ + QDebugStateSaver saver(dbg); dbg.nospace() << "QSize(" << s.width() << ", " << s.height() << ')'; - return dbg.space(); + return dbg; } #endif @@ -870,9 +872,11 @@ QDataStream &operator>>(QDataStream &s, QSizeF &sz) #endif // QT_NO_DATASTREAM #ifndef QT_NO_DEBUG_STREAM -QDebug operator<<(QDebug dbg, const QSizeF &s) { +QDebug operator<<(QDebug dbg, const QSizeF &s) +{ + QDebugStateSaver saver(dbg); dbg.nospace() << "QSizeF(" << s.width() << ", " << s.height() << ')'; - return dbg.space(); + return dbg; } #endif diff --git a/src/corelib/tools/qstringlist.cpp b/src/corelib/tools/qstringlist.cpp index 6b0b39c7bb..a9d704ca4c 100644 --- a/src/corelib/tools/qstringlist.cpp +++ b/src/corelib/tools/qstringlist.cpp @@ -191,20 +191,6 @@ QT_BEGIN_NAMESPACE */ /*! - \fn QStringList::QStringList(const QStringList &other) - - Constructs a copy of the \a other string list. - - This operation takes \l{constant time} because QStringList is - \l{implicitly shared}, making the process of returning a - QStringList from a function very fast. If a shared instance is - modified, it will be copied (copy-on-write), and that takes - \l{linear time}. - - \sa operator=() -*/ - -/*! \fn QStringList::QStringList(const QList<QString> &other) Constructs a copy of \a other. @@ -235,9 +221,14 @@ QT_BEGIN_NAMESPACE integer index. */ -static inline bool caseInsensitiveLessThan(const QString &s1, const QString &s2) -{ - return s1.compare(s2, Qt::CaseInsensitive) < 0; +namespace { +struct CaseInsensitiveLessThan { + typedef bool result_type; + result_type operator()(const QString &s1, const QString &s2) const + { + return s1.compare(s2, Qt::CaseInsensitive) < 0; + } +}; } void QtPrivate::QStringList_sort(QStringList *that, Qt::CaseSensitivity cs) @@ -245,7 +236,7 @@ void QtPrivate::QStringList_sort(QStringList *that, Qt::CaseSensitivity cs) if (cs == Qt::CaseSensitive) std::sort(that->begin(), that->end()); else - std::sort(that->begin(), that->end(), caseInsensitiveLessThan); + std::sort(that->begin(), that->end(), CaseInsensitiveLessThan()); } @@ -457,7 +448,7 @@ QString QtPrivate::QStringList_join(const QStringList *that, const QChar *sep, i if (totalLength == 0) return res; res.reserve(totalLength); - for (int i = 0; i < that->size(); ++i) { + for (int i = 0; i < size; ++i) { if (i) res.append(sep, seplen); res += that->at(i); @@ -492,6 +483,16 @@ QString QtPrivate::QStringList_join(const QStringList *that, const QChar *sep, i the latter string list. */ +/*! + \fn QStringList &QStringList::operator<<(const QList<QString> &other) + \since 5.4 + + \overload + + Appends the \a other string list to the string list and returns a reference to + the latter string list. +*/ + #ifndef QT_NO_DATASTREAM /*! \fn QDataStream &operator>>(QDataStream &in, QStringList &list) @@ -725,7 +726,7 @@ int QtPrivate::QStringList_removeDuplicates(QStringList *that) continue; seen.insert(s); if (j != i) - (*that)[j] = s; + that->swap(i, j); ++j; } if (n != j) diff --git a/src/corelib/tools/qstringlist.h b/src/corelib/tools/qstringlist.h index a962b7c528..1cdafb4ec2 100644 --- a/src/corelib/tools/qstringlist.h +++ b/src/corelib/tools/qstringlist.h @@ -63,7 +63,6 @@ class QStringList : public QList<QString> public: inline QStringList() { } inline explicit QStringList(const QString &i) { append(i); } - inline QStringList(const QStringList &l) : QList<QString>(l) { } inline QStringList(const QList<QString> &l) : QList<QString>(l) { } #ifdef Q_COMPILER_INITIALIZER_LISTS inline QStringList(std::initializer_list<QString> args) : QList<QString>(args) { } @@ -86,6 +85,8 @@ public: { append(str); return *this; } inline QStringList &operator<<(const QStringList &l) { *this += l; return *this; } + inline QStringList &operator<<(const QList<QString> &l) + { *this += l; return *this; } #ifndef QT_NO_REGEXP inline QStringList filter(const QRegExp &rx) const; diff --git a/src/corelib/tools/qtools_p.h b/src/corelib/tools/qtools_p.h index b00343dd39..38073a6e30 100644 --- a/src/corelib/tools/qtools_p.h +++ b/src/corelib/tools/qtools_p.h @@ -58,7 +58,7 @@ QT_BEGIN_NAMESPACE // implemented in qbytearray.cpp -int Q_CORE_EXPORT qAllocMore(int alloc, int extra); +int Q_CORE_EXPORT qAllocMore(int alloc, int extra) Q_DECL_NOTHROW; QT_END_NAMESPACE diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp index 89396540e0..b2b4315545 100644 --- a/src/corelib/tools/qvector.cpp +++ b/src/corelib/tools/qvector.cpp @@ -576,6 +576,28 @@ \sa remove(), QList::removeAt() */ +/*! \fn int QVector::removeAll(const T &t) + \since 5.4 + + Removes all elements that compare equal to \a t from the + vector. Returns the number of elements removed, if any. + + Provided for compatibility with QList. + + \sa removeOne(), QList::removeAll() +*/ + +/*! \fn bool QVector::removeOne(const T &t) + \since 5.4 + + Removes the first element that compares equal to \a t from the + vector. Returns whether an element was, in fact, removed. + + Provided for compatibility with QList. + + \sa removeAll(), QList::removeOne() +*/ + /*! \fn int QVector::length() const \since 5.2 diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index b0be98c296..22031645dc 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -154,6 +154,24 @@ public: // QList compatibility void removeAt(int i) { remove(i); } + int removeAll(const T &t) + { + const const_iterator ce = this->cend(), cit = std::find(this->cbegin(), ce, t); + if (cit == ce) + return 0; + const iterator e = end(), it = std::remove(c2m(cit), e, t); + const int result = std::distance(it, e); + erase(it, e); + return result; + } + bool removeOne(const T &t) + { + const int i = indexOf(t); + if (i < 0) + return false; + remove(i); + return true; + } int length() const { return size(); } T takeAt(int i) { T t = at(i); remove(i); return t; } @@ -250,6 +268,7 @@ private: { return (i <= d->end()) && (d->begin() <= i); } + iterator c2m(const_iterator it) { return begin() + (it - cbegin()); } class AlignmentDummy { Data header; T array[1]; }; }; |