summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/global/qglobal.h6
-rw-r--r--src/corelib/global/qisenum.h12
-rw-r--r--src/corelib/io/io.pri3
-rw-r--r--src/corelib/io/qdatastream.h4
-rw-r--r--src/corelib/io/qdir.h2
-rw-r--r--src/corelib/io/qdiriterator.cpp4
-rw-r--r--src/corelib/io/qdiriterator.h2
-rw-r--r--src/corelib/io/qfile.cpp635
-rw-r--r--src/corelib/io/qfile.h62
-rw-r--r--src/corelib/io/qfile_p.h20
-rw-r--r--src/corelib/io/qfiledevice.cpp736
-rw-r--r--src/corelib/io/qfiledevice.h147
-rw-r--r--src/corelib/io/qfiledevice_p.h104
-rw-r--r--src/corelib/io/qtextstream.cpp2
-rw-r--r--src/corelib/io/qurl.cpp31
-rw-r--r--src/corelib/io/qurl.h1
-rw-r--r--src/corelib/itemmodels/qabstractproxymodel.h2
-rw-r--r--src/corelib/itemmodels/qsortfilterproxymodel.h2
-rw-r--r--src/corelib/itemmodels/qstringlistmodel.h2
-rw-r--r--src/corelib/json/qjsonvalue.cpp2
-rw-r--r--src/corelib/kernel/qcoreapplication.cpp21
-rw-r--r--src/corelib/kernel/qcoreapplication.h4
-rw-r--r--src/corelib/kernel/qcoreevent.h13
-rw-r--r--src/corelib/kernel/qeventloop.cpp13
-rw-r--r--src/corelib/kernel/qmetaobject.cpp754
-rw-r--r--src/corelib/kernel/qmetaobject.h15
-rw-r--r--src/corelib/kernel/qmetaobject_p.h81
-rw-r--r--src/corelib/kernel/qmetaobjectbuilder.cpp341
-rw-r--r--src/corelib/kernel/qmetaobjectbuilder_p.h19
-rw-r--r--src/corelib/kernel/qmetatype.cpp107
-rw-r--r--src/corelib/kernel/qmetatype.h117
-rw-r--r--src/corelib/kernel/qmetatypeswitcher_p.h7
-rw-r--r--src/corelib/kernel/qobject.cpp236
-rw-r--r--src/corelib/kernel/qobject.h28
-rw-r--r--src/corelib/kernel/qobjectdefs.h10
-rw-r--r--src/corelib/kernel/qpointer.h58
-rw-r--r--src/corelib/kernel/qvariant.cpp71
-rw-r--r--src/corelib/kernel/qvariant.h16
-rw-r--r--src/corelib/kernel/qvariant_p.h87
-rw-r--r--src/corelib/statemachine/qstatemachine.cpp38
-rw-r--r--src/corelib/statemachine/qstatemachine.h2
-rw-r--r--src/corelib/tools/qarraydata.cpp111
-rw-r--r--src/corelib/tools/qarraydata.h275
-rw-r--r--src/corelib/tools/qarraydataops.h322
-rw-r--r--src/corelib/tools/qarraydatapointer.h220
-rw-r--r--src/corelib/tools/qbytearray.cpp161
-rw-r--r--src/corelib/tools/qbytearray.h69
-rw-r--r--src/corelib/tools/qhash.cpp4
-rw-r--r--src/corelib/tools/qhash.h4
-rw-r--r--src/corelib/tools/qlinkedlist.cpp2
-rw-r--r--src/corelib/tools/qlinkedlist.h22
-rw-r--r--src/corelib/tools/qlist.cpp24
-rw-r--r--src/corelib/tools/qlist.h60
-rw-r--r--src/corelib/tools/qmap.cpp445
-rw-r--r--src/corelib/tools/qmap.h826
-rw-r--r--src/corelib/tools/qrefcount.h58
-rw-r--r--src/corelib/tools/qregularexpression.h2
-rw-r--r--src/corelib/tools/qsharedpointer.cpp66
-rw-r--r--src/corelib/tools/qsharedpointer.h5
-rw-r--r--src/corelib/tools/qsharedpointer_impl.h17
-rw-r--r--src/corelib/tools/qstring.cpp592
-rw-r--r--src/corelib/tools/qstring.h95
-rw-r--r--src/corelib/tools/qstringbuilder.h64
-rw-r--r--src/corelib/tools/qstringlist.cpp117
-rw-r--r--src/corelib/tools/qstringlist.h44
-rw-r--r--src/corelib/tools/qvector.cpp16
-rw-r--r--src/corelib/tools/qvector.h374
-rw-r--r--src/corelib/tools/tools.pri4
68 files changed, 5394 insertions, 2422 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h
index 99328d52ac..d0d6e851ad 100644
--- a/src/corelib/global/qglobal.h
+++ b/src/corelib/global/qglobal.h
@@ -415,9 +415,15 @@ QT_END_INCLUDE_NAMESPACE
#ifdef Q_COMPILER_EXPLICIT_OVERRIDES
# define Q_DECL_OVERRIDE override
# define Q_DECL_FINAL final
+# ifdef Q_COMPILER_DECLTYPE // required for class-level final to compile in qvariant_p.h
+# define Q_DECL_FINAL_CLASS final
+# else
+# define Q_DECL_FINAL_CLASS
+# endif
#else
# define Q_DECL_OVERRIDE
# define Q_DECL_FINAL
+# define Q_DECL_FINAL_CLASS
#endif
//defines the type for the WNDPROC on windows
diff --git a/src/corelib/global/qisenum.h b/src/corelib/global/qisenum.h
index c9b6ec6695..ef1ccc81a8 100644
--- a/src/corelib/global/qisenum.h
+++ b/src/corelib/global/qisenum.h
@@ -52,12 +52,18 @@ QT_BEGIN_NAMESPACE
# define Q_IS_ENUM(x) __is_enum(x)
# elif defined(Q_CC_MSVC) && defined(_MSC_FULL_VER) && (_MSC_FULL_VER >=140050215)
# define Q_IS_ENUM(x) __is_enum(x)
-# else
-# include <QtCore/qtypetraits.h>
-# define Q_IS_ENUM(x) QtPrivate::is_enum<x>::value
+# elif defined(Q_CC_CLANG)
+# if __has_extension(is_enum)
+# define Q_IS_ENUM(x) __is_enum(x)
+# endif
# endif
#endif
+#ifndef Q_IS_ENUM
+# include <QtCore/qtypetraits.h>
+# define Q_IS_ENUM(x) QtPrivate::is_enum<x>::value
+#endif
+
QT_END_HEADER
QT_END_NAMESPACE
diff --git a/src/corelib/io/io.pri b/src/corelib/io/io.pri
index 29599295ad..9c117abe03 100644
--- a/src/corelib/io/io.pri
+++ b/src/corelib/io/io.pri
@@ -11,6 +11,8 @@ HEADERS += \
io/qdir_p.h \
io/qdiriterator.h \
io/qfile.h \
+ io/qfiledevice.h \
+ io/qfiledevice_p.h \
io/qfileinfo.h \
io/qfileinfo_p.h \
io/qiodevice.h \
@@ -49,6 +51,7 @@ SOURCES += \
io/qdir.cpp \
io/qdiriterator.cpp \
io/qfile.cpp \
+ io/qfiledevice.cpp \
io/qfileinfo.cpp \
io/qiodevice.cpp \
io/qnoncontiguousbytedevice.cpp \
diff --git a/src/corelib/io/qdatastream.h b/src/corelib/io/qdatastream.h
index 752246a543..533911974a 100644
--- a/src/corelib/io/qdatastream.h
+++ b/src/corelib/io/qdatastream.h
@@ -114,7 +114,7 @@ public:
explicit QDataStream(QIODevice *);
QDataStream(QByteArray *, QIODevice::OpenMode flags);
QDataStream(const QByteArray &);
- virtual ~QDataStream();
+ ~QDataStream();
QIODevice *device() const;
void setDevice(QIODevice *);
@@ -385,7 +385,6 @@ Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<aKey, aT> &ma
in >> n;
map.detach();
- map.setInsertInOrder(true);
for (quint32 i = 0; i < n; ++i) {
if (in.status() != QDataStream::Ok)
break;
@@ -395,7 +394,6 @@ Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<aKey, aT> &ma
in >> key >> value;
map.insertMulti(key, value);
}
- map.setInsertInOrder(false);
if (in.status() != QDataStream::Ok)
map.clear();
if (oldStatus != QDataStream::Ok)
diff --git a/src/corelib/io/qdir.h b/src/corelib/io/qdir.h
index 5551ecd2fb..a5105fe2fb 100644
--- a/src/corelib/io/qdir.h
+++ b/src/corelib/io/qdir.h
@@ -52,6 +52,7 @@ QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
+class QDirIterator;
class QDirPrivate;
class Q_CORE_EXPORT QDir
@@ -210,6 +211,7 @@ protected:
QSharedDataPointer<QDirPrivate> d_ptr;
private:
+ friend class QDirIterator;
// Q_DECLARE_PRIVATE equivalent for shared data pointers
QDirPrivate* d_func();
inline const QDirPrivate* d_func() const
diff --git a/src/corelib/io/qdiriterator.cpp b/src/corelib/io/qdiriterator.cpp
index b8536a8dce..56912e8706 100644
--- a/src/corelib/io/qdiriterator.cpp
+++ b/src/corelib/io/qdiriterator.cpp
@@ -410,9 +410,7 @@ bool QDirIteratorPrivate::matchesFilters(const QString &fileName, const QFileInf
*/
QDirIterator::QDirIterator(const QDir &dir, IteratorFlags flags)
{
- // little trick to get hold of the QDirPrivate while there is no API on QDir to give it to us
- class MyQDir : public QDir { public: const QDirPrivate *priv() const { return d_ptr.constData(); } };
- const QDirPrivate *other = static_cast<const MyQDir*>(&dir)->priv();
+ const QDirPrivate *other = dir.d_ptr.constData();
d.reset(new QDirIteratorPrivate(other->dirEntry, other->nameFilters, other->filters, flags, !other->fileEngine.isNull()));
}
diff --git a/src/corelib/io/qdiriterator.h b/src/corelib/io/qdiriterator.h
index 27189e2efd..d2d0645916 100644
--- a/src/corelib/io/qdiriterator.h
+++ b/src/corelib/io/qdiriterator.h
@@ -70,7 +70,7 @@ public:
QDir::Filters filters = QDir::NoFilter,
IteratorFlags flags = NoIteratorFlags);
- virtual ~QDirIterator();
+ ~QDirIterator();
QString next();
bool hasNext() const;
diff --git a/src/corelib/io/qfile.cpp b/src/corelib/io/qfile.cpp
index 6640dca70b..433d4493e5 100644
--- a/src/corelib/io/qfile.cpp
+++ b/src/corelib/io/qfile.cpp
@@ -59,8 +59,6 @@
QT_BEGIN_NAMESPACE
-static const int QFILE_WRITEBUFFER_SIZE = 16384;
-
static QByteArray locale_encode(const QString &f)
{
#if defined(Q_OS_DARWIN)
@@ -86,16 +84,11 @@ QFile::EncoderFn QFilePrivate::encoder = locale_encode;
QFile::DecoderFn QFilePrivate::decoder = locale_decode;
QFilePrivate::QFilePrivate()
- : fileEngine(0), lastWasWrite(false),
- writeBuffer(QFILE_WRITEBUFFER_SIZE), error(QFile::NoError),
- cachedSize(0)
{
}
QFilePrivate::~QFilePrivate()
{
- delete fileEngine;
- fileEngine = 0;
}
bool
@@ -137,39 +130,6 @@ QAbstractFileEngine *QFilePrivate::engine() const
return fileEngine;
}
-inline bool QFilePrivate::ensureFlushed() const
-{
- // This function ensures that the write buffer has been flushed (const
- // because certain const functions need to call it.
- if (lastWasWrite) {
- const_cast<QFilePrivate *>(this)->lastWasWrite = false;
- if (!const_cast<QFile *>(q_func())->flush())
- return false;
- }
- return true;
-}
-
-void
-QFilePrivate::setError(QFile::FileError err)
-{
- error = err;
- errorString.clear();
-}
-
-void
-QFilePrivate::setError(QFile::FileError err, const QString &errStr)
-{
- error = err;
- errorString = errStr;
-}
-
-void
-QFilePrivate::setError(QFile::FileError err, int errNum)
-{
- error = err;
- errorString = qt_error_string(errNum);
-}
-
//************* QFile
/*!
@@ -278,98 +238,18 @@ QFilePrivate::setError(QFile::FileError err, int errNum)
\sa QTextStream, QDataStream, QFileInfo, QDir, {The Qt Resource System}
*/
-/*!
- \enum QFile::FileError
-
- This enum describes the errors that may be returned by the error()
- function.
-
- \value NoError No error occurred.
- \value ReadError An error occurred when reading from the file.
- \value WriteError An error occurred when writing to the file.
- \value FatalError A fatal error occurred.
- \value ResourceError
- \value OpenError The file could not be opened.
- \value AbortError The operation was aborted.
- \value TimeOutError A timeout occurred.
- \value UnspecifiedError An unspecified error occurred.
- \value RemoveError The file could not be removed.
- \value RenameError The file could not be renamed.
- \value PositionError The position in the file could not be changed.
- \value ResizeError The file could not be resized.
- \value PermissionsError The file could not be accessed.
- \value CopyError The file could not be copied.
-
- \omitvalue ConnectError
-*/
-
-/*!
- \enum QFile::Permission
-
- This enum is used by the permission() function to report the
- permissions and ownership of a file. The values may be OR-ed
- together to test multiple permissions and ownership values.
-
- \value ReadOwner The file is readable by the owner of the file.
- \value WriteOwner The file is writable by the owner of the file.
- \value ExeOwner The file is executable by the owner of the file.
- \value ReadUser The file is readable by the user.
- \value WriteUser The file is writable by the user.
- \value ExeUser The file is executable by the user.
- \value ReadGroup The file is readable by the group.
- \value WriteGroup The file is writable by the group.
- \value ExeGroup The file is executable by the group.
- \value ReadOther The file is readable by anyone.
- \value WriteOther The file is writable by anyone.
- \value ExeOther The file is executable by anyone.
-
- \warning Because of differences in the platforms supported by Qt,
- the semantics of ReadUser, WriteUser and ExeUser are
- platform-dependent: On Unix, the rights of the owner of the file
- are returned and on Windows the rights of the current user are
- returned. This behavior might change in a future Qt version.
-
- Note that Qt does not by default check for permissions on NTFS
- file systems, as this may decrease the performance of file
- handling considerably. It is possible to force permission checking
- on NTFS by including the following code in your source:
-
- \snippet doc/src/snippets/ntfsp.cpp 0
-
- Permission checking is then turned on and off by incrementing and
- decrementing \c qt_ntfs_permission_lookup by 1.
-
- \snippet doc/src/snippets/ntfsp.cpp 1
-*/
-
-/*!
- \enum QFile::FileHandleFlag
-
- This enum is used when opening a file to specify additional
- options which only apply to files and not to a generic
- QIODevice.
-
- \value AutoCloseHandle The file handle passed into open() should be
- closed by close(), the default behavior is that close just flushes
- the file and the application is responsible for closing the file handle.
- When opening a file by name, this flag is ignored as Qt always owns the
- file handle and must close it.
- \value DontCloseHandle If not explicitly closed, the underlying file
- handle is left open when the QFile object is destroyed.
- */
-
#ifdef QT_NO_QOBJECT
QFile::QFile()
- : QIODevice(*new QFilePrivate)
+ : QFileDevice(*new QFilePrivate)
{
}
QFile::QFile(const QString &name)
- : QIODevice(*new QFilePrivate)
+ : QFileDevice(*new QFilePrivate)
{
d_func()->fileName = name;
}
QFile::QFile(QFilePrivate &dd)
- : QIODevice(dd)
+ : QFileDevice(dd)
{
}
#else
@@ -377,21 +257,21 @@ QFile::QFile(QFilePrivate &dd)
\internal
*/
QFile::QFile()
- : QIODevice(*new QFilePrivate, 0)
+ : QFileDevice(*new QFilePrivate, 0)
{
}
/*!
Constructs a new file object with the given \a parent.
*/
QFile::QFile(QObject *parent)
- : QIODevice(*new QFilePrivate, parent)
+ : QFileDevice(*new QFilePrivate, parent)
{
}
/*!
Constructs a new file object to represent the file with the given \a name.
*/
QFile::QFile(const QString &name)
- : QIODevice(*new QFilePrivate, 0)
+ : QFileDevice(*new QFilePrivate, 0)
{
Q_D(QFile);
d->fileName = name;
@@ -401,7 +281,7 @@ QFile::QFile(const QString &name)
file with the specified \a name.
*/
QFile::QFile(const QString &name, QObject *parent)
- : QIODevice(*new QFilePrivate, parent)
+ : QFileDevice(*new QFilePrivate, parent)
{
Q_D(QFile);
d->fileName = name;
@@ -410,7 +290,7 @@ QFile::QFile(const QString &name, QObject *parent)
\internal
*/
QFile::QFile(QFilePrivate &dd, QObject *parent)
- : QIODevice(dd, parent)
+ : QFileDevice(dd, parent)
{
}
#endif
@@ -420,7 +300,6 @@ QFile::QFile(QFilePrivate &dd, QObject *parent)
*/
QFile::~QFile()
{
- close();
}
/*!
@@ -962,20 +841,6 @@ QFile::copy(const QString &fileName, const QString &newName)
}
/*!
- Returns true if the file can only be manipulated sequentially;
- otherwise returns false.
-
- Most files support random-access, but some special files may not.
-
- \sa QIODevice::isSequential()
-*/
-bool QFile::isSequential() const
-{
- Q_D(const QFile);
- return d->fileEngine && d->fileEngine->isSequential();
-}
-
-/*!
Opens the file using OpenMode \a mode, returning true if successful;
otherwise false.
@@ -1149,119 +1014,11 @@ bool QFile::open(int fd, OpenMode mode, FileHandleFlags handleFlags)
}
/*!
- Returns the file handle of the file.
-
- This is a small positive integer, suitable for use with C library
- functions such as fdopen() and fcntl(). On systems that use file
- descriptors for sockets (i.e. Unix systems, but not Windows) the handle
- can be used with QSocketNotifier as well.
-
- If the file is not open, or there is an error, handle() returns -1.
-
- This function is not supported on Windows CE.
-
- \sa QSocketNotifier
-*/
-
-int
-QFile::handle() const
-{
- Q_D(const QFile);
- if (!isOpen() || !d->fileEngine)
- return -1;
-
- return d->fileEngine->handle();
-}
-
-/*!
- \enum QFile::MemoryMapFlags
- \since 4.4
-
- This enum describes special options that may be used by the map()
- function.
-
- \value NoOptions No options.
-*/
-
-/*!
- \since 4.4
- Maps \a size bytes of the file into memory starting at \a offset. A file
- should be open for a map to succeed but the file does not need to stay
- open after the memory has been mapped. When the QFile is destroyed
- or a new file is opened with this object, any maps that have not been
- unmapped will automatically be unmapped.
-
- Any mapping options can be passed through \a flags.
-
- Returns a pointer to the memory or 0 if there is an error.
-
- \note On Windows CE 5.0 the file will be closed before mapping occurs.
-
- \sa unmap()
- */
-uchar *QFile::map(qint64 offset, qint64 size, MemoryMapFlags flags)
-{
- Q_D(QFile);
- if (d->engine()
- && d->fileEngine->supportsExtension(QAbstractFileEngine::MapExtension)) {
- unsetError();
- uchar *address = d->fileEngine->map(offset, size, flags);
- if (address == 0)
- d->setError(d->fileEngine->error(), d->fileEngine->errorString());
- return address;
- }
- return 0;
-}
-
-/*!
- \since 4.4
- Unmaps the memory \a address.
-
- Returns true if the unmap succeeds; false otherwise.
-
- \sa map()
- */
-bool QFile::unmap(uchar *address)
-{
- Q_D(QFile);
- if (d->engine()
- && d->fileEngine->supportsExtension(QAbstractFileEngine::UnMapExtension)) {
- unsetError();
- bool success = d->fileEngine->unmap(address);
- if (!success)
- d->setError(d->fileEngine->error(), d->fileEngine->errorString());
- return success;
- }
- d->setError(PermissionsError, tr("No file engine available or engine does not support UnMapExtension"));
- return false;
-}
-
-/*!
- Sets the file size (in bytes) \a sz. Returns true if the file if the
- resize succeeds; false otherwise. If \a sz is larger than the file
- currently is the new bytes will be set to 0, if \a sz is smaller the
- file is simply truncated.
-
- \sa size(), setFileName()
+ \reimp
*/
-
-bool
-QFile::resize(qint64 sz)
+bool QFile::resize(qint64 sz)
{
- Q_D(QFile);
- if (!d->ensureFlushed())
- return false;
- d->engine();
- if (isOpen() && d->fileEngine->pos() > sz)
- seek(sz);
- if(d->fileEngine->setSize(sz)) {
- unsetError();
- d->cachedSize = sz;
- return true;
- }
- d->cachedSize = 0;
- d->setError(QFile::ResizeError, d->fileEngine->errorString());
- return false;
+ return QFileDevice::resize(sz); // for now
}
/*!
@@ -1282,18 +1039,11 @@ QFile::resize(const QString &fileName, qint64 sz)
}
/*!
- Returns the complete OR-ed together combination of
- QFile::Permission for the file.
-
- \sa setPermissions(), setFileName()
+ \reimp
*/
-
-QFile::Permissions
-QFile::permissions() const
+QFile::Permissions QFile::permissions() const
{
- Q_D(const QFile);
- QAbstractFileEngine::FileFlags perms = d->engine()->fileFlags(QAbstractFileEngine::PermsMask) & QAbstractFileEngine::PermsMask;
- return QFile::Permissions((int)perms); //ewww
+ return QFileDevice::permissions(); // for now
}
/*!
@@ -1317,16 +1067,9 @@ QFile::permissions(const QString &fileName)
\sa permissions(), setFileName()
*/
-bool
-QFile::setPermissions(Permissions permissions)
+bool QFile::setPermissions(Permissions permissions)
{
- Q_D(QFile);
- if (d->engine()->setPermissions(permissions)) {
- unsetError();
- return true;
- }
- d->setError(QFile::PermissionsError, d->fileEngine->errorString());
- return false;
+ return QFileDevice::setPermissions(permissions); // for now
}
/*!
@@ -1341,354 +1084,12 @@ QFile::setPermissions(const QString &fileName, Permissions permissions)
return QFile(fileName).setPermissions(permissions);
}
-static inline qint64 _qfile_writeData(QAbstractFileEngine *engine, QRingBuffer *buffer)
-{
- qint64 ret = engine->write(buffer->readPointer(), buffer->nextDataBlockSize());
- if (ret > 0)
- buffer->free(ret);
- return ret;
-}
-
-/*!
- Flushes any buffered data to the file. Returns true if successful;
- otherwise returns false.
-*/
-
-bool
-QFile::flush()
-{
- Q_D(QFile);
- if (!d->fileEngine) {
- qWarning("QFile::flush: No file engine. Is IODevice open?");
- return false;
- }
-
- if (!d->writeBuffer.isEmpty()) {
- qint64 size = d->writeBuffer.size();
- if (_qfile_writeData(d->fileEngine, &d->writeBuffer) != size) {
- QFile::FileError err = d->fileEngine->error();
- if(err == QFile::UnspecifiedError)
- err = QFile::WriteError;
- d->setError(err, d->fileEngine->errorString());
- return false;
- }
- }
-
- if (!d->fileEngine->flush()) {
- QFile::FileError err = d->fileEngine->error();
- if(err == QFile::UnspecifiedError)
- err = QFile::WriteError;
- d->setError(err, d->fileEngine->errorString());
- return false;
- }
- return true;
-}
-
-/*!
- Calls QFile::flush() and closes the file. Errors from flush are ignored.
-
- \sa QIODevice::close()
-*/
-void
-QFile::close()
-{
- Q_D(QFile);
- if(!isOpen())
- return;
- bool flushed = flush();
- QIODevice::close();
-
- // reset write buffer
- d->lastWasWrite = false;
- d->writeBuffer.clear();
-
- // keep earlier error from flush
- if (d->fileEngine->close() && flushed)
- unsetError();
- else if (flushed)
- d->setError(d->fileEngine->error(), d->fileEngine->errorString());
-}
-
-/*!
- Returns the size of the file.
-
- For regular empty files on Unix (e.g. those in \c /proc), this function
- returns 0; the contents of such a file are generated on demand in response
- to you calling read().
-*/
-
-qint64 QFile::size() const
-{
- Q_D(const QFile);
- if (!d->ensureFlushed())
- return 0;
- d->cachedSize = d->engine()->size();
- return d->cachedSize;
-}
-
-/*!
- \reimp
-*/
-
-qint64 QFile::pos() const
-{
- return QIODevice::pos();
-}
-
-/*!
- Returns true if the end of the file has been reached; otherwise returns
- false.
-
- For regular empty files on Unix (e.g. those in \c /proc), this function
- returns true, since the file system reports that the size of such a file is
- 0. Therefore, you should not depend on atEnd() when reading data from such a
- file, but rather call read() until no more data can be read.
-*/
-
-bool QFile::atEnd() const
-{
- Q_D(const QFile);
-
- // If there's buffered data left, we're not at the end.
- if (!d->buffer.isEmpty())
- return false;
-
- if (!isOpen())
- return true;
-
- if (!d->ensureFlushed())
- return false;
-
- // If the file engine knows best, say what it says.
- if (d->fileEngine->supportsExtension(QAbstractFileEngine::AtEndExtension)) {
- // Check if the file engine supports AtEndExtension, and if it does,
- // check if the file engine claims to be at the end.
- return d->fileEngine->atEnd();
- }
-
- // if it looks like we are at the end, or if size is not cached,
- // fall through to bytesAvailable() to make sure.
- if (pos() < d->cachedSize)
- return false;
-
- // Fall back to checking how much is available (will stat files).
- return bytesAvailable() == 0;
-}
-
-/*!
- \fn bool QFile::seek(qint64 pos)
-
- For random-access devices, this function sets the current position
- to \a pos, returning true on success, or false if an error occurred.
- For sequential devices, the default behavior is to do nothing and
- return false.
-
- Seeking beyond the end of a file:
- If the position is beyond the end of a file, then seek() shall not
- immediately extend the file. If a write is performed at this position,
- then the file shall be extended. The content of the file between the
- previous end of file and the newly written data is UNDEFINED and
- varies between platforms and file systems.
-*/
-
-bool QFile::seek(qint64 off)
-{
- Q_D(QFile);
- if (!isOpen()) {
- qWarning("QFile::seek: IODevice is not open");
- return false;
- }
-
- if (!d->ensureFlushed())
- return false;
-
- if (!d->fileEngine->seek(off) || !QIODevice::seek(off)) {
- QFile::FileError err = d->fileEngine->error();
- if(err == QFile::UnspecifiedError)
- err = QFile::PositionError;
- d->setError(err, d->fileEngine->errorString());
- return false;
- }
- unsetError();
- return true;
-}
-
-/*!
- \reimp
-*/
-qint64 QFile::readLineData(char *data, qint64 maxlen)
-{
- Q_D(QFile);
- if (!d->ensureFlushed())
- return -1;
-
- qint64 read;
- if (d->fileEngine->supportsExtension(QAbstractFileEngine::FastReadLineExtension)) {
- read = d->fileEngine->readLine(data, maxlen);
- } else {
- // Fall back to QIODevice's readLine implementation if the engine
- // cannot do it faster.
- read = QIODevice::readLineData(data, maxlen);
- }
-
- if (read < maxlen) {
- // failed to read all requested, may be at the end of file, stop caching size so that it's rechecked
- d->cachedSize = 0;
- }
-
- return read;
-}
-
-/*!
- \reimp
-*/
-
-qint64 QFile::readData(char *data, qint64 len)
-{
- Q_D(QFile);
- unsetError();
- if (!d->ensureFlushed())
- return -1;
-
- qint64 read = d->fileEngine->read(data, len);
- if(read < 0) {
- QFile::FileError err = d->fileEngine->error();
- if(err == QFile::UnspecifiedError)
- err = QFile::ReadError;
- d->setError(err, d->fileEngine->errorString());
- }
-
- if (read < len) {
- // failed to read all requested, may be at the end of file, stop caching size so that it's rechecked
- d->cachedSize = 0;
- }
-
- return read;
-}
-
-/*!
- \internal
-*/
-bool QFilePrivate::putCharHelper(char c)
-{
-#ifdef QT_NO_QOBJECT
- return QIODevicePrivate::putCharHelper(c);
-#else
-
- // Cutoff for code that doesn't only touch the buffer.
- int writeBufferSize = writeBuffer.size();
- if ((openMode & QIODevice::Unbuffered) || writeBufferSize + 1 >= QFILE_WRITEBUFFER_SIZE
-#ifdef Q_OS_WIN
- || ((openMode & QIODevice::Text) && c == '\n' && writeBufferSize + 2 >= QFILE_WRITEBUFFER_SIZE)
-#endif
- ) {
- return QIODevicePrivate::putCharHelper(c);
- }
-
- if (!(openMode & QIODevice::WriteOnly)) {
- if (openMode == QIODevice::NotOpen)
- qWarning("QIODevice::putChar: Closed device");
- else
- qWarning("QIODevice::putChar: ReadOnly device");
- return false;
- }
-
- // Make sure the device is positioned correctly.
- const bool sequential = isSequential();
- if (pos != devicePos && !sequential && !q_func()->seek(pos))
- return false;
-
- lastWasWrite = true;
-
- int len = 1;
-#ifdef Q_OS_WIN
- if ((openMode & QIODevice::Text) && c == '\n') {
- ++len;
- *writeBuffer.reserve(1) = '\r';
- }
-#endif
-
- // Write to buffer.
- *writeBuffer.reserve(1) = c;
-
- if (!sequential) {
- pos += len;
- devicePos += len;
- if (!buffer.isEmpty())
- buffer.skip(len);
- }
-
- return true;
-#endif
-}
-
/*!
\reimp
*/
-
-qint64
-QFile::writeData(const char *data, qint64 len)
-{
- Q_D(QFile);
- unsetError();
- d->lastWasWrite = true;
- bool buffered = !(d->openMode & Unbuffered);
-
- // Flush buffered data if this read will overflow.
- if (buffered && (d->writeBuffer.size() + len) > QFILE_WRITEBUFFER_SIZE) {
- if (!flush())
- return -1;
- }
-
- // Write directly to the engine if the block size is larger than
- // the write buffer size.
- if (!buffered || len > QFILE_WRITEBUFFER_SIZE) {
- qint64 ret = d->fileEngine->write(data, len);
- if(ret < 0) {
- QFile::FileError err = d->fileEngine->error();
- if(err == QFile::UnspecifiedError)
- err = QFile::WriteError;
- d->setError(err, d->fileEngine->errorString());
- }
- return ret;
- }
-
- // Write to the buffer.
- char *writePointer = d->writeBuffer.reserve(len);
- if (len == 1)
- *writePointer = *data;
- else
- ::memcpy(writePointer, data, len);
- return len;
-}
-
-/*!
- Returns the file error status.
-
- The I/O device status returns an error code. For example, if open()
- returns false, or a read/write operation returns -1, this function can
- be called to find out the reason why the operation failed.
-
- \sa unsetError()
-*/
-
-QFile::FileError
-QFile::error() const
-{
- Q_D(const QFile);
- return d->error;
-}
-
-/*!
- Sets the file's error to QFile::NoError.
-
- \sa error()
-*/
-void
-QFile::unsetError()
+qint64 QFile::size() const
{
- Q_D(QFile);
- d->setError(QFile::NoError);
+ return QFileDevice::size(); // for now
}
QT_END_NAMESPACE
diff --git a/src/corelib/io/qfile.h b/src/corelib/io/qfile.h
index 7f370d4214..0ee8f39d95 100644
--- a/src/corelib/io/qfile.h
+++ b/src/corelib/io/qfile.h
@@ -42,7 +42,7 @@
#ifndef QFILE_H
#define QFILE_H
-#include <QtCore/qiodevice.h>
+#include <QtCore/qfiledevice.h>
#include <QtCore/qstring.h>
#include <stdio.h>
@@ -57,7 +57,7 @@ QT_BEGIN_NAMESPACE
class QTemporaryFile;
class QFilePrivate;
-class Q_CORE_EXPORT QFile : public QIODevice
+class Q_CORE_EXPORT QFile : public QFileDevice
{
#ifndef QT_NO_QOBJECT
Q_OBJECT
@@ -65,39 +65,6 @@ class Q_CORE_EXPORT QFile : public QIODevice
Q_DECLARE_PRIVATE(QFile)
public:
-
- enum FileError {
- NoError = 0,
- ReadError = 1,
- WriteError = 2,
- FatalError = 3,
- ResourceError = 4,
- OpenError = 5,
- AbortError = 6,
- TimeOutError = 7,
- UnspecifiedError = 8,
- RemoveError = 9,
- RenameError = 10,
- PositionError = 11,
- ResizeError = 12,
- PermissionsError = 13,
- CopyError = 14
- };
-
- enum Permission {
- ReadOwner = 0x4000, WriteOwner = 0x2000, ExeOwner = 0x1000,
- ReadUser = 0x0400, WriteUser = 0x0200, ExeUser = 0x0100,
- ReadGroup = 0x0040, WriteGroup = 0x0020, ExeGroup = 0x0010,
- ReadOther = 0x0004, WriteOther = 0x0002, ExeOther = 0x0001
- };
- Q_DECLARE_FLAGS(Permissions, Permission)
-
- enum FileHandleFlag {
- AutoCloseHandle = 0x0001,
- DontCloseHandle = 0
- };
- Q_DECLARE_FLAGS(FileHandleFlags, FileHandleFlag)
-
QFile();
QFile(const QString &name);
#ifndef QT_NO_QOBJECT
@@ -106,9 +73,6 @@ public:
#endif
~QFile();
- FileError error() const;
- void unsetError();
-
QString fileName() const;
void setFileName(const QString &name);
@@ -141,18 +105,11 @@ public:
bool copy(const QString &newName);
static bool copy(const QString &fileName, const QString &newName);
- bool isSequential() const;
-
bool open(OpenMode flags);
bool open(FILE *f, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);
bool open(int fd, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);
- virtual void close();
qint64 size() const;
- qint64 pos() const;
- bool seek(qint64 offset);
- bool atEnd() const;
- bool flush();
bool resize(qint64 sz);
static bool resize(const QString &filename, qint64 sz);
@@ -162,15 +119,6 @@ public:
bool setPermissions(Permissions permissionSpec);
static bool setPermissions(const QString &filename, Permissions permissionSpec);
- int handle() const;
-
- enum MemoryMapFlags {
- NoOptions = 0
- };
-
- uchar *map(qint64 offset, qint64 size, MemoryMapFlags flags = NoOptions);
- bool unmap(uchar *address);
-
protected:
#ifdef QT_NO_QOBJECT
QFile(QFilePrivate &dd);
@@ -178,17 +126,11 @@ protected:
QFile(QFilePrivate &dd, QObject *parent = 0);
#endif
- qint64 readData(char *data, qint64 maxlen);
- qint64 writeData(const char *data, qint64 len);
- qint64 readLineData(char *data, qint64 maxlen);
-
private:
friend class QTemporaryFile;
Q_DISABLE_COPY(QFile)
};
-Q_DECLARE_OPERATORS_FOR_FLAGS(QFile::Permissions)
-
QT_END_NAMESPACE
QT_END_HEADER
diff --git a/src/corelib/io/qfile_p.h b/src/corelib/io/qfile_p.h
index 3d2d3d678b..575d7d14b9 100644
--- a/src/corelib/io/qfile_p.h
+++ b/src/corelib/io/qfile_p.h
@@ -53,15 +53,13 @@
// We mean it.
//
-#include "private/qabstractfileengine_p.h"
-#include "private/qiodevice_p.h"
-#include "private/qringbuffer_p.h"
+#include "private/qfiledevice_p.h"
QT_BEGIN_NAMESPACE
class QTemporaryFile;
-class QFilePrivate : public QIODevicePrivate
+class QFilePrivate : public QFileDevicePrivate
{
Q_DECLARE_PUBLIC(QFile)
friend class QTemporaryFile;
@@ -76,20 +74,6 @@ protected:
virtual QAbstractFileEngine *engine() const;
QString fileName;
- mutable QAbstractFileEngine *fileEngine;
-
- bool lastWasWrite;
- QRingBuffer writeBuffer;
- inline bool ensureFlushed() const;
-
- bool putCharHelper(char c);
-
- QFile::FileError error;
- void setError(QFile::FileError err);
- void setError(QFile::FileError err, const QString &errorString);
- void setError(QFile::FileError err, int errNum);
-
- mutable qint64 cachedSize;
private:
static QFile::EncoderFn encoder;
diff --git a/src/corelib/io/qfiledevice.cpp b/src/corelib/io/qfiledevice.cpp
new file mode 100644
index 0000000000..17eedb0bdd
--- /dev/null
+++ b/src/corelib/io/qfiledevice.cpp
@@ -0,0 +1,736 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qplatformdefs.h"
+#include "qfiledevice.h"
+#include "qfiledevice_p.h"
+#include "qfsfileengine_p.h"
+
+#ifdef QT_NO_QOBJECT
+#define tr(X) QString::fromLatin1(X)
+#endif
+
+QT_BEGIN_NAMESPACE
+
+static const int QFILE_WRITEBUFFER_SIZE = 16384;
+
+QFileDevicePrivate::QFileDevicePrivate()
+ : fileEngine(0), lastWasWrite(false),
+ writeBuffer(QFILE_WRITEBUFFER_SIZE), error(QFile::NoError),
+ cachedSize(0)
+{
+}
+
+QFileDevicePrivate::~QFileDevicePrivate()
+{
+ delete fileEngine;
+ fileEngine = 0;
+}
+
+QAbstractFileEngine * QFileDevicePrivate::engine() const
+{
+ if (!fileEngine)
+ fileEngine = new QFSFileEngine;
+ return fileEngine;
+}
+
+void QFileDevicePrivate::setError(QFileDevice::FileError err)
+{
+ error = err;
+ errorString.clear();
+}
+
+void QFileDevicePrivate::setError(QFileDevice::FileError err, const QString &errStr)
+{
+ error = err;
+ errorString = errStr;
+}
+
+void QFileDevicePrivate::setError(QFileDevice::FileError err, int errNum)
+{
+ error = err;
+ errorString = qt_error_string(errNum);
+}
+
+/*!
+ \enum QFileDevice::FileError
+
+ This enum describes the errors that may be returned by the error()
+ function.
+
+ \value NoError No error occurred.
+ \value ReadError An error occurred when reading from the file.
+ \value WriteError An error occurred when writing to the file.
+ \value FatalError A fatal error occurred.
+ \value ResourceError
+ \value OpenError The file could not be opened.
+ \value AbortError The operation was aborted.
+ \value TimeOutError A timeout occurred.
+ \value UnspecifiedError An unspecified error occurred.
+ \value RemoveError The file could not be removed.
+ \value RenameError The file could not be renamed.
+ \value PositionError The position in the file could not be changed.
+ \value ResizeError The file could not be resized.
+ \value PermissionsError The file could not be accessed.
+ \value CopyError The file could not be copied.
+
+ \omitvalue ConnectError
+*/
+
+/*!
+ \enum QFileDevice::Permission
+
+ This enum is used by the permission() function to report the
+ permissions and ownership of a file. The values may be OR-ed
+ together to test multiple permissions and ownership values.
+
+ \value ReadOwner The file is readable by the owner of the file.
+ \value WriteOwner The file is writable by the owner of the file.
+ \value ExeOwner The file is executable by the owner of the file.
+ \value ReadUser The file is readable by the user.
+ \value WriteUser The file is writable by the user.
+ \value ExeUser The file is executable by the user.
+ \value ReadGroup The file is readable by the group.
+ \value WriteGroup The file is writable by the group.
+ \value ExeGroup The file is executable by the group.
+ \value ReadOther The file is readable by anyone.
+ \value WriteOther The file is writable by anyone.
+ \value ExeOther The file is executable by anyone.
+
+ \warning Because of differences in the platforms supported by Qt,
+ the semantics of ReadUser, WriteUser and ExeUser are
+ platform-dependent: On Unix, the rights of the owner of the file
+ are returned and on Windows the rights of the current user are
+ returned. This behavior might change in a future Qt version.
+
+ Note that Qt does not by default check for permissions on NTFS
+ file systems, as this may decrease the performance of file
+ handling considerably. It is possible to force permission checking
+ on NTFS by including the following code in your source:
+
+ \snippet doc/src/snippets/ntfsp.cpp 0
+
+ Permission checking is then turned on and off by incrementing and
+ decrementing \c qt_ntfs_permission_lookup by 1.
+
+ \snippet doc/src/snippets/ntfsp.cpp 1
+*/
+
+//************* QFileDevice
+
+/*!
+ \class QFileDevice
+ \since 5.0
+
+ \brief The QFileDevice class provides an interface for reading from and writing to open files.
+
+ \ingroup io
+
+ \reentrant
+
+ QFileDevice is the base class for I/O devices that can read and write text and binary files
+ and \l{The Qt Resource System}{resources}. QFile offers the main functionality,
+ QFileDevice serves as a base class for sharing functionality with other file devices such
+ as QTemporaryFile, by providing all the operations that can be done on files that have
+ been opened by QFile or QTemporaryFile.
+
+ \sa QFile, QTemporaryFile
+*/
+
+/*!
+ \enum QFileDevice::FileHandleFlag
+
+ This enum is used when opening a file to specify additional
+ options which only apply to files and not to a generic
+ QIODevice.
+
+ \value AutoCloseHandle The file handle passed into open() should be
+ closed by close(), the default behavior is that close just flushes
+ the file and the application is responsible for closing the file handle.
+ When opening a file by name, this flag is ignored as Qt always owns the
+ file handle and must close it.
+ \value DontCloseHandle If not explicitly closed, the underlying file
+ handle is left open when the QFile object is destroyed.
+ */
+
+#ifdef QT_NO_QOBJECT
+QFileDevice::QFileDevice()
+ : QIODevice(*new QFileDevicePrivate)
+{
+}
+QFileDevice::QFileDevice(QFileDevicePrivate &dd)
+ : QIODevice(dd)
+{
+}
+#else
+/*!
+ \internal
+*/
+QFileDevice::QFileDevice()
+ : QIODevice(*new QFileDevicePrivate, 0)
+{
+}
+/*!
+ \internal
+*/
+QFileDevice::QFileDevice(QObject *parent)
+ : QIODevice(*new QFileDevicePrivate, parent)
+{
+}
+/*!
+ \internal
+*/
+QFileDevice::QFileDevice(QFileDevicePrivate &dd, QObject *parent)
+ : QIODevice(dd, parent)
+{
+}
+#endif
+
+/*!
+ Destroys the file device, closing it if necessary.
+*/
+QFileDevice::~QFileDevice()
+{
+ close();
+}
+
+/*!
+ Returns true if the file can only be manipulated sequentially;
+ otherwise returns false.
+
+ Most files support random-access, but some special files may not.
+
+ \sa QIODevice::isSequential()
+*/
+bool QFileDevice::isSequential() const
+{
+ Q_D(const QFileDevice);
+ return d->fileEngine && d->fileEngine->isSequential();
+}
+
+/*!
+ Returns the file handle of the file.
+
+ This is a small positive integer, suitable for use with C library
+ functions such as fdopen() and fcntl(). On systems that use file
+ descriptors for sockets (i.e. Unix systems, but not Windows) the handle
+ can be used with QSocketNotifier as well.
+
+ If the file is not open, or there is an error, handle() returns -1.
+
+ This function is not supported on Windows CE.
+
+ \sa QSocketNotifier
+*/
+int QFileDevice::handle() const
+{
+ Q_D(const QFileDevice);
+ if (!isOpen() || !d->fileEngine)
+ return -1;
+
+ return d->fileEngine->handle();
+}
+
+/*!
+ Returns the name of the file.
+ The default implementation in QFileDevice returns QString().
+*/
+QString QFileDevice::fileName() const
+{
+ return QString();
+}
+
+static inline qint64 _qfile_writeData(QAbstractFileEngine *engine, QRingBuffer *buffer)
+{
+ qint64 ret = engine->write(buffer->readPointer(), buffer->nextDataBlockSize());
+ if (ret > 0)
+ buffer->free(ret);
+ return ret;
+}
+
+/*!
+ Flushes any buffered data to the file. Returns true if successful;
+ otherwise returns false.
+*/
+bool QFileDevice::flush()
+{
+ Q_D(QFileDevice);
+ if (!d->fileEngine) {
+ qWarning("QFileDevice::flush: No file engine. Is IODevice open?");
+ return false;
+ }
+
+ if (!d->writeBuffer.isEmpty()) {
+ qint64 size = d->writeBuffer.size();
+ if (_qfile_writeData(d->fileEngine, &d->writeBuffer) != size) {
+ QFileDevice::FileError err = d->fileEngine->error();
+ if (err == QFileDevice::UnspecifiedError)
+ err = QFileDevice::WriteError;
+ d->setError(err, d->fileEngine->errorString());
+ return false;
+ }
+ }
+
+ if (!d->fileEngine->flush()) {
+ QFileDevice::FileError err = d->fileEngine->error();
+ if (err == QFileDevice::UnspecifiedError)
+ err = QFileDevice::WriteError;
+ d->setError(err, d->fileEngine->errorString());
+ return false;
+ }
+ return true;
+}
+
+/*!
+ Calls QFileDevice::flush() and closes the file. Errors from flush are ignored.
+
+ \sa QIODevice::close()
+*/
+void QFileDevice::close()
+{
+ Q_D(QFileDevice);
+ if (!isOpen())
+ return;
+ bool flushed = flush();
+ QIODevice::close();
+
+ // reset write buffer
+ d->lastWasWrite = false;
+ d->writeBuffer.clear();
+
+ // keep earlier error from flush
+ if (d->fileEngine->close() && flushed)
+ unsetError();
+ else if (flushed)
+ d->setError(d->fileEngine->error(), d->fileEngine->errorString());
+}
+
+/*!
+ \reimp
+*/
+qint64 QFileDevice::pos() const
+{
+ return QIODevice::pos();
+}
+
+/*!
+ Returns true if the end of the file has been reached; otherwise returns
+ false.
+
+ For regular empty files on Unix (e.g. those in \c /proc), this function
+ returns true, since the file system reports that the size of such a file is
+ 0. Therefore, you should not depend on atEnd() when reading data from such a
+ file, but rather call read() until no more data can be read.
+*/
+bool QFileDevice::atEnd() const
+{
+ Q_D(const QFileDevice);
+
+ // If there's buffered data left, we're not at the end.
+ if (!d->buffer.isEmpty())
+ return false;
+
+ if (!isOpen())
+ return true;
+
+ if (!d->ensureFlushed())
+ return false;
+
+ // If the file engine knows best, say what it says.
+ if (d->fileEngine->supportsExtension(QAbstractFileEngine::AtEndExtension)) {
+ // Check if the file engine supports AtEndExtension, and if it does,
+ // check if the file engine claims to be at the end.
+ return d->fileEngine->atEnd();
+ }
+
+ // if it looks like we are at the end, or if size is not cached,
+ // fall through to bytesAvailable() to make sure.
+ if (pos() < d->cachedSize)
+ return false;
+
+ // Fall back to checking how much is available (will stat files).
+ return bytesAvailable() == 0;
+}
+
+/*!
+ \fn bool QFileDevice::seek(qint64 pos)
+
+ For random-access devices, this function sets the current position
+ to \a pos, returning true on success, or false if an error occurred.
+ For sequential devices, the default behavior is to do nothing and
+ return false.
+
+ Seeking beyond the end of a file:
+ If the position is beyond the end of a file, then seek() shall not
+ immediately extend the file. If a write is performed at this position,
+ then the file shall be extended. The content of the file between the
+ previous end of file and the newly written data is UNDEFINED and
+ varies between platforms and file systems.
+*/
+bool QFileDevice::seek(qint64 off)
+{
+ Q_D(QFileDevice);
+ if (!isOpen()) {
+ qWarning("QFileDevice::seek: IODevice is not open");
+ return false;
+ }
+
+ if (!d->ensureFlushed())
+ return false;
+
+ if (!d->fileEngine->seek(off) || !QIODevice::seek(off)) {
+ QFileDevice::FileError err = d->fileEngine->error();
+ if (err == QFileDevice::UnspecifiedError)
+ err = QFileDevice::PositionError;
+ d->setError(err, d->fileEngine->errorString());
+ return false;
+ }
+ unsetError();
+ return true;
+}
+
+/*!
+ \reimp
+*/
+qint64 QFileDevice::readLineData(char *data, qint64 maxlen)
+{
+ Q_D(QFileDevice);
+ if (!d->ensureFlushed())
+ return -1;
+
+ qint64 read;
+ if (d->fileEngine->supportsExtension(QAbstractFileEngine::FastReadLineExtension)) {
+ read = d->fileEngine->readLine(data, maxlen);
+ } else {
+ // Fall back to QIODevice's readLine implementation if the engine
+ // cannot do it faster.
+ read = QIODevice::readLineData(data, maxlen);
+ }
+
+ if (read < maxlen) {
+ // failed to read all requested, may be at the end of file, stop caching size so that it's rechecked
+ d->cachedSize = 0;
+ }
+
+ return read;
+}
+
+/*!
+ \reimp
+*/
+qint64 QFileDevice::readData(char *data, qint64 len)
+{
+ Q_D(QFileDevice);
+ unsetError();
+ if (!d->ensureFlushed())
+ return -1;
+
+ const qint64 read = d->fileEngine->read(data, len);
+ if (read < 0) {
+ QFileDevice::FileError err = d->fileEngine->error();
+ if (err == QFileDevice::UnspecifiedError)
+ err = QFileDevice::ReadError;
+ d->setError(err, d->fileEngine->errorString());
+ }
+
+ if (read < len) {
+ // failed to read all requested, may be at the end of file, stop caching size so that it's rechecked
+ d->cachedSize = 0;
+ }
+
+ return read;
+}
+
+/*!
+ \internal
+*/
+bool QFileDevicePrivate::putCharHelper(char c)
+{
+#ifdef QT_NO_QOBJECT
+ return QIODevicePrivate::putCharHelper(c);
+#else
+
+ // Cutoff for code that doesn't only touch the buffer.
+ int writeBufferSize = writeBuffer.size();
+ if ((openMode & QIODevice::Unbuffered) || writeBufferSize + 1 >= QFILE_WRITEBUFFER_SIZE
+#ifdef Q_OS_WIN
+ || ((openMode & QIODevice::Text) && c == '\n' && writeBufferSize + 2 >= QFILE_WRITEBUFFER_SIZE)
+#endif
+ ) {
+ return QIODevicePrivate::putCharHelper(c);
+ }
+
+ if (!(openMode & QIODevice::WriteOnly)) {
+ if (openMode == QIODevice::NotOpen)
+ qWarning("QIODevice::putChar: Closed device");
+ else
+ qWarning("QIODevice::putChar: ReadOnly device");
+ return false;
+ }
+
+ // Make sure the device is positioned correctly.
+ const bool sequential = isSequential();
+ if (pos != devicePos && !sequential && !q_func()->seek(pos))
+ return false;
+
+ lastWasWrite = true;
+
+ int len = 1;
+#ifdef Q_OS_WIN
+ if ((openMode & QIODevice::Text) && c == '\n') {
+ ++len;
+ *writeBuffer.reserve(1) = '\r';
+ }
+#endif
+
+ // Write to buffer.
+ *writeBuffer.reserve(1) = c;
+
+ if (!sequential) {
+ pos += len;
+ devicePos += len;
+ if (!buffer.isEmpty())
+ buffer.skip(len);
+ }
+
+ return true;
+#endif
+}
+
+/*!
+ \reimp
+*/
+qint64 QFileDevice::writeData(const char *data, qint64 len)
+{
+ Q_D(QFileDevice);
+ unsetError();
+ d->lastWasWrite = true;
+ bool buffered = !(d->openMode & Unbuffered);
+
+ // Flush buffered data if this read will overflow.
+ if (buffered && (d->writeBuffer.size() + len) > QFILE_WRITEBUFFER_SIZE) {
+ if (!flush())
+ return -1;
+ }
+
+ // Write directly to the engine if the block size is larger than
+ // the write buffer size.
+ if (!buffered || len > QFILE_WRITEBUFFER_SIZE) {
+ const qint64 ret = d->fileEngine->write(data, len);
+ if (ret < 0) {
+ QFileDevice::FileError err = d->fileEngine->error();
+ if (err == QFileDevice::UnspecifiedError)
+ err = QFileDevice::WriteError;
+ d->setError(err, d->fileEngine->errorString());
+ }
+ return ret;
+ }
+
+ // Write to the buffer.
+ char *writePointer = d->writeBuffer.reserve(len);
+ if (len == 1)
+ *writePointer = *data;
+ else
+ ::memcpy(writePointer, data, len);
+ return len;
+}
+
+/*!
+ Returns the file error status.
+
+ The I/O device status returns an error code. For example, if open()
+ returns false, or a read/write operation returns -1, this function can
+ be called to find out the reason why the operation failed.
+
+ \sa unsetError()
+*/
+QFileDevice::FileError QFileDevice::error() const
+{
+ Q_D(const QFileDevice);
+ return d->error;
+}
+
+/*!
+ Sets the file's error to QFileDevice::NoError.
+
+ \sa error()
+*/
+void QFileDevice::unsetError()
+{
+ Q_D(QFileDevice);
+ d->setError(QFileDevice::NoError);
+}
+
+/*!
+ Returns the size of the file.
+
+ For regular empty files on Unix (e.g. those in \c /proc), this function
+ returns 0; the contents of such a file are generated on demand in response
+ to you calling read().
+*/
+qint64 QFileDevice::size() const
+{
+ Q_D(const QFileDevice);
+ if (!d->ensureFlushed())
+ return 0;
+ d->cachedSize = d->engine()->size();
+ return d->cachedSize;
+}
+
+/*!
+ Sets the file size (in bytes) \a sz. Returns true if the file if the
+ resize succeeds; false otherwise. If \a sz is larger than the file
+ currently is the new bytes will be set to 0, if \a sz is smaller the
+ file is simply truncated.
+
+ \sa size()
+*/
+bool QFileDevice::resize(qint64 sz)
+{
+ Q_D(QFileDevice);
+ if (!d->ensureFlushed())
+ return false;
+ d->engine();
+ if (isOpen() && d->fileEngine->pos() > sz)
+ seek(sz);
+ if (d->fileEngine->setSize(sz)) {
+ unsetError();
+ d->cachedSize = sz;
+ return true;
+ }
+ d->cachedSize = 0;
+ d->setError(QFile::ResizeError, d->fileEngine->errorString());
+ return false;
+}
+
+/*!
+ Returns the complete OR-ed together combination of
+ QFile::Permission for the file.
+
+ \sa setPermissions()
+*/
+QFile::Permissions QFileDevice::permissions() const
+{
+ Q_D(const QFileDevice);
+ QAbstractFileEngine::FileFlags perms = d->engine()->fileFlags(QAbstractFileEngine::PermsMask) & QAbstractFileEngine::PermsMask;
+ return QFile::Permissions((int)perms); //ewww
+}
+
+/*!
+ Sets the permissions for the file to the \a permissions specified.
+ Returns true if successful, or false if the permissions cannot be
+ modified.
+
+ \sa permissions()
+*/
+bool QFileDevice::setPermissions(Permissions permissions)
+{
+ Q_D(QFileDevice);
+ if (d->engine()->setPermissions(permissions)) {
+ unsetError();
+ return true;
+ }
+ d->setError(QFile::PermissionsError, d->fileEngine->errorString());
+ return false;
+}
+
+/*!
+ \enum QFileDevice::MemoryMapFlags
+ \since 4.4
+
+ This enum describes special options that may be used by the map()
+ function.
+
+ \value NoOptions No options.
+*/
+
+/*!
+ Maps \a size bytes of the file into memory starting at \a offset. A file
+ should be open for a map to succeed but the file does not need to stay
+ open after the memory has been mapped. When the QFile is destroyed
+ or a new file is opened with this object, any maps that have not been
+ unmapped will automatically be unmapped.
+
+ Any mapping options can be passed through \a flags.
+
+ Returns a pointer to the memory or 0 if there is an error.
+
+ \note On Windows CE 5.0 the file will be closed before mapping occurs.
+
+ \sa unmap()
+ */
+uchar *QFileDevice::map(qint64 offset, qint64 size, MemoryMapFlags flags)
+{
+ Q_D(QFileDevice);
+ if (d->engine()
+ && d->fileEngine->supportsExtension(QAbstractFileEngine::MapExtension)) {
+ unsetError();
+ uchar *address = d->fileEngine->map(offset, size, flags);
+ if (address == 0)
+ d->setError(d->fileEngine->error(), d->fileEngine->errorString());
+ return address;
+ }
+ return 0;
+}
+
+/*!
+ Unmaps the memory \a address.
+
+ Returns true if the unmap succeeds; false otherwise.
+
+ \sa map()
+ */
+bool QFileDevice::unmap(uchar *address)
+{
+ Q_D(QFileDevice);
+ if (d->engine()
+ && d->fileEngine->supportsExtension(QAbstractFileEngine::UnMapExtension)) {
+ unsetError();
+ bool success = d->fileEngine->unmap(address);
+ if (!success)
+ d->setError(d->fileEngine->error(), d->fileEngine->errorString());
+ return success;
+ }
+ d->setError(PermissionsError, tr("No file engine available or engine does not support UnMapExtension"));
+ return false;
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/io/qfiledevice.h b/src/corelib/io/qfiledevice.h
new file mode 100644
index 0000000000..bbde91842c
--- /dev/null
+++ b/src/corelib/io/qfiledevice.h
@@ -0,0 +1,147 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QFILEDEVICE_H
+#define QFILEDEVICE_H
+
+#include <QtCore/qiodevice.h>
+#include <QtCore/qstring.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+class QFileDevicePrivate;
+
+class Q_CORE_EXPORT QFileDevice : public QIODevice
+{
+#ifndef QT_NO_QOBJECT
+ Q_OBJECT
+#endif
+ Q_DECLARE_PRIVATE(QFileDevice)
+
+public:
+ enum FileError {
+ NoError = 0,
+ ReadError = 1,
+ WriteError = 2,
+ FatalError = 3,
+ ResourceError = 4,
+ OpenError = 5,
+ AbortError = 6,
+ TimeOutError = 7,
+ UnspecifiedError = 8,
+ RemoveError = 9,
+ RenameError = 10,
+ PositionError = 11,
+ ResizeError = 12,
+ PermissionsError = 13,
+ CopyError = 14
+ };
+
+ enum Permission {
+ ReadOwner = 0x4000, WriteOwner = 0x2000, ExeOwner = 0x1000,
+ ReadUser = 0x0400, WriteUser = 0x0200, ExeUser = 0x0100,
+ ReadGroup = 0x0040, WriteGroup = 0x0020, ExeGroup = 0x0010,
+ ReadOther = 0x0004, WriteOther = 0x0002, ExeOther = 0x0001
+ };
+ Q_DECLARE_FLAGS(Permissions, Permission)
+
+ enum FileHandleFlag {
+ AutoCloseHandle = 0x0001,
+ DontCloseHandle = 0
+ };
+ Q_DECLARE_FLAGS(FileHandleFlags, FileHandleFlag)
+
+ ~QFileDevice();
+
+ FileError error() const;
+ void unsetError();
+
+ virtual void close();
+
+ bool isSequential() const;
+
+ int handle() const;
+ virtual QString fileName() const;
+
+ qint64 pos() const;
+ bool seek(qint64 offset);
+ bool atEnd() const;
+ bool flush();
+
+ qint64 size() const;
+
+ virtual bool resize(qint64 sz);
+ virtual Permissions permissions() const;
+ virtual bool setPermissions(Permissions permissionSpec);
+
+ enum MemoryMapFlags {
+ NoOptions = 0
+ };
+
+ uchar *map(qint64 offset, qint64 size, MemoryMapFlags flags = NoOptions);
+ bool unmap(uchar *address);
+
+protected:
+ QFileDevice();
+#ifdef QT_NO_QOBJECT
+ QFileDevice(QFileDevicePrivate &dd);
+#else
+ explicit QFileDevice(QObject *parent);
+ QFileDevice(QFileDevicePrivate &dd, QObject *parent = 0);
+#endif
+
+ qint64 readData(char *data, qint64 maxlen);
+ qint64 writeData(const char *data, qint64 len);
+ qint64 readLineData(char *data, qint64 maxlen);
+
+private:
+ Q_DISABLE_COPY(QFileDevice)
+};
+
+Q_DECLARE_OPERATORS_FOR_FLAGS(QFileDevice::Permissions)
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif // QFILEDEVICE_H
diff --git a/src/corelib/io/qfiledevice_p.h b/src/corelib/io/qfiledevice_p.h
new file mode 100644
index 0000000000..2550cd73a2
--- /dev/null
+++ b/src/corelib/io/qfiledevice_p.h
@@ -0,0 +1,104 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QFILEDEVICE_P_H
+#define QFILEDEVICE_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "private/qiodevice_p.h"
+#include "private/qringbuffer_p.h"
+
+QT_BEGIN_NAMESPACE
+
+class QAbstractFileEngine;
+class QFSFileEngine;
+
+class QFileDevicePrivate : public QIODevicePrivate
+{
+ Q_DECLARE_PUBLIC(QFileDevice)
+protected:
+ QFileDevicePrivate();
+ ~QFileDevicePrivate();
+
+ virtual QAbstractFileEngine *engine() const;
+
+ QFileDevice::FileHandleFlags handleFlags;
+
+ mutable QAbstractFileEngine *fileEngine;
+ bool lastWasWrite;
+ QRingBuffer writeBuffer;
+ inline bool ensureFlushed() const;
+
+ bool putCharHelper(char c);
+
+ QFileDevice::FileError error;
+ void setError(QFileDevice::FileError err);
+ void setError(QFileDevice::FileError err, const QString &errorString);
+ void setError(QFileDevice::FileError err, int errNum);
+
+ mutable qint64 cachedSize;
+};
+
+inline bool QFileDevicePrivate::ensureFlushed() const
+{
+ // This function ensures that the write buffer has been flushed (const
+ // because certain const functions need to call it.
+ if (lastWasWrite) {
+ const_cast<QFileDevicePrivate *>(this)->lastWasWrite = false;
+ if (!const_cast<QFileDevice *>(q_func())->flush())
+ return false;
+ }
+ return true;
+}
+
+QT_END_NAMESPACE
+
+#endif // QFILEDEVICE_P_H
diff --git a/src/corelib/io/qtextstream.cpp b/src/corelib/io/qtextstream.cpp
index cb703df8c6..0411b463b3 100644
--- a/src/corelib/io/qtextstream.cpp
+++ b/src/corelib/io/qtextstream.cpp
@@ -696,7 +696,7 @@ void QTextStreamPrivate::flushWriteBuffer()
// flush the file
#ifndef QT_NO_QOBJECT
- QFile *file = qobject_cast<QFile *>(device);
+ QFileDevice *file = qobject_cast<QFileDevice *>(device);
bool flushed = !file || file->flush();
#else
bool flushed = true;
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp
index 0659053937..9b15c1fd98 100644
--- a/src/corelib/io/qurl.cpp
+++ b/src/corelib/io/qurl.cpp
@@ -170,6 +170,8 @@
\value RemoveQuery The query part of the URL (following a '?' character)
is removed.
\value RemoveFragment
+ \value PreferLocalFile If the URL is a local file according to isLocalFile()
+ and contains no query or fragment, a local file path is returned.
\value StripTrailingSlash The trailing slash is removed if one is present.
Note that the case folding rules in \l{RFC 3491}{Nameprep}, which QUrl
@@ -331,6 +333,7 @@ public:
void clear();
QByteArray toEncoded(QUrl::FormattingOptions options = QUrl::None) const;
+ bool isLocalFile() const;
QAtomicInt ref;
@@ -3953,6 +3956,9 @@ QByteArray QUrlPrivate::toEncoded(QUrl::FormattingOptions options) const
if (options==0x100) // private - see qHash(QUrl)
return normalized();
+ if ((options & QUrl::PreferLocalFile) && isLocalFile() && !hasQuery && !hasFragment)
+ return encodedPath;
+
QByteArray url;
if (!(options & QUrl::RemoveScheme) && !scheme.isEmpty()) {
@@ -5695,9 +5701,12 @@ QString QUrl::toString(FormattingOptions options) const
QString url;
+ const QString ourPath = path();
+ if ((options & QUrl::PreferLocalFile) && isLocalFile() && !d->hasQuery && !d->hasFragment)
+ return ourPath;
+
if (!(options & QUrl::RemoveScheme) && !d->scheme.isEmpty())
url += d->scheme + QLatin1Char(':');
- QString ourPath = path();
if ((options & QUrl::RemoveAuthority) != QUrl::RemoveAuthority) {
bool doFileScheme = d->scheme == QLatin1String("file") && ourPath.startsWith(QLatin1Char('/'));
QString tmp = d->authority(options);
@@ -5733,6 +5742,7 @@ QString QUrl::toString(FormattingOptions options) const
}
/*!
+ \since 5.0
Returns a string representation of the URL.
The output can be customized by passing flags with \a options.
@@ -5748,13 +5758,16 @@ QString QUrl::url(FormattingOptions options) const
}
/*!
+ \since 5.0
+
Returns a human-displayable string representation of the URL.
The output can be customized by passing flags with \a options.
The option RemovePassword is always enabled, since passwords
should never be shown back to users.
- The resulting QString can be passed back to a QUrl later on,
- but any password that was present initially will be lost.
+ With the default options, the resulting QString can be passed back
+ to a QUrl later on, but any password that was present initially will
+ be lost.
\sa FormattingOptions, toEncoded(), toString()
*/
@@ -6171,6 +6184,13 @@ QString QUrl::toLocalFile() const
return tmp;
}
+bool QUrlPrivate::isLocalFile() const
+{
+ if (scheme.compare(QLatin1String("file"), Qt::CaseInsensitive) != 0)
+ return false; // not file
+ return true;
+}
+
/*!
\since 4.7
Returns true if this URL is pointing to a local file path. A URL is a
@@ -6186,10 +6206,7 @@ bool QUrl::isLocalFile() const
{
if (!d) return false;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
-
- if (d->scheme.compare(QLatin1String("file"), Qt::CaseInsensitive) != 0)
- return false; // not file
- return true;
+ return d->isLocalFile();
}
/*!
diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h
index 3e5c62cd7c..fc49231594 100644
--- a/src/corelib/io/qurl.h
+++ b/src/corelib/io/qurl.h
@@ -76,6 +76,7 @@ public:
RemoveQuery = 0x40,
RemoveFragment = 0x80,
// 0x100: private: normalized
+ PreferLocalFile = 0x200,
StripTrailingSlash = 0x10000
};
diff --git a/src/corelib/itemmodels/qabstractproxymodel.h b/src/corelib/itemmodels/qabstractproxymodel.h
index 3c82199038..d80fcb683c 100644
--- a/src/corelib/itemmodels/qabstractproxymodel.h
+++ b/src/corelib/itemmodels/qabstractproxymodel.h
@@ -59,7 +59,7 @@ class Q_CORE_EXPORT QAbstractProxyModel : public QAbstractItemModel
Q_OBJECT
public:
- QAbstractProxyModel(QObject *parent = 0);
+ explicit QAbstractProxyModel(QObject *parent = 0);
~QAbstractProxyModel();
virtual void setSourceModel(QAbstractItemModel *sourceModel);
diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.h b/src/corelib/itemmodels/qsortfilterproxymodel.h
index 8c7433d307..f18b1fccb1 100644
--- a/src/corelib/itemmodels/qsortfilterproxymodel.h
+++ b/src/corelib/itemmodels/qsortfilterproxymodel.h
@@ -73,7 +73,7 @@ class Q_CORE_EXPORT QSortFilterProxyModel : public QAbstractProxyModel
Q_PROPERTY(int filterRole READ filterRole WRITE setFilterRole)
public:
- QSortFilterProxyModel(QObject *parent = 0);
+ explicit QSortFilterProxyModel(QObject *parent = 0);
~QSortFilterProxyModel();
void setSourceModel(QAbstractItemModel *sourceModel);
diff --git a/src/corelib/itemmodels/qstringlistmodel.h b/src/corelib/itemmodels/qstringlistmodel.h
index d8b3f87f4a..a6bc4e7bd3 100644
--- a/src/corelib/itemmodels/qstringlistmodel.h
+++ b/src/corelib/itemmodels/qstringlistmodel.h
@@ -57,7 +57,7 @@ class Q_CORE_EXPORT QStringListModel : public QAbstractListModel
Q_OBJECT
public:
explicit QStringListModel(QObject *parent = 0);
- QStringListModel(const QStringList &strings, QObject *parent = 0);
+ explicit QStringListModel(const QStringList &strings, QObject *parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
diff --git a/src/corelib/json/qjsonvalue.cpp b/src/corelib/json/qjsonvalue.cpp
index b4a689da60..2eedef67ce 100644
--- a/src/corelib/json/qjsonvalue.cpp
+++ b/src/corelib/json/qjsonvalue.cpp
@@ -400,7 +400,7 @@ QString QJsonValue::toString() const
if (t != String)
return QString();
stringData->ref.ref(); // the constructor below doesn't add a ref.
- return QString(*(const QConstStringData<1> *)stringData);
+ return QString(*(const QStaticStringData<1> *)stringData);
}
/*!
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp
index 967ed447d5..c901bc142e 100644
--- a/src/corelib/kernel/qcoreapplication.cpp
+++ b/src/corelib/kernel/qcoreapplication.cpp
@@ -1513,26 +1513,29 @@ void QCoreApplication::quit()
generated by \l{Qt Designer} provide a \c retranslateUi() function that can be
called.
+ The function returns true on success and false on failure.
+
\sa removeTranslator() translate() QTranslator::load() {Dynamic Translation}
*/
-void QCoreApplication::installTranslator(QTranslator *translationFile)
+bool QCoreApplication::installTranslator(QTranslator *translationFile)
{
if (!translationFile)
- return;
+ return false;
if (!QCoreApplicationPrivate::checkInstance("installTranslator"))
- return;
+ return false;
QCoreApplicationPrivate *d = self->d_func();
d->translators.prepend(translationFile);
#ifndef QT_NO_TRANSLATION_BUILDER
if (translationFile->isEmpty())
- return;
+ return false;
#endif
QEvent ev(QEvent::LanguageChange);
QCoreApplication::sendEvent(self, &ev);
+ return true;
}
/*!
@@ -1540,20 +1543,24 @@ void QCoreApplication::installTranslator(QTranslator *translationFile)
translation files used by this application. (It does not delete the
translation file from the file system.)
+ The function returns true on success and false on failure.
+
\sa installTranslator() translate(), QObject::tr()
*/
-void QCoreApplication::removeTranslator(QTranslator *translationFile)
+bool QCoreApplication::removeTranslator(QTranslator *translationFile)
{
if (!translationFile)
- return;
+ return false;
if (!QCoreApplicationPrivate::checkInstance("removeTranslator"))
- return;
+ return false;
QCoreApplicationPrivate *d = self->d_func();
if (d->translators.removeAll(translationFile) && !self->closingDown()) {
QEvent ev(QEvent::LanguageChange);
QCoreApplication::sendEvent(self, &ev);
+ return true;
}
+ return false;
}
static void replacePercentN(QString *result, int n)
diff --git a/src/corelib/kernel/qcoreapplication.h b/src/corelib/kernel/qcoreapplication.h
index cf76511f25..2c942f9037 100644
--- a/src/corelib/kernel/qcoreapplication.h
+++ b/src/corelib/kernel/qcoreapplication.h
@@ -134,8 +134,8 @@ public:
#endif // QT_NO_LIBRARY
#ifndef QT_NO_TRANSLATION
- static void installTranslator(QTranslator * messageFile);
- static void removeTranslator(QTranslator * messageFile);
+ static bool installTranslator(QTranslator * messageFile);
+ static bool removeTranslator(QTranslator * messageFile);
#endif
enum Encoding { UnicodeUTF8, Latin1, DefaultCodec = Latin1
#if QT_DEPRECATED_SINCE(5, 0)
diff --git a/src/corelib/kernel/qcoreevent.h b/src/corelib/kernel/qcoreevent.h
index bfd99208cc..50ab500d13 100644
--- a/src/corelib/kernel/qcoreevent.h
+++ b/src/corelib/kernel/qcoreevent.h
@@ -269,17 +269,14 @@ public:
ScrollPrepare = 204,
Scroll = 205,
- Map = 206,
- Unmap = 207,
+ Expose = 206,
- Expose = 208,
+ InputMethodQuery = 207,
+ OrientationChange = 208, // Screen orientation has changed
- InputMethodQuery = 209,
- OrientationChange = 210, // Screen orientation has changed
+ TouchCancel = 209,
- TouchCancel = 211,
-
- ThemeChange = 212,
+ ThemeChange = 210,
// 512 reserved for Qt Jambi's MetaCall event
// 513 reserved for Qt Jambi's DeleteOnMainThread event
diff --git a/src/corelib/kernel/qeventloop.cpp b/src/corelib/kernel/qeventloop.cpp
index 58e2c5cd2f..d3a64aae04 100644
--- a/src/corelib/kernel/qeventloop.cpp
+++ b/src/corelib/kernel/qeventloop.cpp
@@ -374,7 +374,7 @@ private:
\brief The QEventLoopLocker class provides a means to quit an event loop when it is no longer needed.
The QEventLoopLocker operates on particular objects - either a QCoreApplication
- instance or a QEventLoop instance.
+ instance, a QEventLoop instance or a QThread instance.
This makes it possible to, for example, run a batch of jobs with an event loop
and exit that event loop after the last job is finished. That is accomplished
@@ -388,7 +388,7 @@ private:
*/
/*!
- Creates an event locker operating on the \p app.
+ Creates an event locker operating on the QCoreApplication.
The application will quit when there are no more QEventLoopLockers operating on it.
@@ -401,7 +401,7 @@ QEventLoopLocker::QEventLoopLocker()
}
/*!
- Creates an event locker operating on the \p app.
+ Creates an event locker operating on the \p loop.
This particular QEventLoop will quit when there are no more QEventLoopLockers operating on it.
@@ -413,6 +413,13 @@ QEventLoopLocker::QEventLoopLocker(QEventLoop *loop)
}
+/*!
+ Creates an event locker operating on the \p thread.
+
+ This particular QThread will quit when there are no more QEventLoopLockers operating on it.
+
+ \sa QThread::quit()
+ */
QEventLoopLocker::QEventLoopLocker(QThread *thread)
: d_ptr(new QEventLoopLockerPrivate(static_cast<QThreadPrivate*>(QObjectPrivate::get(thread))))
{
diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp
index 428c2629ff..75dbb49c81 100644
--- a/src/corelib/kernel/qmetaobject.cpp
+++ b/src/corelib/kernel/qmetaobject.cpp
@@ -146,6 +146,78 @@ QT_BEGIN_NAMESPACE
static inline const QMetaObjectPrivate *priv(const uint* data)
{ return reinterpret_cast<const QMetaObjectPrivate*>(data); }
+static inline const QByteArrayData &stringData(const QMetaObject *mo, int index)
+{
+ Q_ASSERT(priv(mo->d.data)->revision >= 7);
+ const QByteArrayData &data = mo->d.stringdata[index];
+ Q_ASSERT(data.ref.isStatic());
+ Q_ASSERT(data.alloc == 0);
+ Q_ASSERT(data.capacityReserved == 0);
+ Q_ASSERT(data.size >= 0);
+ return data;
+}
+
+static inline QByteArray toByteArray(const QByteArrayData &d)
+{
+ return QByteArray(reinterpret_cast<const QStaticByteArrayData<0> &>(d));
+}
+
+static inline const char *rawStringData(const QMetaObject *mo, int index)
+{
+ return stringData(mo, index).data();
+}
+
+static inline int stringSize(const QMetaObject *mo, int index)
+{
+ return stringData(mo, index).size;
+}
+
+static inline QByteArray typeNameFromTypeInfo(const QMetaObject *mo, uint typeInfo)
+{
+ if (typeInfo & IsUnresolvedType) {
+ return toByteArray(stringData(mo, typeInfo & TypeNameIndexMask));
+ } else {
+ // ### Use the QMetaType::typeName() that returns QByteArray
+ const char *t = QMetaType::typeName(typeInfo);
+ return QByteArray::fromRawData(t, qstrlen(t));
+ }
+}
+
+static inline const char *rawTypeNameFromTypeInfo(const QMetaObject *mo, uint typeInfo)
+{
+ return typeNameFromTypeInfo(mo, typeInfo).constData();
+}
+
+static inline int typeFromTypeInfo(const QMetaObject *mo, uint typeInfo)
+{
+ if (!(typeInfo & IsUnresolvedType))
+ return typeInfo;
+ return QMetaType::type(toByteArray(stringData(mo, typeInfo & TypeNameIndexMask)));
+}
+
+class QMetaMethodPrivate : public QMetaMethod
+{
+public:
+ static const QMetaMethodPrivate *get(const QMetaMethod *q)
+ { return static_cast<const QMetaMethodPrivate *>(q); }
+
+ inline QByteArray signature() const;
+ inline QByteArray name() const;
+ inline int typesDataIndex() const;
+ inline const char *rawReturnTypeName() const;
+ inline int returnType() const;
+ inline int parameterCount() const;
+ inline int parametersDataIndex() const;
+ inline uint parameterTypeInfo(int index) const;
+ inline int parameterType(int index) const;
+ inline void getParameterTypes(int *types) const;
+ inline QList<QByteArray> parameterTypes() const;
+ inline QList<QByteArray> parameterNames() const;
+ inline QByteArray tag() const;
+
+private:
+ QMetaMethodPrivate();
+};
/*!
\since 4.5
@@ -222,19 +294,11 @@ QObject *QMetaObject::newInstance(QGenericArgument val0,
int QMetaObject::static_metacall(Call cl, int idx, void **argv) const
{
const QMetaObjectExtraData *extra = reinterpret_cast<const QMetaObjectExtraData *>(d.extradata);
- if (priv(d.data)->revision >= 6) {
- if (!extra || !extra->static_metacall)
- return 0;
- extra->static_metacall(0, cl, idx, argv);
- return -1;
- } else if (priv(d.data)->revision >= 2) {
- if (!extra || !extra->static_metacall)
- return 0;
- typedef int (*OldMetacall)(QMetaObject::Call, int, void **);
- OldMetacall o = reinterpret_cast<OldMetacall>(extra->static_metacall);
- return o(cl, idx, argv);
- }
- return 0;
+ Q_ASSERT(priv(d.data)->revision >= 6);
+ if (!extra || !extra->static_metacall)
+ return 0;
+ extra->static_metacall(0, cl, idx, argv);
+ return -1;
}
/*!
@@ -249,12 +313,14 @@ int QMetaObject::metacall(QObject *object, Call cl, int idx, void **argv)
}
/*!
- \fn const char *QMetaObject::className() const
-
Returns the class name.
\sa superClass()
*/
+const char *QMetaObject::className() const
+{
+ return rawStringData(this, 0);
+}
/*!
\fn QMetaObject *QMetaObject::superClass() const
@@ -307,7 +373,7 @@ const QObject *QMetaObject::cast(const QObject *obj) const
*/
QString QMetaObject::tr(const char *s, const char *c, int n) const
{
- return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::DefaultCodec, n);
+ return QCoreApplication::translate(rawStringData(this, 0), s, c, QCoreApplication::DefaultCodec, n);
}
/*!
@@ -315,7 +381,7 @@ QString QMetaObject::tr(const char *s, const char *c, int n) const
*/
QString QMetaObject::trUtf8(const char *s, const char *c, int n) const
{
- return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::UnicodeUTF8, n);
+ return QCoreApplication::translate(rawStringData(this, 0), s, c, QCoreApplication::UnicodeUTF8, n);
}
#endif // QT_NO_TRANSLATION
@@ -413,8 +479,7 @@ int QMetaObject::classInfoOffset() const
*/
int QMetaObject::constructorCount() const
{
- if (priv(d.data)->revision < 2)
- return 0;
+ Q_ASSERT(priv(d.data)->revision >= 2);
return priv(d.data)->constructorCount;
}
@@ -495,38 +560,57 @@ int QMetaObject::classInfoCount() const
return n;
}
+// Returns true if the method defined by the given meta-object&handle
+// matches the given name, argument count and argument types, otherwise
+// returns false.
+static bool methodMatch(const QMetaObject *m, int handle,
+ const QByteArray &name, int argc,
+ const QArgumentType *types)
+{
+ Q_ASSERT(priv(m->d.data)->revision >= 7);
+ if (int(m->d.data[handle + 1]) != argc)
+ return false;
+
+ if (toByteArray(stringData(m, m->d.data[handle])) != name)
+ return false;
+
+ int paramsIndex = m->d.data[handle + 2] + 1;
+ for (int i = 0; i < argc; ++i) {
+ uint typeInfo = m->d.data[paramsIndex + i];
+ if (types[i].type()) {
+ if (types[i].type() != typeFromTypeInfo(m, typeInfo))
+ return false;
+ } else {
+ if (types[i].name() != typeNameFromTypeInfo(m, typeInfo))
+ return false;
+ }
+ }
+
+ return true;
+}
+
/** \internal
* helper function for indexOf{Method,Slot,Signal}, returns the relative index of the method within
* the baseObject
* \a MethodType might be MethodSignal or MethodSlot, or 0 to match everything.
-* \a normalizeStringData set to true if we should do a second pass for old moc generated files normalizing all the symbols.
*/
template<int MethodType>
static inline int indexOfMethodRelative(const QMetaObject **baseObject,
- const char *method,
- bool normalizeStringData)
+ const QByteArray &name, int argc,
+ const QArgumentType *types)
{
for (const QMetaObject *m = *baseObject; m; m = m->d.superdata) {
- int i = (MethodType == MethodSignal && priv(m->d.data)->revision >= 4)
- ? (priv(m->d.data)->signalCount - 1) : (priv(m->d.data)->methodCount - 1);
- const int end = (MethodType == MethodSlot && priv(m->d.data)->revision >= 4)
+ Q_ASSERT(priv(m->d.data)->revision >= 7);
+ int i = (MethodType == MethodSignal)
+ ? (priv(m->d.data)->signalCount - 1) : (priv(m->d.data)->methodCount - 1);
+ const int end = (MethodType == MethodSlot)
? (priv(m->d.data)->signalCount) : 0;
- if (!normalizeStringData) {
- for (; i >= end; --i) {
- const char *stringdata = m->d.stringdata + m->d.data[priv(m->d.data)->methodData + 5*i];
- if (method[0] == stringdata[0] && strcmp(method + 1, stringdata + 1) == 0) {
- *baseObject = m;
- return i;
- }
- }
- } else if (priv(m->d.data)->revision < 5) {
- for (; i >= end; --i) {
- const char *stringdata = (m->d.stringdata + m->d.data[priv(m->d.data)->methodData + 5 * i]);
- const QByteArray normalizedSignature = QMetaObject::normalizedSignature(stringdata);
- if (normalizedSignature == method) {
- *baseObject = m;
- return i;
- }
+
+ for (; i >= end; --i) {
+ int handle = priv(m->d.data)->methodData + 5*i;
+ if (methodMatch(m, handle, name, argc, types)) {
+ *baseObject = m;
+ return i;
}
}
}
@@ -546,15 +630,10 @@ static inline int indexOfMethodRelative(const QMetaObject **baseObject,
*/
int QMetaObject::indexOfConstructor(const char *constructor) const
{
- if (priv(d.data)->revision < 2)
- return -1;
- for (int i = priv(d.data)->constructorCount-1; i >= 0; --i) {
- const char *data = d.stringdata + d.data[priv(d.data)->constructorData + 5*i];
- if (data[0] == constructor[0] && strcmp(constructor + 1, data + 1) == 0) {
- return i;
- }
- }
- return -1;
+ Q_ASSERT(priv(d.data)->revision >= 7);
+ QArgumentTypeArray types;
+ QByteArray name = QMetaObjectPrivate::decodeMethodSignature(constructor, types);
+ return QMetaObjectPrivate::indexOfConstructor(this, name, types.size(), types.constData());
}
/*!
@@ -568,16 +647,54 @@ int QMetaObject::indexOfConstructor(const char *constructor) const
int QMetaObject::indexOfMethod(const char *method) const
{
const QMetaObject *m = this;
- int i = indexOfMethodRelative<0>(&m, method, false);
- if (i < 0) {
- m = this;
- i = indexOfMethodRelative<0>(&m, method, true);
- }
+ int i;
+ Q_ASSERT(priv(m->d.data)->revision >= 7);
+ QArgumentTypeArray types;
+ QByteArray name = QMetaObjectPrivate::decodeMethodSignature(method, types);
+ i = indexOfMethodRelative<0>(&m, name, types.size(), types.constData());
if (i >= 0)
i += m->methodOffset();
return i;
}
+// Parses a string of comma-separated types into QArgumentTypes.
+static void argumentTypesFromString(const char *str, const char *end,
+ QArgumentTypeArray &types)
+{
+ Q_ASSERT(str <= end);
+ while (str != end) {
+ if (!types.isEmpty())
+ ++str; // Skip comma
+ const char *begin = str;
+ int level = 0;
+ while (str != end && (level > 0 || *str != ',')) {
+ if (*str == '<')
+ ++level;
+ else if (*str == '>')
+ --level;
+ ++str;
+ }
+ types += QArgumentType(QByteArray(begin, str - begin));
+ }
+}
+
+// Given a method \a signature (e.g. "foo(int,double)"), this function
+// populates the argument \a types array and returns the method name.
+QByteArray QMetaObjectPrivate::decodeMethodSignature(
+ const char *signature, QArgumentTypeArray &types)
+{
+ Q_ASSERT(signature != 0);
+ const char *lparens = strchr(signature, '(');
+ if (!lparens)
+ return QByteArray();
+ const char *rparens = strchr(lparens + 1, ')');
+ if (!rparens || *(rparens+1))
+ return QByteArray();
+ int nameLength = lparens - signature;
+ argumentTypesFromString(lparens + 1, rparens, types);
+ return QByteArray::fromRawData(signature, nameLength);
+}
+
/*!
Finds \a signal and returns its index; otherwise returns -1.
@@ -592,11 +709,11 @@ int QMetaObject::indexOfMethod(const char *method) const
int QMetaObject::indexOfSignal(const char *signal) const
{
const QMetaObject *m = this;
- int i = QMetaObjectPrivate::indexOfSignalRelative(&m, signal, false);
- if (i < 0) {
- m = this;
- i = QMetaObjectPrivate::indexOfSignalRelative(&m, signal, true);
- }
+ int i;
+ Q_ASSERT(priv(m->d.data)->revision >= 7);
+ QArgumentTypeArray types;
+ QByteArray name = QMetaObjectPrivate::decodeMethodSignature(signal, types);
+ i = QMetaObjectPrivate::indexOfSignalRelative(&m, name, types.size(), types.constData());
if (i >= 0)
i += m->methodOffset();
return i;
@@ -608,20 +725,23 @@ int QMetaObject::indexOfSignal(const char *signal) const
\a baseObject will be adjusted to the enclosing QMetaObject, or 0 if the signal is not found
*/
int QMetaObjectPrivate::indexOfSignalRelative(const QMetaObject **baseObject,
- const char *signal,
- bool normalizeStringData)
+ const QByteArray &name, int argc,
+ const QArgumentType *types)
{
- int i = indexOfMethodRelative<MethodSignal>(baseObject, signal, normalizeStringData);
+ int i = indexOfMethodRelative<MethodSignal>(baseObject, name, argc, types);
#ifndef QT_NO_DEBUG
const QMetaObject *m = *baseObject;
if (i >= 0 && m && m->d.superdata) {
- int conflict = m->d.superdata->indexOfMethod(signal);
- if (conflict >= 0)
+ int conflict = indexOfMethod(m->d.superdata, name, argc, types);
+ if (conflict >= 0) {
+ QMetaMethod conflictMethod = m->d.superdata->method(conflict);
qWarning("QMetaObject::indexOfSignal: signal %s from %s redefined in %s",
- signal, m->d.superdata->d.stringdata, m->d.stringdata);
- }
-#endif
- return i;
+ conflictMethod.methodSignature().constData(),
+ rawStringData(m->d.superdata, 0), rawStringData(m, 0));
+ }
+ }
+ #endif
+ return i;
}
/*!
@@ -635,9 +755,11 @@ int QMetaObjectPrivate::indexOfSignalRelative(const QMetaObject **baseObject,
int QMetaObject::indexOfSlot(const char *slot) const
{
const QMetaObject *m = this;
- int i = QMetaObjectPrivate::indexOfSlotRelative(&m, slot, false);
- if (i < 0)
- i = QMetaObjectPrivate::indexOfSlotRelative(&m, slot, true);
+ int i;
+ Q_ASSERT(priv(m->d.data)->revision >= 7);
+ QArgumentTypeArray types;
+ QByteArray name = QMetaObjectPrivate::decodeMethodSignature(slot, types);
+ i = QMetaObjectPrivate::indexOfSlotRelative(&m, name, types.size(), types.constData());
if (i >= 0)
i += m->methodOffset();
return i;
@@ -645,26 +767,112 @@ int QMetaObject::indexOfSlot(const char *slot) const
// same as indexOfSignalRelative but for slots.
int QMetaObjectPrivate::indexOfSlotRelative(const QMetaObject **m,
- const char *slot,
- bool normalizeStringData)
+ const QByteArray &name, int argc,
+ const QArgumentType *types)
+{
+ return indexOfMethodRelative<MethodSlot>(m, name, argc, types);
+}
+
+int QMetaObjectPrivate::indexOfSignal(const QMetaObject *m, const QByteArray &name,
+ int argc, const QArgumentType *types)
{
- return indexOfMethodRelative<MethodSlot>(m, slot, normalizeStringData);
+ int i = indexOfSignalRelative(&m, name, argc, types);
+ if (i >= 0)
+ i += m->methodOffset();
+ return i;
+}
+
+int QMetaObjectPrivate::indexOfSlot(const QMetaObject *m, const QByteArray &name,
+ int argc, const QArgumentType *types)
+{
+ int i = indexOfSlotRelative(&m, name, argc, types);
+ if (i >= 0)
+ i += m->methodOffset();
+ return i;
+}
+
+int QMetaObjectPrivate::indexOfMethod(const QMetaObject *m, const QByteArray &name,
+ int argc, const QArgumentType *types)
+{
+ int i = indexOfMethodRelative<0>(&m, name, argc, types);
+ if (i >= 0)
+ i += m->methodOffset();
+ return i;
+}
+
+int QMetaObjectPrivate::indexOfConstructor(const QMetaObject *m, const QByteArray &name,
+ int argc, const QArgumentType *types)
+{
+ for (int i = priv(m->d.data)->constructorCount-1; i >= 0; --i) {
+ int handle = priv(m->d.data)->constructorData + 5*i;
+ if (methodMatch(m, handle, name, argc, types))
+ return i;
+ }
+ return -1;
+}
+
+/*!
+ \internal
+
+ Returns true if the \a signalTypes and \a methodTypes are
+ compatible; otherwise returns false.
+*/
+bool QMetaObjectPrivate::checkConnectArgs(int signalArgc, const QArgumentType *signalTypes,
+ int methodArgc, const QArgumentType *methodTypes)
+{
+ if (signalArgc < methodArgc)
+ return false;
+ for (int i = 0; i < methodArgc; ++i) {
+ if (signalTypes[i] != methodTypes[i])
+ return false;
+ }
+ return true;
+}
+
+/*!
+ \internal
+
+ Returns true if the \a signal and \a method arguments are
+ compatible; otherwise returns false.
+*/
+bool QMetaObjectPrivate::checkConnectArgs(const QMetaMethodPrivate *signal,
+ const QMetaMethodPrivate *method)
+{
+ if (signal->methodType() != QMetaMethod::Signal)
+ return false;
+ if (signal->parameterCount() < method->parameterCount())
+ return false;
+ const QMetaObject *smeta = signal->enclosingMetaObject();
+ const QMetaObject *rmeta = method->enclosingMetaObject();
+ for (int i = 0; i < method->parameterCount(); ++i) {
+ uint sourceTypeInfo = signal->parameterTypeInfo(i);
+ uint targetTypeInfo = method->parameterTypeInfo(i);
+ if ((sourceTypeInfo & IsUnresolvedType)
+ || (targetTypeInfo & IsUnresolvedType)) {
+ QByteArray sourceName = typeNameFromTypeInfo(smeta, sourceTypeInfo);
+ QByteArray targetName = typeNameFromTypeInfo(rmeta, targetTypeInfo);
+ if (sourceName != targetName)
+ return false;
+ } else {
+ int sourceType = typeFromTypeInfo(smeta, sourceTypeInfo);
+ int targetType = typeFromTypeInfo(rmeta, targetTypeInfo);
+ if (sourceType != targetType)
+ return false;
+ }
+ }
+ return true;
}
static const QMetaObject *QMetaObject_findMetaObject(const QMetaObject *self, const char *name)
{
while (self) {
- if (strcmp(self->d.stringdata, name) == 0)
+ if (strcmp(rawStringData(self, 0), name) == 0)
return self;
if (self->d.extradata) {
const QMetaObject **e;
- if (priv(self->d.data)->revision < 2) {
- e = (const QMetaObject**)(self->d.extradata);
- } else
- {
- const QMetaObjectExtraData *extra = (const QMetaObjectExtraData*)(self->d.extradata);
- e = extra->objects;
- }
+ Q_ASSERT(priv(self->d.data)->revision >= 2);
+ const QMetaObjectExtraData *extra = (const QMetaObjectExtraData*)(self->d.extradata);
+ e = extra->objects;
if (e) {
while (*e) {
if (const QMetaObject *m =QMetaObject_findMetaObject((*e), name))
@@ -690,7 +898,7 @@ int QMetaObject::indexOfEnumerator(const char *name) const
while (m) {
const QMetaObjectPrivate *d = priv(m->d.data);
for (int i = d->enumeratorCount - 1; i >= 0; --i) {
- const char *prop = m->d.stringdata + m->d.data[d->enumeratorData + 4*i];
+ const char *prop = rawStringData(m, m->d.data[d->enumeratorData + 4*i]);
if (name[0] == prop[0] && strcmp(name + 1, prop + 1) == 0) {
i += m->enumeratorOffset();
return i;
@@ -713,7 +921,7 @@ int QMetaObject::indexOfProperty(const char *name) const
while (m) {
const QMetaObjectPrivate *d = priv(m->d.data);
for (int i = d->propertyCount-1; i >= 0; --i) {
- const char *prop = m->d.stringdata + m->d.data[d->propertyData + 3*i];
+ const char *prop = rawStringData(m, m->d.data[d->propertyData + 3*i]);
if (name[0] == prop[0] && strcmp(name + 1, prop + 1) == 0) {
i += m->propertyOffset();
return i;
@@ -722,7 +930,8 @@ int QMetaObject::indexOfProperty(const char *name) const
m = m->d.superdata;
}
- if (priv(this->d.data)->revision >= 3 && (priv(this->d.data)->flags & DynamicMetaObject)) {
+ Q_ASSERT(priv(this->d.data)->revision >= 3);
+ if (priv(this->d.data)->flags & DynamicMetaObject) {
QAbstractDynamicMetaObject *me =
const_cast<QAbstractDynamicMetaObject *>(static_cast<const QAbstractDynamicMetaObject *>(this));
@@ -744,8 +953,7 @@ int QMetaObject::indexOfClassInfo(const char *name) const
const QMetaObject *m = this;
while (m && i < 0) {
for (i = priv(m->d.data)->classInfoCount-1; i >= 0; --i)
- if (strcmp(name, m->d.stringdata
- + m->d.data[priv(m->d.data)->classInfoData + 2*i]) == 0) {
+ if (strcmp(name, rawStringData(m, m->d.data[priv(m->d.data)->classInfoData + 2*i])) == 0) {
i += m->classInfoOffset();
break;
}
@@ -765,7 +973,8 @@ QMetaMethod QMetaObject::constructor(int index) const
{
int i = index;
QMetaMethod result;
- if (priv(d.data)->revision >= 2 && i >= 0 && i < priv(d.data)->constructorCount) {
+ Q_ASSERT(priv(d.data)->revision >= 2);
+ if (i >= 0 && i < priv(d.data)->constructorCount) {
result.mobj = this;
result.handle = priv(d.data)->constructorData + 5*i;
}
@@ -829,16 +1038,16 @@ QMetaProperty QMetaObject::property(int index) const
if (i >= 0 && i < priv(d.data)->propertyCount) {
int handle = priv(d.data)->propertyData + 3*i;
int flags = d.data[handle + 2];
- const char *type = d.stringdata + d.data[handle + 1];
result.mobj = this;
result.handle = handle;
result.idx = i;
if (flags & EnumOrFlag) {
+ const char *type = rawTypeNameFromTypeInfo(this, d.data[handle + 1]);
result.menum = enumerator(indexOfEnumerator(type));
if (!result.menum.isValid()) {
const char *enum_name = type;
- const char *scope_name = d.stringdata;
+ const char *scope_name = rawStringData(this, 0);
char *scope_buffer = 0;
const char *colon = strrchr(enum_name, ':');
@@ -934,6 +1143,21 @@ bool QMetaObject::checkConnectArgs(const char *signal, const char *method)
return false;
}
+/*!
+ \since 5.0
+ \overload
+
+ Returns true if the \a signal and \a method arguments are
+ compatible; otherwise returns false.
+*/
+bool QMetaObject::checkConnectArgs(const QMetaMethod &signal,
+ const QMetaMethod &method)
+{
+ return QMetaObjectPrivate::checkConnectArgs(
+ QMetaMethodPrivate::get(&signal),
+ QMetaMethodPrivate::get(&method));
+}
+
static void qRemoveWhitespace(const char *s, char *d)
{
char last = 0;
@@ -963,7 +1187,9 @@ static char *qNormalizeType(char *d, int &templdepth, QByteArray &result)
--templdepth;
++d;
}
- if (strncmp("void", t, d - t) != 0)
+ // "void" should only be removed if this is part of a signature that has
+ // an explicit void argument; e.g., "void foo(void)" --> "void foo()"
+ if (strncmp("void)", t, d - t + 1) != 0)
result += normalizeTypeInternal(t, d);
return d;
@@ -1230,7 +1456,7 @@ bool QMetaObject::invokeMethod(QObject *obj,
\ingroup objectmodel
- A QMetaMethod has a methodType(), a signature(), a list of
+ A QMetaMethod has a methodType(), a methodSignature(), a list of
parameterTypes() and parameterNames(), a return typeName(), a
tag(), and an access() specifier. You can use invoke() to invoke
the method on an arbitrary QObject.
@@ -1275,72 +1501,244 @@ bool QMetaObject::invokeMethod(QObject *obj,
\internal
*/
+QByteArray QMetaMethodPrivate::signature() const
+{
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ QByteArray result;
+ result.reserve(256);
+ result += name();
+ result += '(';
+ QList<QByteArray> argTypes = parameterTypes();
+ for (int i = 0; i < argTypes.size(); ++i) {
+ if (i)
+ result += ',';
+ result += argTypes.at(i);
+ }
+ result += ')';
+ return result;
+}
+
+QByteArray QMetaMethodPrivate::name() const
+{
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ return toByteArray(stringData(mobj, mobj->d.data[handle]));
+}
+
+int QMetaMethodPrivate::typesDataIndex() const
+{
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ return mobj->d.data[handle + 2];
+}
+
+const char *QMetaMethodPrivate::rawReturnTypeName() const
+{
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ uint typeInfo = mobj->d.data[typesDataIndex()];
+ if (typeInfo & IsUnresolvedType)
+ return rawStringData(mobj, typeInfo & TypeNameIndexMask);
+ else
+ return QMetaType::typeName(typeInfo);
+}
+
+int QMetaMethodPrivate::returnType() const
+{
+ return parameterType(-1);
+}
+
+int QMetaMethodPrivate::parameterCount() const
+{
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ return mobj->d.data[handle + 1];
+}
+
+int QMetaMethodPrivate::parametersDataIndex() const
+{
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ return typesDataIndex() + 1;
+}
+
+uint QMetaMethodPrivate::parameterTypeInfo(int index) const
+{
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ return mobj->d.data[parametersDataIndex() + index];
+}
+
+int QMetaMethodPrivate::parameterType(int index) const
+{
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ return typeFromTypeInfo(mobj, parameterTypeInfo(index));
+}
+
+void QMetaMethodPrivate::getParameterTypes(int *types) const
+{
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ int dataIndex = parametersDataIndex();
+ int argc = parameterCount();
+ for (int i = 0; i < argc; ++i) {
+ int id = typeFromTypeInfo(mobj, mobj->d.data[dataIndex++]);
+ *(types++) = id;
+ }
+}
+
+QList<QByteArray> QMetaMethodPrivate::parameterTypes() const
+{
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ QList<QByteArray> list;
+ int argc = parameterCount();
+ int paramsIndex = parametersDataIndex();
+ for (int i = 0; i < argc; ++i)
+ list += typeNameFromTypeInfo(mobj, mobj->d.data[paramsIndex + i]);
+ return list;
+}
+
+QList<QByteArray> QMetaMethodPrivate::parameterNames() const
+{
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ QList<QByteArray> list;
+ int argc = parameterCount();
+ int namesIndex = parametersDataIndex() + argc;
+ for (int i = 0; i < argc; ++i)
+ list += toByteArray(stringData(mobj, mobj->d.data[namesIndex + i]));
+ return list;
+}
+
+QByteArray QMetaMethodPrivate::tag() const
+{
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ return toByteArray(stringData(mobj, mobj->d.data[handle + 3]));
+}
+
/*!
+ \since 5.0
+
Returns the signature of this method (e.g.,
\c{setValue(double)}).
\sa parameterTypes(), parameterNames()
*/
-const char *QMetaMethod::signature() const
+QByteArray QMetaMethod::methodSignature() const
+{
+ if (!mobj)
+ return QByteArray();
+ return QMetaMethodPrivate::get(this)->signature();
+}
+
+/*!
+ \since 5.0
+
+ Returns the name of this method.
+
+ \sa methodSignature(), parameterCount()
+*/
+QByteArray QMetaMethod::name() const
+{
+ if (!mobj)
+ return QByteArray();
+ return QMetaMethodPrivate::get(this)->name();
+}
+
+/*!
+ \since 5.0
+
+ Returns the return type of this method.
+
+ The return value is one of the types that are registered
+ with QMetaType, or QMetaType::UnknownType if the type is not registered.
+
+ \sa parameterType(), QMetaType, typeName()
+*/
+int QMetaMethod::returnType() const
+ {
+ if (!mobj)
+ return QMetaType::UnknownType;
+ return QMetaMethodPrivate::get(this)->returnType();
+}
+
+/*!
+ \since 5.0
+
+ Returns the number of parameters of this method.
+
+ \sa parameterType(), parameterNames()
+*/
+int QMetaMethod::parameterCount() const
{
if (!mobj)
return 0;
- return mobj->d.stringdata + mobj->d.data[handle];
+ return QMetaMethodPrivate::get(this)->parameterCount();
+}
+
+/*!
+ \since 5.0
+
+ Returns the type of the parameter at the given \a index.
+
+ The return value is one of the types that are registered
+ with QMetaType, or QMetaType::UnknownType if the type is not registered.
+
+ \sa parameterCount(), returnType(), QMetaType
+*/
+int QMetaMethod::parameterType(int index) const
+{
+ if (!mobj || index < 0)
+ return QMetaType::UnknownType;
+ if (index >= QMetaMethodPrivate::get(this)->parameterCount())
+ return QMetaType::UnknownType;
+ return QMetaMethodPrivate::get(this)->parameterType(index);
+}
+
+/*!
+ \since 5.0
+ \internal
+
+ Gets the parameter \a types of this method. The storage
+ for \a types must be able to hold parameterCount() items.
+
+ \sa parameterCount(), returnType(), parameterType()
+*/
+void QMetaMethod::getParameterTypes(int *types) const
+{
+ if (!mobj)
+ return;
+ QMetaMethodPrivate::get(this)->getParameterTypes(types);
}
/*!
Returns a list of parameter types.
- \sa parameterNames(), signature()
+ \sa parameterNames(), methodSignature()
*/
QList<QByteArray> QMetaMethod::parameterTypes() const
{
if (!mobj)
return QList<QByteArray>();
- return QMetaObjectPrivate::parameterTypeNamesFromSignature(
- mobj->d.stringdata + mobj->d.data[handle]);
+ return QMetaMethodPrivate::get(this)->parameterTypes();
}
/*!
Returns a list of parameter names.
- \sa parameterTypes(), signature()
+ \sa parameterTypes(), methodSignature()
*/
QList<QByteArray> QMetaMethod::parameterNames() const
{
QList<QByteArray> list;
if (!mobj)
return list;
- const char *names = mobj->d.stringdata + mobj->d.data[handle + 1];
- if (*names == 0) {
- // do we have one or zero arguments?
- const char *signature = mobj->d.stringdata + mobj->d.data[handle];
- while (*signature && *signature != '(')
- ++signature;
- if (*++signature != ')')
- list += QByteArray();
- } else {
- --names;
- do {
- const char *begin = ++names;
- while (*names && *names != ',')
- ++names;
- list += QByteArray(begin, names - begin);
- } while (*names);
- }
- return list;
+ return QMetaMethodPrivate::get(this)->parameterNames();
}
/*!
- Returns the return type of this method, or an empty string if the
- return type is \e void.
+ Returns the return type name of this method.
+
+ \sa returnType(), QMetaType::type()
*/
const char *QMetaMethod::typeName() const
{
if (!mobj)
return 0;
- return mobj->d.stringdata + mobj->d.data[handle + 2];
+ return QMetaMethodPrivate::get(this)->rawReturnTypeName();
}
/*!
@@ -1377,7 +1775,7 @@ const char *QMetaMethod::tag() const
{
if (!mobj)
return 0;
- return mobj->d.stringdata + mobj->d.data[handle + 3];
+ return QMetaMethodPrivate::get(this)->tag().constData();
}
@@ -1577,28 +1975,7 @@ bool QMetaMethod::invoke(QObject *object,
if (qstrlen(typeNames[paramCount]) <= 0)
break;
}
- int metaMethodArgumentCount = 0;
- {
- // based on QMetaObject::parameterNames()
- const char *names = mobj->d.stringdata + mobj->d.data[handle + 1];
- if (*names == 0) {
- // do we have one or zero arguments?
- const char *signature = mobj->d.stringdata + mobj->d.data[handle];
- while (*signature && *signature != '(')
- ++signature;
- if (*++signature != ')')
- ++metaMethodArgumentCount;
- } else {
- --names;
- do {
- ++names;
- while (*names && *names != ',')
- ++names;
- ++metaMethodArgumentCount;
- } while (*names);
- }
- }
- if (paramCount <= metaMethodArgumentCount)
+ if (paramCount <= QMetaMethodPrivate::get(this)->parameterCount())
return false;
// check connection type
@@ -1633,8 +2010,8 @@ bool QMetaMethod::invoke(QObject *object,
// recompute the methodIndex by reversing the arithmetic in QMetaObject::property()
int idx_relative = ((handle - priv(mobj->d.data)->methodData) / 5);
int idx_offset = mobj->methodOffset();
- QObjectPrivate::StaticMetaCallFunction callFunction =
- (QMetaObjectPrivate::get(mobj)->revision >= 6 && mobj->d.extradata)
+ Q_ASSERT(QMetaObjectPrivate::get(mobj)->revision >= 6);
+ QObjectPrivate::StaticMetaCallFunction callFunction = mobj->d.extradata
? reinterpret_cast<const QMetaObjectExtraData *>(mobj->d.extradata)->static_metacall : 0;
if (connectionType == Qt::DirectConnection) {
@@ -1661,7 +2038,7 @@ bool QMetaMethod::invoke(QObject *object,
for (int i = 1; i < paramCount; ++i) {
types[i] = QMetaType::type(typeNames[i]);
- if (types[i]) {
+ if (types[i] != QMetaType::UnknownType) {
args[i] = QMetaType::create(types[i], param[i]);
++nargs;
} else if (param[i]) {
@@ -1803,7 +2180,7 @@ const char *QMetaEnum::name() const
{
if (!mobj)
return 0;
- return mobj->d.stringdata + mobj->d.data[handle];
+ return rawStringData(mobj, mobj->d.data[handle]);
}
/*!
@@ -1831,7 +2208,7 @@ const char *QMetaEnum::key(int index) const
int count = mobj->d.data[handle + 2];
int data = mobj->d.data[handle + 3];
if (index >= 0 && index < count)
- return mobj->d.stringdata + mobj->d.data[data + 2*index];
+ return rawStringData(mobj, mobj->d.data[data + 2*index]);
return 0;
}
@@ -1878,7 +2255,7 @@ bool QMetaEnum::isFlag() const
*/
const char *QMetaEnum::scope() const
{
- return mobj?mobj->d.stringdata : 0;
+ return mobj?rawStringData(mobj, 0) : 0;
}
/*!
@@ -1910,8 +2287,8 @@ int QMetaEnum::keyToValue(const char *key, bool *ok) const
int count = mobj->d.data[handle + 2];
int data = mobj->d.data[handle + 3];
for (int i = 0; i < count; ++i) {
- if ((!scope || (qstrlen(mobj->d.stringdata) == scope && strncmp(qualified_key, mobj->d.stringdata, scope) == 0))
- && strcmp(key, mobj->d.stringdata + mobj->d.data[data + 2*i]) == 0) {
+ if ((!scope || (stringSize(mobj, 0) == int(scope) && strncmp(qualified_key, rawStringData(mobj, 0), scope) == 0))
+ && strcmp(key, rawStringData(mobj, mobj->d.data[data + 2*i])) == 0) {
if (ok != 0)
*ok = true;
return mobj->d.data[data + 2*i + 1];
@@ -1936,7 +2313,7 @@ const char* QMetaEnum::valueToKey(int value) const
int data = mobj->d.data[handle + 3];
for (int i = 0; i < count; ++i)
if (value == (int)mobj->d.data[data + 2*i + 1])
- return mobj->d.stringdata + mobj->d.data[data + 2*i];
+ return rawStringData(mobj, mobj->d.data[data + 2*i]);
return 0;
}
@@ -1979,8 +2356,8 @@ int QMetaEnum::keysToValue(const char *keys, bool *ok) const
}
int i;
for (i = count-1; i >= 0; --i)
- if ((!scope || (qstrlen(mobj->d.stringdata) == scope && strncmp(qualified_key.constData(), mobj->d.stringdata, scope) == 0))
- && strcmp(key, mobj->d.stringdata + mobj->d.data[data + 2*i]) == 0) {
+ if ((!scope || (stringSize(mobj, 0) == int(scope) && strncmp(qualified_key.constData(), rawStringData(mobj, 0), scope) == 0))
+ && strcmp(key, rawStringData(mobj, mobj->d.data[data + 2*i])) == 0) {
value |= mobj->d.data[data + 2*i + 1];
break;
}
@@ -2013,7 +2390,7 @@ QByteArray QMetaEnum::valueToKeys(int value) const
v = v & ~k;
if (!keys.isEmpty())
keys += '|';
- keys += mobj->d.stringdata + mobj->d.data[data + 2*i];
+ keys += toByteArray(stringData(mobj, mobj->d.data[data + 2*i]));
}
}
return keys;
@@ -2092,7 +2469,7 @@ const char *QMetaProperty::name() const
if (!mobj)
return 0;
int handle = priv(mobj->d.data)->propertyData + 3*idx;
- return mobj->d.stringdata + mobj->d.data[handle];
+ return rawStringData(mobj, mobj->d.data[handle]);
}
/*!
@@ -2105,7 +2482,7 @@ const char *QMetaProperty::typeName() const
if (!mobj)
return 0;
int handle = priv(mobj->d.data)->propertyData + 3*idx;
- return mobj->d.stringdata + mobj->d.data[handle + 1];
+ return rawTypeNameFromTypeInfo(mobj, mobj->d.data[handle + 1]);
}
/*!
@@ -2119,14 +2496,16 @@ QVariant::Type QMetaProperty::type() const
if (!mobj)
return QVariant::Invalid;
int handle = priv(mobj->d.data)->propertyData + 3*idx;
- uint flags = mobj->d.data[handle + 2];
- uint type = flags >> 24;
- if (type)
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ uint type = typeFromTypeInfo(mobj, mobj->d.data[handle + 1]);
+ if (type >= QMetaType::User)
+ return QVariant::UserType;
+ if (type != QMetaType::UnknownType)
return QVariant::Type(type);
if (isEnumType()) {
int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
- if (enumMetaTypeId == 0)
+ if (enumMetaTypeId == QMetaType::UnknownType)
return QVariant::Int;
}
#ifdef QT_COORD_TYPE
@@ -2142,18 +2521,24 @@ QVariant::Type QMetaProperty::type() const
\since 4.2
Returns this property's user type. The return value is one
- of the values that are registered with QMetaType, or 0 if
+ of the values that are registered with QMetaType, or QMetaType::UnknownType if
the type is not registered.
\sa type(), QMetaType, typeName()
*/
int QMetaProperty::userType() const
{
- QVariant::Type tp = type();
- if (tp != QVariant::UserType)
- return tp;
+ if (!mobj)
+ return QMetaType::UnknownType;
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ int handle = priv(mobj->d.data)->propertyData + 3*idx;
+ int type = typeFromTypeInfo(mobj, mobj->d.data[handle + 1]);
+ if (type != QMetaType::UnknownType)
+ return type;
if (isEnumType()) {
int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
+ if (enumMetaTypeId == QMetaType::UnknownType)
+ return QVariant::Int; // Match behavior of QMetaType::type()
return enumMetaTypeId;
}
return QMetaType::type(typeName());
@@ -2249,18 +2634,20 @@ QVariant QMetaProperty::read(const QObject *object) const
with QMetaType)
*/
int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
- if (enumMetaTypeId != 0)
+ if (enumMetaTypeId != QMetaType::UnknownType)
t = enumMetaTypeId;
} else {
int handle = priv(mobj->d.data)->propertyData + 3*idx;
- uint flags = mobj->d.data[handle + 2];
- const char *typeName = mobj->d.stringdata + mobj->d.data[handle + 1];
- t = (flags >> 24);
- if (t == QVariant::Invalid)
+ const char *typeName = 0;
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ uint typeInfo = mobj->d.data[handle + 1];
+ if (!(typeInfo & IsUnresolvedType))
+ t = typeInfo;
+ else {
+ typeName = rawStringData(mobj, typeInfo & TypeNameIndexMask);
t = QMetaType::type(typeName);
- if (t == QVariant::Invalid)
- t = QVariant::nameToType(typeName);
- if (t == QVariant::Invalid) {
+ }
+ if (t == QMetaType::UnknownType) {
qWarning("QMetaProperty::read: Unable to handle unregistered datatype '%s' for property '%s::%s'", typeName, mobj->className(), name());
return QVariant();
}
@@ -2315,17 +2702,24 @@ bool QMetaProperty::write(QObject *object, const QVariant &value) const
return false;
} else if (v.type() != QVariant::Int && v.type() != QVariant::UInt) {
int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
- if ((enumMetaTypeId == 0) || (v.userType() != enumMetaTypeId) || !v.constData())
+ if ((enumMetaTypeId == QMetaType::UnknownType) || (v.userType() != enumMetaTypeId) || !v.constData())
return false;
v = QVariant(*reinterpret_cast<const int *>(v.constData()));
}
v.convert(QVariant::Int);
} else {
int handle = priv(mobj->d.data)->propertyData + 3*idx;
- uint flags = mobj->d.data[handle + 2];
- t = flags >> 24;
- if (t == QVariant::Invalid) {
- const char *typeName = mobj->d.stringdata + mobj->d.data[handle + 1];
+ const char *typeName = 0;
+ Q_ASSERT(priv(mobj->d.data)->revision >= 7);
+ uint typeInfo = mobj->d.data[handle + 1];
+ if (!(typeInfo & IsUnresolvedType))
+ t = typeInfo;
+ else {
+ typeName = rawStringData(mobj, typeInfo & TypeNameIndexMask);
+ t = QMetaType::type(typeName);
+ }
+ if (t == QMetaType::UnknownType) {
+ Q_ASSERT(typeName != 0);
const char *vtypeName = value.typeName();
if (vtypeName && strcmp(typeName, vtypeName) == 0)
t = value.userType();
@@ -2691,7 +3085,7 @@ const char *QMetaClassInfo::name() const
{
if (!mobj)
return 0;
- return mobj->d.stringdata + mobj->d.data[handle];
+ return rawStringData(mobj, mobj->d.data[handle]);
}
/*!
@@ -2703,7 +3097,7 @@ const char* QMetaClassInfo::value() const
{
if (!mobj)
return 0;
- return mobj->d.stringdata + mobj->d.data[handle + 1];
+ return rawStringData(mobj, mobj->d.data[handle + 1]);
}
/*!
diff --git a/src/corelib/kernel/qmetaobject.h b/src/corelib/kernel/qmetaobject.h
index 9e51af7556..095b196dca 100644
--- a/src/corelib/kernel/qmetaobject.h
+++ b/src/corelib/kernel/qmetaobject.h
@@ -57,8 +57,13 @@ class Q_CORE_EXPORT QMetaMethod
public:
inline QMetaMethod() : mobj(0),handle(0) {}
- const char *signature() const;
+ QByteArray methodSignature() const;
+ QByteArray name() const;
const char *typeName() const;
+ int returnType() const;
+ int parameterCount() const;
+ int parameterType(int index) const;
+ void getParameterTypes(int *types) const;
QList<QByteArray> parameterTypes() const;
QList<QByteArray> parameterNames() const;
const char *tag() const;
@@ -137,8 +142,16 @@ public:
inline bool isValid() const { return mobj != 0; }
private:
+#if QT_DEPRECATED_SINCE(5,0)
+ // signature() has been renamed to methodSignature() in Qt 5.
+ // Warning, that function returns a QByteArray; check the life time if
+ // you convert to char*.
+ char *signature(struct renamedInQt5_warning_checkTheLifeTime * = 0) Q_DECL_EQ_DELETE;
+#endif
+
const QMetaObject *mobj;
uint handle;
+ friend class QMetaMethodPrivate;
friend struct QMetaObject;
friend struct QMetaObjectPrivate;
friend class QObject;
diff --git a/src/corelib/kernel/qmetaobject_p.h b/src/corelib/kernel/qmetaobject_p.h
index 59a5c5f280..3b732b4b93 100644
--- a/src/corelib/kernel/qmetaobject_p.h
+++ b/src/corelib/kernel/qmetaobject_p.h
@@ -105,11 +105,64 @@ enum MetaObjectFlags {
RequiresVariantMetaObject = 0x02
};
+enum MetaDataFlags {
+ IsUnresolvedType = 0x80000000,
+ TypeNameIndexMask = 0x7FFFFFFF
+};
+
+class QArgumentType
+{
+public:
+ QArgumentType(int type)
+ : _type(type)
+ {}
+ QArgumentType(const QByteArray &name)
+ : _type(QMetaType::type(name.constData())), _name(name)
+ {}
+ QArgumentType()
+ : _type(0)
+ {}
+ int type() const
+ { return _type; }
+ QByteArray name() const
+ {
+ if (_type && _name.isEmpty())
+ const_cast<QArgumentType *>(this)->_name = QMetaType::typeName(_type);
+ return _name;
+ }
+ bool operator==(const QArgumentType &other) const
+ {
+ if (_type)
+ return _type == other._type;
+ else if (other._type)
+ return false;
+ else
+ return _name == other._name;
+ }
+ bool operator!=(const QArgumentType &other) const
+ {
+ if (_type)
+ return _type != other._type;
+ else if (other._type)
+ return true;
+ else
+ return _name != other._name;
+ }
+
+private:
+ int _type;
+ QByteArray _name;
+};
+
+template <class T, int> class QVarLengthArray;
+typedef QVarLengthArray<QArgumentType, 10> QArgumentTypeArray;
+
+class QMetaMethodPrivate;
class QMutex;
struct QMetaObjectPrivate
{
- enum { OutputRevision = 6 }; // Used by moc and qmetaobjectbuilder
+ enum { OutputRevision = 7 }; // Used by moc, qmetaobjectbuilder and qdbus
int revision;
int className;
@@ -122,17 +175,33 @@ struct QMetaObjectPrivate
int signalCount; //since revision 4
// revision 5 introduces changes in normalized signatures, no new members
// revision 6 added qt_static_metacall as a member of each Q_OBJECT and inside QMetaObject itself
+ // revision 7 is Qt 5
static inline const QMetaObjectPrivate *get(const QMetaObject *metaobject)
{ return reinterpret_cast<const QMetaObjectPrivate*>(metaobject->d.data); }
+ static int originalClone(const QMetaObject *obj, int local_method_index);
+
+ static QByteArray decodeMethodSignature(const char *signature,
+ QArgumentTypeArray &types);
static int indexOfSignalRelative(const QMetaObject **baseObject,
- const char* name,
- bool normalizeStringData);
+ const QByteArray &name, int argc,
+ const QArgumentType *types);
static int indexOfSlotRelative(const QMetaObject **m,
- const char *slot,
- bool normalizeStringData);
- static int originalClone(const QMetaObject *obj, int local_method_index);
+ const QByteArray &name, int argc,
+ const QArgumentType *types);
+ static int indexOfSignal(const QMetaObject *m, const QByteArray &name,
+ int argc, const QArgumentType *types);
+ static int indexOfSlot(const QMetaObject *m, const QByteArray &name,
+ int argc, const QArgumentType *types);
+ static int indexOfMethod(const QMetaObject *m, const QByteArray &name,
+ int argc, const QArgumentType *types);
+ static int indexOfConstructor(const QMetaObject *m, const QByteArray &name,
+ int argc, const QArgumentType *types);
+ static bool checkConnectArgs(int signalArgc, const QArgumentType *signalTypes,
+ int methodArgc, const QArgumentType *methodTypes);
+ static bool checkConnectArgs(const QMetaMethodPrivate *signal,
+ const QMetaMethodPrivate *method);
static QList<QByteArray> parameterTypeNamesFromSignature(const char *signature);
diff --git a/src/corelib/kernel/qmetaobjectbuilder.cpp b/src/corelib/kernel/qmetaobjectbuilder.cpp
index 8bece6636b..59740960c9 100644
--- a/src/corelib/kernel/qmetaobjectbuilder.cpp
+++ b/src/corelib/kernel/qmetaobjectbuilder.cpp
@@ -78,26 +78,17 @@ QT_BEGIN_NAMESPACE
*/
// copied from moc's generator.cpp
-uint qvariant_nameToType(const char* name)
+bool isBuiltinType(const QByteArray &type)
{
- if (!name)
- return 0;
-
- uint tp = QMetaType::type(name);
- return tp < QMetaType::User ? tp : 0;
-}
-
-/*
- Returns true if the type is a QVariant types.
-*/
-bool isVariantType(const char* type)
-{
- return qvariant_nameToType(type) != 0;
+ int id = QMetaType::type(type);
+ if (!id && !type.isEmpty() && type != "void")
+ return false;
+ return (id < QMetaType::User);
}
+// copied from qmetaobject.cpp
static inline const QMetaObjectPrivate *priv(const uint* data)
{ return reinterpret_cast<const QMetaObjectPrivate*>(data); }
-// end of copied lines from qmetaobject.cpp
class QMetaMethodBuilderPrivate
{
@@ -105,7 +96,7 @@ public:
QMetaMethodBuilderPrivate
(QMetaMethod::MethodType _methodType,
const QByteArray& _signature,
- const QByteArray& _returnType = QByteArray(),
+ const QByteArray& _returnType = QByteArray("void"),
QMetaMethod::Access _access = QMetaMethod::Public,
int _revision = 0)
: signature(QMetaObject::normalizedSignature(_signature.constData())),
@@ -136,6 +127,21 @@ public:
{
attributes = ((attributes & ~AccessMask) | (int)value);
}
+
+ QList<QByteArray> parameterTypes() const
+ {
+ return QMetaObjectPrivate::parameterTypeNamesFromSignature(signature);
+ }
+
+ int parameterCount() const
+ {
+ return parameterTypes().size();
+ }
+
+ QByteArray name() const
+ {
+ return signature.left(qMax(signature.indexOf('('), 0));
+ }
};
class QMetaPropertyBuilderPrivate
@@ -429,8 +435,7 @@ QMetaMethodBuilder QMetaObjectBuilder::addMethod(const QByteArray& signature)
\a signature and \a returnType. Returns an object that can be
used to adjust the other attributes of the method. The \a signature
and \a returnType will be normalized before they are added to
- the class. If \a returnType is empty, then it indicates that
- the method has \c{void} as its return type.
+ the class.
\sa method(), methodCount(), removeMethod(), indexOfMethod()
*/
@@ -458,13 +463,13 @@ QMetaMethodBuilder QMetaObjectBuilder::addMethod(const QMetaMethod& prototype)
{
QMetaMethodBuilder method;
if (prototype.methodType() == QMetaMethod::Method)
- method = addMethod(prototype.signature());
+ method = addMethod(prototype.methodSignature());
else if (prototype.methodType() == QMetaMethod::Signal)
- method = addSignal(prototype.signature());
+ method = addSignal(prototype.methodSignature());
else if (prototype.methodType() == QMetaMethod::Slot)
- method = addSlot(prototype.signature());
+ method = addSlot(prototype.methodSignature());
else if (prototype.methodType() == QMetaMethod::Constructor)
- method = addConstructor(prototype.signature());
+ method = addConstructor(prototype.methodSignature());
method.setReturnType(prototype.typeName());
method.setParameterNames(prototype.parameterNames());
method.setTag(prototype.tag());
@@ -501,7 +506,7 @@ QMetaMethodBuilder QMetaObjectBuilder::addSignal(const QByteArray& signature)
{
int index = d->methods.size();
d->methods.append(QMetaMethodBuilderPrivate
- (QMetaMethod::Signal, signature, QByteArray(), QMetaMethod::Protected));
+ (QMetaMethod::Signal, signature, QByteArray("void"), QMetaMethod::Protected));
return QMetaMethodBuilder(this, index);
}
@@ -517,7 +522,8 @@ QMetaMethodBuilder QMetaObjectBuilder::addSignal(const QByteArray& signature)
QMetaMethodBuilder QMetaObjectBuilder::addConstructor(const QByteArray& signature)
{
int index = d->constructors.size();
- d->constructors.append(QMetaMethodBuilderPrivate(QMetaMethod::Constructor, signature));
+ d->constructors.append(QMetaMethodBuilderPrivate(QMetaMethod::Constructor, signature,
+ /*returnType=*/QByteArray()));
return QMetaMethodBuilder(this, -(index + 1));
}
@@ -535,7 +541,7 @@ QMetaMethodBuilder QMetaObjectBuilder::addConstructor(const QByteArray& signatur
QMetaMethodBuilder QMetaObjectBuilder::addConstructor(const QMetaMethod& prototype)
{
Q_ASSERT(prototype.methodType() == QMetaMethod::Constructor);
- QMetaMethodBuilder ctor = addConstructor(prototype.signature());
+ QMetaMethodBuilder ctor = addConstructor(prototype.methodSignature());
ctor.setReturnType(prototype.typeName());
ctor.setParameterNames(prototype.parameterNames());
ctor.setTag(prototype.tag());
@@ -588,7 +594,7 @@ QMetaPropertyBuilder QMetaObjectBuilder::addProperty(const QMetaProperty& protot
if (prototype.hasNotifySignal()) {
// Find an existing method for the notify signal, or add a new one.
QMetaMethod method = prototype.notifySignal();
- int index = indexOfMethod(method.signature());
+ int index = indexOfMethod(method.methodSignature());
if (index == -1)
index = addMethod(method).index();
d->properties[property._index].notifySignal = index;
@@ -731,16 +737,12 @@ void QMetaObjectBuilder::addMetaObject
if ((members & RelatedMetaObjects) != 0) {
const QMetaObject **objects;
- if (priv(prototype->d.data)->revision < 2) {
- objects = (const QMetaObject **)(prototype->d.extradata);
- } else
- {
- const QMetaObjectExtraData *extra = (const QMetaObjectExtraData *)(prototype->d.extradata);
- if (extra)
- objects = extra->objects;
- else
- objects = 0;
- }
+ Q_ASSERT(priv(prototype->d.data)->revision >= 2);
+ const QMetaObjectExtraData *extra = (const QMetaObjectExtraData *)(prototype->d.extradata);
+ if (extra)
+ objects = extra->objects;
+ else
+ objects = 0;
if (objects) {
while (*objects != 0) {
addRelatedMetaObject(*objects);
@@ -750,12 +752,11 @@ void QMetaObjectBuilder::addMetaObject
}
if ((members & StaticMetacall) != 0) {
- if (priv(prototype->d.data)->revision >= 6) {
- const QMetaObjectExtraData *extra =
- (const QMetaObjectExtraData *)(prototype->d.extradata);
- if (extra && extra->static_metacall)
- setStaticMetacallFunction(extra->static_metacall);
- }
+ Q_ASSERT(priv(prototype->d.data)->revision >= 6);
+ const QMetaObjectExtraData *extra =
+ (const QMetaObjectExtraData *)(prototype->d.extradata);
+ if (extra && extra->static_metacall)
+ setStaticMetacallFunction(extra->static_metacall);
}
}
@@ -1070,75 +1071,82 @@ int QMetaObjectBuilder::indexOfClassInfo(const QByteArray& name)
#define ALIGN(size,type) \
(size) = ((size) + sizeof(type) - 1) & ~(sizeof(type) - 1)
-class MetaStringTable
+/*!
+ \class QMetaStringTable
+ \internal
+ \brief The QMetaStringTable class can generate a meta-object string table at runtime.
+*/
+
+QMetaStringTable::QMetaStringTable()
+ : m_index(0) {}
+
+// Enters the given value into the string table (if it hasn't already been
+// entered). Returns the index of the string.
+int QMetaStringTable::enter(const QByteArray &value)
{
-public:
- typedef QHash<QByteArray, int> Entries; // string --> offset mapping
- typedef Entries::const_iterator const_iterator;
- Entries::const_iterator constBegin() const
- { return m_entries.constBegin(); }
- Entries::const_iterator constEnd() const
- { return m_entries.constEnd(); }
+ Entries::iterator it = m_entries.find(value);
+ if (it != m_entries.end())
+ return it.value();
+ int pos = m_index;
+ m_entries.insert(value, pos);
+ ++m_index;
+ return pos;
+}
- MetaStringTable() : m_offset(0) {}
+int QMetaStringTable::preferredAlignment()
+{
+#ifdef Q_ALIGNOF
+ return Q_ALIGNOF(QByteArrayData);
+#else
+ return sizeof(void *);
+#endif
+}
- int enter(const QByteArray &value)
- {
- Entries::iterator it = m_entries.find(value);
- if (it != m_entries.end())
- return it.value();
- int pos = m_offset;
- m_entries.insert(value, pos);
- m_offset += value.size() + 1;
- return pos;
- }
+// Returns the size (in bytes) required for serializing this string table.
+int QMetaStringTable::blobSize() const
+{
+ int size = m_entries.size() * sizeof(QByteArrayData);
+ Entries::const_iterator it;
+ for (it = m_entries.constBegin(); it != m_entries.constEnd(); ++it)
+ size += it.key().size() + 1;
+ return size;
+}
- int arraySize() const { return m_offset; }
+// Writes strings to string data struct.
+// The struct consists of an array of QByteArrayData, followed by a char array
+// containing the actual strings. This format must match the one produced by
+// moc (see generator.cpp).
+void QMetaStringTable::writeBlob(char *out)
+{
+ Q_ASSERT(!(reinterpret_cast<quintptr>(out) & (preferredAlignment()-1)));
-private:
- Entries m_entries;
- int m_offset;
-};
+ int offsetOfStringdataMember = m_entries.size() * sizeof(QByteArrayData);
+ int stringdataOffset = 0;
+ for (int i = 0; i < m_entries.size(); ++i) {
+ const QByteArray &str = m_entries.key(i);
+ int size = str.size();
+ qptrdiff offset = offsetOfStringdataMember + stringdataOffset
+ - i * sizeof(QByteArrayData);
+ const QByteArrayData data = { Q_REFCOUNT_INITIALIZE_STATIC, size, 0, 0, offset };
-// Build the parameter array string for a method.
-static QByteArray buildParameterNames
- (const QByteArray& signature, const QList<QByteArray>& parameterNames)
-{
- // If the parameter name list is specified, then concatenate them.
- if (!parameterNames.isEmpty()) {
- QByteArray names;
- bool first = true;
- foreach (const QByteArray &name, parameterNames) {
- if (first)
- first = false;
- else
- names += (char)',';
- names += name;
- }
- return names;
- }
+ memcpy(out + i * sizeof(QByteArrayData), &data, sizeof(QByteArrayData));
- // Count commas in the signature, excluding those inside template arguments.
- int index = signature.indexOf('(');
- if (index < 0)
- return QByteArray();
- ++index;
- if (index >= signature.size())
- return QByteArray();
- if (signature[index] == ')')
- return QByteArray();
- int count = 1;
- int brackets = 0;
- while (index < signature.size() && signature[index] != ',') {
- char ch = signature[index++];
- if (ch == '<')
- ++brackets;
- else if (ch == '>')
- --brackets;
- else if (ch == ',' && brackets <= 0)
- ++count;
+ memcpy(out + offsetOfStringdataMember + stringdataOffset, str.constData(), size);
+ out[offsetOfStringdataMember + stringdataOffset + size] = '\0';
+
+ stringdataOffset += size + 1;
}
- return QByteArray(count - 1, ',');
+}
+
+// Returns the sum of all parameters (including return type) for the given
+// \a methods. This is needed for calculating the size of the methods'
+// parameter type/name meta-data.
+static int aggregateParameterCount(const QList<QMetaMethodBuilderPrivate> &methods)
+{
+ int sum = 0;
+ for (int i = 0; i < methods.size(); ++i)
+ sum += methods.at(i).parameterCount() + 1; // +1 for return type
+ return sum;
}
// Build a QMetaObject in "buf" based on the information in "d".
@@ -1151,6 +1159,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
Q_UNUSED(expectedSize); // Avoid warning in release mode
int size = 0;
int dataIndex;
+ int paramsIndex;
int enumIndex;
int index;
bool hasRevisionedMethods = d->hasRevisionedMethods();
@@ -1181,8 +1190,13 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
break;
}
}
+ int methodParametersDataSize =
+ ((aggregateParameterCount(d->methods)
+ + aggregateParameterCount(d->constructors)) * 2) // types and parameter names
+ - d->methods.size() // return "parameters" don't have names
+ - d->constructors.size(); // "this" parameters don't have names
if (buf) {
- Q_STATIC_ASSERT_X(QMetaObjectPrivate::OutputRevision == 6, "QMetaObjectBuilder should generate the same version as moc");
+ Q_STATIC_ASSERT_X(QMetaObjectPrivate::OutputRevision == 7, "QMetaObjectBuilder should generate the same version as moc");
pmeta->revision = QMetaObjectPrivate::OutputRevision;
pmeta->flags = d->flags;
pmeta->className = 0; // Class name is always the first string.
@@ -1197,6 +1211,8 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
dataIndex += 5 * d->methods.size();
if (hasRevisionedMethods)
dataIndex += d->methods.size();
+ paramsIndex = dataIndex;
+ dataIndex += methodParametersDataSize;
pmeta->propertyCount = d->properties.size();
pmeta->propertyData = dataIndex;
@@ -1218,6 +1234,8 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
dataIndex += 5 * d->methods.size();
if (hasRevisionedMethods)
dataIndex += d->methods.size();
+ paramsIndex = dataIndex;
+ dataIndex += methodParametersDataSize;
dataIndex += 3 * d->properties.size();
if (hasNotifySignals)
dataIndex += d->properties.size();
@@ -1240,13 +1258,14 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
// Find the start of the data and string tables.
int *data = reinterpret_cast<int *>(pmeta);
size += dataIndex * sizeof(int);
+ ALIGN(size, void *);
char *str = reinterpret_cast<char *>(buf + size);
if (buf) {
if (relocatable) {
- meta->d.stringdata = reinterpret_cast<const char *>((quintptr)size);
+ meta->d.stringdata = reinterpret_cast<const QByteArrayData *>((quintptr)size);
meta->d.data = reinterpret_cast<uint *>((quintptr)pmetaSize);
} else {
- meta->d.stringdata = str;
+ meta->d.stringdata = reinterpret_cast<const QByteArrayData *>(str);
meta->d.data = reinterpret_cast<uint *>(data);
}
}
@@ -1254,7 +1273,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
// Reset the current data position to just past the QMetaObjectPrivate.
dataIndex = MetaObjectPrivateFieldCount;
- MetaStringTable strings;
+ QMetaStringTable strings;
strings.enter(d->className);
// Output the class infos,
@@ -1273,24 +1292,21 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
Q_ASSERT(!buf || dataIndex == pmeta->methodData);
for (index = 0; index < d->methods.size(); ++index) {
QMetaMethodBuilderPrivate *method = &(d->methods[index]);
- int sig = strings.enter(method->signature);
- int params;
- QByteArray names = buildParameterNames
- (method->signature, method->parameterNames);
- params = strings.enter(names);
- int ret = strings.enter(method->returnType);
+ int name = strings.enter(method->name());
+ int argc = method->parameterCount();
int tag = strings.enter(method->tag);
int attrs = method->attributes;
if (buf) {
- data[dataIndex] = sig;
- data[dataIndex + 1] = params;
- data[dataIndex + 2] = ret;
+ data[dataIndex] = name;
+ data[dataIndex + 1] = argc;
+ data[dataIndex + 2] = paramsIndex;
data[dataIndex + 3] = tag;
data[dataIndex + 4] = attrs;
if (method->methodType() == QMetaMethod::Signal)
pmeta->signalCount++;
}
dataIndex += 5;
+ paramsIndex += 1 + argc * 2;
}
if (hasRevisionedMethods) {
for (index = 0; index < d->methods.size(); ++index) {
@@ -1301,23 +1317,59 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
}
}
+ // Output the method parameters in the class.
+ Q_ASSERT(!buf || dataIndex == pmeta->methodData + d->methods.size() * 5
+ + (hasRevisionedMethods ? d->methods.size() : 0));
+ for (int x = 0; x < 2; ++x) {
+ QList<QMetaMethodBuilderPrivate> &methods = (x == 0) ? d->methods : d->constructors;
+ for (index = 0; index < methods.size(); ++index) {
+ QMetaMethodBuilderPrivate *method = &(methods[index]);
+ QList<QByteArray> paramTypeNames = method->parameterTypes();
+ int paramCount = paramTypeNames.size();
+ for (int i = -1; i < paramCount; ++i) {
+ const QByteArray &typeName = (i < 0) ? method->returnType : paramTypeNames.at(i);
+ int typeInfo;
+ if (isBuiltinType(typeName))
+ typeInfo = QMetaType::type(typeName);
+ else
+ typeInfo = IsUnresolvedType | strings.enter(typeName);
+ if (buf)
+ data[dataIndex] = typeInfo;
+ ++dataIndex;
+ }
+
+ QList<QByteArray> paramNames = method->parameterNames;
+ while (paramNames.size() < paramCount)
+ paramNames.append(QByteArray());
+ for (int i = 0; i < paramCount; ++i) {
+ int stringIndex = strings.enter(paramNames.at(i));
+ if (buf)
+ data[dataIndex] = stringIndex;
+ ++dataIndex;
+ }
+ }
+ }
+
// Output the properties in the class.
Q_ASSERT(!buf || dataIndex == pmeta->propertyData);
for (index = 0; index < d->properties.size(); ++index) {
QMetaPropertyBuilderPrivate *prop = &(d->properties[index]);
int name = strings.enter(prop->name);
- int type = strings.enter(prop->type);
+
+ int typeInfo;
+ if (isBuiltinType(prop->type))
+ typeInfo = QMetaType::type(prop->type);
+ else
+ typeInfo = IsUnresolvedType | strings.enter(prop->type);
+
int flags = prop->flags;
- if (!isVariantType(prop->type)) {
+ if (!isBuiltinType(prop->type))
flags |= EnumOrFlag;
- } else {
- flags |= qvariant_nameToType(prop->type) << 24;
- }
if (buf) {
data[dataIndex] = name;
- data[dataIndex + 1] = type;
+ data[dataIndex + 1] = typeInfo;
data[dataIndex + 2] = flags;
}
dataIndex += 3;
@@ -1372,34 +1424,25 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
Q_ASSERT(!buf || dataIndex == pmeta->constructorData);
for (index = 0; index < d->constructors.size(); ++index) {
QMetaMethodBuilderPrivate *method = &(d->constructors[index]);
- int sig = strings.enter(method->signature);
- int params;
- QByteArray names = buildParameterNames
- (method->signature, method->parameterNames);
- params = strings.enter(names);
- int ret = strings.enter(method->returnType);
+ int name = strings.enter(method->name());
+ int argc = method->parameterCount();
int tag = strings.enter(method->tag);
int attrs = method->attributes;
if (buf) {
- data[dataIndex] = sig;
- data[dataIndex + 1] = params;
- data[dataIndex + 2] = ret;
+ data[dataIndex] = name;
+ data[dataIndex + 1] = argc;
+ data[dataIndex + 2] = paramsIndex;
data[dataIndex + 3] = tag;
data[dataIndex + 4] = attrs;
}
dataIndex += 5;
+ paramsIndex += 1 + argc * 2;
}
- size += strings.arraySize();
+ size += strings.blobSize();
- if (buf) {
- // Write strings to string data array.
- MetaStringTable::const_iterator it;
- for (it = strings.constBegin(); it != strings.constEnd(); ++it) {
- memcpy(str + it.value(), it.key().constData(), it.key().size());
- str[it.value() + it.key().size()] = '\0';
- }
- }
+ if (buf)
+ strings.writeBlob(str);
// Output the zero terminator in the data array.
if (buf)
@@ -1508,7 +1551,7 @@ void QMetaObjectBuilder::fromRelocatableData(QMetaObject *output,
quintptr dataOffset = (quintptr)dataMo->d.data;
output->d.superdata = superclass;
- output->d.stringdata = buf + stringdataOffset;
+ output->d.stringdata = reinterpret_cast<const QByteArrayData *>(buf + stringdataOffset);
output->d.data = reinterpret_cast<const uint *>(buf + dataOffset);
output->d.extradata = 0;
}
@@ -1896,7 +1939,7 @@ QByteArray QMetaMethodBuilder::returnType() const
is empty, then the method's return type is \c{void}. The \a value
will be normalized before it is added to the method.
- \sa returnType(), signature()
+ \sa returnType(), parameterTypes(), signature()
*/
void QMetaMethodBuilder::setReturnType(const QByteArray& value)
{
@@ -1906,6 +1949,20 @@ void QMetaMethodBuilder::setReturnType(const QByteArray& value)
}
/*!
+ Returns the list of parameter types for this method.
+
+ \sa returnType(), parameterNames()
+*/
+QList<QByteArray> QMetaMethodBuilder::parameterTypes() const
+{
+ QMetaMethodBuilderPrivate *d = d_func();
+ if (d)
+ return d->parameterTypes();
+ else
+ return QList<QByteArray>();
+}
+
+/*!
Returns the list of parameter names for this method.
\sa setParameterNames()
diff --git a/src/corelib/kernel/qmetaobjectbuilder_p.h b/src/corelib/kernel/qmetaobjectbuilder_p.h
index 86bc354164..4d766a9197 100644
--- a/src/corelib/kernel/qmetaobjectbuilder_p.h
+++ b/src/corelib/kernel/qmetaobjectbuilder_p.h
@@ -56,6 +56,7 @@
#include <QtCore/qobject.h>
#include <QtCore/qmetaobject.h>
#include <QtCore/qdatastream.h>
+#include <QtCore/qhash.h>
#include <QtCore/qmap.h>
@@ -203,6 +204,7 @@ public:
QByteArray returnType() const;
void setReturnType(const QByteArray& value);
+ QList<QByteArray> parameterTypes() const;
QList<QByteArray> parameterNames() const;
void setParameterNames(const QList<QByteArray>& value);
@@ -318,6 +320,23 @@ private:
QMetaEnumBuilderPrivate *d_func() const;
};
+class Q_CORE_EXPORT QMetaStringTable
+{
+public:
+ QMetaStringTable();
+
+ int enter(const QByteArray &value);
+
+ static int preferredAlignment();
+ int blobSize() const;
+ void writeBlob(char *out);
+
+private:
+ typedef QHash<QByteArray, int> Entries; // string --> index mapping
+ Entries m_entries;
+ int m_index;
+};
+
Q_DECLARE_OPERATORS_FOR_FLAGS(QMetaObjectBuilder::AddMembers)
Q_DECLARE_OPERATORS_FOR_FLAGS(QMetaObjectBuilder::MetaObjectFlags)
diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp
index fc9053520c..906334be18 100644
--- a/src/corelib/kernel/qmetatype.cpp
+++ b/src/corelib/kernel/qmetatype.cpp
@@ -63,6 +63,11 @@
# include "qurl.h"
# include "qvariant.h"
# include "qabstractitemmodel.h"
+# include "qregularexpression.h"
+# include "qjsonvalue.h"
+# include "qjsonobject.h"
+# include "qjsonarray.h"
+# include "qjsondocument.h"
#endif
#ifndef QT_NO_GEOM_VARIANT
@@ -110,10 +115,17 @@ template<> struct TypeDefinition<QBitArray> { static const bool IsAvailable = fa
template<> struct TypeDefinition<QUrl> { static const bool IsAvailable = false; };
template<> struct TypeDefinition<QEasingCurve> { static const bool IsAvailable = false; };
template<> struct TypeDefinition<QModelIndex> { static const bool IsAvailable = false; };
+template<> struct TypeDefinition<QJsonValue> { static const bool IsAvailable = false; };
+template<> struct TypeDefinition<QJsonObject> { static const bool IsAvailable = false; };
+template<> struct TypeDefinition<QJsonArray> { static const bool IsAvailable = false; };
+template<> struct TypeDefinition<QJsonDocument> { static const bool IsAvailable = false; };
#endif
#ifdef QT_NO_REGEXP
template<> struct TypeDefinition<QRegExp> { static const bool IsAvailable = false; };
#endif
+#if defined(QT_BOOTSTRAPPED) || defined(QT_NO_REGEXP)
+template<> struct TypeDefinition<QRegularExpression> { static const bool IsAvailable = false; };
+#endif
} // namespace
/*!
@@ -219,6 +231,7 @@ template<> struct TypeDefinition<QRegExp> { static const bool IsAvailable = fals
\value QPoint QPoint
\value QUrl QUrl
\value QRegExp QRegExp
+ \value QRegularExpression QRegularExpression
\value QDateTime QDateTime
\value QPointF QPointF
\value QPalette QPalette
@@ -240,8 +253,13 @@ template<> struct TypeDefinition<QRegExp> { static const bool IsAvailable = fals
\value QVector4D QVector4D
\value QQuaternion QQuaternion
\value QEasingCurve QEasingCurve
+ \value QJsonValue QJsonValue
+ \value QJsonObject QJsonObject
+ \value QJsonArray QJsonArray
+ \value QJsonDocument QJsonDocument
\value User Base value for user types
+ \value UnknownType This is an invalid type id. It is returned from QMetaType for types that are not registered
\omitvalue FirstGuiType
\omitvalue FirstWidgetsType
@@ -311,7 +329,7 @@ static const struct { const char * typeName; int typeNameLength; int type; } typ
QT_FOR_EACH_STATIC_TYPE(QT_ADD_STATIC_METATYPE)
QT_FOR_EACH_STATIC_ALIAS_TYPE(QT_ADD_STATIC_METATYPE_ALIASES_ITER)
QT_FOR_EACH_STATIC_HACKS_TYPE(QT_ADD_STATIC_METATYPE_HACKS_ITER)
- {0, 0, QMetaType::Void}
+ {0, 0, QMetaType::UnknownType}
};
Q_CORE_EXPORT const QMetaTypeInterface *qMetaTypeGuiHelper = 0;
@@ -348,10 +366,7 @@ Q_GLOBAL_STATIC(QReadWriteLock, customTypesLock)
void QMetaType::registerStreamOperators(const char *typeName, SaveOperator saveOp,
LoadOperator loadOp)
{
- int idx = type(typeName);
- if (!idx)
- return;
- registerStreamOperators(idx, saveOp, loadOp);
+ registerStreamOperators(type(typeName), saveOp, loadOp);
}
/*! \internal
@@ -435,7 +450,7 @@ static int qMetaTypeCustomType_unlocked(const char *typeName, int length)
{
const QVector<QCustomTypeInfo> * const ct = customTypes();
if (!ct)
- return 0;
+ return QMetaType::UnknownType;
for (int v = 0; v < ct->count(); ++v) {
const QCustomTypeInfo &customInfo = ct->at(v);
@@ -446,7 +461,7 @@ static int qMetaTypeCustomType_unlocked(const char *typeName, int length)
return v + QMetaType::User;
}
}
- return 0;
+ return QMetaType::UnknownType;
}
/*! \internal
@@ -457,7 +472,7 @@ static int qMetaTypeCustomType_unlocked(const char *typeName, int length)
int QMetaType::registerType(const char *typeName, Deleter deleter,
Creator creator)
{
- return registerType(typeName, deleter, creator, qMetaTypeDestructHelper<void>, qMetaTypeConstructHelper<void>, 0, TypeFlags());
+ return registerType(typeName, deleter, creator, qMetaTypeDestructHelper<void>, qMetaTypeConstructHelper<void>, 0, TypeFlags(), 0);
}
/*! \internal
@@ -472,7 +487,7 @@ int QMetaType::registerType(const char *typeName, Deleter deleter,
Creator creator,
Destructor destructor,
Constructor constructor,
- int size, TypeFlags flags)
+ int size, TypeFlags flags, const QMetaObject *metaObject)
{
QVector<QCustomTypeInfo> *ct = customTypes();
if (!ct || !typeName || !deleter || !creator || !destructor || !constructor)
@@ -489,11 +504,11 @@ int QMetaType::registerType(const char *typeName, Deleter deleter,
int previousSize = 0;
int previousFlags = 0;
- if (!idx) {
+ if (idx == UnknownType) {
QWriteLocker locker(customTypesLock());
idx = qMetaTypeCustomType_unlocked(normalizedTypeName.constData(),
normalizedTypeName.size());
- if (!idx) {
+ if (idx == UnknownType) {
QCustomTypeInfo inf;
inf.typeName = normalizedTypeName;
inf.creator = creator;
@@ -559,12 +574,12 @@ int QMetaType::registerTypedef(const char* typeName, int aliasId)
int idx = qMetaTypeStaticType(normalizedTypeName.constData(),
normalizedTypeName.size());
- if (!idx) {
+ if (idx == UnknownType) {
QWriteLocker locker(customTypesLock());
idx = qMetaTypeCustomType_unlocked(normalizedTypeName.constData(),
normalizedTypeName.size());
- if (!idx) {
+ if (idx == UnknownType) {
QCustomTypeInfo inf;
inf.typeName = normalizedTypeName;
inf.alias = aliasId;
@@ -593,17 +608,20 @@ int QMetaType::registerTypedef(const char* typeName, int aliasId)
*/
bool QMetaType::isRegistered(int type)
{
- if (type >= 0 && type < User) {
- // predefined type
+ // predefined type
+ if ((type >= FirstCoreType && type <= LastCoreType)
+ || (type >= FirstGuiType && type <= LastGuiType)
+ || (type >= FirstWidgetsType && type <= LastWidgetsType)) {
return true;
}
+
QReadLocker locker(customTypesLock());
const QVector<QCustomTypeInfo> * const ct = customTypes();
return ((type >= User) && (ct && ct->count() > type - User) && !ct->at(type - User).typeName.isEmpty());
}
/*!
- Returns a handle to the type called \a typeName, or 0 if there is
+ Returns a handle to the type called \a typeName, or QMetaType::UnknownType if there is
no such type.
\sa isRegistered(), typeName(), Type
@@ -612,17 +630,17 @@ int QMetaType::type(const char *typeName)
{
int length = qstrlen(typeName);
if (!length)
- return 0;
+ return UnknownType;
int type = qMetaTypeStaticType(typeName, length);
- if (!type) {
+ if (type == UnknownType) {
QReadLocker locker(customTypesLock());
type = qMetaTypeCustomType_unlocked(typeName, length);
#ifndef QT_NO_QOBJECT
- if (!type) {
+ if (type == UnknownType) {
const NS(QByteArray) normalizedTypeName = QMetaObject::normalizedType(typeName);
type = qMetaTypeStaticType(normalizedTypeName.constData(),
normalizedTypeName.size());
- if (!type) {
+ if (type == UnknownType) {
type = qMetaTypeCustomType_unlocked(normalizedTypeName.constData(),
normalizedTypeName.size());
}
@@ -653,11 +671,16 @@ bool QMetaType::save(QDataStream &stream, int type, const void *data)
return false;
switch(type) {
+ case QMetaType::UnknownType:
case QMetaType::Void:
case QMetaType::VoidStar:
case QMetaType::QObjectStar:
case QMetaType::QWidgetStar:
case QMetaType::QModelIndex:
+ case QMetaType::QJsonValue:
+ case QMetaType::QJsonObject:
+ case QMetaType::QJsonArray:
+ case QMetaType::QJsonDocument:
return false;
case QMetaType::Long:
stream << qlonglong(*static_cast<const long *>(data));
@@ -779,10 +802,15 @@ bool QMetaType::save(QDataStream &stream, int type, const void *data)
break;
#endif
#ifndef QT_BOOTSTRAPPED
+#ifndef QT_NO_REGEXP
+ case QMetaType::QRegularExpression:
+ stream << *static_cast<const NS(QRegularExpression)*>(data);
+ break;
+#endif // QT_NO_REGEXP
case QMetaType::QEasingCurve:
stream << *static_cast<const NS(QEasingCurve)*>(data);
break;
-#endif
+#endif // QT_BOOTSTRAPPED
case QMetaType::QFont:
case QMetaType::QPixmap:
case QMetaType::QBrush:
@@ -858,11 +886,16 @@ bool QMetaType::load(QDataStream &stream, int type, void *data)
return false;
switch(type) {
+ case QMetaType::UnknownType:
case QMetaType::Void:
case QMetaType::VoidStar:
case QMetaType::QObjectStar:
case QMetaType::QWidgetStar:
case QMetaType::QModelIndex:
+ case QMetaType::QJsonValue:
+ case QMetaType::QJsonObject:
+ case QMetaType::QJsonArray:
+ case QMetaType::QJsonDocument:
return false;
case QMetaType::Long: {
qlonglong l;
@@ -990,10 +1023,15 @@ bool QMetaType::load(QDataStream &stream, int type, void *data)
break;
#endif
#ifndef QT_BOOTSTRAPPED
+#ifndef QT_NO_REGEXP
+ case QMetaType::QRegularExpression:
+ stream >> *static_cast< NS(QRegularExpression)*>(data);
+ break;
+#endif // QT_NO_REGEXP
case QMetaType::QEasingCurve:
stream >> *static_cast< NS(QEasingCurve)*>(data);
break;
-#endif
+#endif // QT_BOOTSTRAPPED
case QMetaType::QFont:
case QMetaType::QPixmap:
case QMetaType::QBrush:
@@ -1084,6 +1122,7 @@ public:
template<typename T>
void *delegate(const T *copy) { return CreatorImpl<T>::Create(m_type, copy); }
void *delegate(const void*) { return 0; }
+ void *delegate(const QMetaTypeSwitcher::UnknownType *) { return 0; }
void *delegate(const QMetaTypeSwitcher::NotBuiltinType *copy)
{
QMetaType::Creator creator;
@@ -1146,6 +1185,7 @@ public:
template<typename T>
void delegate(const T *where) { DestroyerImpl<T>::Destroy(m_type, const_cast<T*>(where)); }
void delegate(const void *) {}
+ void delegate(const QMetaTypeSwitcher::UnknownType*) {}
void delegate(const QMetaTypeSwitcher::NotBuiltinType *where) { customTypeDestroyer(m_type, (void*)where); }
private:
@@ -1209,6 +1249,7 @@ public:
template<typename T>
void *delegate(const T *copy) { return ConstructorImpl<T>::Construct(m_type, m_where, copy); }
void *delegate(const void *) { return m_where; }
+ void *delegate(const QMetaTypeSwitcher::UnknownType*) { return m_where; }
void *delegate(const QMetaTypeSwitcher::NotBuiltinType *copy) { return customTypeConstructor(m_type, m_where, copy); }
private:
@@ -1298,6 +1339,7 @@ public:
template<typename T>
void delegate(const T *where) { DestructorImpl<T>::Destruct(m_type, const_cast<T*>(where)); }
void delegate(const void *) {}
+ void delegate(const QMetaTypeSwitcher::UnknownType*) {}
void delegate(const QMetaTypeSwitcher::NotBuiltinType *where) { customTypeDestructor(m_type, (void*)where); }
private:
@@ -1367,6 +1409,7 @@ public:
template<typename T>
int delegate(const T*) { return SizeOfImpl<T>::Size(m_type); }
+ int delegate(const QMetaTypeSwitcher::UnknownType*) { return 0; }
int delegate(const QMetaTypeSwitcher::NotBuiltinType*) { return customTypeSizeOf(m_type); }
private:
static int customTypeSizeOf(const int type)
@@ -1433,6 +1476,7 @@ public:
template<typename T>
quint32 delegate(const T*) { return FlagsImpl<T>::Flags(m_type); }
quint32 delegate(const void*) { return 0; }
+ quint32 delegate(const QMetaTypeSwitcher::UnknownType*) { return 0; }
quint32 delegate(const QMetaTypeSwitcher::NotBuiltinType*) { return customTypeFlags(m_type); }
private:
const int m_type;
@@ -1620,6 +1664,7 @@ public:
template<typename T>
void delegate(const T*) { TypeInfoImpl<T>(m_type, info); }
void delegate(const void*) {}
+ void delegate(const QMetaTypeSwitcher::UnknownType*) {}
void delegate(const QMetaTypeSwitcher::NotBuiltinType*) { customTypeInfo(m_type); }
private:
void customTypeInfo(const uint type)
@@ -1640,7 +1685,7 @@ QMetaType QMetaType::typeInfo(const int type)
{
TypeInfo typeInfo(type);
QMetaTypeSwitcher::switcher<void>(typeInfo, type, 0);
- return typeInfo.info.creator || !type ? QMetaType(QMetaType::NoExtensionFlags
+ return typeInfo.info.creator || type == Void ? QMetaType(QMetaType::NoExtensionFlags
, static_cast<const QMetaTypeInterface *>(0) // typeInfo::info is a temporary variable, we can't return address of it.
, typeInfo.info.creator
, typeInfo.info.deleter
@@ -1650,27 +1695,25 @@ QMetaType QMetaType::typeInfo(const int type)
, typeInfo.info.destructor
, typeInfo.info.size
, typeInfo.info.flags
- , type)
- : QMetaType(-1);
+ , type
+ , 0)
+ : QMetaType(UnknownType);
}
QMetaType::QMetaType(const int typeId)
: m_typeId(typeId)
{
- if (Q_UNLIKELY(typeId == -1)) {
+ if (Q_UNLIKELY(typeId == UnknownType)) {
// Constructs invalid QMetaType instance.
m_extensionFlags = 0xffffffff;
Q_ASSERT(!isValid());
} else {
// TODO it can be better.
*this = QMetaType::typeInfo(typeId);
- if (m_typeId > 0 && !m_creator) {
+ if (m_typeId == UnknownType)
m_extensionFlags = 0xffffffff;
- m_typeId = -1;
- }
- if (m_typeId == QMetaType::Void) {
+ else if (m_typeId == QMetaType::Void)
m_extensionFlags = CreateEx | DestroyEx | ConstructEx | DestructEx;
- }
}
}
@@ -1686,6 +1729,7 @@ QMetaType::QMetaType(const QMetaType &other)
, m_typeFlags(other.m_typeFlags)
, m_extensionFlags(other.m_extensionFlags)
, m_typeId(other.m_typeId)
+ , m_metaObject(other.m_metaObject)
{}
QMetaType &QMetaType::operator =(const QMetaType &other)
@@ -1701,6 +1745,7 @@ QMetaType &QMetaType::operator =(const QMetaType &other)
m_extensionFlags = other.m_extensionFlags;
m_extension = other.m_extension; // space reserved for future use
m_typeId = other.m_typeId;
+ m_metaObject = other.m_metaObject;
return *this;
}
diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h
index bf1b126200..21f4bc7afe 100644
--- a/src/corelib/kernel/qmetatype.h
+++ b/src/corelib/kernel/qmetatype.h
@@ -60,7 +60,7 @@ QT_BEGIN_NAMESPACE
// F is a tuple: (QMetaType::TypeName, QMetaType::TypeNameID, RealType)
#define QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(F)\
- F(Void, 0, void) \
+ F(Void, 43, void) \
F(Bool, 1, bool) \
F(Int, 2, int) \
F(UInt, 3, uint) \
@@ -102,6 +102,11 @@ QT_BEGIN_NAMESPACE
F(QUuid, 30, QUuid) \
F(QVariant, 41, QVariant) \
F(QModelIndex, 42, QModelIndex) \
+ F(QRegularExpression, 44, QRegularExpression) \
+ F(QJsonValue, 45, QJsonValue) \
+ F(QJsonObject, 46, QJsonObject) \
+ F(QJsonArray, 47, QJsonArray) \
+ F(QJsonDocument, 48, QJsonDocument) \
#define QT_FOR_EACH_STATIC_CORE_POINTER(F)\
F(QObjectStar, 39, QObject*) \
@@ -179,6 +184,7 @@ QT_BEGIN_NAMESPACE
class QDataStream;
class QMetaTypeInterface;
+struct QMetaObject;
class Q_CORE_EXPORT QMetaType {
enum ExtensionFlag { NoExtensionFlags,
@@ -193,8 +199,8 @@ public:
// these are merged with QVariant
QT_FOR_EACH_STATIC_TYPE(QT_DEFINE_METATYPE_ID)
- FirstCoreType = Void,
- LastCoreType = QModelIndex,
+ FirstCoreType = Bool,
+ LastCoreType = QJsonDocument,
FirstGuiType = QFont,
LastGuiType = QPolygonF,
FirstWidgetsType = QIcon,
@@ -203,7 +209,8 @@ public:
QReal = sizeof(qreal) == sizeof(double) ? Double : Float,
- User = 256
+ UnknownType = 0,
+ User = 1024
};
enum TypeFlag {
@@ -236,7 +243,8 @@ public:
Destructor destructor,
Constructor constructor,
int size,
- QMetaType::TypeFlags flags);
+ QMetaType::TypeFlags flags,
+ const QMetaObject *metaObject);
static int registerTypedef(const char *typeName, int aliasId);
static int type(const char *typeName);
static const char *typeName(int type);
@@ -280,7 +288,8 @@ private:
Destructor destructor,
uint sizeOf,
uint theTypeFlags,
- int typeId);
+ int typeId,
+ const QMetaObject *metaObject);
QMetaType(const QMetaType &other);
QMetaType &operator =(const QMetaType &);
inline bool isExtended(const ExtensionFlag flag) const { return m_extensionFlags & flag; }
@@ -306,6 +315,7 @@ private:
uint m_typeFlags;
uint m_extensionFlags;
int m_typeId;
+ const QMetaObject *m_metaObject; // Placeholder for Qt 5.1 feature.
};
#undef QT_DEFINE_METATYPE_ID
@@ -366,33 +376,11 @@ void qMetaTypeLoadHelper(QDataStream &stream, void *t)
template <> inline void qMetaTypeLoadHelper<void>(QDataStream &, void *) {}
#endif // QT_NO_DATASTREAM
-template <typename T>
-struct QMetaTypeId
-{
- enum { Defined = 0 };
-};
-
-template <typename T>
-struct QMetaTypeId2
-{
- enum { Defined = QMetaTypeId<T>::Defined };
- static inline int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
-};
-
class QObject;
class QWidget;
-namespace QtPrivate {
- template <typename T, bool Defined = QMetaTypeId2<T>::Defined>
- struct QMetaTypeIdHelper {
- static inline int qt_metatype_id()
- { return QMetaTypeId2<T>::qt_metatype_id(); }
- };
- template <typename T> struct QMetaTypeIdHelper<T, false> {
- static inline int qt_metatype_id()
- { return -1; }
- };
-
+namespace QtPrivate
+{
template<typename T>
struct IsPointerToTypeDerivedFromQObject
{
@@ -430,6 +418,49 @@ namespace QtPrivate {
enum { Value = sizeof(checkType(static_cast<T*>(0))) == sizeof(yes_type) };
};
+ template<typename T, bool = IsPointerToTypeDerivedFromQObject<T>::Value>
+ struct MetaObjectForType
+ {
+ static inline const QMetaObject *value() { return 0; }
+ };
+ template<typename T>
+ struct MetaObjectForType<T*, /* isPointerToTypeDerivedFromQObject = */ true>
+ {
+ static inline const QMetaObject *value() { return &T::staticMetaObject; }
+ };
+}
+
+template <typename T, bool = QtPrivate::IsPointerToTypeDerivedFromQObject<T>::Value>
+struct QMetaTypeIdQObject
+{
+ enum {
+ Defined = 0
+ };
+};
+
+template <typename T>
+struct QMetaTypeId : public QMetaTypeIdQObject<T>
+{
+};
+
+template <typename T>
+struct QMetaTypeId2
+{
+ enum { Defined = QMetaTypeId<T>::Defined };
+ static inline int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
+};
+
+namespace QtPrivate {
+ template <typename T, bool Defined = QMetaTypeId2<T>::Defined>
+ struct QMetaTypeIdHelper {
+ static inline int qt_metatype_id()
+ { return QMetaTypeId2<T>::qt_metatype_id(); }
+ };
+ template <typename T> struct QMetaTypeIdHelper<T, false> {
+ static inline int qt_metatype_id()
+ { return -1; }
+ };
+
// Function pointers don't derive from QObject
template <class Result> struct IsPointerToTypeDerivedFromQObject<Result(*)()> { enum { Value = false }; };
template <class Result, class Arg0> struct IsPointerToTypeDerivedFromQObject<Result(*)(Arg0)> { enum { Value = false }; };
@@ -465,7 +496,8 @@ int qRegisterMetaType(const char *typeName
qMetaTypeDestructHelper<T>,
qMetaTypeConstructHelper<T>,
sizeof(T),
- flags);
+ flags,
+ QtPrivate::MetaObjectForType<T>::value());
}
#ifndef QT_NO_DATASTREAM
@@ -506,6 +538,23 @@ inline int qRegisterMetaType(
#endif
}
+template <typename T>
+struct QMetaTypeIdQObject<T*, /* isPointerToTypeDerivedFromQObject */ true>
+{
+ enum {
+ Defined = 1
+ };
+
+ static int qt_metatype_id()
+ {
+ static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0);
+ if (!metatype_id.load())
+ metatype_id.storeRelease(qRegisterMetaType<T*>(QByteArray(T::staticMetaObject.className() + QByteArrayLiteral("*")).constData(),
+ reinterpret_cast<T**>(quintptr(-1))));
+ return metatype_id.loadAcquire();
+ }
+};
+
#ifndef QT_NO_DATASTREAM
template <typename T>
inline int qRegisterMetaTypeStreamOperators()
@@ -630,7 +679,8 @@ inline QMetaType::QMetaType(const ExtensionFlag extensionFlags, const QMetaTypeI
Destructor destructor,
uint size,
uint theTypeFlags,
- int typeId)
+ int typeId,
+ const QMetaObject *metaObject)
: m_creator(creator)
, m_deleter(deleter)
, m_saveOp(saveOp)
@@ -641,6 +691,7 @@ inline QMetaType::QMetaType(const ExtensionFlag extensionFlags, const QMetaTypeI
, m_typeFlags(theTypeFlags)
, m_extensionFlags(extensionFlags)
, m_typeId(typeId)
+ , m_metaObject(metaObject)
{
if (Q_UNLIKELY(isExtended(CtorEx) || typeId == QMetaType::Void))
ctor(info);
@@ -654,7 +705,7 @@ inline QMetaType::~QMetaType()
inline bool QMetaType::isValid() const
{
- return m_typeId >= 0;
+ return m_typeId != UnknownType;
}
inline bool QMetaType::isRegistered() const
diff --git a/src/corelib/kernel/qmetatypeswitcher_p.h b/src/corelib/kernel/qmetatypeswitcher_p.h
index e9c15ea214..ffd188c972 100644
--- a/src/corelib/kernel/qmetatypeswitcher_p.h
+++ b/src/corelib/kernel/qmetatypeswitcher_p.h
@@ -59,7 +59,8 @@ QT_BEGIN_NAMESPACE
class QMetaTypeSwitcher {
public:
- class NotBuiltinType;
+ class NotBuiltinType; // type is not a built-in type, but it may be a custom type or an unknown type
+ class UnknownType; // type not known to QMetaType system
template<class ReturnType, class DelegateObject>
static ReturnType switcher(DelegateObject &logic, int type, const void *data);
};
@@ -74,7 +75,11 @@ ReturnType QMetaTypeSwitcher::switcher(DelegateObject &logic, int type, const vo
switch (QMetaType::Type(type)) {
QT_FOR_EACH_STATIC_TYPE(QT_METATYPE_SWICHER_CASE)
+ case QMetaType::UnknownType:
+ return logic.delegate(static_cast<UnknownType const *>(data));
default:
+ if (type < QMetaType::User)
+ return logic.delegate(static_cast<UnknownType const *>(data));
return logic.delegate(static_cast<NotBuiltinType const *>(data));
}
}
diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp
index 25adee7cc7..e41a7cf92e 100644
--- a/src/corelib/kernel/qobject.cpp
+++ b/src/corelib/kernel/qobject.cpp
@@ -50,6 +50,7 @@
#include "qvariant.h"
#include "qmetaobject.h"
#include <qregexp.h>
+#include <qregularexpression.h>
#include <qthread.h>
#include <private/qthread_p.h>
#include <qdebug.h>
@@ -105,6 +106,30 @@ static int *queuedConnectionTypes(const QList<QByteArray> &typeNames)
return types;
}
+static int *queuedConnectionTypes(const QArgumentType *argumentTypes, int argc)
+{
+ QScopedArrayPointer<int> types(new int [argc + 1]);
+ for (int i = 0; i < argc; ++i) {
+ const QArgumentType &type = argumentTypes[i];
+ if (type.type())
+ types[i] = type.type();
+ else if (type.name().endsWith('*'))
+ types[i] = QMetaType::VoidStar;
+ else
+ types[i] = QMetaType::type(type.name());
+
+ if (!types[i]) {
+ qWarning("QObject::connect: Cannot queue arguments of type '%s'\n"
+ "(Make sure '%s' is registered using qRegisterMetaType().)",
+ type.name().constData(), type.name().constData());
+ return 0;
+ }
+ }
+ types[argc] = 0;
+
+ return types.take();
+}
+
static QBasicMutex _q_ObjectMutexPool[131];
/** \internal
@@ -220,9 +245,8 @@ static void computeOffsets(const QMetaObject *metaobject, int *signalOffset, int
while (m) {
const QMetaObjectPrivate *d = QMetaObjectPrivate::get(m);
*methodOffset += d->methodCount;
- *signalOffset += (d->revision >= 4) ? d->signalCount : d->methodCount;
- /*Before Qt 4.6 (revision 4), the signalCount information was not generated by moc.
- so for compatibility we consider all the method as slot for old moc output*/
+ Q_ASSERT(d->revision >= 4);
+ *signalOffset += d->signalCount;
m = m->d.superdata;
}
}
@@ -1534,7 +1558,21 @@ void QObject::killTimer(int id)
Returns the children of this object that can be cast to type T
and that have names matching the regular expression \a regExp,
or an empty list if there are no such objects.
- The search is performed recursively.
+ The search is performed recursively, unless \a options specifies the
+ option FindDirectChildrenOnly.
+*/
+
+/*!
+ \fn QList<T> QObject::findChildren(const QRegularExpression &re, Qt::FindChildOptions options) const
+ \overload findChildren()
+
+ \since 5.0
+
+ Returns the children of this object that can be cast to type T
+ and that have names matching the regular expression \a re,
+ or an empty list if there are no such objects.
+ The search is performed recursively, unless \a options specifies the
+ option FindDirectChildrenOnly.
*/
/*!
@@ -1587,7 +1625,7 @@ void QObject::killTimer(int id)
/*!
\internal
*/
-void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QRegExp *re,
+void qt_qFindChildren_helper(const QObject *parent, const QString &name,
const QMetaObject &mo, QList<void*> *list, Qt::FindChildOptions options)
{
if (!parent || !list)
@@ -1597,18 +1635,59 @@ void qt_qFindChildren_helper(const QObject *parent, const QString &name, const Q
for (int i = 0; i < children.size(); ++i) {
obj = children.at(i);
if (mo.cast(obj)) {
- if (re) {
- if (re->indexIn(obj->objectName()) != -1)
- list->append(obj);
- } else {
- if (name.isNull() || obj->objectName() == name)
- list->append(obj);
- }
+ if (name.isNull() || obj->objectName() == name)
+ list->append(obj);
+ }
+ if (options & Qt::FindChildrenRecursively)
+ qt_qFindChildren_helper(obj, name, mo, list, options);
+ }
+}
+
+#ifndef QT_NO_REGEXP
+/*!
+ \internal
+*/
+void qt_qFindChildren_helper(const QObject *parent, const QRegExp &re,
+ const QMetaObject &mo, QList<void*> *list, Qt::FindChildOptions options)
+{
+ if (!parent || !list)
+ return;
+ const QObjectList &children = parent->children();
+ QObject *obj;
+ for (int i = 0; i < children.size(); ++i) {
+ obj = children.at(i);
+ if (mo.cast(obj) && re.indexIn(obj->objectName()) != -1)
+ list->append(obj);
+
+ if (options & Qt::FindChildrenRecursively)
+ qt_qFindChildren_helper(obj, re, mo, list, options);
+ }
+}
+#endif // QT_NO_REGEXP
+
+#ifndef QT_NO_REGEXP
+/*!
+ \internal
+*/
+void qt_qFindChildren_helper(const QObject *parent, const QRegularExpression &re,
+ const QMetaObject &mo, QList<void*> *list, Qt::FindChildOptions options)
+{
+ if (!parent || !list)
+ return;
+ const QObjectList &children = parent->children();
+ QObject *obj;
+ for (int i = 0; i < children.size(); ++i) {
+ obj = children.at(i);
+ if (mo.cast(obj)) {
+ QRegularExpressionMatch m = re.match(obj->objectName());
+ if (m.hasMatch())
+ list->append(obj);
}
if (options & Qt::FindChildrenRecursively)
- qt_qFindChildren_helper(obj, name, re, mo, list, options);
+ qt_qFindChildren_helper(obj, re, mo, list, options);
}
}
+#endif // QT_NO_REGEXP
/*! \internal
*/
@@ -2179,12 +2258,12 @@ static inline void check_and_warn_compat(const QMetaObject *sender, const QMetaM
if (signal.attributes() & QMetaMethod::Compatibility) {
if (!(method.attributes() & QMetaMethod::Compatibility))
qWarning("QObject::connect: Connecting from COMPAT signal (%s::%s)",
- sender->className(), signal.signature());
+ sender->className(), signal.methodSignature().constData());
} else if ((method.attributes() & QMetaMethod::Compatibility) &&
method.methodType() == QMetaMethod::Signal) {
qWarning("QObject::connect: Connecting from %s::%s to COMPAT slot (%s::%s)",
- sender->className(), signal.signature(),
- receiver->className(), method.signature());
+ sender->className(), signal.methodSignature().constData(),
+ receiver->className(), method.methodSignature().constData());
}
}
@@ -2283,20 +2362,21 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
const QMetaObject *smeta = sender->metaObject();
const char *signal_arg = signal;
++signal; //skip code
- int signal_index = QMetaObjectPrivate::indexOfSignalRelative(&smeta, signal, false);
+ QArgumentTypeArray signalTypes;
+ Q_ASSERT(QMetaObjectPrivate::get(smeta)->revision >= 7);
+ QByteArray signalName = QMetaObjectPrivate::decodeMethodSignature(signal, signalTypes);
+ int signal_index = QMetaObjectPrivate::indexOfSignalRelative(
+ &smeta, signalName, signalTypes.size(), signalTypes.constData());
if (signal_index < 0) {
// check for normalized signatures
tmp_signal_name = QMetaObject::normalizedSignature(signal - 1);
signal = tmp_signal_name.constData() + 1;
+ signalTypes.clear();
+ signalName = QMetaObjectPrivate::decodeMethodSignature(signal, signalTypes);
smeta = sender->metaObject();
- signal_index = QMetaObjectPrivate::indexOfSignalRelative(&smeta, signal, false);
- }
- if (signal_index < 0) {
- // re-use tmp_signal_name and signal from above
-
- smeta = sender->metaObject();
- signal_index = QMetaObjectPrivate::indexOfSignalRelative(&smeta, signal, true);
+ signal_index = QMetaObjectPrivate::indexOfSignalRelative(
+ &smeta, signalName, signalTypes.size(), signalTypes.constData());
}
if (signal_index < 0) {
err_method_notfound(sender, signal_arg, "connect");
@@ -2317,34 +2397,38 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
const char *method_arg = method;
++method; // skip code
+ QByteArray methodName;
+ QArgumentTypeArray methodTypes;
const QMetaObject *rmeta = receiver->metaObject();
int method_index_relative = -1;
+ Q_ASSERT(QMetaObjectPrivate::get(rmeta)->revision >= 7);
switch (membcode) {
case QSLOT_CODE:
- method_index_relative = QMetaObjectPrivate::indexOfSlotRelative(&rmeta, method, false);
+ method_index_relative = QMetaObjectPrivate::indexOfSlotRelative(
+ &rmeta, methodName, methodTypes.size(), methodTypes.constData());
break;
case QSIGNAL_CODE:
- method_index_relative = QMetaObjectPrivate::indexOfSignalRelative(&rmeta, method, false);
+ method_index_relative = QMetaObjectPrivate::indexOfSignalRelative(
+ &rmeta, methodName, methodTypes.size(), methodTypes.constData());
break;
}
-
if (method_index_relative < 0) {
// check for normalized methods
tmp_method_name = QMetaObject::normalizedSignature(method);
method = tmp_method_name.constData();
+ methodTypes.clear();
+ methodName = QMetaObjectPrivate::decodeMethodSignature(method, methodTypes);
// rmeta may have been modified above
rmeta = receiver->metaObject();
switch (membcode) {
case QSLOT_CODE:
- method_index_relative = QMetaObjectPrivate::indexOfSlotRelative(&rmeta, method, false);
- if (method_index_relative < 0)
- method_index_relative = QMetaObjectPrivate::indexOfSlotRelative(&rmeta, method, true);
+ method_index_relative = QMetaObjectPrivate::indexOfSlotRelative(
+ &rmeta, methodName, methodTypes.size(), methodTypes.constData());
break;
case QSIGNAL_CODE:
- method_index_relative = QMetaObjectPrivate::indexOfSignalRelative(&rmeta, method, false);
- if (method_index_relative < 0)
- method_index_relative = QMetaObjectPrivate::indexOfSignalRelative(&rmeta, method, true);
+ method_index_relative = QMetaObjectPrivate::indexOfSignalRelative(
+ &rmeta, methodName, methodTypes.size(), methodTypes.constData());
break;
}
}
@@ -2355,7 +2439,8 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
return QMetaObject::Connection(0);
}
- if (!QMetaObject::checkConnectArgs(signal, method)) {
+ if (!QMetaObjectPrivate::checkConnectArgs(signalTypes.size(), signalTypes.constData(),
+ methodTypes.size(), methodTypes.constData())) {
qWarning("QObject::connect: Incompatible sender/receiver arguments"
"\n %s::%s --> %s::%s",
sender->metaObject()->className(), signal,
@@ -2365,8 +2450,9 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
int *types = 0;
if ((type == Qt::QueuedConnection)
- && !(types = queuedConnectionTypes(smeta->method(signal_absolute_index).parameterTypes())))
+ && !(types = queuedConnectionTypes(signalTypes.constData(), signalTypes.size()))) {
return QMetaObject::Connection(0);
+ }
#ifndef QT_NO_DEBUG
if (warnCompat) {
@@ -2424,17 +2510,17 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMetho
|| method.methodType() == QMetaMethod::Constructor) {
qWarning("QObject::connect: Cannot connect %s::%s to %s::%s",
sender ? sender->metaObject()->className() : "(null)",
- signal.signature(),
+ signal.methodSignature().constData(),
receiver ? receiver->metaObject()->className() : "(null)",
- method.signature() );
+ method.methodSignature().constData() );
return QMetaObject::Connection(0);
}
- // Reconstructing SIGNAL() macro result for signal.signature() string
+ // Reconstructing SIGNAL() macro result for signal.methodSignature() string
QByteArray signalSignature;
- signalSignature.reserve(qstrlen(signal.signature())+1);
+ signalSignature.reserve(signal.methodSignature().size()+1);
signalSignature.append((char)(QSIGNAL_CODE + '0'));
- signalSignature.append(signal.signature());
+ signalSignature.append(signal.methodSignature());
int signal_index;
int method_index;
@@ -2448,20 +2534,20 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMetho
const QMetaObject *rmeta = receiver->metaObject();
if (signal_index == -1) {
qWarning("QObject::connect: Can't find signal %s on instance of class %s",
- signal.signature(), smeta->className());
+ signal.methodSignature().constData(), smeta->className());
return QMetaObject::Connection(0);
}
if (method_index == -1) {
qWarning("QObject::connect: Can't find method %s on instance of class %s",
- method.signature(), rmeta->className());
+ method.methodSignature().constData(), rmeta->className());
return QMetaObject::Connection(0);
}
- if (!QMetaObject::checkConnectArgs(signal.signature(), method.signature())) {
+ if (!QMetaObject::checkConnectArgs(signal.methodSignature().constData(), method.methodSignature().constData())) {
qWarning("QObject::connect: Incompatible sender/receiver arguments"
"\n %s::%s --> %s::%s",
- smeta->className(), signal.signature(),
- rmeta->className(), method.signature());
+ smeta->className(), signal.methodSignature().constData(),
+ rmeta->className(), method.methodSignature().constData());
return QMetaObject::Connection(0);
}
@@ -2609,12 +2695,21 @@ bool QObject::disconnect(const QObject *sender, const char *signal,
*/
bool res = false;
const QMetaObject *smeta = sender->metaObject();
+ QByteArray signalName;
+ QArgumentTypeArray signalTypes;
+ Q_ASSERT(QMetaObjectPrivate::get(smeta)->revision >= 7);
+ if (signal)
+ signalName = QMetaObjectPrivate::decodeMethodSignature(signal, signalTypes);
+ QByteArray methodName;
+ QArgumentTypeArray methodTypes;
+ Q_ASSERT(!receiver || QMetaObjectPrivate::get(receiver->metaObject())->revision >= 7);
+ if (method)
+ methodName = QMetaObjectPrivate::decodeMethodSignature(method, methodTypes);
do {
int signal_index = -1;
if (signal) {
- signal_index = QMetaObjectPrivate::indexOfSignalRelative(&smeta, signal, false);
- if (signal_index < 0)
- signal_index = QMetaObjectPrivate::indexOfSignalRelative(&smeta, signal, true);
+ signal_index = QMetaObjectPrivate::indexOfSignalRelative(
+ &smeta, signalName, signalTypes.size(), signalTypes.constData());
if (signal_index < 0)
break;
signal_index = QMetaObjectPrivate::originalClone(smeta, signal_index);
@@ -2629,7 +2724,8 @@ bool QObject::disconnect(const QObject *sender, const char *signal,
} else {
const QMetaObject *rmeta = receiver->metaObject();
do {
- int method_index = rmeta->indexOfMethod(method);
+ int method_index = QMetaObjectPrivate::indexOfMethod(
+ rmeta, methodName, methodTypes.size(), methodTypes.constData());
if (method_index >= 0)
while (method_index < rmeta->methodOffset())
rmeta = rmeta->superClass();
@@ -2693,24 +2789,24 @@ bool QObject::disconnect(const QObject *sender, const QMetaMethod &signal,
if(signal.methodType() != QMetaMethod::Signal) {
qWarning("QObject::%s: Attempt to %s non-signal %s::%s",
"disconnect","unbind",
- sender->metaObject()->className(), signal.signature());
+ sender->metaObject()->className(), signal.methodSignature().constData());
return false;
}
}
if (method.mobj) {
if(method.methodType() == QMetaMethod::Constructor) {
qWarning("QObject::disconect: cannot use constructor as argument %s::%s",
- receiver->metaObject()->className(), method.signature());
+ receiver->metaObject()->className(), method.methodSignature().constData());
return false;
}
}
- // Reconstructing SIGNAL() macro result for signal.signature() string
+ // Reconstructing SIGNAL() macro result for signal.methodSignature() string
QByteArray signalSignature;
if (signal.mobj) {
- signalSignature.reserve(qstrlen(signal.signature())+1);
+ signalSignature.reserve(signal.methodSignature().size()+1);
signalSignature.append((char)(QSIGNAL_CODE + '0'));
- signalSignature.append(signal.signature());
+ signalSignature.append(signal.methodSignature());
}
int signal_index;
@@ -2724,13 +2820,13 @@ bool QObject::disconnect(const QObject *sender, const QMetaMethod &signal,
// is -1 then this signal is not a member of sender.
if (signal.mobj && signal_index == -1) {
qWarning("QObject::disconect: signal %s not found on class %s",
- signal.signature(), sender->metaObject()->className());
+ signal.methodSignature().constData(), sender->metaObject()->className());
return false;
}
// If this condition is true then method is not a member of receeiver.
if (receiver && method.mobj && method_index == -1) {
qWarning("QObject::disconect: method %s not found on class %s",
- method.signature(), receiver->metaObject()->className());
+ method.methodSignature().constData(), receiver->metaObject()->className());
return false;
}
@@ -2865,8 +2961,9 @@ QObjectPrivate::Connection *QMetaObjectPrivate::connect(const QObject *sender, i
QObject *r = const_cast<QObject *>(receiver);
int method_offset = rmeta ? rmeta->methodOffset() : 0;
+ Q_ASSERT(!rmeta || QMetaObjectPrivate::get(rmeta)->revision >= 6);
QObjectPrivate::StaticMetaCallFunction callFunction =
- (rmeta && QMetaObjectPrivate::get(rmeta)->revision >= 6 && rmeta->d.extradata)
+ (rmeta && rmeta->d.extradata)
? reinterpret_cast<const QMetaObjectExtraData *>(rmeta->d.extradata)->static_metacall : 0;
QOrderedMutexLocker locker(signalSlotLock(sender),
@@ -3045,7 +3142,8 @@ void QMetaObject::connectSlotsByName(QObject *o)
Q_ASSERT(mo);
const QObjectList list = o->findChildren<QObject *>(QString());
for (int i = 0; i < mo->methodCount(); ++i) {
- const char *slot = mo->method(i).signature();
+ QByteArray slotSignature = mo->method(i).methodSignature();
+ const char *slot = slotSignature.constData();
Q_ASSERT(slot);
if (slot[0] != 'o' || slot[1] != 'n' || slot[2] != '_')
continue;
@@ -3065,7 +3163,7 @@ void QMetaObject::connectSlotsByName(QObject *o)
if (method.methodType() != QMetaMethod::Signal)
continue;
- if (!qstrncmp(method.signature(), slot + len + 4, slotlen)) {
+ if (!qstrncmp(method.methodSignature().constData(), slot + len + 4, slotlen)) {
int signalOffset, methodOffset;
computeOffsets(method.enclosingMetaObject(), &signalOffset, &methodOffset);
sigIndex = k + - methodOffset + signalOffset;
@@ -3311,9 +3409,11 @@ int QObjectPrivate::signalIndex(const char *signalName) const
{
Q_Q(const QObject);
const QMetaObject *base = q->metaObject();
- int relative_index = QMetaObjectPrivate::indexOfSignalRelative(&base, signalName, false);
- if (relative_index < 0)
- relative_index = QMetaObjectPrivate::indexOfSignalRelative(&base, signalName, true);
+ Q_ASSERT(QMetaObjectPrivate::get(base)->revision >= 7);
+ QArgumentTypeArray types;
+ QByteArray name = QMetaObjectPrivate::decodeMethodSignature(signalName, types);
+ int relative_index = QMetaObjectPrivate::indexOfSignalRelative(
+ &base, name, types.size(), types.constData());
if (relative_index < 0)
return relative_index;
relative_index = QMetaObjectPrivate::originalClone(base, relative_index);
@@ -3536,7 +3636,7 @@ void QObject::dumpObjectInfo()
offset = methodOffset - signalOffset;
}
const QMetaMethod signal = metaObject()->method(signal_index + offset);
- qDebug(" signal: %s", signal.signature());
+ qDebug(" signal: %s", signal.methodSignature().constData());
// receivers
const QObjectPrivate::Connection *c =
@@ -3552,7 +3652,7 @@ void QObject::dumpObjectInfo()
qDebug(" --> %s::%s %s",
receiverMetaObject->className(),
c->receiver->objectName().isEmpty() ? "unnamed" : qPrintable(c->receiver->objectName()),
- method.signature());
+ method.methodSignature().constData());
c = c->nextConnectionList;
}
}
@@ -3569,7 +3669,7 @@ void QObject::dumpObjectInfo()
qDebug(" <-- %s::%s %s",
s->sender->metaObject()->className(),
s->sender->objectName().isEmpty() ? "unnamed" : qPrintable(s->sender->objectName()),
- slot.signature());
+ slot.methodSignature().constData());
}
} else {
qDebug(" <None>");
@@ -4048,9 +4148,9 @@ QMetaObject::Connection QObject::connectImpl(const QObject *sender, void **signa
locker.unlock();
// reconstruct the signature to call connectNotify
- const char *sig = senderMetaObject->d.stringdata + senderMetaObject->d.data[
- reinterpret_cast<const QMetaObjectPrivate*>(senderMetaObject->d.data)->methodData
- + 5 * (signal_index - signalOffset)];
+ const char *sig;
+ QByteArray tmp_sig = senderMetaObject->method(signal_index - signalOffset + methodOffset).methodSignature();
+ sig = tmp_sig.constData();
QVarLengthArray<char> signalSignature(qstrlen(sig) + 2);
signalSignature.data()[0] = char(QSIGNAL_CODE + '0');
strcpy(signalSignature.data() + 1 , sig);
diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h
index 9f09617071..37057bea50 100644
--- a/src/corelib/kernel/qobject.h
+++ b/src/corelib/kernel/qobject.h
@@ -73,13 +73,20 @@ class QWidget;
#ifndef QT_NO_REGEXP
class QRegExp;
#endif
+#ifndef QT_NO_REGEXP
+class QRegularExpression;
+#endif
#ifndef QT_NO_USERDATA
class QObjectUserData;
#endif
typedef QList<QObject*> QObjectList;
-Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QRegExp *re,
+Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QString &name,
+ const QMetaObject &mo, QList<void *> *list, Qt::FindChildOptions options);
+Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QRegExp &re,
+ const QMetaObject &mo, QList<void *> *list, Qt::FindChildOptions options);
+Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QRegularExpression &re,
const QMetaObject &mo, QList<void *> *list, Qt::FindChildOptions options);
Q_CORE_EXPORT QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo, Qt::FindChildOptions options);
@@ -163,7 +170,7 @@ public:
QList<void *> *voidList;
} u;
u.typedList = &list;
- qt_qFindChildren_helper(this, aName, 0, reinterpret_cast<T>(0)->staticMetaObject, u.voidList, options);
+ qt_qFindChildren_helper(this, aName, reinterpret_cast<T>(0)->staticMetaObject, u.voidList, options);
return list;
}
@@ -177,7 +184,22 @@ public:
QList<void *> *voidList;
} u;
u.typedList = &list;
- qt_qFindChildren_helper(this, QString(), &re, reinterpret_cast<T>(0)->staticMetaObject, u.voidList, options);
+ qt_qFindChildren_helper(this, re, reinterpret_cast<T>(0)->staticMetaObject, u.voidList, options);
+ return list;
+ }
+#endif
+
+#ifndef QT_NO_REGEXP
+ template<typename T>
+ inline QList<T> findChildren(const QRegularExpression &re, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
+ {
+ QList<T> list;
+ union {
+ QList<T> *typedList;
+ QList<void *> *voidList;
+ } u;
+ u.typedList = &list;
+ qt_qFindChildren_helper(this, re, reinterpret_cast<T>(0)->staticMetaObject, u.voidList, options);
return list;
}
#endif
diff --git a/src/corelib/kernel/qobjectdefs.h b/src/corelib/kernel/qobjectdefs.h
index 4b3829b53e..4c98bad0f9 100644
--- a/src/corelib/kernel/qobjectdefs.h
+++ b/src/corelib/kernel/qobjectdefs.h
@@ -50,11 +50,12 @@ QT_BEGIN_NAMESPACE
class QByteArray;
+struct QByteArrayData;
class QString;
#ifndef Q_MOC_OUTPUT_REVISION
-#define Q_MOC_OUTPUT_REVISION 64
+#define Q_MOC_OUTPUT_REVISION 65
#endif
// The following macros are our "extensions" to C++
@@ -326,6 +327,8 @@ struct Q_CORE_EXPORT QMetaObject
QMetaProperty userProperty() const;
static bool checkConnectArgs(const char *signal, const char *method);
+ static bool checkConnectArgs(const QMetaMethod &signal,
+ const QMetaMethod &method);
static QByteArray normalizedSignature(const char *method);
static QByteArray normalizedType(const char *type);
@@ -439,7 +442,7 @@ struct Q_CORE_EXPORT QMetaObject
struct { // private data
const QMetaObject *superdata;
- const char *stringdata;
+ const QByteArrayData *stringdata;
const uint *data;
const void *extradata;
} d;
@@ -480,9 +483,6 @@ struct QMetaObjectExtraData
StaticMetacallFunction static_metacall;
};
-inline const char *QMetaObject::className() const
-{ return d.stringdata; }
-
inline const QMetaObject *QMetaObject::superClass() const
{ return d.superdata; }
diff --git a/src/corelib/kernel/qpointer.h b/src/corelib/kernel/qpointer.h
index 836c13e3fc..a3035ebc94 100644
--- a/src/corelib/kernel/qpointer.h
+++ b/src/corelib/kernel/qpointer.h
@@ -44,40 +44,60 @@
#include <QtCore/qsharedpointer.h>
+#ifndef QT_NO_QOBJECT
+
QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
-template <class T>
-class QPointer
+class QPointerBase
{
- QWeakPointer<T> wp;
+ QWeakPointer<QObject> wp;
-public:
- inline QPointer() : wp() { }
- inline QPointer(T *p) : wp(p) { }
- inline QPointer(const QPointer<T> &p) : wp(p.wp) { }
- inline ~QPointer() { }
+protected:
+ inline QPointerBase() : wp() { }
+ inline QPointerBase(QObject *p) : wp(p) { }
+ // compiler-generated copy/move ctor/assignment operators are fine! (even though public)
+ inline ~QPointerBase() { }
- inline QPointer<T> &operator=(const QPointer<T> &p)
- { wp = p.wp; return *this; }
- inline QPointer<T> &operator=(T* p)
- { wp = p; return *this; }
+ inline QObject* data() const
+ { return wp.data(); }
+
+ inline void assign(QObject *p)
+ { wp = p; }
inline bool isNull() const
{ return wp.isNull(); }
+};
+
+template <class T>
+class QPointer : private QPointerBase
+{
+public:
+ inline QPointer() { }
+ inline QPointer(T *p) : QPointerBase(p) { }
+ // compiler-generated copy/move ctor/assignment operators are fine!
+ inline ~QPointer() { }
+
+ inline QPointer<T> &operator=(T* p)
+ { QPointerBase::assign(p); return *this; }
+ inline T* data() const
+ { return static_cast<T*>(QPointerBase::data()); }
inline T* operator->() const
- { return wp.data(); }
+ { return data(); }
inline T& operator*() const
- { return *wp.data(); }
+ { return *data(); }
inline operator T*() const
- { return wp.data(); }
- inline T* data() const
- { return wp.data(); }
+ { return data(); }
+#ifdef qdoc
+ inline bool isNull() const;
+#else
+ using QPointerBase::isNull;
+#endif
};
-
+template <class T> Q_DECLARE_TYPEINFO_BODY(QPointer<T>, Q_MOVABLE_TYPE);
#if (!defined(Q_CC_SUN) || (__SUNPRO_CC >= 0x580)) // ambiguity between const T * and T *
@@ -163,4 +183,6 @@ QT_END_NAMESPACE
QT_END_HEADER
+#endif // QT_NO_QOBJECT
+
#endif // QPOINTER_H
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp
index e630b5bba8..19b999a1e2 100644
--- a/src/corelib/kernel/qvariant.cpp
+++ b/src/corelib/kernel/qvariant.cpp
@@ -48,6 +48,7 @@
#include "qdatetime.h"
#include "qeasingcurve.h"
#include "qlist.h"
+#include "qregularexpression.h"
#include "qstring.h"
#include "qstringlist.h"
#include "qurl.h"
@@ -55,6 +56,10 @@
#include "quuid.h"
#ifndef QT_BOOTSTRAPPED
#include "qabstractitemmodel.h"
+#include "qjsonvalue.h"
+#include "qjsonobject.h"
+#include "qjsonarray.h"
+#include "qjsondocument.h"
#endif
#include "private/qvariant_p.h"
#include "qmetatype_p.h"
@@ -91,8 +96,6 @@ public:
{
Handlers[name] = handler;
}
-
- inline void unregisterHandler(const QModulesPrivate::Names name);
};
} // namespace
@@ -106,6 +109,11 @@ struct TypeDefinition {
#ifdef QT_BOOTSTRAPPED
template<> struct TypeDefinition<QEasingCurve> { static const bool IsAvailable = false; };
template<> struct TypeDefinition<QModelIndex> { static const bool IsAvailable = false; };
+template<> struct TypeDefinition<QRegularExpression> { static const bool IsAvailable = false; };
+template<> struct TypeDefinition<QJsonValue> { static const bool IsAvailable = false; };
+template<> struct TypeDefinition<QJsonObject> { static const bool IsAvailable = false; };
+template<> struct TypeDefinition<QJsonArray> { static const bool IsAvailable = false; };
+template<> struct TypeDefinition<QJsonDocument> { static const bool IsAvailable = false; };
#endif
#ifdef QT_NO_GEOM_VARIANT
template<> struct TypeDefinition<QRect> { static const bool IsAvailable = false; };
@@ -882,21 +890,11 @@ Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler()
return &qt_kernel_variant_handler;
}
-inline void HandlersManager::unregisterHandler(const QModulesPrivate::Names name)
-{
- Handlers[name] = &qt_dummy_variant_handler;
-}
-
Q_CORE_EXPORT void QVariantPrivate::registerHandler(const int /* Modules::Names */name, const QVariant::Handler *handler)
{
handlerManager.registerHandler(static_cast<QModulesPrivate::Names>(name), handler);
}
-Q_CORE_EXPORT void QVariantPrivate::unregisterHandler(const int /* Modules::Names */ name)
-{
- handlerManager.unregisterHandler(static_cast<QModulesPrivate::Names>(name));
-}
-
/*!
\class QVariant
\brief The QVariant class acts like a union for the most common Qt data types.
@@ -1028,6 +1026,7 @@ Q_CORE_EXPORT void QVariantPrivate::unregisterHandler(const int /* Modules::Name
\value Rect a QRect
\value RectF a QRectF
\value RegExp a QRegExp
+ \value RegularExpression a QRegularExpression
\value Region a QRegion
\value Size a QSize
\value SizeF a QSizeF
@@ -1358,6 +1357,14 @@ QVariant::QVariant(const char *val)
Constructs a new variant with the regexp value \a regExp.
*/
+/*!
+ \fn QVariant::QVariant(const QRegularExpression &re)
+
+ \since 5.0
+
+ Constructs a new variant with the regular expression value \a re.
+*/
+
/*! \since 4.2
\fn QVariant::QVariant(Qt::GlobalColor color)
@@ -1446,7 +1453,10 @@ QVariant::QVariant(const QUrl &u) { d.is_null = false; d.type = Url; v_construct
QVariant::QVariant(const QLocale &l) { d.is_null = false; d.type = Locale; v_construct<QLocale>(&d, l); }
#ifndef QT_NO_REGEXP
QVariant::QVariant(const QRegExp &regExp) { d.is_null = false; d.type = RegExp; v_construct<QRegExp>(&d, regExp); }
-#endif
+#ifndef QT_BOOTSTRAPPED
+QVariant::QVariant(const QRegularExpression &re) { d.is_null = false; d.type = QMetaType::QRegularExpression; v_construct<QRegularExpression>(&d, re); }
+#endif // QT_BOOTSTRAPPED
+#endif // QT_NO_REGEXP
QVariant::QVariant(Qt::GlobalColor color) { create(62, &color); }
/*!
@@ -1697,7 +1707,7 @@ void QVariant::load(QDataStream &s)
QByteArray name;
s >> name;
typeId = QMetaType::type(name.constData());
- if (!typeId) {
+ if (typeId == QMetaType::UnknownType) {
s.setStatus(QDataStream::ReadCorruptData);
return;
}
@@ -2127,6 +2137,24 @@ QRegExp QVariant::toRegExp() const
#endif
/*!
+ \fn QRegularExpression QVariant::toRegularExpression() const
+ \since 5.0
+
+ Returns the variant as a QRegularExpression if the variant has type() \l
+ QRegularExpression; otherwise returns an empty QRegularExpression.
+
+ \sa canConvert(), convert()
+*/
+#ifndef QT_BOOTSTRAPPED
+#ifndef QT_NO_REGEXP
+QRegularExpression QVariant::toRegularExpression() const
+{
+ return qVariantToHelper<QRegularExpression>(d, handlerManager);
+}
+#endif
+#endif
+
+/*!
\fn QChar QVariant::toChar() const
Returns the variant as a QChar if the variant has type() \l Char,
@@ -2646,15 +2674,24 @@ bool QVariant::isNull() const
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QVariant &v)
{
- dbg.nospace() << "QVariant(" << QMetaType::typeName(v.userType()) << ", ";
- handlerManager[v.d.type]->debugStream(dbg, v);
+ const uint typeId = v.d.type;
+ dbg.nospace() << "QVariant(";
+ if (typeId != QMetaType::UnknownType) {
+ dbg.nospace() << QMetaType::typeName(typeId) << ", ";
+ handlerManager[typeId]->debugStream(dbg, v);
+ } else {
+ dbg.nospace() << "Invalid";
+ }
dbg.nospace() << ')';
return dbg.space();
}
QDebug operator<<(QDebug dbg, const QVariant::Type p)
{
- dbg.nospace() << "QVariant::" << QMetaType::typeName(p);
+ dbg.nospace() << "QVariant::"
+ << (int(p) != int(QMetaType::UnknownType)
+ ? QMetaType::typeName(p)
+ : "Invalid");
return dbg.space();
}
#endif
diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h
index 5da482d5cd..cc502d93a7 100644
--- a/src/corelib/kernel/qvariant.h
+++ b/src/corelib/kernel/qvariant.h
@@ -76,7 +76,8 @@ class QRect;
class QRectF;
#ifndef QT_NO_REGEXP
class QRegExp;
-#endif
+class QRegularExpression;
+#endif // QT_NO_REGEXP
class QTextFormat;
class QTextLength;
class QUrl;
@@ -126,7 +127,7 @@ class Q_CORE_EXPORT QVariant
{
public:
enum Type {
- Invalid = QMetaType::Void,
+ Invalid = QMetaType::UnknownType,
Bool = QMetaType::Bool,
Int = QMetaType::Int,
UInt = QMetaType::UInt,
@@ -154,6 +155,7 @@ class Q_CORE_EXPORT QVariant
Point = QMetaType::QPoint,
PointF = QMetaType::QPointF,
RegExp = QMetaType::QRegExp,
+ RegularExpression = QMetaType::QRegularExpression,
Hash = QMetaType::QVariantHash,
EasingCurve = QMetaType::QEasingCurve,
Uuid = QMetaType::QUuid,
@@ -239,7 +241,10 @@ class Q_CORE_EXPORT QVariant
QVariant(const QLocale &locale);
#ifndef QT_NO_REGEXP
QVariant(const QRegExp &regExp);
-#endif
+#ifndef QT_BOOTSRAPPED
+ QVariant(const QRegularExpression &re);
+#endif // QT_BOOTSTRAPPED
+#endif // QT_NO_REGEXP
#ifndef QT_BOOTSTRAPPED
QVariant(const QEasingCurve &easing);
#endif
@@ -302,7 +307,10 @@ class Q_CORE_EXPORT QVariant
QLocale toLocale() const;
#ifndef QT_NO_REGEXP
QRegExp toRegExp() const;
-#endif
+#ifndef QT_BOOTSTRAPPED
+ QRegularExpression toRegularExpression() const;
+#endif // QT_BOOTSTRAPPED
+#endif // QT_NO_REGEXP
#ifndef QT_BOOTSTRAPPED
QEasingCurve toEasingCurve() const;
#endif
diff --git a/src/corelib/kernel/qvariant_p.h b/src/corelib/kernel/qvariant_p.h
index a754bc4363..2f5c4f54ee 100644
--- a/src/corelib/kernel/qvariant_p.h
+++ b/src/corelib/kernel/qvariant_p.h
@@ -172,7 +172,12 @@ class QVariantComparator {
};
template<typename T>
struct FilteredComparator<T, /* IsAcceptedType = */ false> {
- static bool compare(const QVariant::Private *, const QVariant::Private *) { return false; }
+ static bool compare(const QVariant::Private *, const QVariant::Private *)
+ {
+ // It is not possible to construct a QVariant containing not fully defined type
+ Q_ASSERT(false);
+ return false;
+ }
};
public:
QVariantComparator(const QVariant::Private *a, const QVariant::Private *b)
@@ -187,7 +192,11 @@ public:
return FilteredComparator<T>::compare(m_a, m_b);
}
- bool delegate(const void*) { return true; }
+ bool delegate(const void*) { Q_ASSERT(false); return true; }
+ bool delegate(const QMetaTypeSwitcher::UnknownType*)
+ {
+ return true; // for historical reason invalid variant == invalid variant
+ }
bool delegate(const QMetaTypeSwitcher::NotBuiltinType*) { return false; }
protected:
const QVariant::Private *m_a;
@@ -203,6 +212,19 @@ class QVariantIsNull
/// \internal
/// This class checks if a type T has method called isNull. Result is kept in the Value property
/// TODO Can we somehow generalize it? A macro version?
+#if defined(Q_COMPILER_DECLTYPE) // C++11 version
+ template<typename T>
+ class HasIsNullMethod {
+ struct Yes { char unused[1]; };
+ struct No { char unused[2]; };
+ Q_STATIC_ASSERT(sizeof(Yes) != sizeof(No));
+
+ template<class C> static decltype(static_cast<const C*>(0)->isNull(), Yes()) test(int);
+ template<class C> static No test(...);
+ public:
+ static const bool Value = (sizeof(test<T>(0)) == sizeof(Yes));
+ };
+#else // C++98 version (doesn't work for final classes)
template<typename T, bool IsClass = QTypeInfo<T>::isComplex>
class HasIsNullMethod
{
@@ -211,7 +233,7 @@ class QVariantIsNull
Q_STATIC_ASSERT(sizeof(Yes) != sizeof(No));
struct FallbackMixin { bool isNull() const; };
- struct Derived : public T, public FallbackMixin {};
+ struct Derived : public T, public FallbackMixin {}; // <- doesn't work for final classes
template<class C, C> struct TypeCheck {};
template<class C> static Yes test(...);
@@ -227,6 +249,7 @@ class QVariantIsNull
public:
static const bool Value = false;
};
+#endif
// TODO This part should go to autotests during HasIsNullMethod generalization.
Q_STATIC_ASSERT(!HasIsNullMethod<bool>::Value);
@@ -236,6 +259,12 @@ class QVariantIsNull
Q_STATIC_ASSERT(!HasIsNullMethod<SelfTest2>::Value);
struct SelfTest3 : public SelfTest1 {};
Q_STATIC_ASSERT(HasIsNullMethod<SelfTest3>::Value);
+ struct SelfTestFinal1 Q_DECL_FINAL_CLASS { bool isNull() const; };
+ Q_STATIC_ASSERT(HasIsNullMethod<SelfTestFinal1>::Value);
+ struct SelfTestFinal2 Q_DECL_FINAL_CLASS {};
+ Q_STATIC_ASSERT(!HasIsNullMethod<SelfTestFinal2>::Value);
+ struct SelfTestFinal3 Q_DECL_FINAL_CLASS : public SelfTest1 {};
+ Q_STATIC_ASSERT(HasIsNullMethod<SelfTestFinal3>::Value);
template<typename T, bool HasIsNull = HasIsNullMethod<T>::Value>
struct CallFilteredIsNull
@@ -281,8 +310,14 @@ public:
return CallIsNull<T>::isNull(m_d);
}
// we need that as sizof(void) is undefined and it is needed in HasIsNullMethod
- bool delegate(const void *) { return m_d->is_null; }
- bool delegate(const QMetaTypeSwitcher::NotBuiltinType *) { return m_d->is_null; }
+ bool delegate(const void *) { Q_ASSERT(false); return m_d->is_null; }
+ bool delegate(const QMetaTypeSwitcher::UnknownType *) { return m_d->is_null; }
+ bool delegate(const QMetaTypeSwitcher::NotBuiltinType *)
+ {
+ // QVariantIsNull is used only for built-in types
+ Q_ASSERT(false);
+ return m_d->is_null;
+ }
protected:
const QVariant::Private *m_d;
};
@@ -348,14 +383,24 @@ public:
void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
{
- qWarning("Trying to construct an instance of an invalid type, type id: %i", m_x->type);
- m_x->type = QVariant::Invalid;
+ // QVariantConstructor is used only for built-in types.
+ Q_ASSERT(false);
}
void delegate(const void*)
{
- // QMetaType::Void == QVariant::Invalid, creating an invalid value creates invalid QVariant
- // TODO it might go away, check is needed
+ qWarning("Trying to create a QVariant instance of QMetaType::Void type, an invalid QVariant will be constructed instead");
+ m_x->type = QMetaType::UnknownType;
+ m_x->is_shared = false;
+ m_x->is_null = !m_copy;
+ }
+
+ void delegate(const QMetaTypeSwitcher::UnknownType*)
+ {
+ if (m_x->type != QMetaType::UnknownType) {
+ qWarning("Trying to construct an instance of an invalid type, type id: %i", m_x->type);
+ m_x->type = QMetaType::UnknownType;
+ }
m_x->is_shared = false;
m_x->is_null = !m_copy;
}
@@ -376,7 +421,11 @@ class QVariantDestructor
};
template<typename T>
struct FilteredDestructor<T, /* IsAcceptedType = */ false> {
- FilteredDestructor(QVariant::Private *) {} // ignore non accessible types
+ FilteredDestructor(QVariant::Private *)
+ {
+ // It is not possible to create not accepted type
+ Q_ASSERT(false);
+ }
};
public:
@@ -398,17 +447,18 @@ public:
void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
{
- qWarning("Trying to destruct an instance of an invalid type, type id: %i", m_d->type);
+ // QVariantDestructor class is used only for a built-in type
+ Q_ASSERT(false);
}
// Ignore nonconstructible type
- void delegate(const void*) {}
+ void delegate(const QMetaTypeSwitcher::UnknownType*) {}
+ void delegate(const void*) { Q_ASSERT(false); }
private:
QVariant::Private *m_d;
};
namespace QVariantPrivate {
Q_CORE_EXPORT void registerHandler(const int /* Modules::Names */ name, const QVariant::Handler *handler);
-Q_CORE_EXPORT void unregisterHandler(const int /* Modules::Names */ name);
}
#if !defined(QT_NO_DEBUG_STREAM)
@@ -424,9 +474,10 @@ class QVariantDebugStream
};
template<typename T>
struct Filtered<T, /* IsAcceptedType = */ false> {
- Filtered(QDebug dbg, QVariant::Private *d)
+ Filtered(QDebug /* dbg */, QVariant::Private *)
{
- dbg.nospace() << "QVariant::Type(" << d->type << ")";
+ // It is not possible to construct not acccepted type, QVariantConstructor creates an invalid variant for them
+ Q_ASSERT(false);
}
};
@@ -444,12 +495,14 @@ public:
void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
{
- qWarning("Trying to stream an instance of an invalid type, type id: %i", m_d->type);
+ // QVariantDebugStream class is used only for a built-in type
+ Q_ASSERT(false);
}
- void delegate(const void*)
+ void delegate(const QMetaTypeSwitcher::UnknownType*)
{
m_debugStream.nospace() << "QVariant::Invalid";
}
+ void delegate(const void*) { Q_ASSERT(false); }
private:
QDebug m_debugStream;
QVariant::Private *m_d;
diff --git a/src/corelib/statemachine/qstatemachine.cpp b/src/corelib/statemachine/qstatemachine.cpp
index 7ff005f9a1..be96d895a2 100644
--- a/src/corelib/statemachine/qstatemachine.cpp
+++ b/src/corelib/statemachine/qstatemachine.cpp
@@ -1657,7 +1657,7 @@ void QStateMachinePrivate::handleTransitionSignal(QObject *sender, int signalInd
#ifdef QSTATEMACHINE_DEBUG
qDebug() << q_func() << ": sending signal event ( sender =" << sender
- << ", signal =" << sender->metaObject()->method(signalIndex).signature() << ')';
+ << ", signal =" << sender->metaObject()->method(signalIndex).methodSignature().constData() << ')';
#endif
postInternalEvent(new QStateMachine::SignalEvent(sender, signalIndex, vargs));
processEvents(DirectProcessing);
@@ -2211,10 +2211,29 @@ void QStateMachine::removeDefaultAnimation(QAbstractAnimation *animation)
// Begin moc-generated code -- modify carefully (check "HAND EDIT" parts)!
+struct qt_meta_stringdata_QSignalEventGenerator_t {
+ QByteArrayData data[3];
+ char stringdata[32];
+};
+#define QT_MOC_LITERAL(idx, ofs, len) { \
+ Q_REFCOUNT_INITIALIZE_STATIC, len, 0, 0, \
+ offsetof(qt_meta_stringdata_QSignalEventGenerator_t, stringdata) + ofs \
+ - idx * sizeof(QByteArrayData) \
+ }
+static const qt_meta_stringdata_QSignalEventGenerator_t qt_meta_stringdata_QSignalEventGenerator = {
+ {
+QT_MOC_LITERAL(0, 0, 21),
+QT_MOC_LITERAL(1, 22, 7),
+QT_MOC_LITERAL(2, 30, 0)
+ },
+ "QSignalEventGenerator\0execute\0\0"
+};
+#undef QT_MOC_LITERAL
+
static const uint qt_meta_data_QSignalEventGenerator[] = {
// content:
- 6, // revision
+ 7, // revision
0, // classname
0, 0, // classinfo
1, 14, // methods
@@ -2224,14 +2243,13 @@ static const uint qt_meta_data_QSignalEventGenerator[] = {
0, // flags
0, // signalCount
- // slots: signature, parameters, type, tag, flags
- 23, 22, 22, 22, 0x0a,
+ // slots: name, argc, parameters, tag, flags
+ 1, 0, 19, 2, 0x0a,
- 0 // eod
-};
+ // slots: parameters
+ QMetaType::Void,
-static const char qt_meta_stringdata_QSignalEventGenerator[] = {
- "QSignalEventGenerator\0\0execute()\0"
+ 0 // eod
};
void QSignalEventGenerator::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
@@ -2252,7 +2270,7 @@ const QMetaObjectExtraData QSignalEventGenerator::staticMetaObjectExtraData = {
};
const QMetaObject QSignalEventGenerator::staticMetaObject = {
- { &QObject::staticMetaObject, qt_meta_stringdata_QSignalEventGenerator,
+ { &QObject::staticMetaObject, qt_meta_stringdata_QSignalEventGenerator.data,
qt_meta_data_QSignalEventGenerator, &staticMetaObjectExtraData }
};
@@ -2264,7 +2282,7 @@ const QMetaObject *QSignalEventGenerator::metaObject() const
void *QSignalEventGenerator::qt_metacast(const char *_clname)
{
if (!_clname) return 0;
- if (!strcmp(_clname, qt_meta_stringdata_QSignalEventGenerator))
+ if (!strcmp(_clname, qt_meta_stringdata_QSignalEventGenerator.stringdata))
return static_cast<void*>(const_cast< QSignalEventGenerator*>(this));
return QObject::qt_metacast(_clname);
}
diff --git a/src/corelib/statemachine/qstatemachine.h b/src/corelib/statemachine/qstatemachine.h
index c9c60976d1..b3aeb41016 100644
--- a/src/corelib/statemachine/qstatemachine.h
+++ b/src/corelib/statemachine/qstatemachine.h
@@ -119,7 +119,7 @@ public:
NoCommonAncestorForTransitionError
};
- QStateMachine(QObject *parent = 0);
+ explicit QStateMachine(QObject *parent = 0);
~QStateMachine();
void addState(QAbstractState *state);
diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp
new file mode 100644
index 0000000000..8498d0e4d5
--- /dev/null
+++ b/src/corelib/tools/qarraydata.cpp
@@ -0,0 +1,111 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtCore/qarraydata.h>
+#include <QtCore/private/qtools_p.h>
+
+#include <stdlib.h>
+
+QT_BEGIN_NAMESPACE
+
+const QArrayData QArrayData::shared_null = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, 0 };
+
+static const QArrayData qt_array_empty = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, 0 };
+static const QArrayData qt_array_unsharable_empty = { { Q_BASIC_ATOMIC_INITIALIZER(0) }, 0, 0, 0, 0 };
+
+QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment,
+ size_t capacity, AllocationOptions options)
+{
+ // Alignment is a power of two
+ Q_ASSERT(alignment >= Q_ALIGNOF(QArrayData)
+ && !(alignment & (alignment - 1)));
+
+ // Don't allocate empty headers
+ if (!(options & RawData) && !capacity)
+ return !(options & Unsharable)
+ ? const_cast<QArrayData *>(&qt_array_empty)
+ : const_cast<QArrayData *>(&qt_array_unsharable_empty);
+
+ size_t headerSize = sizeof(QArrayData);
+
+ // Allocate extra (alignment - Q_ALIGNOF(QArrayData)) padding bytes so we
+ // can properly align the data array. This assumes malloc is able to
+ // provide appropriate alignment for the header -- as it should!
+ // Padding is skipped when allocating a header for RawData.
+ if (!(options & RawData))
+ headerSize += (alignment - Q_ALIGNOF(QArrayData));
+
+ // Allocate additional space if array is growing
+ if (options & Grow)
+ capacity = qAllocMore(objectSize * capacity, headerSize) / objectSize;
+
+ size_t allocSize = headerSize + objectSize * capacity;
+
+ QArrayData *header = static_cast<QArrayData *>(::malloc(allocSize));
+ if (header) {
+ quintptr data = (quintptr(header) + sizeof(QArrayData) + alignment - 1)
+ & ~(alignment - 1);
+
+ header->ref.atomic.store(bool(!(options & Unsharable)));
+ header->size = 0;
+ header->alloc = capacity;
+ header->capacityReserved = bool(options & CapacityReserved);
+ header->offset = data - quintptr(header);
+ }
+
+ return header;
+}
+
+void QArrayData::deallocate(QArrayData *data, size_t objectSize,
+ size_t alignment)
+{
+ // Alignment is a power of two
+ Q_ASSERT(alignment >= Q_ALIGNOF(QArrayData)
+ && !(alignment & (alignment - 1)));
+ Q_UNUSED(objectSize) Q_UNUSED(alignment)
+
+ if (data == &qt_array_unsharable_empty)
+ return;
+
+ ::free(data);
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h
new file mode 100644
index 0000000000..351a75aade
--- /dev/null
+++ b/src/corelib/tools/qarraydata.h
@@ -0,0 +1,275 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QARRAYDATA_H
+#define QARRAYDATA_H
+
+#include <QtCore/qrefcount.h>
+#include <string.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+struct Q_CORE_EXPORT QArrayData
+{
+ QtPrivate::RefCount ref;
+ int size;
+ uint alloc : 31;
+ uint capacityReserved : 1;
+
+ qptrdiff offset; // in bytes from beginning of header
+
+ void *data()
+ {
+ Q_ASSERT(size == 0
+ || offset < 0 || size_t(offset) >= sizeof(QArrayData));
+ return reinterpret_cast<char *>(this) + offset;
+ }
+
+ const void *data() const
+ {
+ Q_ASSERT(size == 0
+ || offset < 0 || size_t(offset) >= sizeof(QArrayData));
+ return reinterpret_cast<const char *>(this) + offset;
+ }
+
+ // This refers to array data mutability, not "header data" represented by
+ // data members in QArrayData. Shared data (array and header) must still
+ // follow COW principles.
+ bool isMutable() const
+ {
+ return alloc != 0;
+ }
+
+ enum AllocationOption {
+ CapacityReserved = 0x1,
+ Unsharable = 0x2,
+ RawData = 0x4,
+ Grow = 0x8,
+
+ Default = 0
+ };
+
+ Q_DECLARE_FLAGS(AllocationOptions, AllocationOption)
+
+ AllocationOptions detachFlags() const
+ {
+ AllocationOptions result;
+ if (!ref.isSharable())
+ result |= Unsharable;
+ if (capacityReserved)
+ result |= CapacityReserved;
+ return result;
+ }
+
+ AllocationOptions cloneFlags() const
+ {
+ AllocationOptions result;
+ if (capacityReserved)
+ result |= CapacityReserved;
+ return result;
+ }
+
+ static QArrayData *allocate(size_t objectSize, size_t alignment,
+ size_t capacity, AllocationOptions options = Default)
+ Q_REQUIRED_RESULT;
+ static void deallocate(QArrayData *data, size_t objectSize,
+ size_t alignment);
+
+ static const QArrayData shared_null;
+};
+
+Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::AllocationOptions)
+
+template <class T>
+struct QTypedArrayData
+ : QArrayData
+{
+ typedef T *iterator;
+ typedef const T *const_iterator;
+
+ T *data() { return static_cast<T *>(QArrayData::data()); }
+ const T *data() const { return static_cast<const T *>(QArrayData::data()); }
+
+ T *begin() { return data(); }
+ T *end() { return data() + size; }
+ const T *begin() const { return data(); }
+ const T *end() const { return data() + size; }
+
+ class AlignmentDummy { QArrayData header; T data; };
+
+ static QTypedArrayData *allocate(size_t capacity,
+ AllocationOptions options = Default) Q_REQUIRED_RESULT
+ {
+ return static_cast<QTypedArrayData *>(QArrayData::allocate(sizeof(T),
+ Q_ALIGNOF(AlignmentDummy), capacity, options));
+ }
+
+ static void deallocate(QArrayData *data)
+ {
+ QArrayData::deallocate(data, sizeof(T), Q_ALIGNOF(AlignmentDummy));
+ }
+
+ static QTypedArrayData *fromRawData(const T *data, size_t n,
+ AllocationOptions options = Default)
+ {
+ QTypedArrayData *result = allocate(0, options | RawData);
+ if (result) {
+ Q_ASSERT(!result->ref.isShared()); // No shared empty, please!
+
+ result->offset = reinterpret_cast<const char *>(data)
+ - reinterpret_cast<const char *>(result);
+ result->size = n;
+ }
+ return result;
+ }
+
+ static QTypedArrayData *sharedNull()
+ {
+ return static_cast<QTypedArrayData *>(
+ const_cast<QArrayData *>(&QArrayData::shared_null));
+ }
+};
+
+template <class T, size_t N>
+struct QStaticArrayData
+{
+ QArrayData header;
+ T data[N];
+};
+
+// Support for returning QArrayDataPointer<T> from functions
+template <class T>
+struct QArrayDataPointerRef
+{
+ QTypedArrayData<T> *ptr;
+};
+
+#define Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(type, size) { \
+ Q_REFCOUNT_INITIALIZE_STATIC, size, 0, 0, \
+ (sizeof(QArrayData) + (Q_ALIGNOF(type) - 1)) \
+ & ~(Q_ALIGNOF(type) - 1) } \
+ /**/
+
+////////////////////////////////////////////////////////////////////////////////
+// Q_ARRAY_LITERAL
+
+// The idea here is to place a (read-only) copy of header and array data in an
+// mmappable portion of the executable (typically, .rodata section). This is
+// accomplished by hiding a static const instance of QStaticArrayData, which is
+// POD.
+
+#if defined(Q_COMPILER_VARIADIC_MACROS)
+#if defined(Q_COMPILER_LAMBDA)
+// Hide array inside a lambda
+#define Q_ARRAY_LITERAL(Type, ...) \
+ ([]() -> QArrayDataPointerRef<Type> { \
+ /* MSVC 2010 Doesn't support static variables in a lambda, but */ \
+ /* happily accepts them in a static function of a lambda-local */ \
+ /* struct :-) */ \
+ struct StaticWrapper { \
+ static QArrayDataPointerRef<Type> get() \
+ { \
+ Q_ARRAY_LITERAL_IMPL(Type, __VA_ARGS__) \
+ return ref; \
+ } \
+ }; \
+ return StaticWrapper::get(); \
+ }()) \
+ /**/
+#elif defined(Q_CC_GNU)
+// Hide array within GCC's __extension__ {( )} block
+#define Q_ARRAY_LITERAL(Type, ...) \
+ __extension__ ({ \
+ Q_ARRAY_LITERAL_IMPL(Type, __VA_ARGS__) \
+ ref; \
+ }) \
+ /**/
+#endif
+#endif // defined(Q_COMPILER_VARIADIC_MACROS)
+
+#if defined(Q_ARRAY_LITERAL)
+#define Q_ARRAY_LITERAL_IMPL(Type, ...) \
+ union { Type type_must_be_POD; } dummy; Q_UNUSED(dummy) \
+ \
+ /* Portable compile-time array size computation */ \
+ Type data[] = { __VA_ARGS__ }; Q_UNUSED(data) \
+ enum { Size = sizeof(data) / sizeof(data[0]) }; \
+ \
+ static const QStaticArrayData<Type, Size> literal = { \
+ Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(Type, Size), { __VA_ARGS__ } }; \
+ \
+ QArrayDataPointerRef<Type> ref = \
+ { static_cast<QTypedArrayData<Type> *>( \
+ const_cast<QArrayData *>(&literal.header)) }; \
+ /**/
+#else
+// As a fallback, memory is allocated and data copied to the heap.
+
+// The fallback macro does NOT use variadic macros and does NOT support
+// variable number of arguments. It is suitable for char arrays.
+
+namespace QtPrivate {
+ template <class T, size_t N>
+ inline QArrayDataPointerRef<T> qMakeArrayLiteral(const T (&array)[N])
+ {
+ union { T type_must_be_POD; } dummy; Q_UNUSED(dummy)
+
+ QArrayDataPointerRef<T> result = { QTypedArrayData<T>::allocate(N) };
+ Q_CHECK_PTR(result.ptr);
+
+ ::memcpy(result.ptr->data(), array, N * sizeof(T));
+ result.ptr->size = N;
+
+ return result;
+ }
+}
+
+#define Q_ARRAY_LITERAL(Type, Array) \
+ QT_PREPEND_NAMESPACE(QtPrivate::qMakeArrayLiteral)<Type>( Array )
+#endif // !defined(Q_ARRAY_LITERAL)
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif // include guard
diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h
new file mode 100644
index 0000000000..1b8ed3372d
--- /dev/null
+++ b/src/corelib/tools/qarraydataops.h
@@ -0,0 +1,322 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QARRAYDATAOPS_H
+#define QARRAYDATAOPS_H
+
+#include <QtCore/qarraydata.h>
+
+#include <new>
+#include <string.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+namespace QtPrivate {
+
+template <class T>
+struct QPodArrayOps
+ : QTypedArrayData<T>
+{
+ void copyAppend(const T *b, const T *e)
+ {
+ Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(b < e);
+ Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+
+ ::memcpy(this->end(), b, (e - b) * sizeof(T));
+ this->size += e - b;
+ }
+
+ void copyAppend(size_t n, const T &t)
+ {
+ Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(n <= this->alloc - uint(this->size));
+
+ T *iter = this->end();
+ const T *const end = iter + n;
+ for (; iter != end; ++iter)
+ ::memcpy(iter, &t, sizeof(T));
+ this->size += n;
+ }
+
+ void destroyAll() // Call from destructors, ONLY!
+ {
+ Q_ASSERT(this->ref.atomic.load() == 0);
+
+ // As this is to be called only from destructor, it doesn't need to be
+ // exception safe; size not updated.
+ }
+
+ void insert(T *where, const T *b, const T *e)
+ {
+ Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
+ Q_ASSERT(b < e);
+ Q_ASSERT(e <= where || b > this->end()); // No overlap
+ Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+
+ ::memmove(where + (e - b), where, (this->end() - where) * sizeof(T));
+ ::memcpy(where, b, (e - b) * sizeof(T));
+ this->size += (e - b);
+ }
+};
+
+template <class T>
+struct QGenericArrayOps
+ : QTypedArrayData<T>
+{
+ void copyAppend(const T *b, const T *e)
+ {
+ Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(b < e);
+ Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+
+ T *iter = this->end();
+ for (; b != e; ++iter, ++b) {
+ new (iter) T(*b);
+ ++this->size;
+ }
+ }
+
+ void copyAppend(size_t n, const T &t)
+ {
+ Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(n <= this->alloc - uint(this->size));
+
+ T *iter = this->end();
+ const T *const end = iter + n;
+ for (; iter != end; ++iter) {
+ new (iter) T(t);
+ ++this->size;
+ }
+ }
+
+ void destroyAll() // Call from destructors, ONLY
+ {
+ // As this is to be called only from destructor, it doesn't need to be
+ // exception safe; size not updated.
+
+ Q_ASSERT(this->ref.atomic.load() == 0);
+
+ const T *const b = this->begin();
+ const T *i = this->end();
+
+ while (i != b)
+ (--i)->~T();
+ }
+
+ void insert(T *where, const T *b, const T *e)
+ {
+ Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
+ Q_ASSERT(b < e);
+ Q_ASSERT(e <= where || b > this->end()); // No overlap
+ Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+
+ // Array may be truncated at where in case of exceptions
+
+ T *const end = this->end();
+ const T *readIter = end;
+ T *writeIter = end + (e - b);
+
+ const T *const step1End = where + qMax(e - b, end - where);
+
+ struct Destructor
+ {
+ Destructor(T *&it)
+ : iter(&it)
+ , end(it)
+ {
+ }
+
+ void commit()
+ {
+ iter = &end;
+ }
+
+ ~Destructor()
+ {
+ for (; *iter != end; --*iter)
+ (*iter)->~T();
+ }
+
+ T **iter;
+ T *end;
+ } destroyer(writeIter);
+
+ // Construct new elements in array
+ do {
+ --readIter, --writeIter;
+ new (writeIter) T(*readIter);
+ } while (writeIter != step1End);
+
+ while (writeIter != end) {
+ --e, --writeIter;
+ new (writeIter) T(*e);
+ }
+
+ destroyer.commit();
+ this->size += destroyer.end - end;
+
+ // Copy assign over existing elements
+ while (readIter != where) {
+ --readIter, --writeIter;
+ *writeIter = *readIter;
+ }
+
+ while (writeIter != where) {
+ --e, --writeIter;
+ *writeIter = *e;
+ }
+ }
+};
+
+template <class T>
+struct QMovableArrayOps
+ : QGenericArrayOps<T>
+{
+ // using QGenericArrayOps<T>::copyAppend;
+ // using QGenericArrayOps<T>::destroyAll;
+
+ void insert(T *where, const T *b, const T *e)
+ {
+ Q_ASSERT(!this->ref.isShared());
+ Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
+ Q_ASSERT(b < e);
+ Q_ASSERT(e <= where || b > this->end()); // No overlap
+ Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+
+ // Provides strong exception safety guarantee,
+ // provided T::~T() nothrow
+
+ struct ReversibleDisplace
+ {
+ ReversibleDisplace(T *start, T *finish, size_t diff)
+ : begin(start)
+ , end(finish)
+ , displace(diff)
+ {
+ ::memmove(begin + displace, begin, (end - begin) * sizeof(T));
+ }
+
+ void commit() { displace = 0; }
+
+ ~ReversibleDisplace()
+ {
+ if (displace)
+ ::memmove(begin, begin + displace, (end - begin) * sizeof(T));
+ }
+
+ T *const begin;
+ T *const end;
+ size_t displace;
+
+ } displace(where, this->end(), size_t(e - b));
+
+ struct CopyConstructor
+ {
+ CopyConstructor(T *w) : where(w) {}
+
+ void copy(const T *src, const T *const srcEnd)
+ {
+ n = 0;
+ for (; src != srcEnd; ++src) {
+ new (where + n) T(*src);
+ ++n;
+ }
+ n = 0;
+ }
+
+ ~CopyConstructor()
+ {
+ while (n)
+ where[--n].~T();
+ }
+
+ T *const where;
+ size_t n;
+ } copier(where);
+
+ copier.copy(b, e);
+ displace.commit();
+ this->size += (e - b);
+ }
+};
+
+template <class T, class = void>
+struct QArrayOpsSelector
+{
+ typedef QGenericArrayOps<T> Type;
+};
+
+template <class T>
+struct QArrayOpsSelector<T,
+ typename QEnableIf<
+ !QTypeInfo<T>::isComplex && !QTypeInfo<T>::isStatic
+ >::Type>
+{
+ typedef QPodArrayOps<T> Type;
+};
+
+template <class T>
+struct QArrayOpsSelector<T,
+ typename QEnableIf<
+ QTypeInfo<T>::isComplex && !QTypeInfo<T>::isStatic
+ >::Type>
+{
+ typedef QMovableArrayOps<T> Type;
+};
+
+} // namespace QtPrivate
+
+template <class T>
+struct QArrayDataOps
+ : QtPrivate::QArrayOpsSelector<T>::Type
+{
+};
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif // include guard
diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h
new file mode 100644
index 0000000000..f5ad53aa54
--- /dev/null
+++ b/src/corelib/tools/qarraydatapointer.h
@@ -0,0 +1,220 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QARRAYDATAPOINTER_H
+#define QARRAYDATAPOINTER_H
+
+#include <QtCore/qarraydataops.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+template <class T>
+struct QArrayDataPointer
+{
+private:
+ typedef QTypedArrayData<T> Data;
+ typedef QArrayDataOps<T> DataOps;
+
+public:
+ QArrayDataPointer()
+ : d(Data::sharedNull())
+ {
+ }
+
+ QArrayDataPointer(const QArrayDataPointer &other)
+ : d(other.d->ref.ref()
+ ? other.d
+ : other.clone(other.d->cloneFlags()))
+ {
+ }
+
+ explicit QArrayDataPointer(QTypedArrayData<T> *ptr)
+ : d(ptr)
+ {
+ Q_CHECK_PTR(ptr);
+ }
+
+ QArrayDataPointer(QArrayDataPointerRef<T> ref)
+ : d(ref.ptr)
+ {
+ }
+
+ QArrayDataPointer &operator=(const QArrayDataPointer &other)
+ {
+ QArrayDataPointer tmp(other);
+ this->swap(tmp);
+ return *this;
+ }
+
+#ifdef Q_COMPILER_RVALUE_REFS
+ QArrayDataPointer(QArrayDataPointer &&other)
+ : d(other.d)
+ {
+ other.d = Data::sharedNull();
+ }
+
+ QArrayDataPointer &operator=(QArrayDataPointer &&other)
+ {
+ this->swap(other);
+ return *this;
+ }
+#endif
+
+ DataOps &operator*() const
+ {
+ Q_ASSERT(d);
+ return *static_cast<DataOps *>(d);
+ }
+
+ DataOps *operator->() const
+ {
+ Q_ASSERT(d);
+ return static_cast<DataOps *>(d);
+ }
+
+ ~QArrayDataPointer()
+ {
+ if (!d->ref.deref()) {
+ (*this)->destroyAll();
+ Data::deallocate(d);
+ }
+ }
+
+ bool isNull() const
+ {
+ return d == Data::sharedNull();
+ }
+
+ Data *data() const
+ {
+ return d;
+ }
+
+ void setSharable(bool sharable)
+ {
+ // Can't call setSharable on static read-only data, like shared_null
+ // and the internal shared-empties.
+ if (d->alloc == 0 && d->size == 0) {
+ d = Data::allocate(0, sharable
+ ? QArrayData::Default
+ : QArrayData::Unsharable);
+ return;
+ }
+
+ detach();
+ d->ref.setSharable(sharable);
+ }
+
+ void swap(QArrayDataPointer &other)
+ {
+ qSwap(d, other.d);
+ }
+
+ void clear()
+ {
+ QArrayDataPointer tmp(d);
+ d = Data::sharedNull();
+ }
+
+ bool detach()
+ {
+ if (!d->isMutable() || d->ref.isShared()) {
+ Data *copy = clone(d->detachFlags());
+ QArrayDataPointer old(d);
+ d = copy;
+ return true;
+ }
+
+ return false;
+ }
+
+private:
+ Data *clone(QArrayData::AllocationOptions options) const Q_REQUIRED_RESULT
+ {
+ QArrayDataPointer copy(Data::allocate(d->alloc ? d->alloc : d->size,
+ options));
+ if (d->size)
+ copy->copyAppend(d->begin(), d->end());
+
+ Data *result = copy.d;
+ copy.d = Data::sharedNull();
+ return result;
+ }
+
+ Data *d;
+};
+
+template <class T>
+inline bool operator==(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs)
+{
+ return lhs.data() == rhs.data();
+}
+
+template <class T>
+inline bool operator!=(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs)
+{
+ return lhs.data() != rhs.data();
+}
+
+template <class T>
+inline void qSwap(QArrayDataPointer<T> &p1, QArrayDataPointer<T> &p2)
+{
+ p1.swap(p2);
+}
+
+QT_END_NAMESPACE
+
+namespace std
+{
+ template <class T>
+ inline void swap(
+ QT_PREPEND_NAMESPACE(QArrayDataPointer)<T> &p1,
+ QT_PREPEND_NAMESPACE(QArrayDataPointer)<T> &p2)
+ {
+ p1.swap(p2);
+ }
+}
+
+QT_END_HEADER
+
+#endif // include guard
diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp
index db22a774f4..7f4d35d82c 100644
--- a/src/corelib/tools/qbytearray.cpp
+++ b/src/corelib/tools/qbytearray.cpp
@@ -57,7 +57,7 @@
#include <string.h>
#include <stdlib.h>
-#define IS_RAW_DATA(d) ((d)->offset != 0)
+#define IS_RAW_DATA(d) ((d)->offset != sizeof(QByteArrayData))
QT_BEGIN_NAMESPACE
@@ -69,24 +69,25 @@ int qFindByteArray(
int qAllocMore(int alloc, int extra)
{
- if (alloc == 0 && extra == 0)
- return 0;
- const int page = 1 << 12;
- int nalloc;
- alloc += extra;
- if (alloc < 1<<6) {
- nalloc = (1<<3) + ((alloc >>3) << 3);
- } else {
- // don't do anything if the loop will overflow signed int.
- if (alloc >= INT_MAX/2)
- return INT_MAX;
- nalloc = (alloc < page) ? 1 << 3 : page;
- while (nalloc < alloc) {
- if (nalloc <= 0)
- return INT_MAX;
- nalloc *= 2;
- }
- }
+ Q_ASSERT(alloc >= 0 && extra >= 0);
+ Q_ASSERT(alloc < (1 << 30) - extra);
+
+ unsigned nalloc = alloc + extra;
+
+ // Round up to next power of 2
+
+ // Assuming container is growing, always overshoot
+ //--nalloc;
+
+ nalloc |= nalloc >> 1;
+ nalloc |= nalloc >> 2;
+ nalloc |= nalloc >> 4;
+ nalloc |= nalloc >> 8;
+ nalloc |= nalloc >> 16;
+ ++nalloc;
+
+ Q_ASSERT(nalloc > unsigned(alloc + extra));
+
return nalloc - extra;
}
@@ -554,7 +555,7 @@ QByteArray qUncompress(const uchar* data, int nbytes)
}
d.take(); // realloc was successful
d.reset(p);
- d->offset = 0;
+ d->offset = sizeof(QByteArrayData);
int res = ::uncompress((uchar*)d->data(), &len,
(uchar*)data+4, nbytes-4);
@@ -576,11 +577,11 @@ QByteArray qUncompress(const uchar* data, int nbytes)
d.take(); // realloc was successful
d.reset(p);
}
- d->ref = 1;
+ d->ref.initializeOwned();
d->size = len;
d->alloc = len;
d->capacityReserved = false;
- d->offset = 0;
+ d->offset = sizeof(QByteArrayData);
d->data()[len] = 0;
return QByteArray(d.take(), 0, 0);
@@ -614,10 +615,10 @@ static inline char qToLower(char c)
return c;
}
-const QConstByteArrayData<1> QByteArray::shared_null = { { Q_REFCOUNT_INITIALIZER(-1),
- 0, 0, 0, { 0 } }, { 0 } };
-const QConstByteArrayData<1> QByteArray::shared_empty = { { Q_REFCOUNT_INITIALIZER(-1),
- 0, 0, 0, { 0 } }, { 0 } };
+const QStaticByteArrayData<1> QByteArray::shared_null = { { Q_REFCOUNT_INITIALIZE_STATIC,
+ 0, 0, 0, sizeof(QByteArrayData) }, { 0 } };
+const QStaticByteArrayData<1> QByteArray::shared_empty = { { Q_REFCOUNT_INITIALIZE_STATIC,
+ 0, 0, 0, sizeof(QByteArrayData) }, { 0 } };
/*!
\class QByteArray
@@ -916,7 +917,7 @@ QByteArray &QByteArray::operator=(const char *str)
x = const_cast<Data *>(&shared_empty.ba);
} else {
int len = qstrlen(str);
- if (d->ref != 1 || len > int(d->alloc) || (len < d->size && len < int(d->alloc) >> 1))
+ if (d->ref.isShared() || len > int(d->alloc) || (len < d->size && len < int(d->alloc) >> 1))
realloc(len);
x = d;
memcpy(x->data(), str, len + 1); // include null terminator
@@ -1301,38 +1302,16 @@ void QByteArray::chop(int n)
\sa isEmpty()
*/
-/*! \fn QByteArray::QByteArray(const char *str)
-
- Constructs a byte array initialized with the string \a str.
-
- QByteArray makes a deep copy of the string data.
-*/
-
-QByteArray::QByteArray(const char *str)
-{
- if (!str) {
- d = const_cast<Data *>(&shared_null.ba);
- } else if (!*str) {
- d = const_cast<Data *>(&shared_empty.ba);
- } else {
- int len = qstrlen(str);
- d = static_cast<Data *>(malloc(sizeof(Data) + len + 1));
- Q_CHECK_PTR(d);
- d->ref = 1;
- d->size = len;
- d->alloc = len;
- d->capacityReserved = false;
- d->offset = 0;
- memcpy(d->data(), str, len+1); // include null terminator
- }
-}
-
/*!
Constructs a byte array containing the first \a size bytes of
array \a data.
If \a data is 0, a null byte array is constructed.
+ If \a size is negative, \a data is assumed to point to a nul-terminated
+ string and its length is determined dynamically. The terminating
+ nul-character is not considered part of the byte array.
+
QByteArray makes a deep copy of the string data.
\sa fromRawData()
@@ -1342,18 +1321,22 @@ QByteArray::QByteArray(const char *data, int size)
{
if (!data) {
d = const_cast<Data *>(&shared_null.ba);
- } else if (size <= 0) {
- d = const_cast<Data *>(&shared_empty.ba);
} else {
- d = static_cast<Data *>(malloc(sizeof(Data) + size + 1));
- Q_CHECK_PTR(d);
- d->ref = 1;
- d->size = size;
- d->alloc = size;
- d->capacityReserved = false;
- d->offset = 0;
- memcpy(d->data(), data, size);
- d->data()[size] = '\0';
+ if (size < 0)
+ size = strlen(data);
+ if (!size) {
+ d = const_cast<Data *>(&shared_empty.ba);
+ } else {
+ d = static_cast<Data *>(malloc(sizeof(Data) + size + 1));
+ Q_CHECK_PTR(d);
+ d->ref.initializeOwned();
+ d->size = size;
+ d->alloc = size;
+ d->capacityReserved = false;
+ d->offset = sizeof(QByteArrayData);
+ memcpy(d->data(), data, size);
+ d->data()[size] = '\0';
+ }
}
}
@@ -1367,15 +1350,15 @@ QByteArray::QByteArray(const char *data, int size)
QByteArray::QByteArray(int size, char ch)
{
if (size <= 0) {
- d = const_cast<Data *>(&shared_null.ba);
+ d = const_cast<Data *>(&shared_empty.ba);
} else {
d = static_cast<Data *>(malloc(sizeof(Data) + size + 1));
Q_CHECK_PTR(d);
- d->ref = 1;
+ d->ref.initializeOwned();
d->size = size;
d->alloc = size;
d->capacityReserved = false;
- d->offset = 0;
+ d->offset = sizeof(QByteArrayData);
memset(d->data(), ch, size);
d->data()[size] = '\0';
}
@@ -1391,11 +1374,11 @@ QByteArray::QByteArray(int size, Qt::Initialization)
{
d = static_cast<Data *>(malloc(sizeof(Data) + size + 1));
Q_CHECK_PTR(d);
- d->ref = 1;
+ d->ref.initializeOwned();
d->size = size;
d->alloc = size;
d->capacityReserved = false;
- d->offset = 0;
+ d->offset = sizeof(QByteArrayData);
d->data()[size] = '\0';
}
@@ -1417,7 +1400,7 @@ void QByteArray::resize(int size)
if (size < 0)
size = 0;
- if (d->offset && d->ref == 1 && size < d->size) {
+ if (IS_RAW_DATA(d) && !d->ref.isShared() && size < d->size) {
d->size = size;
return;
}
@@ -1438,15 +1421,15 @@ void QByteArray::resize(int size)
//
Data *x = static_cast<Data *>(malloc(sizeof(Data) + size + 1));
Q_CHECK_PTR(x);
- x->ref = 1;
+ x->ref.initializeOwned();
x->size = size;
x->alloc = size;
x->capacityReserved = false;
- x->offset = 0;
+ x->offset = sizeof(QByteArrayData);
x->data()[size] = '\0';
d = x;
} else {
- if (d->ref != 1 || size > int(d->alloc)
+ if (d->ref.isShared() || size > int(d->alloc)
|| (!d->capacityReserved && size < d->size && size < int(d->alloc) >> 1))
realloc(qAllocMore(size, sizeof(Data)));
if (int(d->alloc) >= size) {
@@ -1477,14 +1460,14 @@ QByteArray &QByteArray::fill(char ch, int size)
void QByteArray::realloc(int alloc)
{
- if (d->ref != 1 || d->offset) {
+ if (d->ref.isShared() || IS_RAW_DATA(d)) {
Data *x = static_cast<Data *>(malloc(sizeof(Data) + alloc + 1));
Q_CHECK_PTR(x);
- x->ref = 1;
+ x->ref.initializeOwned();
x->size = qMin(alloc, d->size);
x->alloc = alloc;
x->capacityReserved = d->capacityReserved;
- x->offset = 0;
+ x->offset = sizeof(QByteArrayData);
::memcpy(x->data(), d->data(), x->size);
x->data()[x->size] = '\0';
if (!d->ref.deref())
@@ -1494,7 +1477,7 @@ void QByteArray::realloc(int alloc)
Data *x = static_cast<Data *>(::realloc(d, sizeof(Data) + alloc + 1));
Q_CHECK_PTR(x);
x->alloc = alloc;
- x->offset = 0;
+ x->offset = sizeof(QByteArrayData);
d = x;
}
}
@@ -1516,7 +1499,7 @@ void QByteArray::expand(int i)
QByteArray QByteArray::nulTerminated() const
{
// is this fromRawData?
- if (!d->offset)
+ if (!IS_RAW_DATA(d))
return *this; // no, then we're sure we're zero terminated
QByteArray copy(*this);
@@ -1578,7 +1561,7 @@ QByteArray &QByteArray::prepend(const char *str)
QByteArray &QByteArray::prepend(const char *str, int len)
{
if (str) {
- if (d->ref != 1 || d->size + len > int(d->alloc))
+ if (d->ref.isShared() || d->size + len > int(d->alloc))
realloc(qAllocMore(d->size + len, sizeof(Data)));
memmove(d->data()+len, d->data(), d->size);
memcpy(d->data(), str, len);
@@ -1596,7 +1579,7 @@ QByteArray &QByteArray::prepend(const char *str, int len)
QByteArray &QByteArray::prepend(char ch)
{
- if (d->ref != 1 || d->size + 1 > int(d->alloc))
+ if (d->ref.isShared() || d->size + 1 > int(d->alloc))
realloc(qAllocMore(d->size + 1, sizeof(Data)));
memmove(d->data()+1, d->data(), d->size);
d->data()[0] = ch;
@@ -1634,7 +1617,7 @@ QByteArray &QByteArray::append(const QByteArray &ba)
if ((d == &shared_null.ba || d == &shared_empty.ba) && !IS_RAW_DATA(ba.d)) {
*this = ba;
} else if (ba.d != &shared_null.ba) {
- if (d->ref != 1 || d->size + ba.d->size > int(d->alloc))
+ if (d->ref.isShared() || d->size + ba.d->size > int(d->alloc))
realloc(qAllocMore(d->size + ba.d->size, sizeof(Data)));
memcpy(d->data() + d->size, ba.d->data(), ba.d->size);
d->size += ba.d->size;
@@ -1668,7 +1651,7 @@ QByteArray& QByteArray::append(const char *str)
{
if (str) {
int len = qstrlen(str);
- if (d->ref != 1 || d->size + len > int(d->alloc))
+ if (d->ref.isShared() || d->size + len > int(d->alloc))
realloc(qAllocMore(d->size + len, sizeof(Data)));
memcpy(d->data() + d->size, str, len + 1); // include null terminator
d->size += len;
@@ -1693,7 +1676,7 @@ QByteArray &QByteArray::append(const char *str, int len)
if (len < 0)
len = qstrlen(str);
if (str && len) {
- if (d->ref != 1 || d->size + len > int(d->alloc))
+ if (d->ref.isShared() || d->size + len > int(d->alloc))
realloc(qAllocMore(d->size + len, sizeof(Data)));
memcpy(d->data() + d->size, str, len); // include null terminator
d->size += len;
@@ -1710,7 +1693,7 @@ QByteArray &QByteArray::append(const char *str, int len)
QByteArray& QByteArray::append(char ch)
{
- if (d->ref != 1 || d->size + 1 > int(d->alloc))
+ if (d->ref.isShared() || d->size + 1 > int(d->alloc))
realloc(qAllocMore(d->size + 1, sizeof(Data)));
d->data()[d->size++] = ch;
d->data()[d->size] = '\0';
@@ -3901,11 +3884,11 @@ QByteArray QByteArray::fromRawData(const char *data, int size)
} else {
x = static_cast<Data *>(malloc(sizeof(Data) + 1));
Q_CHECK_PTR(x);
- x->ref = 1;
+ x->ref.initializeOwned();
x->size = size;
x->alloc = 0;
x->capacityReserved = false;
- x->offset = data - (x->d + sizeof(qptrdiff));
+ x->offset = data - reinterpret_cast<char *>(x);
}
return QByteArray(x, 0, 0);
}
@@ -3926,14 +3909,14 @@ QByteArray QByteArray::fromRawData(const char *data, int size)
*/
QByteArray &QByteArray::setRawData(const char *data, uint size)
{
- if (d->ref != 1 || d->alloc) {
+ if (d->ref.isShared() || d->alloc) {
*this = fromRawData(data, size);
} else {
if (data) {
d->size = size;
- d->offset = data - (d->d + sizeof(qptrdiff));
+ d->offset = data - reinterpret_cast<char *>(d);
} else {
- d->offset = 0;
+ d->offset = sizeof(QByteArrayData);
d->size = 0;
*d->data() = 0;
}
diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h
index 7711d7349e..1e70e26be3 100644
--- a/src/corelib/tools/qbytearray.h
+++ b/src/corelib/tools/qbytearray.h
@@ -124,32 +124,31 @@ struct QByteArrayData
int size;
uint alloc : 31;
uint capacityReserved : 1;
- union {
- qptrdiff offset; // will always work as we add/subtract from a ushort ptr
- char d[sizeof(qptrdiff)];
- };
- inline char *data() { return d + sizeof(qptrdiff) + offset; }
- inline const char *data() const { return d + sizeof(qptrdiff) + offset; }
+
+ qptrdiff offset;
+
+ inline char *data() { return reinterpret_cast<char *>(this) + offset; }
+ inline const char *data() const { return reinterpret_cast<const char *>(this) + offset; }
};
-template<int N> struct QConstByteArrayData
+template<int N> struct QStaticByteArrayData
{
- const QByteArrayData ba;
- const char data[N + 1];
+ QByteArrayData ba;
+ char data[N + 1];
};
-template<int N> struct QConstByteArrayDataPtr
+template<int N> struct QStaticByteArrayDataPtr
{
- const QConstByteArrayData<N> *ptr;
+ const QStaticByteArrayData<N> *ptr;
};
#if defined(Q_COMPILER_LAMBDA)
-# define QByteArrayLiteral(str) ([]() -> QConstByteArrayDataPtr<sizeof(str) - 1> { \
+# define QByteArrayLiteral(str) ([]() -> QStaticByteArrayDataPtr<sizeof(str) - 1> { \
enum { Size = sizeof(str) - 1 }; \
- static const QConstByteArrayData<Size> qbytearray_literal = \
- { { Q_REFCOUNT_INITIALIZER(-1), Size, 0, 0, { 0 } }, str }; \
- QConstByteArrayDataPtr<Size> holder = { &qbytearray_literal }; \
+ static const QStaticByteArrayData<Size> qbytearray_literal = \
+ { { Q_REFCOUNT_INITIALIZE_STATIC, Size, 0, 0, sizeof(QByteArrayData) }, str }; \
+ QStaticByteArrayDataPtr<Size> holder = { &qbytearray_literal }; \
return holder; }())
#elif defined(Q_CC_GNU)
@@ -160,9 +159,9 @@ template<int N> struct QConstByteArrayDataPtr
# define QByteArrayLiteral(str) \
__extension__ ({ \
enum { Size = sizeof(str) - 1 }; \
- static const QConstByteArrayData<Size> qbytearray_literal = \
- { { Q_REFCOUNT_INITIALIZER(-1), Size, 0, 0, { 0 } }, str }; \
- QConstByteArrayDataPtr<Size> holder = { &qbytearray_literal }; \
+ static const QStaticByteArrayData<Size> qbytearray_literal = \
+ { { Q_REFCOUNT_INITIALIZE_STATIC, Size, 0, 0, sizeof(QByteArrayData) }, str }; \
+ QStaticByteArrayDataPtr<Size> holder = { &qbytearray_literal }; \
holder; })
#endif
@@ -180,8 +179,7 @@ private:
public:
inline QByteArray();
- QByteArray(const char *);
- QByteArray(const char *, int size);
+ QByteArray(const char *, int size = -1);
QByteArray(int size, char c);
QByteArray(int size, Qt::Initialization);
inline QByteArray(const QByteArray &);
@@ -207,10 +205,8 @@ public:
void squeeze();
#ifndef QT_NO_CAST_FROM_BYTEARRAY
-#if QT_DEPRECATED_SINCE(5, 0)
- QT_DEPRECATED operator const char *() const { return constData(); }
- QT_DEPRECATED operator const void *() const { return constData(); }
-#endif
+ operator const char *() const;
+ operator const void *() const;
#endif
char *data();
const char *data() const;
@@ -381,16 +377,16 @@ public:
bool isNull() const;
template <int n>
- inline QByteArray(const QConstByteArrayData<n> &dd)
+ inline QByteArray(const QStaticByteArrayData<n> &dd)
: d(const_cast<QByteArrayData *>(&dd.ba)) {}
template <int N>
- Q_DECL_CONSTEXPR inline QByteArray(QConstByteArrayDataPtr<N> dd)
+ Q_DECL_CONSTEXPR inline QByteArray(QStaticByteArrayDataPtr<N> dd)
: d(const_cast<QByteArrayData *>(&dd.ptr->ba)) {}
private:
operator QNoImplicitBoolCast() const;
- static const QConstByteArrayData<1> shared_null;
- static const QConstByteArrayData<1> shared_empty;
+ static const QStaticByteArrayData<1> shared_null;
+ static const QStaticByteArrayData<1> shared_empty;
Data *d;
QByteArray(Data *dd, int /*dummy*/, int /*dummy*/) : d(dd) {}
void realloc(int alloc);
@@ -419,6 +415,12 @@ inline char QByteArray::operator[](uint i) const
inline bool QByteArray::isEmpty() const
{ return d->size == 0; }
+#ifndef QT_NO_CAST_FROM_BYTEARRAY
+inline QByteArray::operator const char *() const
+{ return d->data(); }
+inline QByteArray::operator const void *() const
+{ return d->data(); }
+#endif
inline char *QByteArray::data()
{ detach(); return d->data(); }
inline const char *QByteArray::data() const
@@ -426,9 +428,9 @@ inline const char *QByteArray::data() const
inline const char *QByteArray::constData() const
{ return d->data(); }
inline void QByteArray::detach()
-{ if (d->ref != 1 || d->offset) realloc(d->size); }
+{ if (d->ref.isShared() || (d->offset != sizeof(QByteArrayData))) realloc(d->size); }
inline bool QByteArray::isDetached() const
-{ return d->ref == 1; }
+{ return !d->ref.isShared(); }
inline QByteArray::QByteArray(const QByteArray &a) : d(a.d)
{ d->ref.ref(); }
@@ -437,7 +439,7 @@ inline int QByteArray::capacity() const
inline void QByteArray::reserve(int asize)
{
- if (d->ref != 1 || asize > int(d->alloc))
+ if (d->ref.isShared() || asize > int(d->alloc))
realloc(asize);
if (!d->capacityReserved) {
@@ -448,11 +450,12 @@ inline void QByteArray::reserve(int asize)
inline void QByteArray::squeeze()
{
- if (d->ref > 1 || d->size < int(d->alloc))
+ if (d->ref.isShared() || d->size < int(d->alloc))
realloc(d->size);
if (d->capacityReserved) {
- // cannot set unconditionally, since d could be the shared_null/shared_empty (which is const)
+ // cannot set unconditionally, since d could be shared_null or
+ // otherwise static.
d->capacityReserved = false;
}
}
diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp
index 62fc6c5d86..e418158a20 100644
--- a/src/corelib/tools/qhash.cpp
+++ b/src/corelib/tools/qhash.cpp
@@ -166,7 +166,7 @@ static int countBits(int hint)
const int MinNumBits = 4;
const QHashData QHashData::shared_null = {
- 0, 0, Q_REFCOUNT_INITIALIZER(-1), 0, 0, MinNumBits, 0, 0, true, false, 0
+ 0, 0, Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, MinNumBits, 0, 0, true, false, 0
};
void *QHashData::allocateNode(int nodeAlign)
@@ -196,7 +196,7 @@ QHashData *QHashData::detach_helper(void (*node_duplicate)(Node *, void *),
d = new QHashData;
d->fakeNext = 0;
d->buckets = 0;
- d->ref = 1;
+ d->ref.initializeOwned();
d->size = size;
d->nodeSize = nodeSize;
d->userNumBits = userNumBits;
diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h
index 55fa788543..73162b6cf1 100644
--- a/src/corelib/tools/qhash.h
+++ b/src/corelib/tools/qhash.h
@@ -288,8 +288,8 @@ public:
void reserve(int size);
inline void squeeze() { reserve(1); }
- inline void detach() { if (d->ref != 1) detach_helper(); }
- inline bool isDetached() const { return d->ref == 1; }
+ inline void detach() { if (d->ref.isShared()) detach_helper(); }
+ inline bool isDetached() const { return !d->ref.isShared(); }
inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QHashData::shared_null) d->sharable = sharable; }
inline bool isSharedWith(const QHash<Key, T> &other) const { return d == other.d; }
diff --git a/src/corelib/tools/qlinkedlist.cpp b/src/corelib/tools/qlinkedlist.cpp
index 50f6f447b2..1502f70d56 100644
--- a/src/corelib/tools/qlinkedlist.cpp
+++ b/src/corelib/tools/qlinkedlist.cpp
@@ -46,7 +46,7 @@ QT_BEGIN_NAMESPACE
const QLinkedListData QLinkedListData::shared_null = {
const_cast<QLinkedListData *>(&QLinkedListData::shared_null),
const_cast<QLinkedListData *>(&QLinkedListData::shared_null),
- Q_REFCOUNT_INITIALIZER(-1), 0, true
+ Q_REFCOUNT_INITIALIZE_STATIC, 0, true
};
/*! \class QLinkedList
diff --git a/src/corelib/tools/qlinkedlist.h b/src/corelib/tools/qlinkedlist.h
index 2b23fc230c..27d0ffe875 100644
--- a/src/corelib/tools/qlinkedlist.h
+++ b/src/corelib/tools/qlinkedlist.h
@@ -94,8 +94,8 @@ public:
inline int size() const { return d->size; }
inline void detach()
- { if (d->ref != 1) detach_helper(); }
- inline bool isDetached() const { return d->ref == 1; }
+ { if (d->ref.isShared()) detach_helper(); }
+ inline bool isDetached() const { return !d->ref.isShared(); }
inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QLinkedListData::shared_null) d->sharable = sharable; }
inline bool isSharedWith(const QLinkedList<T> &other) const { return d == other.d; }
@@ -243,8 +243,6 @@ private:
template <typename T>
inline QLinkedList<T>::~QLinkedList()
{
- if (!d)
- return;
if (!d->ref.deref())
free(d);
}
@@ -254,7 +252,7 @@ void QLinkedList<T>::detach_helper()
{
union { QLinkedListData *d; Node *e; } x;
x.d = new QLinkedListData;
- x.d->ref = 1;
+ x.d->ref.initializeOwned();
x.d->size = d->size;
x.d->sharable = true;
Node *original = e->n;
@@ -267,6 +265,7 @@ void QLinkedList<T>::detach_helper()
copy = copy->n;
} QT_CATCH(...) {
copy->n = x.e;
+ Q_ASSERT(!x.d->ref.deref()); // Don't trigger assert in free
free(x.d);
QT_RETHROW;
}
@@ -283,14 +282,13 @@ void QLinkedList<T>::free(QLinkedListData *x)
{
Node *y = reinterpret_cast<Node*>(x);
Node *i = y->n;
- if (x->ref == 0) {
- while(i != y) {
- Node *n = i;
- i = i->n;
- delete n;
- }
- delete x;
+ Q_ASSERT(x->ref.atomic.load() == 0);
+ while (i != y) {
+ Node *n = i;
+ i = i->n;
+ delete n;
}
+ delete x;
}
template <typename T>
diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp
index 9b40e2d37a..c87b3e7d24 100644
--- a/src/corelib/tools/qlist.cpp
+++ b/src/corelib/tools/qlist.cpp
@@ -59,7 +59,7 @@ QT_BEGIN_NAMESPACE
the number of elements in the list.
*/
-const QListData::Data QListData::shared_null = { Q_REFCOUNT_INITIALIZER(-1), 0, 0, 0, true, { 0 } };
+const QListData::Data QListData::shared_null = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, { 0 } };
static int grow(int size)
{
@@ -87,8 +87,7 @@ QListData::Data *QListData::detach_grow(int *idx, int num)
Data* t = static_cast<Data *>(::malloc(DataHeaderSize + alloc * sizeof(void *)));
Q_CHECK_PTR(t);
- t->ref = 1;
- t->sharable = true;
+ t->ref.initializeOwned();
t->alloc = alloc;
// The space reservation algorithm's optimization is biased towards appending:
// Something which looks like an append will put the data at the beginning,
@@ -129,8 +128,7 @@ QListData::Data *QListData::detach(int alloc)
Data* t = static_cast<Data *>(::malloc(DataHeaderSize + alloc * sizeof(void *)));
Q_CHECK_PTR(t);
- t->ref = 1;
- t->sharable = true;
+ t->ref.initializeOwned();
t->alloc = alloc;
if (!alloc) {
t->begin = 0;
@@ -146,7 +144,7 @@ QListData::Data *QListData::detach(int alloc)
void QListData::realloc(int alloc)
{
- Q_ASSERT(d->ref == 1);
+ Q_ASSERT(!d->ref.isShared());
Data *x = static_cast<Data *>(::realloc(d, DataHeaderSize + alloc * sizeof(void *)));
Q_CHECK_PTR(x);
@@ -159,7 +157,7 @@ void QListData::realloc(int alloc)
// ensures that enough space is available to append n elements
void **QListData::append(int n)
{
- Q_ASSERT(d->ref == 1);
+ Q_ASSERT(!d->ref.isShared());
int e = d->end;
if (e + n > d->alloc) {
int b = d->begin;
@@ -190,7 +188,7 @@ void **QListData::append(const QListData& l)
void **QListData::prepend()
{
- Q_ASSERT(d->ref == 1);
+ Q_ASSERT(!d->ref.isShared());
if (d->begin == 0) {
if (d->end >= d->alloc / 3)
realloc(grow(d->alloc + 1));
@@ -208,7 +206,7 @@ void **QListData::prepend()
void **QListData::insert(int i)
{
- Q_ASSERT(d->ref == 1);
+ Q_ASSERT(!d->ref.isShared());
if (i <= 0)
return prepend();
int size = d->end - d->begin;
@@ -247,7 +245,7 @@ void **QListData::insert(int i)
void QListData::remove(int i)
{
- Q_ASSERT(d->ref == 1);
+ Q_ASSERT(!d->ref.isShared());
i += d->begin;
if (i - d->begin < d->end - i) {
if (int offset = i - d->begin)
@@ -262,7 +260,7 @@ void QListData::remove(int i)
void QListData::remove(int i, int n)
{
- Q_ASSERT(d->ref == 1);
+ Q_ASSERT(!d->ref.isShared());
i += d->begin;
int middle = i + n/2;
if (middle - d->begin < d->end - middle) {
@@ -278,7 +276,7 @@ void QListData::remove(int i, int n)
void QListData::move(int from, int to)
{
- Q_ASSERT(d->ref == 1);
+ Q_ASSERT(!d->ref.isShared());
if (from == to)
return;
@@ -318,7 +316,7 @@ void QListData::move(int from, int to)
void **QListData::erase(void **xi)
{
- Q_ASSERT(d->ref == 1);
+ Q_ASSERT(!d->ref.isShared());
int i = xi - (d->array + d->begin);
remove(i);
return d->array + d->begin + i;
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h
index 3d55b3002d..a15b0c9124 100644
--- a/src/corelib/tools/qlist.h
+++ b/src/corelib/tools/qlist.h
@@ -71,7 +71,6 @@ struct Q_CORE_EXPORT QListData {
struct Data {
QtPrivate::RefCount ref;
int alloc, begin, end;
- uint sharable : 1;
void *array[1];
};
enum { DataHeaderSize = sizeof(Data) - sizeof(void *) };
@@ -114,7 +113,7 @@ class QList
public:
inline QList() : d(const_cast<QListData::Data *>(&QListData::shared_null)) { }
- inline QList(const QList<T> &l) : d(l.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }
+ QList(const QList<T> &l);
~QList();
QList<T> &operator=(const QList<T> &l);
#ifdef Q_COMPILER_RVALUE_REFS
@@ -132,17 +131,25 @@ public:
inline int size() const { return p.size(); }
- inline void detach() { if (d->ref != 1) detach_helper(); }
+ inline void detach() { if (d->ref.isShared()) detach_helper(); }
inline void detachShared()
{
// The "this->" qualification is needed for GCCE.
- if (d->ref != 1 && this->d != &QListData::shared_null)
+ if (d->ref.isShared() && this->d != &QListData::shared_null)
detach_helper();
}
- inline bool isDetached() const { return d->ref == 1; }
- inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QListData::shared_null) d->sharable = sharable; }
+ inline bool isDetached() const { return !d->ref.isShared(); }
+ inline void setSharable(bool sharable)
+ {
+ if (sharable == d->ref.isSharable())
+ return;
+ if (!sharable)
+ detach();
+ if (d != &QListData::shared_null)
+ d->ref.setSharable(sharable);
+ }
inline bool isSharedWith(const QList<T> &other) const { return d == other.d; }
inline bool isEmpty() const { return p.isEmpty(); }
@@ -421,13 +428,8 @@ template <typename T>
Q_INLINE_TEMPLATE QList<T> &QList<T>::operator=(const QList<T> &l)
{
if (d != l.d) {
- QListData::Data *o = l.d;
- o->ref.ref();
- if (!d->ref.deref())
- dealloc(d);
- d = o;
- if (!d->sharable)
- detach_helper();
+ QList<T> tmp(l);
+ tmp.swap(*this);
}
return *this;
}
@@ -480,7 +482,7 @@ template <typename T>
Q_OUTOFLINE_TEMPLATE void QList<T>::reserve(int alloc)
{
if (d->alloc < alloc) {
- if (d->ref != 1)
+ if (d->ref.isShared())
detach_helper(alloc);
else
p.realloc(alloc);
@@ -490,7 +492,7 @@ Q_OUTOFLINE_TEMPLATE void QList<T>::reserve(int alloc)
template <typename T>
Q_OUTOFLINE_TEMPLATE void QList<T>::append(const T &t)
{
- if (d->ref != 1) {
+ if (d->ref.isShared()) {
Node *n = detach_helper_grow(INT_MAX, 1);
QT_TRY {
node_construct(n, t);
@@ -524,7 +526,7 @@ Q_OUTOFLINE_TEMPLATE void QList<T>::append(const T &t)
template <typename T>
inline void QList<T>::prepend(const T &t)
{
- if (d->ref != 1) {
+ if (d->ref.isShared()) {
Node *n = detach_helper_grow(0, 1);
QT_TRY {
node_construct(n, t);
@@ -558,7 +560,7 @@ inline void QList<T>::prepend(const T &t)
template <typename T>
inline void QList<T>::insert(int i, const T &t)
{
- if (d->ref != 1) {
+ if (d->ref.isShared()) {
Node *n = detach_helper_grow(i, 1);
QT_TRY {
node_construct(n, t);
@@ -710,6 +712,28 @@ Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper()
}
template <typename T>
+Q_OUTOFLINE_TEMPLATE QList<T>::QList(const QList<T> &l)
+ : d(l.d)
+{
+ if (!d->ref.ref()) {
+ p.detach(d->alloc);
+
+ struct Cleanup
+ {
+ Cleanup(QListData::Data *d) : d_(d) {}
+ ~Cleanup() { if (d_) qFree(d_); }
+
+ QListData::Data *d_;
+ } tryCatch(d);
+
+ node_copy(reinterpret_cast<Node *>(p.begin()),
+ reinterpret_cast<Node *>(p.end()),
+ reinterpret_cast<Node *>(l.p.begin()));
+ tryCatch.d_ = 0;
+ }
+}
+
+template <typename T>
Q_OUTOFLINE_TEMPLATE QList<T>::~QList()
{
if (!d->ref.deref())
@@ -804,7 +828,7 @@ Q_OUTOFLINE_TEMPLATE QList<T> &QList<T>::operator+=(const QList<T> &l)
if (isEmpty()) {
*this = l;
} else {
- Node *n = (d->ref != 1)
+ Node *n = (d->ref.isShared())
? detach_helper_grow(INT_MAX, l.size())
: reinterpret_cast<Node *>(p.append(l.p));
QT_TRY {
diff --git a/src/corelib/tools/qmap.cpp b/src/corelib/tools/qmap.cpp
index e0b53fc64e..90521d343c 100644
--- a/src/corelib/tools/qmap.cpp
+++ b/src/corelib/tools/qmap.cpp
@@ -50,170 +50,316 @@
QT_BEGIN_NAMESPACE
-const QMapData QMapData::shared_null = {
- const_cast<QMapData *>(&shared_null),
- { const_cast<QMapData *>(&shared_null), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- Q_REFCOUNT_INITIALIZER(-1), 0, 0, 0, false, true, false, 0
-};
+const QMapDataBase QMapDataBase::shared_null = { Q_REFCOUNT_INITIALIZE_STATIC, 0, { 0, 0, 0 } };
-QMapData *QMapData::createData(int alignment)
+const QMapNodeBase *QMapNodeBase::nextNode() const
{
- QMapData *d = new QMapData;
- Q_CHECK_PTR(d);
- Node *e = reinterpret_cast<Node *>(d);
- e->backward = e;
- e->forward[0] = e;
- d->ref = 1;
- d->topLevel = 0;
- d->size = 0;
- d->randomBits = 0;
- d->insertInOrder = false;
- d->sharable = true;
- d->strictAlignment = alignment > 8;
- d->reserved = 0;
- return d;
+ const QMapNodeBase *n = this;
+ if (n->right) {
+ n = n->right;
+ while (n->left)
+ n = n->left;
+ } else {
+ const QMapNodeBase *y = n->parent();
+ while (y && n == y->right) {
+ n = y;
+ y = n->parent();
+ }
+ n = y;
+ }
+ return n;
}
-void QMapData::continueFreeData(int offset)
+const QMapNodeBase *QMapNodeBase::previousNode() const
{
- Node *e = reinterpret_cast<Node *>(this);
- Node *cur = e->forward[0];
- Node *prev;
- while (cur != e) {
- prev = cur;
- cur = cur->forward[0];
- if (strictAlignment)
- qFreeAligned(reinterpret_cast<char *>(prev) - offset);
- else
- free(reinterpret_cast<char *>(prev) - offset);
+ const QMapNodeBase *n = this;
+ if (n->left) {
+ n = n->left;
+ while (n->right)
+ n = n->right;
+ } else {
+ const QMapNodeBase *y = n->parent();
+ while (y && n == y->left) {
+ n = y;
+ y = n->parent();
+ }
+ n = y;
}
- delete this;
+ return n;
}
-/*!
- Creates a new node inside the data structure.
-
- \a update is an array with pointers to the node after which the new node
- should be inserted. Because of the strange skip list data structure there
- could be several pointers to this node on different levels.
- \a offset is an amount of bytes that needs to reserved just before the
- QMapData::Node structure.
-
- \a alignment dictates the alignment for the data.
- \internal
- \since 4.6
-*/
-QMapData::Node *QMapData::node_create(Node *update[], int offset, int alignment)
+void QMapDataBase::rotateLeft(QMapNodeBase *x)
{
- int level = 0;
- uint mask = (1 << Sparseness) - 1;
-
- while ((randomBits & mask) == mask && level < LastLevel) {
- ++level;
- mask <<= Sparseness;
- }
-
- if (level > topLevel) {
- Node *e = reinterpret_cast<Node *>(this);
- level = ++topLevel;
- e->forward[level] = e;
- update[level] = e;
- }
-
- ++randomBits;
- if (level == 3 && !insertInOrder)
- randomBits = qrand();
+ QMapNodeBase *&root = header.left;
+ QMapNodeBase *y = x->right;
+ x->right = y->left;
+ if (y->left != 0)
+ y->left->setParent(x);
+ y->setParent(x->parent());
+ if (x == root)
+ root = y;
+ else if (x == x->parent()->left)
+ x->parent()->left = y;
+ else
+ x->parent()->right = y;
+ y->left = x;
+ x->setParent(y);
+}
- void *concreteNode = strictAlignment ?
- qMallocAligned(offset + sizeof(Node) + level * sizeof(Node *), alignment) :
- malloc(offset + sizeof(Node) + level * sizeof(Node *));
- Q_CHECK_PTR(concreteNode);
- Node *abstractNode = reinterpret_cast<Node *>(reinterpret_cast<char *>(concreteNode) + offset);
+void QMapDataBase::rotateRight(QMapNodeBase *x)
+{
+ QMapNodeBase *&root = header.left;
+ QMapNodeBase *y = x->left;
+ x->left = y->right;
+ if (y->right != 0)
+ y->right->setParent(x);
+ y->setParent(x->parent());
+ if (x == root)
+ root = y;
+ else if (x == x->parent()->right)
+ x->parent()->right = y;
+ else
+ x->parent()->left = y;
+ y->right = x;
+ x->setParent(y);
+}
- abstractNode->backward = update[0];
- update[0]->forward[0]->backward = abstractNode;
- for (int i = level; i >= 0; i--) {
- abstractNode->forward[i] = update[i]->forward[i];
- update[i]->forward[i] = abstractNode;
- update[i] = abstractNode;
+void QMapDataBase::rebalance(QMapNodeBase *x)
+{
+ QMapNodeBase *&root = header.left;
+ x->setColor(QMapNodeBase::Red);
+ while (x != root && x->parent()->color() == QMapNodeBase::Red) {
+ if (x->parent() == x->parent()->parent()->left) {
+ QMapNodeBase *y = x->parent()->parent()->right;
+ if (y && y->color() == QMapNodeBase::Red) {
+ x->parent()->setColor(QMapNodeBase::Black);
+ y->setColor(QMapNodeBase::Black);
+ x->parent()->parent()->setColor(QMapNodeBase::Red);
+ x = x->parent()->parent();
+ } else {
+ if (x == x->parent()->right) {
+ x = x->parent();
+ rotateLeft(x);
+ }
+ x->parent()->setColor(QMapNodeBase::Black);
+ x->parent()->parent()->setColor(QMapNodeBase::Red);
+ rotateRight (x->parent()->parent());
+ }
+ } else {
+ QMapNodeBase *y = x->parent()->parent()->left;
+ if (y && y->color() == QMapNodeBase::Red) {
+ x->parent()->setColor(QMapNodeBase::Black);
+ y->setColor(QMapNodeBase::Black);
+ x->parent()->parent()->setColor(QMapNodeBase::Red);
+ x = x->parent()->parent();
+ } else {
+ if (x == x->parent()->left) {
+ x = x->parent();
+ rotateRight(x);
+ }
+ x->parent()->setColor(QMapNodeBase::Black);
+ x->parent()->parent()->setColor(QMapNodeBase::Red);
+ rotateLeft(x->parent()->parent());
+ }
+ }
}
- ++size;
- return abstractNode;
+ root->setColor(QMapNodeBase::Black);
}
-void QMapData::node_delete(Node *update[], int offset, Node *node)
+void QMapDataBase::freeNodeAndRebalance(QMapNodeBase *z)
{
- node->forward[0]->backward = node->backward;
-
- for (int i = 0; i <= topLevel; ++i) {
- if (update[i]->forward[i] != node)
- break;
- update[i]->forward[i] = node->forward[i];
+ QMapNodeBase *&root = header.left;
+ QMapNodeBase *y = z;
+ QMapNodeBase *x;
+ QMapNodeBase *x_parent;
+ if (y->left == 0) {
+ x = y->right;
+ } else {
+ if (y->right == 0) {
+ x = y->left;
+ } else {
+ y = y->right;
+ while (y->left != 0)
+ y = y->left;
+ x = y->right;
+ }
}
+ if (y != z) {
+ z->left->setParent(y);
+ y->left = z->left;
+ if (y != z->right) {
+ x_parent = y->parent();
+ if (x)
+ x->setParent(y->parent());
+ y->parent()->left = x;
+ y->right = z->right;
+ z->right->setParent(y);
+ } else {
+ x_parent = y;
+ }
+ if (root == z)
+ root = y;
+ else if (z->parent()->left == z)
+ z->parent()->left = y;
+ else
+ z->parent()->right = y;
+ y->setParent(z->parent());
+ // Swap the colors
+ QMapNodeBase::Color c = y->color();
+ y->setColor(z->color());
+ z->setColor(c);
+ y = z;
+ } else {
+ x_parent = y->parent();
+ if (x)
+ x->setParent(y->parent());
+ if (root == z)
+ root = x;
+ else if (z->parent()->left == z)
+ z->parent()->left = x;
+ else
+ z->parent()->right = x;
+ }
+ if (y->color() != QMapNodeBase::Red) {
+ while (x != root && (x == 0 || x->color() == QMapNodeBase::Black)) {
+ if (x == x_parent->left) {
+ QMapNodeBase *w = x_parent->right;
+ if (w->color() == QMapNodeBase::Red) {
+ w->setColor(QMapNodeBase::Black);
+ x_parent->setColor(QMapNodeBase::Red);
+ rotateLeft(x_parent);
+ w = x_parent->right;
+ }
+ if ((w->left == 0 || w->left->color() == QMapNodeBase::Black) &&
+ (w->right == 0 || w->right->color() == QMapNodeBase::Black)) {
+ w->setColor(QMapNodeBase::Red);
+ x = x_parent;
+ x_parent = x_parent->parent();
+ } else {
+ if (w->right == 0 || w->right->color() == QMapNodeBase::Black) {
+ if (w->left)
+ w->left->setColor(QMapNodeBase::Black);
+ w->setColor(QMapNodeBase::Red);
+ rotateRight(w);
+ w = x_parent->right;
+ }
+ w->setColor(x_parent->color());
+ x_parent->setColor(QMapNodeBase::Black);
+ if (w->right)
+ w->right->setColor(QMapNodeBase::Black);
+ rotateLeft(x_parent);
+ break;
+ }
+ } else {
+ QMapNodeBase *w = x_parent->left;
+ if (w->color() == QMapNodeBase::Red) {
+ w->setColor(QMapNodeBase::Black);
+ x_parent->setColor(QMapNodeBase::Red);
+ rotateRight(x_parent);
+ w = x_parent->left;
+ }
+ if ((w->right == 0 || w->right->color() == QMapNodeBase::Black) &&
+ (w->left == 0 || w->left->color() == QMapNodeBase::Black)) {
+ w->setColor(QMapNodeBase::Red);
+ x = x_parent;
+ x_parent = x_parent->parent();
+ } else {
+ if (w->left == 0 || w->left->color() == QMapNodeBase::Black) {
+ if (w->right)
+ w->right->setColor(QMapNodeBase::Black);
+ w->setColor(QMapNodeBase::Red);
+ rotateLeft(w);
+ w = x_parent->left;
+ }
+ w->setColor(x_parent->color());
+ x_parent->setColor(QMapNodeBase::Black);
+ if (w->left)
+ w->left->setColor(QMapNodeBase::Black);
+ rotateRight(x_parent);
+ break;
+ }
+ }
+ }
+ if (x)
+ x->setColor(QMapNodeBase::Black);
+ }
+ free(y);
--size;
- if (strictAlignment)
- qFreeAligned(reinterpret_cast<char *>(node) - offset);
- else
- free(reinterpret_cast<char *>(node) - offset);
}
-#ifdef QT_QMAP_DEBUG
+static inline int qMapAlignmentThreshold()
+{
+ // malloc on 32-bit platforms should return pointers that are 8-byte
+ // aligned or more while on 64-bit platforms they should be 16-byte aligned
+ // or more
+ return 2 * sizeof(void*);
+}
-uint QMapData::adjust_ptr(Node *node)
+static inline void *qMapAllocate(int alloc, int alignment)
{
- if (node == reinterpret_cast<Node *>(this)) {
- return (uint)0xDEADBEEF;
- } else {
- return (uint)node;
- }
+ return alignment > qMapAlignmentThreshold()
+ ? qMallocAligned(alloc, alignment)
+ : ::malloc(alloc);
}
-void QMapData::dump()
+static inline void qMapDeallocate(QMapNodeBase *node, int alignment)
{
- qDebug("Map data (ref = %d, size = %d, randomBits = %#.8x)", int(ref), size, randomBits);
+ if (alignment > qMapAlignmentThreshold())
+ qFreeAligned(node);
+ else
+ ::free(node);
+}
- QString preOutput;
- QVector<QString> output(topLevel + 1);
- Node *e = reinterpret_cast<Node *>(this);
+QMapNodeBase *QMapDataBase::createNode(int alloc, int alignment, QMapNodeBase *parent, bool left)
+{
+ QMapNodeBase *node = static_cast<QMapNodeBase *>(qMapAllocate(alloc, alignment));
+ Q_CHECK_PTR(node);
- QString str;
- str.sprintf(" %.8x", adjust_ptr(reinterpret_cast<Node *>(this)));
- preOutput += str;
+ memset(node, 0, alloc);
+ ++size;
- Node *update[LastLevel + 1];
- for (int i = 0; i <= topLevel; ++i) {
- str.sprintf("%d: [%.8x] -", i, adjust_ptr(reinterpret_cast<Node *>(forward[i])));
- output[i] += str;
- update[i] = reinterpret_cast<Node *>(forward[i]);
+ if (parent) {
+ if (left) {
+ parent->left = node;
+ } else {
+ parent->right = node;
+ }
+ node->setParent(parent);
+ rebalance(node);
}
+ return node;
+}
+
+void QMapDataBase::freeTree(QMapNodeBase *root, int alignment)
+{
+ if (root->left)
+ freeTree(root->left, alignment);
+ if (root->right)
+ freeTree(root->right, alignment);
+ qMapDeallocate(root, alignment);
+}
- Node *node = reinterpret_cast<Node *>(forward[0]);
- while (node != e) {
- int level = 0;
- while (level < topLevel && update[level + 1] == node)
- ++level;
+QMapDataBase *QMapDataBase::createData()
+{
+ QMapDataBase *d = new QMapDataBase;
- str.sprintf(" %.8x", adjust_ptr(node));
- preOutput += str;
+ d->ref.initializeOwned();
+ d->size = 0;
- for (int i = 0; i <= level; ++i) {
- str.sprintf("-> [%.8x] -", adjust_ptr(node->forward[i]));
- output[i] += str;
- update[i] = node->forward[i];
- }
- for (int j = level + 1; j <= topLevel; ++j)
- output[j] += QLatin1String("---------------");
- node = node->forward[0];
- }
+ d->header.p = 0;
+ d->header.left = 0;
+ d->header.right = 0;
- qDebug("%s", preOutput.ascii());
- for (int i = 0; i <= topLevel; ++i)
- qDebug("%s", output[i].ascii());
+ return d;
+}
+
+void QMapDataBase::freeData(QMapDataBase *d)
+{
+ delete d;
}
-#endif
/*!
\class QMap
@@ -482,11 +628,6 @@ void QMapData::dump()
\internal
*/
-/*! \fn void QMap::setInsertInOrder(bool sharable)
-
- \internal
-*/
-
/*! \fn void QMap::clear()
Removes all items from the map.
@@ -529,22 +670,21 @@ void QMapData::dump()
/*! \fn const T QMap::value(const Key &key) const
- Returns the value associated with the key \a key.
-
- If the map contains no item with key \a key, the function
- returns a \l{default-constructed value}. If there are multiple
- items for \a key in the map, the value of the most recently
- inserted one is returned.
-
- \sa key(), values(), contains(), operator[]()
*/
/*! \fn const T QMap::value(const Key &key, const T &defaultValue) const
\overload
+ Returns the value associated with the key \a key.
+
If the map contains no item with key \a key, the function returns
- \a defaultValue.
+ \a defaultValue. If no \a defaultValue is specified, the function
+ returns a \l{default-constructed value}. If there are multiple
+ items for \a key in the map, the value of the most recently
+ inserted one is returned.
+
+ \sa key(), values(), contains(), operator[]()
*/
/*! \fn T &QMap::operator[](const Key &key)
@@ -606,32 +746,21 @@ void QMapData::dump()
by value.
*/
-/*! \fn Key QMap::key(const T &value) const
-
- Returns the first key with value \a value.
-
- If the map contains no item with value \a value, the function
- returns a \link {default-constructed value} default-constructed
- key \endlink.
-
- This function can be slow (\l{linear time}), because QMap's
- internal data structure is optimized for fast lookup by key, not
- by value.
-
- \sa value(), keys()
-*/
-
/*!
\fn Key QMap::key(const T &value, const Key &defaultKey) const
\since 4.3
\overload
Returns the first key with value \a value, or \a defaultKey if
- the map contains no item with value \a value.
+ the map contains no item with value \a value. If no \a defaultKey
+ is provided the function returns a \link {default-constructed value}
+ default-constructed key \endlink.
This function can be slow (\l{linear time}), because QMap's
internal data structure is optimized for fast lookup by key, not
by value.
+
+ \sa value(), keys()
*/
/*! \fn QList<T> QMap::values() const
diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h
index a00f9477f6..3f46a8ad1e 100644
--- a/src/corelib/tools/qmap.h
+++ b/src/corelib/tools/qmap.h
@@ -45,6 +45,11 @@
#include <QtCore/qiterator.h>
#include <QtCore/qlist.h>
#include <QtCore/qrefcount.h>
+#include <QtCore/qpair.h>
+
+#ifdef Q_MAP_DEBUG
+#include <QtCore/qdebug.h>
+#endif
#ifndef QT_NO_STL
#include <map>
@@ -56,39 +61,6 @@ QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
-
-struct Q_CORE_EXPORT QMapData
-{
- struct Node {
- Node *backward;
- Node *forward[1];
- };
- enum { LastLevel = 11, Sparseness = 3 };
-
- QMapData *backward;
- QMapData *forward[QMapData::LastLevel + 1];
- QtPrivate::RefCount ref;
- int topLevel;
- int size;
- uint randomBits;
- uint insertInOrder : 1;
- uint sharable : 1;
- uint strictAlignment : 1;
- uint reserved : 29;
-
- static QMapData *createData(int alignment);
- void continueFreeData(int offset);
- Node *node_create(Node *update[], int offset, int alignment);
- void node_delete(Node *update[], int offset, Node *node);
-#ifdef QT_QMAP_DEBUG
- uint adjust_ptr(Node *node);
- void dump();
-#endif
-
- static const QMapData shared_null;
-};
-
-
/*
QMap uses qMapLessThanKey() to compare keys. The default
implementation uses operator<(). For pointer types,
@@ -104,85 +76,275 @@ template <class Key> inline bool qMapLessThanKey(const Key &key1, const Key &key
return key1 < key2;
}
-template <class Ptr> inline bool qMapLessThanKey(Ptr *key1, Ptr *key2)
+template <class Ptr> inline bool qMapLessThanKey(const Ptr *key1, const Ptr *key2)
{
- Q_ASSERT(sizeof(quintptr) == sizeof(Ptr *));
+ Q_STATIC_ASSERT(sizeof(quintptr) == sizeof(const Ptr *));
return quintptr(key1) < quintptr(key2);
}
-template <class Ptr> inline bool qMapLessThanKey(const Ptr *key1, const Ptr *key2)
+struct QMapDataBase;
+template <class Key, class T> struct QMapData;
+
+struct Q_CORE_EXPORT QMapNodeBase
{
- Q_ASSERT(sizeof(quintptr) == sizeof(const Ptr *));
- return quintptr(key1) < quintptr(key2);
-}
+ quintptr p;
+ QMapNodeBase *left;
+ QMapNodeBase *right;
+
+ enum Color { Red = 0, Black = 1 };
+ enum { Mask = 3 }; // reserve the second bit as well
+
+ const QMapNodeBase *nextNode() const;
+ QMapNodeBase *nextNode() { return const_cast<QMapNodeBase *>(const_cast<const QMapNodeBase *>(this)->nextNode()); }
+ const QMapNodeBase *previousNode() const;
+ QMapNodeBase *previousNode() { return const_cast<QMapNodeBase *>(const_cast<const QMapNodeBase *>(this)->previousNode()); }
+
+ Color color() const { return Color(p & 1); }
+ void setColor(Color c) { if (c == Black) p |= Black; else p &= ~Black; }
+ QMapNodeBase *parent() const { return reinterpret_cast<QMapNodeBase *>(p & ~Mask); }
+ void setParent(QMapNodeBase *pp) { p = (p & Mask) | quintptr(pp); }
+
+ QMapNodeBase *minimumNode() { QMapNodeBase *n = this; while (n->left) n = n->left; return n; }
+ QMapNodeBase *maximumNode() { QMapNodeBase *n = this; while (n->right) n = n->right; return n; }
+ const QMapNodeBase *minimumNode() const { const QMapNodeBase *n = this; while (n->left) n = n->left; return n; }
+ const QMapNodeBase *maximumNode() const { const QMapNodeBase *n = this; while (n->right) n = n->right; return n; }
+};
template <class Key, class T>
-struct QMapNode {
+struct QMapNode : public QMapNodeBase
+{
Key key;
T value;
+ inline QMapNode *leftNode() const { return static_cast<QMapNode *>(left); }
+ inline QMapNode *rightNode() const { return static_cast<QMapNode *>(right); }
+
+ inline const QMapNode *nextNode() const { return static_cast<const QMapNode *>(QMapNodeBase::nextNode()); }
+ inline const QMapNode *previousNode() const { return static_cast<const QMapNode *>(QMapNodeBase::previousNode()); }
+ inline QMapNode *nextNode() { return static_cast<QMapNode *>(QMapNodeBase::nextNode()); }
+ inline QMapNode *previousNode() { return static_cast<QMapNode *>(QMapNodeBase::previousNode()); }
+
+ QMapNode *minimumNode() { return static_cast<QMapNode *>(QMapNodeBase::minimumNode()); }
+ QMapNode *maximumNode() { return static_cast<QMapNode *>(QMapNodeBase::maximumNode()); }
+ const QMapNode *minimumNode() const { return static_cast<QMapNode *>(QMapNodeBase::minimumNode()); }
+ const QMapNode *maximumNode() const { return static_cast<QMapNode *>(QMapNodeBase::maximumNode()); }
+
+ QMapNode<Key, T> *copy(QMapData<Key, T> *d) const;
+
+ void destroySubTree();
+
+ QMapNode<Key, T> *lowerBound(const Key &key);
+ QMapNode<Key, T> *upperBound(const Key &key);
+
private:
- // never access these members through this structure.
- // see below
- QMapData::Node *backward;
- QMapData::Node *forward[1];
+ QMapNode() Q_DECL_EQ_DELETE;
+ Q_DISABLE_COPY(QMapNode)
};
template <class Key, class T>
-struct QMapPayloadNode
+inline QMapNode<Key, T> *QMapNode<Key, T>::lowerBound(const Key &akey)
{
- Key key;
- T value;
+ QMapNode<Key, T> *n = this;
+ QMapNode<Key, T> *last = 0;
+ while (n) {
+ if (!qMapLessThanKey(n->key, akey)) {
+ last = n;
+ n = n->leftNode();
+ } else {
+ n = n->rightNode();
+ }
+ }
+ return last;
+}
-private:
- // QMap::e is a pointer to QMapData::Node, which matches the member
- // below. However, the memory allocation node in QMapData::node_create
- // allocates sizeof(QMapPayloNode) and incorrectly calculates the offset
- // of 'backward' below. If the alignment of QMapPayloadNode is larger
- // than the alignment of a pointer, the 'backward' member is aligned to
- // the end of this structure, not to 'value' above, and will occupy the
- // tail-padding area.
- //
- // e.g., on a 32-bit archictecture with Key = int and
- // sizeof(T) = alignof(T) = 8
- // 0 4 8 12 16 20 24 byte
- // | key | PAD | value |backward| PAD | correct layout
- // | key | PAD | value | |backward| how it's actually used
- // |<----- value of QMap::payload() = 20 ----->|
- QMapData::Node *backward;
+template <class Key, class T>
+inline QMapNode<Key, T> *QMapNode<Key, T>::upperBound(const Key &akey)
+{
+ QMapNode<Key, T> *n = this;
+ QMapNode<Key, T> *last = 0;
+ while (n) {
+ if (qMapLessThanKey(akey, n->key)) {
+ last = n;
+ n = n->leftNode();
+ } else {
+ n = n->rightNode();
+ }
+ }
+ return last;
+}
+
+
+
+struct Q_CORE_EXPORT QMapDataBase
+{
+ QtPrivate::RefCount ref;
+ int size;
+ QMapNodeBase header;
+
+ void rotateLeft(QMapNodeBase *x);
+ void rotateRight(QMapNodeBase *x);
+ void rebalance(QMapNodeBase *x);
+ void freeNodeAndRebalance(QMapNodeBase *z);
+
+ QMapNodeBase *createNode(int size, int alignment, QMapNodeBase *parent, bool left);
+ void freeTree(QMapNodeBase *root, int alignment);
+
+ static const QMapDataBase shared_null;
+
+ static QMapDataBase *createData();
+ static void freeData(QMapDataBase *d);
};
template <class Key, class T>
-class QMap
+struct QMapData : public QMapDataBase
{
typedef QMapNode<Key, T> Node;
- typedef QMapPayloadNode<Key, T> PayloadNode;
- union {
- QMapData *d;
- QMapData::Node *e;
- };
+ Node *root() const { return static_cast<Node *>(header.left); }
- static inline int payload() { return sizeof(PayloadNode) - sizeof(QMapData::Node *); }
- static inline int alignment() {
-#ifdef Q_ALIGNOF
- return int(qMax(sizeof(void*), Q_ALIGNOF(Node)));
-#else
- return 0;
-#endif
+ const Node *end() const { return static_cast<const Node *>(&header); }
+ Node *end() { return static_cast<Node *>(&header); }
+ const Node *begin() const { if (root()) return root()->minimumNode(); return end(); }
+ Node *begin() { if (root()) return root()->minimumNode(); return end(); }
+
+ void deleteNode(Node *z);
+ Node *findNode(const Key &akey) const;
+ void nodeRange(const Key &akey, Node **first, Node **last);
+
+ Node *createNode(const Key &k, const T &v, Node *parent = 0, bool left = false)
+ {
+ Node *n = static_cast<Node *>(QMapDataBase::createNode(sizeof(Node), Q_ALIGNOF(Node),
+ parent, left));
+ QT_TRY {
+ new (&n->key) Key(k);
+ QT_TRY {
+ new (&n->value) T(v);
+ } QT_CATCH(...) {
+ n->key.~Key();
+ QT_RETHROW;
+ }
+ } QT_CATCH(...) {
+ QMapDataBase::freeNodeAndRebalance(n);
+ QT_RETHROW;
+ }
+ return n;
}
- static inline Node *concrete(QMapData::Node *node) {
- return reinterpret_cast<Node *>(reinterpret_cast<char *>(node) - payload());
+
+ static QMapData *create() {
+ return static_cast<QMapData *>(createData());
}
+ void free() {
+ if (root()) {
+ root()->destroySubTree();
+ freeTree(header.left, Q_ALIGNOF(Node));
+ }
+ freeData(this);
+ }
+};
+
+template <class Key, class T>
+QMapNode<Key, T> *QMapNode<Key, T>::copy(QMapData<Key, T> *d) const
+{
+ QMapNode<Key, T> *n = d->createNode(key, value);
+ n->setColor(color());
+ if (left) {
+ n->left = leftNode()->copy(d);
+ n->left->setParent(n);
+ } else {
+ n->left = 0;
+ }
+ if (right) {
+ n->right = rightNode()->copy(d);
+ n->right->setParent(n);
+ } else {
+ n->right = 0;
+ }
+ return n;
+}
+
+template <class Key, class T>
+void QMapNode<Key, T>::destroySubTree()
+{
+ if (QTypeInfo<Key>::isComplex)
+ key.~Key();
+ if (QTypeInfo<T>::isComplex)
+ value.~T();
+ if (QTypeInfo<Key>::isComplex || QTypeInfo<T>::isComplex) {
+ if (left)
+ leftNode()->destroySubTree();
+ if (right)
+ rightNode()->destroySubTree();
+ }
+}
+
+template <class Key, class T>
+void QMapData<Key, T>::deleteNode(QMapNode<Key, T> *z)
+{
+ if (QTypeInfo<Key>::isComplex)
+ z->key.~Key();
+ if (QTypeInfo<T>::isComplex)
+ z->value.~T();
+ freeNodeAndRebalance(z);
+}
+
+template <class Key, class T>
+QMapNode<Key, T> *QMapData<Key, T>::findNode(const Key &akey) const
+{
+ Node *lb = root()->lowerBound(akey);
+ if (lb && !qMapLessThanKey(akey, lb->key))
+ return lb;
+ return 0;
+}
+
+
+template <class Key, class T>
+void QMapData<Key, T>::nodeRange(const Key &akey, QMapNode<Key, T> **first, QMapNode<Key, T> **last)
+{
+ Node *n = root();
+ Node *l = end();
+ while (n) {
+ if (qMapLessThanKey(akey, n->key)) {
+ l = n;
+ n = n->leftNode();
+ } else if (qMapLessThanKey(n->key, akey)) {
+ n = n->rightNode();
+ } else {
+ *first = n->leftNode()->lowerBound(akey);
+ if (!*first)
+ *first = n;
+ *last = n->rightNode()->upperBound(akey);
+ if (!*last)
+ *last = l;
+ return;
+ }
+ }
+ *first = *last = l;
+}
+
+
+template <class Key, class T>
+class QMap
+{
+ typedef QMapNode<Key, T> Node;
+
+ QMapData<Key, T> *d;
+
public:
- inline QMap() : d(const_cast<QMapData *>(&QMapData::shared_null)) { }
- inline QMap(const QMap<Key, T> &other) : d(other.d)
- { d->ref.ref(); if (!d->sharable) detach(); }
- inline ~QMap() { if (!d) return; if (!d->ref.deref()) freeData(d); }
+ inline QMap() : d(static_cast<QMapData<Key, T> *>(const_cast<QMapDataBase *>(&QMapDataBase::shared_null))) { }
+ QMap(const QMap<Key, T> &other);
+
+ inline ~QMap() { if (!d->ref.deref()) d->free(); }
QMap<Key, T> &operator=(const QMap<Key, T> &other);
#ifdef Q_COMPILER_RVALUE_REFS
+ inline QMap(QMap<Key, T> &&other)
+ : d(other.d)
+ {
+ other.d = static_cast<QMapData<Key, T> *>(
+ const_cast<QMapDataBase *>(&QMapDataBase::shared_null));
+ }
+
inline QMap<Key, T> &operator=(QMap<Key, T> &&other)
{ qSwap(d, other.d); return *this; }
#endif
@@ -199,11 +361,18 @@ public:
inline bool isEmpty() const { return d->size == 0; }
- inline void detach() { if (d->ref != 1) detach_helper(); }
- inline bool isDetached() const { return d->ref == 1; }
- inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QMapData::shared_null) d->sharable = sharable; }
+ inline void detach() { if (d->ref.isShared()) detach_helper(); }
+ inline bool isDetached() const { return !d->ref.isShared(); }
+ inline void setSharable(bool sharable)
+ {
+ if (sharable == d->ref.isSharable())
+ return;
+ if (!sharable)
+ detach();
+ // Don't call on shared_null
+ d->ref.setSharable(sharable);
+ }
inline bool isSharedWith(const QMap<Key, T> &other) const { return d == other.d; }
- inline void setInsertInOrder(bool ordered) { if (ordered) detach(); if (d != &QMapData::shared_null) d->insertInOrder = ordered; }
void clear();
@@ -211,10 +380,8 @@ public:
T take(const Key &key);
bool contains(const Key &key) const;
- const Key key(const T &value) const;
- const Key key(const T &value, const Key &defaultKey) const;
- const T value(const Key &key) const;
- const T value(const Key &key, const T &defaultValue) const;
+ const Key key(const T &value, const Key &defaultKey = Key()) const;
+ const T value(const Key &key, const T &defaultValue = T()) const;
T &operator[](const Key &key);
const T operator[](const Key &key) const;
@@ -230,7 +397,7 @@ public:
class iterator
{
friend class const_iterator;
- QMapData::Node *i;
+ Node *i;
public:
typedef std::bidirectional_iterator_tag iterator_category;
@@ -239,34 +406,32 @@ public:
typedef T *pointer;
typedef T &reference;
- // ### Qt 5: get rid of 'operator Node *'
- inline operator QMapData::Node *() const { return i; }
inline iterator() : i(0) { }
- inline iterator(QMapData::Node *node) : i(node) { }
+ inline iterator(Node *node) : i(node) { }
- inline const Key &key() const { return concrete(i)->key; }
- inline T &value() const { return concrete(i)->value; }
- inline T &operator*() const { return concrete(i)->value; }
- inline T *operator->() const { return &concrete(i)->value; }
+ inline const Key &key() const { return i->key; }
+ inline T &value() const { return i->value; }
+ inline T &operator*() const { return i->value; }
+ inline T *operator->() const { return &i->value; }
inline bool operator==(const iterator &o) const { return i == o.i; }
inline bool operator!=(const iterator &o) const { return i != o.i; }
inline iterator &operator++() {
- i = i->forward[0];
+ i = i->nextNode();
return *this;
}
inline iterator operator++(int) {
iterator r = *this;
- i = i->forward[0];
+ i = i->nextNode();
return r;
}
inline iterator &operator--() {
- i = i->backward;
+ i = i->previousNode();
return *this;
}
inline iterator operator--(int) {
iterator r = *this;
- i = i->backward;
+ i = i->previousNode();
return r;
}
inline iterator operator+(int j) const
@@ -275,7 +440,6 @@ public:
inline iterator &operator+=(int j) { return *this = *this + j; }
inline iterator &operator-=(int j) { return *this = *this - j; }
- // ### Qt 5: not sure this is necessary anymore
#ifdef QT_STRICT_ITERATORS
private:
#else
@@ -285,17 +449,14 @@ public:
{ return i == o.i; }
inline bool operator!=(const const_iterator &o) const
{ return i != o.i; }
-
- private:
- // ### Qt 5: remove
- inline operator bool() const { return false; }
+ friend class QMap<Key, T>;
};
friend class iterator;
class const_iterator
{
friend class iterator;
- QMapData::Node *i;
+ const Node *i;
public:
typedef std::bidirectional_iterator_tag iterator_category;
@@ -304,10 +465,8 @@ public:
typedef const T *pointer;
typedef const T &reference;
- // ### Qt 5: get rid of 'operator Node *'
- inline operator QMapData::Node *() const { return i; }
inline const_iterator() : i(0) { }
- inline const_iterator(QMapData::Node *node) : i(node) { }
+ inline const_iterator(const Node *node) : i(node) { }
#ifdef QT_STRICT_ITERATORS
explicit inline const_iterator(const iterator &o)
#else
@@ -315,29 +474,29 @@ public:
#endif
{ i = o.i; }
- inline const Key &key() const { return concrete(i)->key; }
- inline const T &value() const { return concrete(i)->value; }
- inline const T &operator*() const { return concrete(i)->value; }
- inline const T *operator->() const { return &concrete(i)->value; }
+ inline const Key &key() const { return i->key; }
+ inline const T &value() const { return i->value; }
+ inline const T &operator*() const { return i->value; }
+ inline const T *operator->() const { return &i->value; }
inline bool operator==(const const_iterator &o) const { return i == o.i; }
inline bool operator!=(const const_iterator &o) const { return i != o.i; }
inline const_iterator &operator++() {
- i = i->forward[0];
+ i = i->nextNode();
return *this;
}
inline const_iterator operator++(int) {
const_iterator r = *this;
- i = i->forward[0];
+ i = i->nextNode();
return r;
}
inline const_iterator &operator--() {
- i = i->backward;
+ i = i->previousNode();
return *this;
}
inline const_iterator operator--(int) {
const_iterator r = *this;
- i = i->backward;
+ i = i->previousNode();
return r;
}
inline const_iterator operator+(int j) const
@@ -346,31 +505,24 @@ public:
inline const_iterator &operator+=(int j) { return *this = *this + j; }
inline const_iterator &operator-=(int j) { return *this = *this - j; }
- // ### Qt 5: not sure this is necessary anymore
#ifdef QT_STRICT_ITERATORS
private:
inline bool operator==(const iterator &o) const { return operator==(const_iterator(o)); }
inline bool operator!=(const iterator &o) const { return operator!=(const_iterator(o)); }
#endif
-
- private:
- // ### Qt 5: remove
- inline operator bool() const { return false; }
+ friend class QMap<Key, T>;
};
friend class const_iterator;
// STL style
- inline iterator begin() { detach(); return iterator(e->forward[0]); }
- inline const_iterator begin() const { return const_iterator(e->forward[0]); }
- inline const_iterator cbegin() const { return const_iterator(e->forward[0]); }
- inline const_iterator constBegin() const { return const_iterator(e->forward[0]); }
- inline iterator end() {
- detach();
- return iterator(e);
- }
- inline const_iterator end() const { return const_iterator(e); }
- inline const_iterator cend() const { return const_iterator(e); }
- inline const_iterator constEnd() const { return const_iterator(e); }
+ inline iterator begin() { detach(); return iterator(d->begin()); }
+ inline const_iterator begin() const { return const_iterator(d->begin()); }
+ inline const_iterator constBegin() const { return const_iterator(d->begin()); }
+ inline const_iterator cbegin() const { return const_iterator(d->begin()); }
+ inline iterator end() { detach(); return iterator(d->end()); }
+ inline const_iterator end() const { return const_iterator(d->end()); }
+ inline const_iterator constEnd() const { return const_iterator(d->end()); }
+ inline const_iterator cend() const { return const_iterator(d->end()); }
iterator erase(iterator it);
// more Qt
@@ -394,110 +546,52 @@ public:
typedef qptrdiff difference_type;
typedef int size_type;
inline bool empty() const { return isEmpty(); }
+ QPair<iterator, iterator> equal_range(const Key &akey);
-#ifdef QT_QMAP_DEBUG
- inline void dump() const { d->dump(); }
+#ifdef Q_MAP_DEBUG
+ void dump() const;
#endif
private:
void detach_helper();
- void freeData(QMapData *d);
- QMapData::Node *findNode(const Key &key) const;
- QMapData::Node *mutableFindNode(QMapData::Node *update[], const Key &key) const;
- QMapData::Node *node_create(QMapData *d, QMapData::Node *update[], const Key &key,
- const T &value);
};
template <class Key, class T>
-Q_INLINE_TEMPLATE QMap<Key, T> &QMap<Key, T>::operator=(const QMap<Key, T> &other)
+inline QMap<Key, T>::QMap(const QMap<Key, T> &other)
{
- if (d != other.d) {
- QMapData* o = other.d;
- o->ref.ref();
- if (!d->ref.deref())
- freeData(d);
- d = o;
- if (!d->sharable)
- detach_helper();
- }
- return *this;
-}
-
-template <class Key, class T>
-Q_INLINE_TEMPLATE void QMap<Key, T>::clear()
-{
- *this = QMap<Key, T>();
-}
-
-template <class Key, class T>
-Q_INLINE_TEMPLATE typename QMapData::Node *
-QMap<Key, T>::node_create(QMapData *adt, QMapData::Node *aupdate[], const Key &akey, const T &avalue)
-{
- QMapData::Node *abstractNode = adt->node_create(aupdate, payload(), alignment());
- QT_TRY {
- Node *concreteNode = concrete(abstractNode);
- new (&concreteNode->key) Key(akey);
- QT_TRY {
- new (&concreteNode->value) T(avalue);
- } QT_CATCH(...) {
- concreteNode->key.~Key();
- QT_RETHROW;
+ if (other.d->ref.ref()) {
+ d = other.d;
+ } else {
+ d = QMapData<Key, T>::create();
+ if (other.d->header.left) {
+ d->header.left = static_cast<Node *>(other.d->header.left)->copy(d);
+ d->header.left->setParent(&d->header);
}
- } QT_CATCH(...) {
- adt->node_delete(aupdate, payload(), abstractNode);
- QT_RETHROW;
}
-
- // clean up the update array for further insertions
- /*
- for (int i = 0; i <= d->topLevel; ++i) {
- if ( aupdate[i]==reinterpret_cast<QMapData::Node *>(adt) || aupdate[i]->forward[i] != abstractNode)
- break;
- aupdate[i] = abstractNode;
- }
-*/
-
- return abstractNode;
}
template <class Key, class T>
-Q_INLINE_TEMPLATE QMapData::Node *QMap<Key, T>::findNode(const Key &akey) const
+Q_INLINE_TEMPLATE QMap<Key, T> &QMap<Key, T>::operator=(const QMap<Key, T> &other)
{
- QMapData::Node *cur = e;
- QMapData::Node *next = e;
-
- for (int i = d->topLevel; i >= 0; i--) {
- while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, akey))
- cur = next;
- }
-
- if (next != e && !qMapLessThanKey<Key>(akey, concrete(next)->key)) {
- return next;
- } else {
- return e;
+ if (d != other.d) {
+ QMap<Key, T> tmp(other);
+ tmp.swap(*this);
}
+ return *this;
}
template <class Key, class T>
-Q_INLINE_TEMPLATE const T QMap<Key, T>::value(const Key &akey) const
+Q_INLINE_TEMPLATE void QMap<Key, T>::clear()
{
- QMapData::Node *node;
- if (d->size == 0 || (node = findNode(akey)) == e) {
- return T();
- } else {
- return concrete(node)->value;
- }
+ *this = QMap<Key, T>();
}
+
template <class Key, class T>
Q_INLINE_TEMPLATE const T QMap<Key, T>::value(const Key &akey, const T &adefaultValue) const
{
- QMapData::Node *node;
- if (d->size == 0 || (node = findNode(akey)) == e) {
- return adefaultValue;
- } else {
- return concrete(node)->value;
- }
+ Node *n = d->findNode(akey);
+ return n ? n->value : adefaultValue;
}
template <class Key, class T>
@@ -510,24 +604,25 @@ template <class Key, class T>
Q_INLINE_TEMPLATE T &QMap<Key, T>::operator[](const Key &akey)
{
detach();
-
- QMapData::Node *update[QMapData::LastLevel + 1];
- QMapData::Node *node = mutableFindNode(update, akey);
- if (node == e)
- node = node_create(d, update, akey, T());
- return concrete(node)->value;
+ Node *n = d->findNode(akey);
+ if (!n)
+ return *insert(akey, T());
+ return n->value;
}
template <class Key, class T>
Q_INLINE_TEMPLATE int QMap<Key, T>::count(const Key &akey) const
{
+ Node *firstNode;
+ Node *lastNode;
+ d->nodeRange(akey, &firstNode, &lastNode);
+
+ const_iterator first(firstNode);
+ const const_iterator last(lastNode);
int cnt = 0;
- QMapData::Node *node = findNode(akey);
- if (node != e) {
- do {
- ++cnt;
- node = node->forward[0];
- } while (node != e && !qMapLessThanKey<Key>(akey, concrete(node)->key));
+ while (first != last) {
+ ++cnt;
+ ++first;
}
return cnt;
}
@@ -535,23 +630,34 @@ Q_INLINE_TEMPLATE int QMap<Key, T>::count(const Key &akey) const
template <class Key, class T>
Q_INLINE_TEMPLATE bool QMap<Key, T>::contains(const Key &akey) const
{
- return findNode(akey) != e;
+ return d->findNode(akey) != 0;
}
template <class Key, class T>
-Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insert(const Key &akey,
- const T &avalue)
+Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insert(const Key &akey, const T &avalue)
{
detach();
-
- QMapData::Node *update[QMapData::LastLevel + 1];
- QMapData::Node *node = mutableFindNode(update, akey);
- if (node == e) {
- node = node_create(d, update, akey, avalue);
- } else {
- concrete(node)->value = avalue;
+ Node *n = d->root();
+ Node *y = d->end();
+ Node *last = 0;
+ bool left = true;
+ while (n) {
+ y = n;
+ if (!qMapLessThanKey(n->key, akey)) {
+ last = n;
+ left = true;
+ n = n->leftNode();
+ } else {
+ left = false;
+ n = n->rightNode();
+ }
}
- return iterator(node);
+ if (last && !qMapLessThanKey(akey, last->key)) {
+ last->value = avalue;
+ return iterator(last);
+ }
+ Node *z = d->createNode(akey, avalue, y, left);
+ return iterator(z);
}
template <class Key, class T>
@@ -559,29 +665,37 @@ Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insertMulti(cons
const T &avalue)
{
detach();
-
- QMapData::Node *update[QMapData::LastLevel + 1];
- mutableFindNode(update, akey);
- return iterator(node_create(d, update, akey, avalue));
+ Node* y = d->end();
+ Node* x = static_cast<Node *>(d->root());
+ bool left = true;
+ while (x != 0) {
+ left = !qMapLessThanKey(x->key, akey);
+ y = x;
+ x = left ? x->leftNode() : x->rightNode();
+ }
+ Node *z = d->createNode(akey, avalue, y, left);
+ return iterator(z);
}
template <class Key, class T>
-Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator QMap<Key, T>::find(const Key &akey) const
+Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator QMap<Key, T>::constFind(const Key &akey) const
{
- return const_iterator(findNode(akey));
+ Node *n = d->findNode(akey);
+ return const_iterator(n ? n : d->end());
}
template <class Key, class T>
-Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator QMap<Key, T>::constFind(const Key &akey) const
+Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator QMap<Key, T>::find(const Key &akey) const
{
- return const_iterator(findNode(akey));
+ return constFind(akey);
}
template <class Key, class T>
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::find(const Key &akey)
{
detach();
- return iterator(findNode(akey));
+ Node *n = d->findNode(akey);
+ return iterator(n ? n : d->end());
}
template <class Key, class T>
@@ -598,56 +712,46 @@ Q_INLINE_TEMPLATE QMap<Key, T> &QMap<Key, T>::unite(const QMap<Key, T> &other)
}
template <class Key, class T>
-Q_OUTOFLINE_TEMPLATE void QMap<Key, T>::freeData(QMapData *x)
+QPair<typename QMap<Key, T>::iterator, typename QMap<Key, T>::iterator> QMap<Key, T>::equal_range(const Key &akey)
{
- if (QTypeInfo<Key>::isComplex || QTypeInfo<T>::isComplex) {
- QMapData *cur = x;
- QMapData *next = cur->forward[0];
- while (next != x) {
- cur = next;
- next = cur->forward[0];
-#if defined(_MSC_VER)
-#pragma warning(disable:4189)
-#endif
- Node *concreteNode = concrete(reinterpret_cast<QMapData::Node *>(cur));
- concreteNode->key.~Key();
- concreteNode->value.~T();
-#if defined(_MSC_VER)
-#pragma warning(default:4189)
-#endif
+ detach();
+ Node *first, *last;
+ d->nodeRange(akey, &first, &last);
+ return QPair<iterator, iterator>(iterator(first), iterator(last));
+}
+
+#ifdef Q_MAP_DEBUG
+template <class Key, class T>
+void QMap<Key, T>::dump() const
+{
+ const_iterator it = begin();
+ qDebug() << "map dump:";
+ while (it != end()) {
+ const QMapNodeBase *n = it.i;
+ int depth = 0;
+ while (n && n != d->root()) {
+ ++depth;
+ n = n->parent();
}
+ QByteArray space(4*depth, ' ');
+ qDebug() << space << (it.i->color() == Node::Red ? "Red " : "Black") << it.i << it.i->left << it.i->right
+ << it.key() << it.value();
+ ++it;
}
- x->continueFreeData(payload());
+ qDebug() << "---------";
}
+#endif
template <class Key, class T>
Q_OUTOFLINE_TEMPLATE int QMap<Key, T>::remove(const Key &akey)
{
detach();
-
- QMapData::Node *update[QMapData::LastLevel + 1];
- QMapData::Node *cur = e;
- QMapData::Node *next = e;
- int oldSize = d->size;
-
- for (int i = d->topLevel; i >= 0; i--) {
- while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, akey))
- cur = next;
- update[i] = cur;
- }
-
- if (next != e && !qMapLessThanKey<Key>(akey, concrete(next)->key)) {
- bool deleteNext = true;
- do {
- cur = next;
- next = cur->forward[0];
- deleteNext = (next != e && !qMapLessThanKey<Key>(concrete(cur)->key, concrete(next)->key));
- concrete(cur)->key.~Key();
- concrete(cur)->value.~T();
- d->node_delete(update, payload(), cur);
- } while (deleteNext);
+ int n = 0;
+ while (Node *node = d->findNode(akey)) {
+ d->deleteNode(node);
+ ++n;
}
- return oldSize - d->size;
+ return n;
}
template <class Key, class T>
@@ -655,21 +759,10 @@ Q_OUTOFLINE_TEMPLATE T QMap<Key, T>::take(const Key &akey)
{
detach();
- QMapData::Node *update[QMapData::LastLevel + 1];
- QMapData::Node *cur = e;
- QMapData::Node *next = e;
-
- for (int i = d->topLevel; i >= 0; i--) {
- while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, akey))
- cur = next;
- update[i] = cur;
- }
-
- if (next != e && !qMapLessThanKey<Key>(akey, concrete(next)->key)) {
- T t = concrete(next)->value;
- concrete(next)->key.~Key();
- concrete(next)->value.~T();
- d->node_delete(update, payload(), next);
+ Node *node = d->findNode(akey);
+ if (node) {
+ T t = node->value;
+ d->deleteNode(node);
return t;
}
return T();
@@ -678,82 +771,26 @@ Q_OUTOFLINE_TEMPLATE T QMap<Key, T>::take(const Key &akey)
template <class Key, class T>
Q_OUTOFLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::erase(iterator it)
{
- QMapData::Node *update[QMapData::LastLevel + 1];
- QMapData::Node *cur = e;
- QMapData::Node *next = e;
-
- if (it == iterator(e))
+ if (it == iterator(d->end()))
return it;
- for (int i = d->topLevel; i >= 0; i--) {
- while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, it.key()))
- cur = next;
- update[i] = cur;
- }
-
- while (next != e) {
- cur = next;
- next = cur->forward[0];
- if (cur == it) {
- concrete(cur)->key.~Key();
- concrete(cur)->value.~T();
- d->node_delete(update, payload(), cur);
- return iterator(next);
- }
-
- for (int i = 0; i <= d->topLevel; ++i) {
- if (update[i]->forward[i] != cur)
- break;
- update[i] = cur;
- }
- }
- return end();
+ Node *n = it.i;
+ ++it;
+ d->deleteNode(n);
+ return it;
}
template <class Key, class T>
Q_OUTOFLINE_TEMPLATE void QMap<Key, T>::detach_helper()
{
- union { QMapData *d; QMapData::Node *e; } x;
- x.d = QMapData::createData(alignment());
- if (d->size) {
- x.d->insertInOrder = true;
- QMapData::Node *update[QMapData::LastLevel + 1];
- QMapData::Node *cur = e->forward[0];
- update[0] = x.e;
- while (cur != e) {
- QT_TRY {
- Node *concreteNode = concrete(cur);
- node_create(x.d, update, concreteNode->key, concreteNode->value);
- } QT_CATCH(...) {
- freeData(x.d);
- QT_RETHROW;
- }
- cur = cur->forward[0];
- }
- x.d->insertInOrder = false;
+ QMapData<Key, T> *x = QMapData<Key, T>::create();
+ if (d->header.left) {
+ x->header.left = static_cast<Node *>(d->header.left)->copy(x);
+ x->header.left->setParent(&x->header);
}
if (!d->ref.deref())
- freeData(d);
- d = x.d;
-}
-
-template <class Key, class T>
-Q_OUTOFLINE_TEMPLATE QMapData::Node *QMap<Key, T>::mutableFindNode(QMapData::Node *aupdate[],
- const Key &akey) const
-{
- QMapData::Node *cur = e;
- QMapData::Node *next = e;
-
- for (int i = d->topLevel; i >= 0; i--) {
- while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, akey))
- cur = next;
- aupdate[i] = cur;
- }
- if (next != e && !qMapLessThanKey<Key>(akey, concrete(next)->key)) {
- return next;
- } else {
- return e;
- }
+ d->free();
+ d = x;
}
template <class Key, class T>
@@ -769,7 +806,7 @@ Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::uniqueKeys() const
do {
if (++i == end())
goto break_out_of_outer_loop;
- } while (!(aKey < i.key())); // loop while (key == i.key())
+ } while (!qMapLessThanKey(aKey, i.key())); // loop while (key == i.key())
}
}
break_out_of_outer_loop:
@@ -803,12 +840,6 @@ Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::keys(const T &avalue) const
}
template <class Key, class T>
-Q_OUTOFLINE_TEMPLATE const Key QMap<Key, T>::key(const T &avalue) const
-{
- return key(avalue, Key());
-}
-
-template <class Key, class T>
Q_OUTOFLINE_TEMPLATE const Key QMap<Key, T>::key(const T &avalue, const Key &defaultKey) const
{
const_iterator i = begin();
@@ -838,49 +869,54 @@ template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QList<T> QMap<Key, T>::values(const Key &akey) const
{
QList<T> res;
- QMapData::Node *node = findNode(akey);
- if (node != e) {
+ Node *n = d->findNode(akey);
+ if (n) {
+ const_iterator it(n);
do {
- res.append(concrete(node)->value);
- node = node->forward[0];
- } while (node != e && !qMapLessThanKey<Key>(akey, concrete(node)->key));
+ res.append(*it);
+ ++it;
+ } while (it != constEnd() && !qMapLessThanKey<Key>(akey, it.key()));
}
return res;
}
template <class Key, class T>
-Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator
-QMap<Key, T>::lowerBound(const Key &akey) const
+Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator QMap<Key, T>::lowerBound(const Key &akey) const
{
- QMapData::Node *update[QMapData::LastLevel + 1];
- mutableFindNode(update, akey);
- return const_iterator(update[0]->forward[0]);
+ Node *lb = d->root()->lowerBound(akey);
+ if (!lb)
+ lb = d->end();
+ return const_iterator(lb);
}
template <class Key, class T>
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::lowerBound(const Key &akey)
{
detach();
- return static_cast<QMapData::Node *>(const_cast<const QMap *>(this)->lowerBound(akey));
+ Node *lb = d->root()->lowerBound(akey);
+ if (!lb)
+ lb = d->end();
+ return iterator(lb);
}
template <class Key, class T>
Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator
QMap<Key, T>::upperBound(const Key &akey) const
{
- QMapData::Node *update[QMapData::LastLevel + 1];
- mutableFindNode(update, akey);
- QMapData::Node *node = update[0]->forward[0];
- while (node != e && !qMapLessThanKey<Key>(akey, concrete(node)->key))
- node = node->forward[0];
- return const_iterator(node);
+ Node *ub = d->root()->upperBound(akey);
+ if (!ub)
+ ub = d->end();
+ return const_iterator(ub);
}
template <class Key, class T>
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::upperBound(const Key &akey)
{
detach();
- return static_cast<QMapData::Node *>(const_cast<const QMap *>(this)->upperBound(akey));
+ Node *ub = d->root()->upperBound(akey);
+ if (!ub)
+ ub = d->end();
+ return iterator(ub);
}
template <class Key, class T>
@@ -907,14 +943,12 @@ Q_OUTOFLINE_TEMPLATE bool QMap<Key, T>::operator==(const QMap<Key, T> &other) co
template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QMap<Key, T>::QMap(const std::map<Key, T> &other)
{
- d = QMapData::createData(alignment());
- d->insertInOrder = true;
+ d = QMapData<Key, T>::create();
typename std::map<Key,T>::const_iterator it = other.end();
while (it != other.begin()) {
--it;
insert((*it).first, (*it).second);
}
- d->insertInOrder = false;
}
template <class Key, class T>
diff --git a/src/corelib/tools/qrefcount.h b/src/corelib/tools/qrefcount.h
index bc673214fa..16b1d339b3 100644
--- a/src/corelib/tools/qrefcount.h
+++ b/src/corelib/tools/qrefcount.h
@@ -55,37 +55,61 @@ namespace QtPrivate
class RefCount
{
public:
- inline void ref() {
- if (atomic.load() > 0)
+ inline bool ref() {
+ int count = atomic.load();
+ if (count == 0) // !isSharable
+ return false;
+ if (count != -1) // !isStatic
atomic.ref();
+ return true;
}
inline bool deref() {
- if (atomic.load() <= 0)
+ int count = atomic.load();
+ if (count == 0) // !isSharable
+ return false;
+ if (count == -1) // isStatic
return true;
return atomic.deref();
}
- inline bool operator==(int value) const
- { return atomic.load() == value; }
- inline bool operator!=(int value) const
- { return atomic.load() != value; }
- inline bool operator!() const
- { return !atomic.load(); }
- inline operator int() const
- { return atomic.load(); }
- inline RefCount &operator=(int value)
- { atomic.store(value); return *this; }
- inline RefCount &operator=(const RefCount &other)
- { atomic.store(other.atomic.load()); return *this; }
+ bool setSharable(bool sharable)
+ {
+ Q_ASSERT(!isShared());
+ if (sharable)
+ return atomic.testAndSetRelaxed(0, 1);
+ else
+ return atomic.testAndSetRelaxed(1, 0);
+ }
+
+ bool isStatic() const
+ {
+ // Persistent object, never deleted
+ return atomic.load() == -1;
+ }
+
+ bool isSharable() const
+ {
+ // Sharable === Shared ownership.
+ return atomic.load() != 0;
+ }
+
+ bool isShared() const
+ {
+ int count = atomic.load();
+ return (count != 1) && (count != 0);
+ }
+
+ void initializeOwned() { atomic.store(1); }
+ void initializeUnsharable() { atomic.store(0); }
QBasicAtomicInt atomic;
};
-#define Q_REFCOUNT_INITIALIZER(a) { Q_BASIC_ATOMIC_INITIALIZER(a) }
-
}
+#define Q_REFCOUNT_INITIALIZE_STATIC { Q_BASIC_ATOMIC_INITIALIZER(-1) }
+
QT_END_NAMESPACE
QT_END_HEADER
diff --git a/src/corelib/tools/qregularexpression.h b/src/corelib/tools/qregularexpression.h
index 3ca83c9e27..57cb29035b 100644
--- a/src/corelib/tools/qregularexpression.h
+++ b/src/corelib/tools/qregularexpression.h
@@ -239,8 +239,6 @@ Q_DECLARE_TYPEINFO(QRegularExpressionMatchIterator, Q_MOVABLE_TYPE);
QT_END_NAMESPACE
-Q_DECLARE_METATYPE(QRegularExpression)
-
QT_END_HEADER
#endif // QT_NO_REGEXP
diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp
index 77c3d1e2cb..50e555d968 100644
--- a/src/corelib/tools/qsharedpointer.cpp
+++ b/src/corelib/tools/qsharedpointer.cpp
@@ -698,6 +698,35 @@
*/
/*!
+ \fn void QSharedPointer::reset()
+ \since 5.0
+
+ Same as clear(). For std::shared_ptr compatibility.
+*/
+
+/*!
+ \fn void QSharedPointer::reset(T *t)
+ \since 5.0
+
+ Resets this QSharedPointer object to point to \a t
+ instead. Equivalent to:
+ \code
+ QSharedPointer<T> other(t); this->swap(other);
+ \endcode
+*/
+
+/*!
+ \fn void QSharedPointer::reset(T *t, Deleter deleter)
+ \since 5.0
+
+ Resets this QSharedPointer object to point to \a t
+ instead, with deleter \a deleter. Equivalent to:
+ \code
+ QSharedPointer<T> other(t, deleter); this->swap(other);
+ \endcode
+*/
+
+/*!
\fn QWeakPointer::QWeakPointer()
Creates a QWeakPointer that points to nothing.
@@ -1380,47 +1409,14 @@ Q_GLOBAL_STATIC(KnownPointers, knownPointers)
QT_BEGIN_NAMESPACE
namespace QtSharedPointer {
- Q_CORE_EXPORT void internalSafetyCheckAdd(const volatile void *);
- Q_CORE_EXPORT void internalSafetyCheckRemove(const volatile void *);
Q_AUTOTEST_EXPORT void internalSafetyCheckCleanCheck();
}
/*!
\internal
*/
-void QtSharedPointer::internalSafetyCheckAdd(const volatile void *)
-{
- // Qt 4.5 compatibility
- // this function is broken by design, so it was replaced with internalSafetyCheckAdd2
- //
- // it's broken because we tracked the pointers added and
- // removed from QSharedPointer, converted to void*.
- // That is, this is supposed to track the "top-of-object" pointer in
- // case of multiple inheritance.
- //
- // However, it doesn't work well in some compilers:
- // if you create an object with a class of type A and the last reference
- // is dropped of type B, then the value passed to internalSafetyCheckRemove could
- // be different than was added. That would leave dangling addresses.
- //
- // So instead, we track the pointer by the d-pointer instead.
-}
-
-/*!
- \internal
-*/
-void QtSharedPointer::internalSafetyCheckRemove(const volatile void *)
-{
- // Qt 4.5 compatibility
- // see comments above
-}
-
-/*!
- \internal
-*/
-void QtSharedPointer::internalSafetyCheckAdd2(const void *d_ptr, const volatile void *ptr)
+void QtSharedPointer::internalSafetyCheckAdd(const void *d_ptr, const volatile void *ptr)
{
- // see comments above for the rationale for this function
KnownPointers *const kp = knownPointers();
if (!kp)
return; // end-game: the application is being destroyed already
@@ -1453,7 +1449,7 @@ void QtSharedPointer::internalSafetyCheckAdd2(const void *d_ptr, const volatile
/*!
\internal
*/
-void QtSharedPointer::internalSafetyCheckRemove2(const void *d_ptr)
+void QtSharedPointer::internalSafetyCheckRemove(const void *d_ptr)
{
KnownPointers *const kp = knownPointers();
if (!kp)
diff --git a/src/corelib/tools/qsharedpointer.h b/src/corelib/tools/qsharedpointer.h
index 4033b5d422..b0a1b7619d 100644
--- a/src/corelib/tools/qsharedpointer.h
+++ b/src/corelib/tools/qsharedpointer.h
@@ -85,6 +85,11 @@ public:
void clear();
+ void reset();
+ void reset(T *t);
+ template <typename Deleter>
+ void reset(T *t, Deleter deleter);
+
// casts:
template <class X> QSharedPointer<X> staticCast() const;
template <class X> QSharedPointer<X> dynamicCast() const;
diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h
index 58010dd8d9..fadb4e0586 100644
--- a/src/corelib/tools/qsharedpointer_impl.h
+++ b/src/corelib/tools/qsharedpointer_impl.h
@@ -105,8 +105,8 @@ namespace QtSharedPointer {
template <class X, class Y> QSharedPointer<X> copyAndSetPointer(X * ptr, const QSharedPointer<Y> &src);
// used in debug mode to verify the reuse of pointers
- Q_CORE_EXPORT void internalSafetyCheckAdd2(const void *, const volatile void *);
- Q_CORE_EXPORT void internalSafetyCheckRemove2(const void *);
+ Q_CORE_EXPORT void internalSafetyCheckAdd(const void *, const volatile void *);
+ Q_CORE_EXPORT void internalSafetyCheckRemove(const void *);
template <class T, typename Klass, typename RetVal>
inline void executeDeleter(T *t, RetVal (Klass:: *memberDeleter)())
@@ -247,7 +247,7 @@ namespace QtSharedPointer {
}
static void safetyCheckDeleter(ExternalRefCountData *self)
{
- internalSafetyCheckRemove2(self);
+ internalSafetyCheckRemove(self);
deleter(self);
}
@@ -290,7 +290,7 @@ namespace QtSharedPointer {
}
static void safetyCheckDeleter(ExternalRefCountData *self)
{
- internalSafetyCheckRemove2(self);
+ internalSafetyCheckRemove(self);
deleter(self);
}
@@ -374,7 +374,7 @@ namespace QtSharedPointer {
Basic<T>::internalConstruct(ptr);
if (ptr) d->setQObjectShared(ptr, true);
#ifdef QT_SHAREDPOINTER_TRACK_POINTERS
- if (ptr) internalSafetyCheckAdd2(d, ptr);
+ if (ptr) internalSafetyCheckAdd(d, ptr);
#endif
}
@@ -510,6 +510,13 @@ public:
inline void swap(QSharedPointer &other)
{ QSharedPointer<T>::internalSwap(other); }
+ inline void reset() { clear(); }
+ inline void reset(T *t)
+ { QSharedPointer copy(t); swap(copy); }
+ template <typename Deleter>
+ inline void reset(T *t, Deleter deleter)
+ { QSharedPointer copy(t, deleter); swap(copy); }
+
template <class X>
QSharedPointer<X> staticCast() const
{
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index f1eb398c60..bb66fdbdec 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -41,6 +41,7 @@
#include "qstringlist.h"
#include "qregexp.h"
+#include "qregularexpression.h"
#include "qunicodetables_p.h"
#ifndef QT_NO_TEXTCODEC
#include <qtextcodec.h>
@@ -94,6 +95,8 @@
#define ULLONG_MAX quint64_C(18446744073709551615)
#endif
+#define IS_RAW_DATA(d) ((d)->offset != sizeof(QStringData))
+
QT_BEGIN_NAMESPACE
#ifdef QT_USE_ICU
@@ -793,8 +796,8 @@ const QString::Null QString::null = { };
\sa split()
*/
-const QConstStringData<1> QString::shared_null = { { Q_REFCOUNT_INITIALIZER(-1), 0, 0, false, { 0 } }, { 0 } };
-const QConstStringData<1> QString::shared_empty = { { Q_REFCOUNT_INITIALIZER(-1), 0, 0, false, { 0 } }, { 0 } };
+const QStaticStringData<1> QString::shared_null = { { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, false, sizeof(QStringData) }, { 0 } };
+const QStaticStringData<1> QString::shared_empty = { { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, false, sizeof(QStringData) }, { 0 } };
int QString::grow(int size)
{
@@ -1033,63 +1036,44 @@ int QString::toUcs4_helper(const ushort *uc, int length, uint *out)
Constructs a string initialized with the first \a size characters
of the QChar array \a unicode.
+ If \a unicode is 0, a null string is constructed.
+
+ If \a size is negative, \a unicode is assumed to point to a nul-terminated
+ array and its length is determined dynamically. The terminating
+ nul-character is not considered part of the string.
+
QString makes a deep copy of the string data. The unicode data is copied as
is and the Byte Order Mark is preserved if present.
+
+ \sa fromRawData()
*/
QString::QString(const QChar *unicode, int size)
{
if (!unicode) {
d = const_cast<Data *>(&shared_null.str);
- } else if (size <= 0) {
- d = const_cast<Data *>(&shared_empty.str);
} else {
- d = (Data*) ::malloc(sizeof(Data)+(size+1)*sizeof(QChar));
- Q_CHECK_PTR(d);
- d->ref = 1;
- d->size = size;
- d->alloc = (uint) size;
- d->capacityReserved = false;
- d->offset = 0;
- memcpy(d->data(), unicode, size * sizeof(QChar));
- d->data()[size] = '\0';
+ if (size < 0) {
+ size = 0;
+ while (unicode[size] != 0)
+ ++size;
+ }
+ if (!size) {
+ d = const_cast<Data *>(&shared_empty.str);
+ } else {
+ d = (Data*) ::malloc(sizeof(Data)+(size+1)*sizeof(QChar));
+ Q_CHECK_PTR(d);
+ d->ref.initializeOwned();
+ d->size = size;
+ d->alloc = (uint) size;
+ d->capacityReserved = false;
+ d->offset = sizeof(QStringData);
+ memcpy(d->data(), unicode, size * sizeof(QChar));
+ d->data()[size] = '\0';
+ }
}
}
/*!
- \since 4.7
-
- Constructs a string initialized with the characters of the QChar array
- \a unicode, which must be terminated with a 0.
-
- QString makes a deep copy of the string data. The unicode data is copied as
- is and the Byte Order Mark is preserved if present.
-*/
-QString::QString(const QChar *unicode)
-{
- if (!unicode) {
- d = const_cast<Data *>(&shared_null.str);
- } else {
- int size = 0;
- while (unicode[size] != 0)
- ++size;
- if (!size) {
- d = const_cast<Data *>(&shared_empty.str);
- } else {
- d = (Data*) ::malloc(sizeof(Data)+(size+1)*sizeof(QChar));
- Q_CHECK_PTR(d);
- d->ref = 1;
- d->size = size;
- d->alloc = (uint) size;
- d->capacityReserved = false;
- d->offset = 0;
- memcpy(d->data(), unicode, size * sizeof(QChar));
- d->data()[size] = '\0';
- }
- }
-}
-
-
-/*!
Constructs a string of the given \a size with every character set
to \a ch.
@@ -1102,11 +1086,11 @@ QString::QString(int size, QChar ch)
} else {
d = (Data*) ::malloc(sizeof(Data)+(size+1)*sizeof(QChar));
Q_CHECK_PTR(d);
- d->ref = 1;
+ d->ref.initializeOwned();
d->size = size;
d->alloc = (uint) size;
d->capacityReserved = false;
- d->offset = 0;
+ d->offset = sizeof(QStringData);
d->data()[size] = '\0';
ushort *i = d->data() + size;
ushort *b = d->data();
@@ -1126,11 +1110,11 @@ QString::QString(int size, Qt::Initialization)
{
d = (Data*) ::malloc(sizeof(Data)+(size+1)*sizeof(QChar));
Q_CHECK_PTR(d);
- d->ref = 1;
+ d->ref.initializeOwned();
d->size = size;
d->alloc = (uint) size;
d->capacityReserved = false;
- d->offset = 0;
+ d->offset = sizeof(QStringData);
d->data()[size] = '\0';
}
@@ -1148,11 +1132,11 @@ QString::QString(QChar ch)
{
d = (Data *) ::malloc(sizeof(Data) + 2*sizeof(QChar));
Q_CHECK_PTR(d);
- d->ref = 1;
+ d->ref.initializeOwned();
d->size = 1;
d->alloc = 1;
d->capacityReserved = false;
- d->offset = 0;
+ d->offset = sizeof(QStringData);
d->data()[0] = ch.unicode();
d->data()[1] = '\0';
}
@@ -1250,7 +1234,7 @@ void QString::resize(int size)
if (size < 0)
size = 0;
- if (d->offset && d->ref == 1 && size < d->size) {
+ if (IS_RAW_DATA(d) && !d->ref.isShared() && size < d->size) {
d->size = size;
return;
}
@@ -1261,7 +1245,7 @@ void QString::resize(int size)
QString::free(d);
d = x;
} else {
- if (d->ref != 1 || size > int(d->alloc) ||
+ if (d->ref.isShared() || size > int(d->alloc) ||
(!d->capacityReserved && size < d->size && size < int(d->alloc) >> 1))
realloc(grow(size));
if (int(d->alloc) >= size) {
@@ -1324,14 +1308,14 @@ void QString::resize(int size)
// ### Qt 5: rename reallocData() to avoid confusion. 197625
void QString::realloc(int alloc)
{
- if (d->ref != 1 || d->offset) {
+ if (d->ref.isShared() || IS_RAW_DATA(d)) {
Data *x = static_cast<Data *>(::malloc(sizeof(Data) + (alloc+1) * sizeof(QChar)));
Q_CHECK_PTR(x);
- x->ref = 1;
+ x->ref.initializeOwned();
x->size = qMin(alloc, d->size);
x->alloc = (uint) alloc;
x->capacityReserved = d->capacityReserved;
- x->offset =0;
+ x->offset = sizeof(QStringData);
::memcpy(x->data(), d->data(), x->size * sizeof(QChar));
x->data()[x->size] = 0;
if (!d->ref.deref())
@@ -1342,7 +1326,7 @@ void QString::realloc(int alloc)
Q_CHECK_PTR(p);
d = p;
d->alloc = alloc;
- d->offset = 0;
+ d->offset = sizeof(QStringData);
}
}
@@ -1554,7 +1538,7 @@ QString &QString::append(const QString &str)
if (d == &shared_null.str) {
operator=(str);
} else {
- if (d->ref != 1 || d->size + str.d->size > int(d->alloc))
+ if (d->ref.isShared() || d->size + str.d->size > int(d->alloc))
realloc(grow(d->size + str.d->size));
memcpy(d->data() + d->size, str.d->data(), str.d->size * sizeof(QChar));
d->size += str.d->size;
@@ -1574,7 +1558,7 @@ QString &QString::append(const QLatin1String &str)
const uchar *s = (const uchar *)str.latin1();
if (s) {
int len = str.size();
- if (d->ref != 1 || d->size + len > int(d->alloc))
+ if (d->ref.isShared() || d->size + len > int(d->alloc))
realloc(grow(d->size + len));
ushort *i = d->data() + d->size;
while ((*i++ = *s++))
@@ -1617,7 +1601,7 @@ QString &QString::append(const QLatin1String &str)
*/
QString &QString::append(QChar ch)
{
- if (d->ref != 1 || d->size + 1 > int(d->alloc))
+ if (d->ref.isShared() || d->size + 1 > int(d->alloc))
realloc(grow(d->size + 1));
d->data()[d->size++] = ch.unicode();
d->data()[d->size] = '\0';
@@ -1776,6 +1760,18 @@ QString &QString::remove(QChar ch, Qt::CaseSensitivity cs)
*/
/*!
+ \fn QString &QString::remove(const QRegularExpression &re)
+ \since 5.0
+
+ Removes every occurrence of the regular expression \a re in the
+ string, and returns a reference to the string. For example:
+
+ \snippet doc/src/snippets/qstring/main.cpp 96
+
+ \sa indexOf(), lastIndexOf(), replace()
+*/
+
+/*!
\fn QString &QString::replace(int position, int n, const QString &after)
Replaces \a n characters beginning at index \a position with
@@ -2958,6 +2954,138 @@ QString& QString::replace(const QRegExp &rx, const QString &after)
}
#endif
+#ifndef QT_NO_REGEXP
+#ifndef QT_BOOTSTRAPPED
+/*!
+ \overload replace()
+ \since 5.0
+
+ Replaces every occurrence of the regular expression \a re in the
+ string with \a after. Returns a reference to the string. For
+ example:
+
+ \snippet doc/src/snippets/qstring/main.cpp 87
+
+ For regular expressions containing capturing groups,
+ occurrences of \bold{\\1}, \bold{\\2}, ..., in \a after are replaced
+ with the string captured by the corresponding capturing group.
+
+ \snippet doc/src/snippets/qstring/main.cpp 88
+
+ \sa indexOf(), lastIndexOf(), remove(), QRegularExpression, QRegularExpressionMatch
+*/
+QString &QString::replace(const QRegularExpression &re, const QString &after)
+{
+ if (!re.isValid()) {
+ qWarning("QString::replace: invalid QRegularExpresssion object");
+ return *this;
+ }
+
+ const QString copy(*this);
+ QRegularExpressionMatchIterator iterator = re.globalMatch(copy);
+ if (!iterator.hasNext()) // no matches at all
+ return *this;
+
+ realloc();
+
+ int numCaptures = re.captureCount();
+
+ // 1. build the backreferences vector, holding where the backreferences
+ // are in the replacement string
+ QVector<QStringCapture> backReferences;
+ const int al = after.length();
+ const QChar *ac = after.unicode();
+
+ for (int i = 0; i < al - 1; i++) {
+ if (ac[i] == QLatin1Char('\\')) {
+ int no = ac[i + 1].digitValue();
+ if (no > 0 && no <= numCaptures) {
+ QStringCapture backReference;
+ backReference.pos = i;
+ backReference.len = 2;
+
+ if (i < al - 2) {
+ int secondDigit = ac[i + 2].digitValue();
+ if (secondDigit != -1 && ((no * 10) + secondDigit) <= numCaptures) {
+ no = (no * 10) + secondDigit;
+ ++backReference.len;
+ }
+ }
+
+ backReference.no = no;
+ backReferences.append(backReference);
+ }
+ }
+ }
+
+ // 2. iterate on the matches. For every match, copy in chunks
+ // - the part before the match
+ // - the after string, with the proper replacements for the backreferences
+
+ int newLength = 0; // length of the new string, with all the replacements
+ int lastEnd = 0;
+ QVector<QStringRef> chunks;
+ while (iterator.hasNext()) {
+ QRegularExpressionMatch match = iterator.next();
+ int len;
+ // add the part before the match
+ len = match.capturedStart() - lastEnd;
+ if (len > 0) {
+ chunks << copy.midRef(lastEnd, len);
+ newLength += len;
+ }
+
+ lastEnd = 0;
+ // add the after string, with replacements for the backreferences
+ foreach (const QStringCapture &backReference, backReferences) {
+ // part of "after" before the backreference
+ len = backReference.pos - lastEnd;
+ if (len > 0) {
+ chunks << after.midRef(lastEnd, len);
+ newLength += len;
+ }
+
+ // backreference itself
+ len = match.capturedLength(backReference.no);
+ if (len > 0) {
+ chunks << copy.midRef(match.capturedStart(backReference.no), len);
+ newLength += len;
+ }
+
+ lastEnd = backReference.pos + backReference.len;
+ }
+
+ // add the last part of the after string
+ len = after.length() - lastEnd;
+ if (len > 0) {
+ chunks << after.midRef(lastEnd, len);
+ newLength += len;
+ }
+
+ lastEnd = match.capturedEnd();
+ }
+
+ // 3. trailing string after the last match
+ if (copy.length() > lastEnd) {
+ chunks << copy.midRef(lastEnd);
+ newLength += copy.length() - lastEnd;
+ }
+
+ // 4. assemble the chunks together
+ resize(newLength);
+ int i = 0;
+ QChar *uc = data();
+ foreach (const QStringRef &chunk, chunks) {
+ int len = chunk.length();
+ memcpy(uc + i, chunk.unicode(), len * sizeof(QChar));
+ i += len;
+ }
+
+ return *this;
+}
+#endif // QT_BOOTSTRAPPED
+#endif // QT_NO_REGEXP
+
/*!
Returns the number of (potentially overlapping) occurrences of
the string \a str in this string.
@@ -3157,6 +3285,118 @@ int QString::count(const QRegExp& rx) const
}
#endif // QT_NO_REGEXP
+#ifndef QT_NO_REGEXP
+#ifndef QT_BOOTSTRAPPED
+/*!
+ \overload indexOf()
+ \since 5.0
+
+ Returns the index position of the first match of the regular
+ expression \a re in the string, searching forward from index
+ position \a from. Returns -1 if \a re didn't match anywhere.
+
+ Example:
+
+ \snippet doc/src/snippets/qstring/main.cpp 93
+*/
+int QString::indexOf(const QRegularExpression& re, int from) const
+{
+ if (!re.isValid()) {
+ qWarning("QString::indexOf: invalid QRegularExpresssion object");
+ return -1;
+ }
+
+ QRegularExpressionMatch match = re.match(*this, from);
+ if (match.hasMatch())
+ return match.capturedStart();
+
+ return -1;
+}
+
+/*!
+ \overload lastIndexOf()
+ \since 5.0
+
+ Returns the index position of the last match of the regular
+ expression \a re in the string, which starts before the index
+ position \a from. Returns -1 if \a re didn't match anywhere.
+
+ Example:
+
+ \snippet doc/src/snippets/qstring/main.cpp 94
+*/
+int QString::lastIndexOf(const QRegularExpression &re, int from) const
+{
+ if (!re.isValid()) {
+ qWarning("QString::lastIndexOf: invalid QRegularExpresssion object");
+ return -1;
+ }
+
+ int endpos = (from < 0) ? (size() + from + 1) : (from + 1);
+
+ QRegularExpressionMatchIterator iterator = re.globalMatch(*this);
+ int lastIndex = -1;
+ while (iterator.hasNext()) {
+ QRegularExpressionMatch match = iterator.next();
+ int start = match.capturedStart();
+ if (start < endpos)
+ lastIndex = start;
+ else
+ break;
+ }
+
+ return lastIndex;
+}
+
+/*! \overload contains()
+ \since 5.0
+
+ Returns true if the regular expression \a re matches somewhere in
+ this string; otherwise returns false.
+*/
+bool QString::contains(const QRegularExpression &re) const
+{
+ if (!re.isValid()) {
+ qWarning("QString::contains: invalid QRegularExpresssion object");
+ return false;
+ }
+ QRegularExpressionMatch match = re.match(*this);
+ return match.hasMatch();
+}
+
+/*!
+ \overload count()
+ \since 5.0
+
+ Returns the number of times the regular expression \a re matches
+ in the string.
+
+ This function counts overlapping matches, so in the example
+ below, there are four instances of "ana" or "ama":
+
+ \snippet doc/src/snippets/qstring/main.cpp 95
+*/
+int QString::count(const QRegularExpression &re) const
+{
+ if (!re.isValid()) {
+ qWarning("QString::count: invalid QRegularExpresssion object");
+ return 0;
+ }
+ int count = 0;
+ int index = -1;
+ int len = length();
+ while (index < len - 1) {
+ QRegularExpressionMatch match = re.match(*this, index + 1);
+ if (!match.hasMatch())
+ break;
+ index = match.capturedStart();
+ count++;
+ }
+ return count;
+}
+#endif // QT_BOOTSTRAPPED
+#endif // QT_NO_REGEXP
+
/*! \fn int QString::count() const
\overload count()
@@ -3284,6 +3524,49 @@ public:
QString string;
};
+static QString extractSections(const QList<qt_section_chunk> &sections,
+ int start,
+ int end,
+ QString::SectionFlags flags)
+{
+ if (start < 0)
+ start += sections.count();
+ if (end < 0)
+ end += sections.count();
+
+ QString ret;
+ int x = 0;
+ int first_i = start, last_i = end;
+ for (int i = 0; x <= end && i < sections.size(); ++i) {
+ const qt_section_chunk &section = sections.at(i);
+ const bool empty = (section.length == section.string.length());
+ if (x >= start) {
+ if (x == start)
+ first_i = i;
+ if (x == end)
+ last_i = i;
+ if (x != start)
+ ret += section.string;
+ else
+ ret += section.string.mid(section.length);
+ }
+ if (!empty || !(flags & QString::SectionSkipEmpty))
+ x++;
+ }
+
+ if ((flags & QString::SectionIncludeLeadingSep) && first_i < sections.size()) {
+ const qt_section_chunk &section = sections.at(first_i);
+ ret.prepend(section.string.left(section.length));
+ }
+
+ if ((flags & QString::SectionIncludeTrailingSep) && last_i+1 <= sections.size()-1) {
+ const qt_section_chunk &section = sections.at(last_i+1);
+ ret += section.string.left(section.length);
+ }
+
+ return ret;
+}
+
/*!
\overload section()
@@ -3317,41 +3600,57 @@ QString QString::section(const QRegExp &reg, int start, int end, SectionFlags fl
}
sections.append(qt_section_chunk(last_len, QString(uc + last_m, n - last_m)));
- if(start < 0)
- start += sections.count();
- if(end < 0)
- end += sections.count();
+ return extractSections(sections, start, end, flags);
+}
+#endif
- QString ret;
- int x = 0;
- int first_i = start, last_i = end;
- for (int i = 0; x <= end && i < sections.size(); ++i) {
- const qt_section_chunk &section = sections.at(i);
- const bool empty = (section.length == section.string.length());
- if (x >= start) {
- if(x == start)
- first_i = i;
- if(x == end)
- last_i = i;
- if(x != start)
- ret += section.string;
- else
- ret += section.string.mid(section.length);
- }
- if (!empty || !(flags & SectionSkipEmpty))
- x++;
- }
- if((flags & SectionIncludeLeadingSep) && first_i < sections.size()) {
- const qt_section_chunk &section = sections.at(first_i);
- ret.prepend(section.string.left(section.length));
+#ifndef QT_NO_REGEXP
+#ifndef QT_BOOTSTRAPPED
+/*!
+ \overload section()
+ \since 5.0
+
+ This string is treated as a sequence of fields separated by the
+ regular expression, \a re.
+
+ \snippet doc/src/snippets/qstring/main.cpp 89
+
+ \warning Using this QRegularExpression version is much more expensive than
+ the overloaded string and character versions.
+
+ \sa split() simplified()
+*/
+QString QString::section(const QRegularExpression &re, int start, int end, SectionFlags flags) const
+{
+ if (!re.isValid()) {
+ qWarning("QString::section: invalid QRegularExpression object");
+ return QString();
}
- if((flags & SectionIncludeTrailingSep) && last_i+1 <= sections.size()-1) {
- const qt_section_chunk &section = sections.at(last_i+1);
- ret += section.string.left(section.length);
+
+ const QChar *uc = unicode();
+ if (!uc)
+ return QString();
+
+ QRegularExpression sep(re);
+ if (flags & SectionCaseInsensitiveSeps)
+ sep.setPatternOptions(sep.patternOptions() | QRegularExpression::CaseInsensitiveOption);
+
+ QList<qt_section_chunk> sections;
+ int n = length(), m = 0, last_m = 0, last_len = 0;
+ QRegularExpressionMatchIterator iterator = sep.globalMatch(*this);
+ while (iterator.hasNext()) {
+ QRegularExpressionMatch match = iterator.next();
+ m = match.capturedStart();
+ sections.append(qt_section_chunk(last_len, QString(uc + last_m, m - last_m)));
+ last_m = m;
+ last_len = match.capturedLength();
}
- return ret;
+ sections.append(qt_section_chunk(last_len, QString(uc + last_m, n - last_m)));
+
+ return extractSections(sections, start, end, flags);
}
-#endif
+#endif // QT_BOOTSTRAPPED
+#endif // QT_NO_REGEXP
/*!
Returns a substring that contains the \a n leftmost characters
@@ -3408,15 +3707,17 @@ QString QString::right(int n) const
QString QString::mid(int position, int n) const
{
- if (d == &shared_null.str || position > d->size)
+ if (position > d->size)
return QString();
- if (n < 0)
- n = d->size - position;
if (position < 0) {
+ if (n < 0 || n + position >= d->size)
+ return *this;
+ if (n + position <= 0)
+ return QString();
+
n += position;
position = 0;
- }
- if (n + position > d->size)
+ } else if (n < 0 || n > d->size - position)
n = d->size - position;
if (position == 0 && n == d->size)
return *this;
@@ -3771,11 +4072,11 @@ QString::Data *QString::fromLatin1_helper(const char *str, int size)
size = qstrlen(str);
d = static_cast<Data *>(::malloc(sizeof(Data) + (size+1) * sizeof(QChar)));
Q_CHECK_PTR(d);
- d->ref = 1;
+ d->ref.initializeOwned();
d->size = size;
d->alloc = (uint) size;
d->capacityReserved = false;
- d->offset = 0;
+ d->offset = sizeof(QStringData);
d->data()[size] = '\0';
ushort *dst = d->data();
/* SIMD:
@@ -4783,7 +5084,7 @@ int QString::localeAwareCompare_helper(const QChar *data1, int length1,
const ushort *QString::utf16() const
{
- if (d->offset)
+ if (IS_RAW_DATA(d))
const_cast<QString*>(this)->realloc(); // ensure '\\0'-termination for ::fromRawData strings
return d->data();
}
@@ -6110,6 +6411,62 @@ QStringList QString::split(const QRegExp &rx, SplitBehavior behavior) const
}
#endif
+#ifndef QT_NO_REGEXP
+#ifndef QT_BOOTSTRAPPED
+/*!
+ \overload
+ \since 5.0
+
+ Splits the string into substrings wherever the regular expression
+ \a re matches, and returns the list of those strings. If \a re
+ does not match anywhere in the string, split() returns a
+ single-element list containing this string.
+
+ Here's an example where we extract the words in a sentence
+ using one or more whitespace characters as the separator:
+
+ \snippet doc/src/snippets/qstring/main.cpp 90
+
+ Here's a similar example, but this time we use any sequence of
+ non-word characters as the separator:
+
+ \snippet doc/src/snippets/qstring/main.cpp 91
+
+ Here's a third example where we use a zero-length assertion,
+ \bold{\\b} (word boundary), to split the string into an
+ alternating sequence of non-word and word tokens:
+
+ \snippet doc/src/snippets/qstring/main.cpp 92
+
+ \sa QStringList::join(), section()
+*/
+QStringList QString::split(const QRegularExpression &re, SplitBehavior behavior) const
+{
+ QStringList list;
+ if (!re.isValid()) {
+ qWarning("QString::split: invalid QRegularExpression object");
+ return list;
+ }
+
+ int start = 0;
+ int end = 0;
+ QRegularExpressionMatchIterator iterator = re.globalMatch(*this);
+ while (iterator.hasNext()) {
+ QRegularExpressionMatch match = iterator.next();
+ end = match.capturedStart();
+ if (start != end || behavior == KeepEmptyParts)
+ list.append(mid(start, end - start));
+ start = match.capturedEnd();
+ }
+
+ if (start != size() || behavior == KeepEmptyParts)
+ list.append(mid(start));
+
+ return list;
+}
+#endif // QT_BOOTSTRAPPED
+#endif // QT_NO_REGEXP
+
/*!
\enum QString::NormalizationForm
@@ -7085,11 +7442,11 @@ QString QString::fromRawData(const QChar *unicode, int size)
} else {
x = static_cast<Data *>(::malloc(sizeof(Data) + sizeof(ushort)));
Q_CHECK_PTR(x);
- x->ref = 1;
+ x->ref.initializeOwned();
x->size = size;
x->alloc = 0;
x->capacityReserved = false;
- x->offset = (const ushort *)unicode - (x->d + sizeof(qptrdiff)/sizeof(ushort));
+ x->offset = reinterpret_cast<const char *>(unicode) - reinterpret_cast<char *>(x);
}
return QString(x, 0);
}
@@ -7110,14 +7467,14 @@ QString QString::fromRawData(const QChar *unicode, int size)
*/
QString &QString::setRawData(const QChar *unicode, int size)
{
- if (d->ref != 1 || d->alloc) {
+ if (d->ref.isShared() || d->alloc) {
*this = fromRawData(unicode, size);
} else {
if (unicode) {
d->size = size;
- d->offset = (const ushort *)unicode - (d->d + sizeof(qptrdiff)/sizeof(ushort));
+ d->offset = reinterpret_cast<const char *>(unicode) - reinterpret_cast<char *>(d);
} else {
- d->offset = 0;
+ d->offset = sizeof(QStringData);
d->size = 0;
}
}
@@ -7197,6 +7554,17 @@ QString &QString::setRawData(const QChar *unicode, int size)
\sa latin1()
*/
+/*! \fn QLatin1String::QLatin1String(const QByteArray &str)
+
+ Constructs a QLatin1String object that stores \a str.
+
+ The string data is \e not copied. The caller must be able to
+ guarantee that \a str will not be deleted or modified as long as
+ the QLatin1String object exists.
+
+ \sa latin1()
+*/
+
/*! \fn const char *QLatin1String::latin1() const
Returns the Latin-1 string stored in this object.
@@ -8077,15 +8445,17 @@ QStringRef QString::rightRef(int n) const
QStringRef QString::midRef(int position, int n) const
{
- if (d == &shared_null.str || position > d->size)
+ if (position > d->size)
return QStringRef();
- if (n < 0)
- n = d->size - position;
if (position < 0) {
+ if (n < 0 || n + position >= d->size)
+ return QStringRef(this, 0, d->size);
+ if (n + position <= 0)
+ return QStringRef();
+
n += position;
position = 0;
- }
- if (n + position > d->size)
+ } else if (n < 0 || n > d->size - position)
n = d->size - position;
return QStringRef(this, position, n);
}
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
index 437e98b662..042d80bea5 100644
--- a/src/corelib/tools/qstring.h
+++ b/src/corelib/tools/qstring.h
@@ -64,6 +64,7 @@ QT_BEGIN_NAMESPACE
class QCharRef;
class QRegExp;
+class QRegularExpression;
class QStringList;
class QTextCodec;
class QLatin1String;
@@ -75,25 +76,24 @@ struct QStringData {
int size;
uint alloc : 31;
uint capacityReserved : 1;
- union {
- qptrdiff offset; // will always work as we add/subtract from a ushort ptr
- ushort d[sizeof(qptrdiff)/sizeof(ushort)];
- };
- inline ushort *data() { return d + sizeof(qptrdiff)/sizeof(ushort) + offset; }
- inline const ushort *data() const { return d + sizeof(qptrdiff)/sizeof(ushort) + offset; }
+
+ qptrdiff offset;
+
+ inline ushort *data() { return reinterpret_cast<ushort *>(reinterpret_cast<char *>(this) + offset); }
+ inline const ushort *data() const { return reinterpret_cast<const ushort *>(reinterpret_cast<const char *>(this) + offset); }
};
-template<int N> struct QConstStringData;
-template<int N> struct QConstStringDataPtr
+template<int N> struct QStaticStringData;
+template<int N> struct QStaticStringDataPtr
{
- const QConstStringData<N> *ptr;
+ const QStaticStringData<N> *ptr;
};
#if defined(Q_COMPILER_UNICODE_STRINGS)
-template<int N> struct QConstStringData
+template<int N> struct QStaticStringData
{
- const QStringData str;
- const char16_t data[N + 1];
+ QStringData str;
+ char16_t data[N + 1];
};
#define QT_UNICODE_LITERAL_II(str) u"" str
@@ -102,10 +102,10 @@ template<int N> struct QConstStringData
|| (defined(__SIZEOF_WCHAR_T__) && __SIZEOF_WCHAR_T__ == 2) \
|| (!defined(__SIZEOF_WCHAR_T__) && defined(WCHAR_MAX) && (WCHAR_MAX - 0 < 65536))
// wchar_t is 2 bytes
-template<int N> struct QConstStringData
+template<int N> struct QStaticStringData
{
- const QStringData str;
- const wchar_t data[N + 1];
+ QStringData str;
+ wchar_t data[N + 1];
};
#if defined(Q_CC_MSVC)
@@ -115,21 +115,21 @@ template<int N> struct QConstStringData
#endif
#else
-template<int N> struct QConstStringData
+template<int N> struct QStaticStringData
{
- const QStringData str;
- const ushort data[N + 1];
+ QStringData str;
+ ushort data[N + 1];
};
#endif
#if defined(QT_UNICODE_LITERAL_II)
# define QT_UNICODE_LITERAL(str) QT_UNICODE_LITERAL_II(str)
# if defined(Q_COMPILER_LAMBDA)
-# define QStringLiteral(str) ([]() -> QConstStringDataPtr<sizeof(QT_UNICODE_LITERAL(str))/2 - 1> { \
+# define QStringLiteral(str) ([]() -> QStaticStringDataPtr<sizeof(QT_UNICODE_LITERAL(str))/2 - 1> { \
enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \
- static const QConstStringData<Size> qstring_literal = \
- { { Q_REFCOUNT_INITIALIZER(-1), Size, 0, 0, { 0 } }, QT_UNICODE_LITERAL(str) }; \
- QConstStringDataPtr<Size> holder = { &qstring_literal }; \
+ static const QStaticStringData<Size> qstring_literal = \
+ { { Q_REFCOUNT_INITIALIZE_STATIC, Size, 0, 0, sizeof(QStringData) }, QT_UNICODE_LITERAL(str) }; \
+ QStaticStringDataPtr<Size> holder = { &qstring_literal }; \
return holder; }())
# elif defined(Q_CC_GNU)
@@ -140,9 +140,9 @@ template<int N> struct QConstStringData
# define QStringLiteral(str) \
__extension__ ({ \
enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \
- static const QConstStringData<Size> qstring_literal = \
- { { Q_REFCOUNT_INITIALIZER(-1), Size, 0, 0, { 0 } }, QT_UNICODE_LITERAL(str) }; \
- QConstStringDataPtr<Size> holder = { &qstring_literal }; \
+ static const QStaticStringData<Size> qstring_literal = \
+ { { Q_REFCOUNT_INITIALIZE_STATIC, Size, 0, 0, sizeof(QStringData) }, QT_UNICODE_LITERAL(str) }; \
+ QStaticStringDataPtr<Size> holder = { &qstring_literal }; \
holder; })
# endif
#endif
@@ -160,8 +160,7 @@ public:
typedef QStringData Data;
inline QString();
- QString(const QChar *unicode, int size); // Qt5: don't cap size < 0
- explicit QString(const QChar *unicode); // Qt5: merge with the above
+ explicit QString(const QChar *unicode, int size = -1);
QString(QChar c);
QString(int size, QChar c);
inline QString(const QLatin1String &latin1);
@@ -285,6 +284,13 @@ public:
inline bool contains(QRegExp &rx) const { return indexOf(rx) != -1; }
#endif
+#ifndef QT_NO_REGEXP
+ int indexOf(const QRegularExpression &re, int from = 0) const;
+ int lastIndexOf(const QRegularExpression &re, int from = -1) const;
+ bool contains(const QRegularExpression &re) const;
+ int count(const QRegularExpression &re) const;
+#endif
+
enum SectionFlag {
SectionDefault = 0x00,
SectionSkipEmpty = 0x01,
@@ -299,7 +305,9 @@ public:
#ifndef QT_NO_REGEXP
QString section(const QRegExp &reg, int start, int end = -1, SectionFlags flags = SectionDefault) const;
#endif
-
+#ifndef QT_NO_REGEXP
+ QString section(const QRegularExpression &re, int start, int end = -1, SectionFlags flags = SectionDefault) const;
+#endif
QString left(int n) const Q_REQUIRED_RESULT;
QString right(int n) const Q_REQUIRED_RESULT;
QString mid(int position, int n = -1) const Q_REQUIRED_RESULT;
@@ -340,7 +348,7 @@ public:
inline QString &prepend(const QLatin1String &s) { return insert(0, s); }
inline QString &operator+=(QChar c) {
- if (d->ref != 1 || d->size + 1 > int(d->alloc))
+ if (d->ref.isShared() || d->size + 1 > int(d->alloc))
realloc(grow(d->size + 1));
d->data()[d->size++] = c.unicode();
d->data()[d->size] = '\0';
@@ -372,6 +380,11 @@ public:
inline QString &remove(const QRegExp &rx)
{ return replace(rx, QString()); }
#endif
+#ifndef QT_NO_REGEXP
+ QString &replace(const QRegularExpression &re, const QString &after);
+ inline QString &remove(const QRegularExpression &re)
+ { return replace(re, QString()); }
+#endif
enum SplitBehavior { KeepEmptyParts, SkipEmptyParts };
@@ -382,7 +395,9 @@ public:
#ifndef QT_NO_REGEXP
QStringList split(const QRegExp &sep, SplitBehavior behavior = KeepEmptyParts) const Q_REQUIRED_RESULT;
#endif
-
+#ifndef QT_NO_REGEXP
+ QStringList split(const QRegularExpression &sep, SplitBehavior behavior = KeepEmptyParts) const Q_REQUIRED_RESULT;
+#endif
enum NormalizationForm {
NormalizationForm_D,
NormalizationForm_C,
@@ -594,9 +609,9 @@ public:
QString(int size, Qt::Initialization);
template <int n>
- inline QString(const QConstStringData<n> &dd) : d(const_cast<QStringData *>(&dd.str)) {}
+ inline QString(const QStaticStringData<n> &dd) : d(const_cast<QStringData *>(&dd.str)) {}
template <int N>
- Q_DECL_CONSTEXPR inline QString(QConstStringDataPtr<N> dd) : d(const_cast<QStringData *>(&dd.ptr->str)) {}
+ Q_DECL_CONSTEXPR inline QString(QStaticStringDataPtr<N> dd) : d(const_cast<QStringData *>(&dd.ptr->str)) {}
private:
#if defined(QT_NO_CAST_FROM_ASCII) && !defined(Q_NO_DECLARED_NOT_DEFINED)
@@ -608,8 +623,8 @@ private:
QString &operator=(const QByteArray &a);
#endif
- static const QConstStringData<1> shared_null;
- static const QConstStringData<1> shared_empty;
+ static const QStaticStringData<1> shared_null;
+ static const QStaticStringData<1> shared_empty;
Data *d;
inline QString(Data *dd, int /*dummy*/) : d(dd) {}
@@ -651,6 +666,7 @@ class QLatin1String
public:
Q_DECL_CONSTEXPR inline explicit QLatin1String(const char *s) : m_size(s ? int(strlen(s)) : 0), m_data(s) {}
Q_DECL_CONSTEXPR inline explicit QLatin1String(const char *s, int sz) : m_size(sz), m_data(s) {}
+ inline explicit QLatin1String(const QByteArray &s) : m_size(int(strlen(s.constData()))), m_data(s.constData()) {}
inline const char *latin1() const { return m_data; }
inline int size() const { return m_size; }
@@ -711,9 +727,9 @@ inline QChar *QString::data()
inline const QChar *QString::constData() const
{ return reinterpret_cast<const QChar*>(d->data()); }
inline void QString::detach()
-{ if (d->ref != 1 || d->offset) realloc(); }
+{ if (d->ref.isShared() || (d->offset != sizeof(QStringData))) realloc(); }
inline bool QString::isDetached() const
-{ return d->ref == 1; }
+{ return !d->ref.isShared(); }
inline QString &QString::operator=(const QLatin1String &s)
{
*this = fromLatin1(s.latin1(), s.size());
@@ -876,7 +892,7 @@ inline QString::~QString() { if (!d->ref.deref()) free(d); }
inline void QString::reserve(int asize)
{
- if (d->ref != 1 || asize > int(d->alloc))
+ if (d->ref.isShared() || asize > int(d->alloc))
realloc(asize);
if (!d->capacityReserved) {
@@ -887,11 +903,12 @@ inline void QString::reserve(int asize)
inline void QString::squeeze()
{
- if (d->ref > 1 || d->size < int(d->alloc))
+ if (d->ref.isShared() || d->size < int(d->alloc))
realloc();
if (d->capacityReserved) {
- // cannot set unconditionally, since d could be the shared_null/shared_empty (which is const)
+ // cannot set unconditionally, since d could be shared_null or
+ // otherwise static.
d->capacityReserved = false;
}
}
diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h
index e34425be7e..0b85e590cd 100644
--- a/src/corelib/tools/qstringbuilder.h
+++ b/src/corelib/tools/qstringbuilder.h
@@ -42,6 +42,13 @@
#ifndef QSTRINGBUILDER_H
#define QSTRINGBUILDER_H
+#if 0
+// syncqt can not handle the templates in this file, and it doesn't need to
+// process them anyway because they are internal.
+#pragma qt_class(QStringBuilder)
+#pragma qt_sync_stop_processing
+#endif
+
#include <QtCore/qstring.h>
#include <QtCore/qbytearray.h>
@@ -64,8 +71,37 @@ protected:
template <typename T> struct QConcatenable {};
+namespace QtStringBuilder {
+ template <typename A, typename B> struct ConvertToTypeHelper
+ { typedef A ConvertTo; };
+ template <typename T> struct ConvertToTypeHelper<T, QString>
+ { typedef QString ConvertTo; };
+}
+
+template<typename Builder, typename T>
+struct QStringBuilderCommon
+{
+ T toUpper() const { return resolved().toUpper(); }
+ T toLower() const { return resolved().toLower(); }
+
+protected:
+ const T resolved() const { return *static_cast<const Builder*>(this); }
+};
+
+template<typename Builder, typename T>
+struct QStringBuilderBase : public QStringBuilderCommon<Builder, T>
+{
+};
+
+template<typename Builder>
+struct QStringBuilderBase<Builder, QString> : public QStringBuilderCommon<Builder, QString>
+{
+ QByteArray toLatin1() const { return this->resolved().toLatin1(); }
+ QByteArray toLocal8Bit() const { return this->resolved().toLocal8Bit(); }
+};
+
template <typename A, typename B>
-class QStringBuilder
+class QStringBuilder : public QStringBuilderBase<QStringBuilder<A, B>, typename QtStringBuilder::ConvertToTypeHelper<typename QConcatenable<A>::ConvertTo, typename QConcatenable<B>::ConvertTo>::ConvertTo>
{
public:
QStringBuilder(const A &a_, const B &b_) : a(a_), b(b_) {}
@@ -94,7 +130,6 @@ private:
public:
operator ConvertTo() const { return convertTo<ConvertTo>(); }
- QByteArray toLatin1() const { return convertTo<QString>().toLatin1(); }
int size() const { return Concatenable::size(*this); }
const A &a;
@@ -102,21 +137,20 @@ public:
};
template <>
-class QStringBuilder <QString, QString>
+class QStringBuilder <QString, QString> : public QStringBuilderBase<QStringBuilder<QString, QString>, QString>
{
public:
QStringBuilder(const QString &a_, const QString &b_) : a(a_), b(b_) {}
operator QString() const
{ QString r(a); r += b; return r; }
- QByteArray toLatin1() const { return QString(*this).toLatin1(); }
const QString &a;
const QString &b;
};
template <>
-class QStringBuilder <QByteArray, QByteArray>
+class QStringBuilder <QByteArray, QByteArray> : public QStringBuilderBase<QStringBuilder<QByteArray, QByteArray>, QByteArray>
{
public:
QStringBuilder(const QByteArray &a_, const QByteArray &b_) : a(a_), b(b_) {}
@@ -219,9 +253,9 @@ template <> struct QConcatenable<QString> : private QAbstractConcatenable
}
};
-template <int N> struct QConcatenable<QConstStringDataPtr<N> > : private QAbstractConcatenable
+template <int N> struct QConcatenable<QStaticStringDataPtr<N> > : private QAbstractConcatenable
{
- typedef QConstStringDataPtr<N> type;
+ typedef QStaticStringDataPtr<N> type;
typedef QString ConvertTo;
enum { ExactSize = true };
static int size(const type &) { return N; }
@@ -324,9 +358,9 @@ template <> struct QConcatenable<QByteArray> : private QAbstractConcatenable
}
};
-template <int N> struct QConcatenable<QConstByteArrayDataPtr<N> > : private QAbstractConcatenable
+template <int N> struct QConcatenable<QStaticByteArrayDataPtr<N> > : private QAbstractConcatenable
{
- typedef QConstByteArrayDataPtr<N> type;
+ typedef QStaticByteArrayDataPtr<N> type;
typedef QByteArray ConvertTo;
enum { ExactSize = false };
static int size(const type &) { return N; }
@@ -338,19 +372,11 @@ template <int N> struct QConcatenable<QConstByteArrayDataPtr<N> > : private QAbs
#endif
static inline void appendTo(const type &ba, char *&out)
{
- const char *a = ba.ptr->data;
- while (*a)
- *out++ = *a++;
+ ::memcpy(out, ba.ptr->data, N);
+ out += N;
}
};
-namespace QtStringBuilder {
- template <typename A, typename B> struct ConvertToTypeHelper
- { typedef A ConvertTo; };
- template <typename T> struct ConvertToTypeHelper<T, QString>
- { typedef QString ConvertTo; };
-}
-
template <typename A, typename B>
struct QConcatenable< QStringBuilder<A, B> >
{
diff --git a/src/corelib/tools/qstringlist.cpp b/src/corelib/tools/qstringlist.cpp
index b4ec0c6498..50e155db81 100644
--- a/src/corelib/tools/qstringlist.cpp
+++ b/src/corelib/tools/qstringlist.cpp
@@ -41,6 +41,7 @@
#include <qstringlist.h>
#include <qset.h>
+#include <qregularexpression.h>
QT_BEGIN_NAMESPACE
@@ -304,6 +305,28 @@ QStringList QtPrivate::QStringList_filter(const QStringList *that, const QRegExp
}
#endif
+#ifndef QT_BOOTSTRAPPED
+#ifndef QT_NO_REGEXP
+/*!
+ \fn QStringList QStringList::filter(const QRegularExpression &re) const
+ \overload
+ \since 5.0
+
+ Returns a list of all the strings that match the regular
+ expression \a re.
+*/
+QStringList QtPrivate::QStringList_filter(const QStringList *that, const QRegularExpression &re)
+{
+ QStringList res;
+ for (int i = 0; i < that->size(); ++i) {
+ if (that->at(i).contains(re))
+ res << that->at(i);
+ }
+ return res;
+}
+#endif // QT_NO_REGEXP
+#endif // QT_BOOTSTRAPPED
+
/*!
\fn QStringList &QStringList::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs)
@@ -357,6 +380,39 @@ void QtPrivate::QStringList_replaceInStrings(QStringList *that, const QRegExp &r
}
#endif
+#ifndef QT_BOOTSTRAPPED
+#ifndef QT_NO_REGEXP
+/*!
+ \fn QStringList &QStringList::replaceInStrings(const QRegularExpression &re, const QString &after)
+ \overload
+ \since 5.0
+
+ Replaces every occurrence of the regular expression \a re, in each of the
+ string lists's strings, with \a after. Returns a reference to the string
+ list.
+
+ For example:
+
+ \snippet doc/src/snippets/qstringlist/main.cpp 5
+ \snippet doc/src/snippets/qstringlist/main.cpp 16
+
+ For regular expressions that contain capturing groups,
+ occurrences of \b{\\1}, \b{\\2}, ..., in \a after are
+ replaced with the string captured by the corresponding capturing group.
+
+ For example:
+
+ \snippet doc/src/snippets/qstringlist/main.cpp 5
+ \snippet doc/src/snippets/qstringlist/main.cpp 17
+*/
+void QtPrivate::QStringList_replaceInStrings(QStringList *that, const QRegularExpression &re, const QString &after)
+{
+ for (int i = 0; i < that->size(); ++i)
+ (*that)[i].replace(re, after);
+}
+#endif // QT_NO_REGEXP
+#endif // QT_BOOTSTRAPPED
+
/*!
\fn QString QStringList::join(const QString &separator) const
@@ -542,6 +598,67 @@ int QtPrivate::QStringList_lastIndexOf(const QStringList *that, QRegExp &rx, int
}
#endif
+#ifndef QT_BOOTSTRAPPED
+#ifndef QT_NO_REGEXP
+/*!
+ \fn int QStringList::indexOf(const QRegularExpression &re, int from) const
+ \overload
+ \since 5.0
+
+ Returns the index position of the first match of \a re in
+ the list, searching forward from index position \a from. Returns
+ -1 if no item matched.
+
+ \sa lastIndexOf()
+*/
+int QtPrivate::QStringList_indexOf(const QStringList *that, const QRegularExpression &re, int from)
+{
+ if (from < 0)
+ from = qMax(from + that->size(), 0);
+
+ QString exactPattern = QLatin1String("\\A(?:") + re.pattern() + QLatin1String(")\\z");
+ QRegularExpression exactRe(exactPattern, re.patternOptions());
+
+ for (int i = from; i < that->size(); ++i) {
+ QRegularExpressionMatch m = exactRe.match(that->at(i));
+ if (m.hasMatch())
+ return i;
+ }
+ return -1;
+}
+
+/*!
+ \fn int QStringList::lastIndexOf(const QRegularExpression &re, int from) const
+ \overload
+ \since 5.0
+
+ Returns the index position of the last exact match of \a re in
+ the list, searching backward from index position \a from. If \a
+ from is -1 (the default), the search starts at the last item.
+ Returns -1 if no item matched.
+
+ \sa indexOf()
+*/
+int QtPrivate::QStringList_lastIndexOf(const QStringList *that, const QRegularExpression &re, int from)
+{
+ if (from < 0)
+ from += that->size();
+ else if (from >= that->size())
+ from = that->size() - 1;
+
+ QString exactPattern = QLatin1String("\\A(?:") + re.pattern() + QLatin1String(")\\z");
+ QRegularExpression exactRe(exactPattern, re.patternOptions());
+
+ for (int i = from; i >= 0; --i) {
+ QRegularExpressionMatch m = exactRe.match(that->at(i));
+ if (m.hasMatch())
+ return i;
+ }
+ return -1;
+}
+#endif // QT_NO_REGEXP
+#endif // QT_BOOTSTRAPPED
+
/*!
\fn int QStringList::indexOf(const QString &value, int from = 0) const
diff --git a/src/corelib/tools/qstringlist.h b/src/corelib/tools/qstringlist.h
index 260008f183..bf9c2e14bb 100644
--- a/src/corelib/tools/qstringlist.h
+++ b/src/corelib/tools/qstringlist.h
@@ -55,6 +55,7 @@ QT_BEGIN_NAMESPACE
class QRegExp;
+class QRegularExpression;
typedef QListIterator<QString> QStringListIterator;
typedef QMutableListIterator<QString> QMutableStringListIterator;
@@ -95,6 +96,16 @@ public:
inline int indexOf(QRegExp &rx, int from = 0) const;
inline int lastIndexOf(QRegExp &rx, int from = -1) const;
#endif
+
+#ifndef QT_BOOTSTRAPPED
+#ifndef QT_NO_REGEXP
+ inline QStringList filter(const QRegularExpression &re) const;
+ inline QStringList &replaceInStrings(const QRegularExpression &re, const QString &after);
+ inline int indexOf(const QRegularExpression &re, int from = 0) const;
+ inline int lastIndexOf(const QRegularExpression &re, int from = -1) const;
+#endif // QT_NO_REGEXP
+#endif // QT_BOOTSTRAPPED
+
#if !defined(Q_NO_USING_KEYWORD)
using QList<QString>::indexOf;
using QList<QString>::lastIndexOf;
@@ -127,6 +138,15 @@ namespace QtPrivate {
int Q_CORE_EXPORT QStringList_indexOf(const QStringList *that, QRegExp &rx, int from);
int Q_CORE_EXPORT QStringList_lastIndexOf(const QStringList *that, QRegExp &rx, int from);
#endif
+
+#ifndef QT_BOOTSTRAPPED
+#ifndef QT_NO_REGEXP
+ void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QRegularExpression &rx, const QString &after);
+ QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, const QRegularExpression &re);
+ int Q_CORE_EXPORT QStringList_indexOf(const QStringList *that, const QRegularExpression &re, int from);
+ int Q_CORE_EXPORT QStringList_lastIndexOf(const QStringList *that, const QRegularExpression &re, int from);
+#endif // QT_NO_REGEXP
+#endif // QT_BOOTSTRAPPED
}
inline void QStringList::sort()
@@ -193,6 +213,30 @@ inline int QStringList::lastIndexOf(QRegExp &rx, int from) const
}
#endif
+#ifndef QT_BOOTSTRAPPED
+#ifndef QT_NO_REGEXP
+inline QStringList &QStringList::replaceInStrings(const QRegularExpression &rx, const QString &after)
+{
+ QtPrivate::QStringList_replaceInStrings(this, rx, after);
+ return *this;
+}
+
+inline QStringList QStringList::filter(const QRegularExpression &rx) const
+{
+ return QtPrivate::QStringList_filter(this, rx);
+}
+
+inline int QStringList::indexOf(const QRegularExpression &rx, int from) const
+{
+ return QtPrivate::QStringList_indexOf(this, rx, from);
+}
+
+inline int QStringList::lastIndexOf(const QRegularExpression &rx, int from) const
+{
+ return QtPrivate::QStringList_lastIndexOf(this, rx, from);
+}
+#endif // QT_NO_REGEXP
+#endif // QT_BOOTSTRAPPED
#ifndef QT_NO_DATASTREAM
inline QDataStream &operator>>(QDataStream &in, QStringList &list)
diff --git a/src/corelib/tools/qvector.cpp b/src/corelib/tools/qvector.cpp
index 0f5c2d3cd9..0026338047 100644
--- a/src/corelib/tools/qvector.cpp
+++ b/src/corelib/tools/qvector.cpp
@@ -54,15 +54,7 @@ static inline int alignmentThreshold()
return 2 * sizeof(void*);
}
-const QVectorData QVectorData::shared_null = { Q_REFCOUNT_INITIALIZER(-1), 0, 0, true, false, 0 };
-
-QVectorData *QVectorData::malloc(int sizeofTypedData, int size, int sizeofT, QVectorData *init)
-{
- QVectorData* p = (QVectorData *)::malloc(sizeofTypedData + (size - 1) * sizeofT);
- Q_CHECK_PTR(p);
- ::memcpy(p, init, sizeofTypedData + (qMin(size, init->alloc) - 1) * sizeofT);
- return p;
-}
+const QVectorData QVectorData::shared_null = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, false, 0 };
QVectorData *QVectorData::allocate(int size, int alignment)
{
@@ -84,11 +76,9 @@ void QVectorData::free(QVectorData *x, int alignment)
::free(x);
}
-int QVectorData::grow(int sizeofTypedData, int size, int sizeofT, bool excessive)
+int QVectorData::grow(int sizeOfHeader, int size, int sizeOfT)
{
- if (excessive)
- return size + size / 2;
- return qAllocMore(size * sizeofT, sizeofTypedData - sizeofT) / sizeofT;
+ return qAllocMore(size * sizeOfT, sizeOfHeader) / sizeOfT;
}
/*!
diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h
index 09aabe5d73..226d0bb258 100644
--- a/src/corelib/tools/qvector.h
+++ b/src/corelib/tools/qvector.h
@@ -65,37 +65,28 @@ QT_BEGIN_NAMESPACE
struct Q_CORE_EXPORT QVectorData
{
QtPrivate::RefCount ref;
- int alloc;
int size;
-#if defined(Q_PROCESSOR_SPARC) && defined(Q_CC_GNU) && defined(__LP64__) && defined(QT_BOOTSTRAPPED)
- // workaround for bug in gcc 3.4.2
- uint sharable;
- uint capacity;
- uint reserved;
-#else
- uint sharable : 1;
- uint capacity : 1;
- uint reserved : 30;
-#endif
+ uint alloc : 31;
+ uint capacityReserved : 1;
+
+ qptrdiff offset;
+
+ void* data() { return reinterpret_cast<char *>(this) + this->offset; }
static const QVectorData shared_null;
- // ### Qt 5: rename to 'allocate()'. The current name causes problems for
- // some debugges when the QVector is member of a class within an unnamed namespace.
- // ### Qt 5: can be removed completely. (Ralf)
- static QVectorData *malloc(int sizeofTypedData, int size, int sizeofT, QVectorData *init);
static QVectorData *allocate(int size, int alignment);
static QVectorData *reallocate(QVectorData *old, int newsize, int oldsize, int alignment);
static void free(QVectorData *data, int alignment);
- static int grow(int sizeofTypedData, int size, int sizeofT, bool excessive);
+ static int grow(int sizeOfHeader, int size, int sizeOfT);
};
template <typename T>
-struct QVectorTypedData : private QVectorData
-{ // private inheritance as we must not access QVectorData member thought QVectorTypedData
- // as this would break strict aliasing rules. (in the case of shared_null)
- T array[1];
+struct QVectorTypedData : QVectorData
+{
+ T* begin() { return reinterpret_cast<T *>(this->data()); }
+ T* end() { return begin() + this->size; }
- static inline void free(QVectorTypedData<T> *x, int alignment) { QVectorData::free(static_cast<QVectorData *>(x), alignment); }
+ static QVectorTypedData *sharedNull() { return static_cast<QVectorTypedData *>(const_cast<QVectorData *>(&QVectorData::shared_null)); }
};
class QRegion;
@@ -104,27 +95,30 @@ template <typename T>
class QVector
{
typedef QVectorTypedData<T> Data;
- union {
- QVectorData *d;
-#if defined(Q_CC_SUN) && (__SUNPRO_CC <= 0x550)
- QVectorTypedData<T> *p;
-#else
- Data *p;
-#endif
- };
+ Data *d;
public:
- // ### Qt 5: Consider making QVector non-shared to get at least one
- // "really fast" container. See tests/benchmarks/corelib/tools/qvector/
- inline QVector() : d(const_cast<QVectorData *>(&QVectorData::shared_null)) { }
+ inline QVector() : d(Data::sharedNull()) { }
explicit QVector(int size);
QVector(int size, const T &t);
- inline QVector(const QVector<T> &v) : d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }
- inline ~QVector() { if (!d) return; if (!d->ref.deref()) free(p); }
+ inline QVector(const QVector<T> &v)
+ {
+ if (v.d->ref.ref()) {
+ d = v.d;
+ } else {
+ d = Data::sharedNull();
+ realloc(0, int(v.d->alloc));
+ qCopy(v.d->begin(), v.d->end(), d->begin());
+ d->size = v.d->size;
+ d->capacityReserved = v.d->capacityReserved;
+ }
+ }
+
+ inline ~QVector() { if (!d->ref.deref()) free(d); }
QVector<T> &operator=(const QVector<T> &v);
#ifdef Q_COMPILER_RVALUE_REFS
inline QVector<T> operator=(QVector<T> &&other)
- { qSwap(p, other.p); return *this; }
+ { qSwap(d, other.d); return *this; }
#endif
inline void swap(QVector<T> &other) { qSwap(d, other.d); }
#ifdef Q_COMPILER_INITIALIZER_LISTS
@@ -139,18 +133,27 @@ public:
void resize(int size);
- inline int capacity() const { return d->alloc; }
+ inline int capacity() const { return int(d->alloc); }
void reserve(int size);
- inline void squeeze() { realloc(d->size, d->size); d->capacity = 0; }
+ inline void squeeze() { realloc(d->size, d->size); d->capacityReserved = 0; }
+
+ inline void detach() { if (!isDetached()) detach_helper(); }
+ inline bool isDetached() const { return !d->ref.isShared(); }
+ inline void setSharable(bool sharable)
+ {
+ if (sharable == d->ref.isSharable())
+ return;
+ if (!sharable)
+ detach();
+ if (d != Data::sharedNull())
+ d->ref.setSharable(sharable);
+ }
- inline void detach() { if (d->ref != 1) detach_helper(); }
- inline bool isDetached() const { return d->ref == 1; }
- inline void setSharable(bool sharable) { if (!sharable) detach(); if (d != &QVectorData::shared_null) d->sharable = sharable; }
inline bool isSharedWith(const QVector<T> &other) const { return d == other.d; }
- inline T *data() { detach(); return p->array; }
- inline const T *data() const { return p->array; }
- inline const T *constData() const { return p->array; }
+ inline T *data() { detach(); return d->begin(); }
+ inline const T *data() const { return d->begin(); }
+ inline const T *constData() const { return d->begin(); }
void clear();
const T &at(int i) const;
@@ -243,14 +246,14 @@ public:
typedef T* iterator;
typedef const T* const_iterator;
#endif
- inline iterator begin() { detach(); return p->array; }
- inline const_iterator begin() const { return p->array; }
- inline const_iterator cbegin() const { return p->array; }
- inline const_iterator constBegin() const { return p->array; }
- inline iterator end() { detach(); return p->array + d->size; }
- inline const_iterator end() const { return p->array + d->size; }
- inline const_iterator cend() const { return p->array + d->size; }
- inline const_iterator constEnd() const { return p->array + d->size; }
+ inline iterator begin() { detach(); return d->begin(); }
+ inline const_iterator begin() const { return d->begin(); }
+ inline const_iterator cbegin() const { return d->begin(); }
+ inline const_iterator constBegin() const { return d->begin(); }
+ inline iterator end() { detach(); return d->end(); }
+ inline const_iterator end() const { return d->end(); }
+ inline const_iterator cend() const { return d->end(); }
+ inline const_iterator constEnd() const { return d->end(); }
iterator insert(iterator before, int n, const T &x);
inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); }
iterator erase(iterator begin, iterator end);
@@ -315,46 +318,49 @@ private:
friend class QRegion; // Optimization for QRegion::rects()
void detach_helper();
- QVectorData *malloc(int alloc);
+ Data *malloc(int alloc);
void realloc(int size, int alloc);
void free(Data *d);
- int sizeOfTypedData() {
- // this is more or less the same as sizeof(Data), except that it doesn't
- // count the padding at the end
- return reinterpret_cast<const char *>(&(reinterpret_cast<const Data *>(this))->array[1]) - reinterpret_cast<const char *>(this);
+
+ class AlignmentDummy { QVectorData header; T array[1]; };
+
+ static Q_DECL_CONSTEXPR int offsetOfTypedData()
+ {
+ // (non-POD)-safe offsetof(AlignmentDummy, array)
+ return (sizeof(QVectorData) + (alignOfTypedData() - 1)) & ~(alignOfTypedData() - 1);
}
- inline int alignOfTypedData() const
+ static Q_DECL_CONSTEXPR int alignOfTypedData()
{
#ifdef Q_ALIGNOF
- return qMax<int>(sizeof(void*), Q_ALIGNOF(Data));
+ return Q_ALIGNOF(AlignmentDummy);
#else
- return 0;
+ return sizeof(void *);
#endif
}
};
template <typename T>
void QVector<T>::detach_helper()
-{ realloc(d->size, d->alloc); }
+{ realloc(d->size, int(d->alloc)); }
template <typename T>
void QVector<T>::reserve(int asize)
-{ if (asize > d->alloc) realloc(d->size, asize); if (d->ref == 1) d->capacity = 1; }
+{ if (asize > int(d->alloc)) realloc(d->size, asize); if (isDetached()) d->capacityReserved = 1; }
template <typename T>
void QVector<T>::resize(int asize)
-{ realloc(asize, (asize > d->alloc || (!d->capacity && asize < d->size && asize < (d->alloc >> 1))) ?
- QVectorData::grow(sizeOfTypedData(), asize, sizeof(T), QTypeInfo<T>::isStatic)
- : d->alloc); }
+{ realloc(asize, (asize > int(d->alloc) || (!d->capacityReserved && asize < d->size && asize < int(d->alloc >> 1))) ?
+ QVectorData::grow(offsetOfTypedData(), asize, sizeof(T))
+ : int(d->alloc)); }
template <typename T>
inline void QVector<T>::clear()
{ *this = QVector<T>(); }
template <typename T>
inline const T &QVector<T>::at(int i) const
{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::at", "index out of range");
- return p->array[i]; }
+ return d->begin()[i]; }
template <typename T>
inline const T &QVector<T>::operator[](int i) const
{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
- return p->array[i]; }
+ return d->begin()[i]; }
template <typename T>
inline T &QVector<T>::operator[](int i)
{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
@@ -390,39 +396,37 @@ inline void QVector<T>::replace(int i, const T &t)
template <typename T>
QVector<T> &QVector<T>::operator=(const QVector<T> &v)
{
- QVectorData *o = v.d;
- o->ref.ref();
- if (!d->ref.deref())
- free(p);
- d = o;
- if (!d->sharable)
- detach_helper();
+ if (v.d != d) {
+ QVector<T> tmp(v);
+ tmp.swap(*this);
+ }
return *this;
}
template <typename T>
-inline QVectorData *QVector<T>::malloc(int aalloc)
+inline typename QVector<T>::Data *QVector<T>::malloc(int aalloc)
{
- QVectorData *vectordata = QVectorData::allocate(sizeOfTypedData() + (aalloc - 1) * sizeof(T), alignOfTypedData());
+ QVectorData *vectordata = QVectorData::allocate(offsetOfTypedData() + aalloc * sizeof(T), alignOfTypedData());
Q_CHECK_PTR(vectordata);
- return vectordata;
+ return static_cast<Data *>(vectordata);
}
template <typename T>
QVector<T>::QVector(int asize)
{
d = malloc(asize);
- d->ref = 1;
- d->alloc = d->size = asize;
- d->sharable = true;
- d->capacity = false;
+ d->ref.initializeOwned();
+ d->size = asize;
+ d->alloc = uint(d->size);
+ d->capacityReserved = false;
+ d->offset = offsetOfTypedData();
if (QTypeInfo<T>::isComplex) {
- T* b = p->array;
- T* i = p->array + d->size;
+ T* b = d->begin();
+ T* i = d->end();
while (i != b)
new (--i) T;
} else {
- qMemSet(p->array, 0, asize * sizeof(T));
+ qMemSet(d->begin(), 0, asize * sizeof(T));
}
}
@@ -430,12 +434,13 @@ template <typename T>
QVector<T>::QVector(int asize, const T &t)
{
d = malloc(asize);
- d->ref = 1;
- d->alloc = d->size = asize;
- d->sharable = true;
- d->capacity = false;
- T* i = p->array + d->size;
- while (i != p->array)
+ d->ref.initializeOwned();
+ d->size = asize;
+ d->alloc = uint(d->size);
+ d->capacityReserved = false;
+ d->offset = offsetOfTypedData();
+ T* i = d->end();
+ while (i != d->begin())
new (--i) T(t);
}
@@ -444,14 +449,22 @@ template <typename T>
QVector<T>::QVector(std::initializer_list<T> args)
{
d = malloc(int(args.size()));
- d->ref = 1;
- d->alloc = d->size = int(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));
+ d->ref.initializeOwned();
+ d->size = int(args.size());
+ d->alloc = uint(d->size);
+ d->capacityReserved = false;
+ d->offset = offsetOfTypedData();
+ if (QTypeInfo<T>::isComplex) {
+ T* b = d->begin();
+ T* i = d->end();
+ const T* s = args.end();
+ while (i != b)
+ new(--i) T(*--s);
+ } else {
+ // std::initializer_list<T>::iterator is guaranteed to be
+ // const T* ([support.initlist]/1), so can be memcpy'ed away from:
+ ::memcpy(d->begin(), args.begin(), args.size() * sizeof(T));
+ }
}
#endif
@@ -459,14 +472,12 @@ template <typename T>
void QVector<T>::free(Data *x)
{
if (QTypeInfo<T>::isComplex) {
- T* b = x->array;
- union { QVectorData *d; Data *p; } u;
- u.p = x;
- T* i = b + u.d->size;
+ T* b = x->begin();
+ T* i = b + x->size;
while (i-- != b)
i->~T();
}
- x->free(x, alignOfTypedData());
+ Data::free(x, alignOfTypedData());
}
template <typename T>
@@ -475,84 +486,82 @@ void QVector<T>::realloc(int asize, int aalloc)
Q_ASSERT(asize <= aalloc);
T *pOld;
T *pNew;
- union { QVectorData *d; Data *p; } x;
- x.d = d;
+ Data *x = d;
- if (QTypeInfo<T>::isComplex && asize < d->size && d->ref == 1 ) {
+ if (QTypeInfo<T>::isComplex && asize < d->size && isDetached()) {
// call the destructor on all objects that need to be
// destroyed when shrinking
- pOld = p->array + d->size;
- pNew = p->array + asize;
+ pOld = d->begin() + d->size;
+ pNew = d->begin() + asize;
while (asize < d->size) {
(--pOld)->~T();
d->size--;
}
}
- if (aalloc != d->alloc || d->ref != 1) {
+ if (aalloc != int(d->alloc) || !isDetached()) {
// (re)allocate memory
if (QTypeInfo<T>::isStatic) {
- x.d = malloc(aalloc);
- Q_CHECK_PTR(x.p);
- x.d->size = 0;
- } else if (d->ref != 1) {
- x.d = malloc(aalloc);
- Q_CHECK_PTR(x.p);
+ x = malloc(aalloc);
+ Q_CHECK_PTR(x);
+ x->size = 0;
+ } else if (!isDetached()) {
+ x = malloc(aalloc);
+ Q_CHECK_PTR(x);
if (QTypeInfo<T>::isComplex) {
- x.d->size = 0;
+ x->size = 0;
} else {
- ::memcpy(x.p, p, sizeOfTypedData() + (qMin(aalloc, d->alloc) - 1) * sizeof(T));
- x.d->size = d->size;
+ ::memcpy(x, d, offsetOfTypedData() + qMin(uint(aalloc), d->alloc) * sizeof(T));
+ x->size = d->size;
}
} else {
QT_TRY {
- QVectorData *mem = QVectorData::reallocate(d, sizeOfTypedData() + (aalloc - 1) * sizeof(T),
- sizeOfTypedData() + (d->alloc - 1) * sizeof(T), alignOfTypedData());
+ QVectorData *mem = QVectorData::reallocate(d, offsetOfTypedData() + aalloc * sizeof(T),
+ offsetOfTypedData() + d->alloc * sizeof(T), alignOfTypedData());
Q_CHECK_PTR(mem);
- x.d = d = mem;
- x.d->size = d->size;
+ x = d = static_cast<Data *>(mem);
+ x->size = d->size;
} QT_CATCH (const std::bad_alloc &) {
- if (aalloc > d->alloc) // ignore the error in case we are just shrinking.
+ if (aalloc > int(d->alloc)) // ignore the error in case we are just shrinking.
QT_RETHROW;
}
}
- x.d->ref = 1;
- x.d->alloc = aalloc;
- x.d->sharable = true;
- x.d->capacity = d->capacity;
- x.d->reserved = 0;
+ x->ref.initializeOwned();
+ x->alloc = uint(aalloc);
+ x->capacityReserved = d->capacityReserved;
+ x->offset = offsetOfTypedData();
}
if (QTypeInfo<T>::isComplex) {
QT_TRY {
- pOld = p->array + x.d->size;
- pNew = x.p->array + x.d->size;
+ pOld = d->begin() + x->size;
+ pNew = x->begin() + x->size;
// copy objects from the old array into the new array
const int toMove = qMin(asize, d->size);
- while (x.d->size < toMove) {
+ while (x->size < toMove) {
new (pNew++) T(*pOld++);
- x.d->size++;
+ x->size++;
}
// construct all new objects when growing
- while (x.d->size < asize) {
+ while (x->size < asize) {
new (pNew++) T;
- x.d->size++;
+ x->size++;
}
} QT_CATCH (...) {
- free(x.p);
+ free(x);
QT_RETHROW;
}
- } else if (asize > x.d->size) {
+ } else if (asize > x->size) {
// initialize newly allocated memory to 0
- qMemSet(x.p->array + x.d->size, 0, (asize - x.d->size) * sizeof(T));
+ qMemSet(x->end(), 0, (asize - x->size) * sizeof(T));
}
- x.d->size = asize;
+ x->size = asize;
- if (d != x.d) {
+ if (d != x) {
if (!d->ref.deref())
- free(p);
- d = x.d;
+ free(d);
+ d = x;
}
}
@@ -562,31 +571,31 @@ Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const
if (i < 0 || i >= d->size) {
return T();
}
- return p->array[i];
+ return d->begin()[i];
}
template<typename T>
Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i, const T &defaultValue) const
{
- return ((i < 0 || i >= d->size) ? defaultValue : p->array[i]);
+ return ((i < 0 || i >= d->size) ? defaultValue : d->begin()[i]);
}
template <typename T>
void QVector<T>::append(const T &t)
{
- if (d->ref != 1 || d->size + 1 > d->alloc) {
+ if (!isDetached() || d->size + 1 > int(d->alloc)) {
const T copy(t);
- realloc(d->size, (d->size + 1 > d->alloc) ?
- QVectorData::grow(sizeOfTypedData(), d->size + 1, sizeof(T), QTypeInfo<T>::isStatic)
- : d->alloc);
+ realloc(d->size, (d->size + 1 > int(d->alloc)) ?
+ QVectorData::grow(offsetOfTypedData(), d->size + 1, sizeof(T))
+ : int(d->alloc));
if (QTypeInfo<T>::isComplex)
- new (p->array + d->size) T(copy);
+ new (d->end()) T(copy);
else
- p->array[d->size] = copy;
+ *d->end() = copy;
} else {
if (QTypeInfo<T>::isComplex)
- new (p->array + d->size) T(t);
+ new (d->end()) T(t);
else
- p->array[d->size] = t;
+ *d->end() = t;
}
++d->size;
}
@@ -594,27 +603,26 @@ void QVector<T>::append(const T &t)
template <typename T>
typename QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, const T &t)
{
- int offset = int(before - p->array);
+ int offset = int(before - d->begin());
if (n != 0) {
const T copy(t);
- if (d->ref != 1 || d->size + n > d->alloc)
- realloc(d->size, QVectorData::grow(sizeOfTypedData(), d->size + n, sizeof(T),
- QTypeInfo<T>::isStatic));
+ if (!isDetached() || d->size + n > int(d->alloc))
+ realloc(d->size, QVectorData::grow(offsetOfTypedData(), d->size + n, sizeof(T)));
if (QTypeInfo<T>::isStatic) {
- T *b = p->array + d->size;
- T *i = p->array + d->size + n;
+ T *b = d->end();
+ T *i = d->end() + n;
while (i != b)
new (--i) T;
- i = p->array + d->size;
+ i = d->end();
T *j = i + n;
- b = p->array + offset;
+ b = d->begin() + offset;
while (i != b)
*--j = *--i;
i = b+n;
while (i != b)
*--i = copy;
} else {
- T *b = p->array + offset;
+ T *b = d->begin() + offset;
T *i = b + n;
memmove(i, b, (d->size - offset) * sizeof(T));
while (i != b)
@@ -622,29 +630,29 @@ typename QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, c
}
d->size += n;
}
- return p->array + offset;
+ return d->begin() + offset;
}
template <typename T>
typename QVector<T>::iterator QVector<T>::erase(iterator abegin, iterator aend)
{
- int f = int(abegin - p->array);
- int l = int(aend - p->array);
+ int f = int(abegin - d->begin());
+ int l = int(aend - d->begin());
int n = l - f;
detach();
if (QTypeInfo<T>::isComplex) {
- qCopy(p->array+l, p->array+d->size, p->array+f);
- T *i = p->array+d->size;
- T* b = p->array+d->size-n;
+ qCopy(d->begin()+l, d->end(), d->begin()+f);
+ T *i = d->end();
+ T* b = d->end()-n;
while (i != b) {
--i;
i->~T();
}
} else {
- memmove(p->array + f, p->array + l, (d->size-l)*sizeof(T));
+ memmove(d->begin() + f, d->begin() + l, (d->size-l)*sizeof(T));
}
d->size -= n;
- return p->array + f;
+ return d->begin() + f;
}
template <typename T>
@@ -654,9 +662,9 @@ bool QVector<T>::operator==(const QVector<T> &v) const
return false;
if (d == v.d)
return true;
- T* b = p->array;
+ T* b = d->begin();
T* i = b + d->size;
- T* j = v.p->array + d->size;
+ T* j = v.d->end();
while (i != b)
if (!(*--i == *--j))
return false;
@@ -669,8 +677,8 @@ QVector<T> &QVector<T>::fill(const T &from, int asize)
const T copy(from);
resize(asize < 0 ? d->size : asize);
if (d->size) {
- T *i = p->array + d->size;
- T *b = p->array;
+ T *i = d->end();
+ T *b = d->begin();
while (i != b)
*--i = copy;
}
@@ -683,9 +691,9 @@ QVector<T> &QVector<T>::operator+=(const QVector &l)
int newSize = d->size + l.d->size;
realloc(d->size, newSize);
- T *w = p->array + newSize;
- T *i = l.p->array + l.d->size;
- T *b = l.p->array;
+ T *w = d->begin() + newSize;
+ T *i = l.d->end();
+ T *b = l.d->begin();
while (i != b) {
if (QTypeInfo<T>::isComplex)
new (--w) T(*--i);
@@ -702,11 +710,11 @@ int QVector<T>::indexOf(const T &t, int from) const
if (from < 0)
from = qMax(from + d->size, 0);
if (from < d->size) {
- T* n = p->array + from - 1;
- T* e = p->array + d->size;
+ T* n = d->begin() + from - 1;
+ T* e = d->end();
while (++n != e)
if (*n == t)
- return n - p->array;
+ return n - d->begin();
}
return -1;
}
@@ -719,8 +727,8 @@ int QVector<T>::lastIndexOf(const T &t, int from) const
else if (from >= d->size)
from = d->size-1;
if (from >= 0) {
- T* b = p->array;
- T* n = p->array + from + 1;
+ T* b = d->begin();
+ T* n = d->begin() + from + 1;
while (n != b) {
if (*--n == t)
return n - b;
@@ -732,8 +740,8 @@ int QVector<T>::lastIndexOf(const T &t, int from) const
template <typename T>
bool QVector<T>::contains(const T &t) const
{
- T* b = p->array;
- T* i = p->array + d->size;
+ T* b = d->begin();
+ T* i = d->end();
while (i != b)
if (*--i == t)
return true;
@@ -744,8 +752,8 @@ template <typename T>
int QVector<T>::count(const T &t) const
{
int c = 0;
- T* b = p->array;
- T* i = p->array + d->size;
+ T* b = d->begin();
+ T* i = d->end();
while (i != b)
if (*--i == t)
++c;
diff --git a/src/corelib/tools/tools.pri b/src/corelib/tools/tools.pri
index 757b425176..ec02454c67 100644
--- a/src/corelib/tools/tools.pri
+++ b/src/corelib/tools/tools.pri
@@ -2,6 +2,9 @@
HEADERS += \
tools/qalgorithms.h \
+ tools/qarraydata.h \
+ tools/qarraydataops.h \
+ tools/qarraydatapointer.h \
tools/qbitarray.h \
tools/qbytearray.h \
tools/qbytearraymatcher.h \
@@ -57,6 +60,7 @@ HEADERS += \
SOURCES += \
+ tools/qarraydata.cpp \
tools/qbitarray.cpp \
tools/qbytearray.cpp \
tools/qbytearraymatcher.cpp \