diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2012-05-23 21:10:31 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-12-23 17:51:45 +0100 |
commit | 7ee9551b62b050f7e9aefdc19e19bac5ba263507 (patch) | |
tree | 4b65eee1e0b83e1c2eeb8a40dad685f6709e7001 /src/corelib | |
parent | f4b4b4414e202fb2873579ed3f50aa9e20710c02 (diff) |
Add support for multiple arguments to QSharedPointer::create()
Requires C++11 rvalue references and variadic templates so we can
implement perfect forwarding.
Change-Id: I62e47d1ffd0c61e8386f9f246aa79031b7430b46
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/tools/qsharedpointer.cpp | 31 | ||||
-rw-r--r-- | src/corelib/tools/qsharedpointer.h | 3 | ||||
-rw-r--r-- | src/corelib/tools/qsharedpointer_impl.h | 27 |
3 files changed, 61 insertions, 0 deletions
diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp index 167771027e..c9456c44c2 100644 --- a/src/corelib/tools/qsharedpointer.cpp +++ b/src/corelib/tools/qsharedpointer.cpp @@ -605,6 +605,37 @@ */ /*! + \fn QSharedPointer<T> QSharedPointer::create() + \since 4.6 + + Creates a QSharedPointer object and allocates a new item of type \tt T. The + QSharedPointer internals and the object are allocated in one single memory + allocation, which could help reduce memory fragmentation in a long-running + application. + + This function calls the default constructor for type \tt T. +*/ + +/*! + \fn QSharedPointer<T> QSharedPointer::create(...) + \overload + \since 5.1 + + Creates a QSharedPointer object and allocates a new item of type \tt T. The + QSharedPointer internals and the object are allocated in one single memory + allocation, which could help reduce memory fragmentation in a long-running + application. + + This function will attempt to call a constructor for type \tt T that can + accept all the arguments passed. Arguments will be perfectly-forwarded. + + \note This function is only available with a C++11 compiler that supports + perfect forwarding of an arbitrary number of arguments. If the compiler + does not support the necessary C++11 features, you must use the overload + that calls the default constructor. +*/ + +/*! \fn QWeakPointer<T> QSharedPointer::toWeakRef() const Returns a weak reference object that shares the pointer referenced diff --git a/src/corelib/tools/qsharedpointer.h b/src/corelib/tools/qsharedpointer.h index cfc5cadbb0..a430153bca 100644 --- a/src/corelib/tools/qsharedpointer.h +++ b/src/corelib/tools/qsharedpointer.h @@ -95,6 +95,9 @@ public: template <class X> QSharedPointer<X> dynamicCast() const; template <class X> QSharedPointer<X> constCast() const; template <class X> QSharedPointer<X> objectCast() const; + + static inline QSharedPointer<T> create(); + static inline QSharedPointer<T> create(...); }; template <class T> diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 6f3e577e55..6393cc3970 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -62,6 +62,10 @@ QT_END_HEADER #include <QtCore/qobject.h> // for qobject_cast #include <QtCore/qhash.h> // for qHash +#if defined(Q_COMPILER_RVALUE_REFS) && defined(Q_COMPILER_VARIADIC_TEMPLATES) +# include <utility> // for std::forward +#endif + QT_BEGIN_HEADER QT_BEGIN_NAMESPACE @@ -389,6 +393,28 @@ public: QWeakPointer<T> toWeakRef() const; +#if defined(Q_COMPILER_RVALUE_REFS) && defined(Q_COMPILER_VARIADIC_TEMPLATES) + template <typename... Args> + static QSharedPointer<T> create(Args && ...arguments) + { + typedef QtSharedPointer::ExternalRefCountWithContiguousData<T> Private; +# ifdef QT_SHAREDPOINTER_TRACK_POINTERS + typename Private::DestroyerFn destroy = &Private::safetyCheckDeleter; +# else + typename Private::DestroyerFn destroy = &Private::deleter; +# endif + QSharedPointer<T> result(Qt::Uninitialized); + result.d = Private::create(&result.value, destroy); + + // now initialize the data + new (result.data()) T(std::forward<Args>(arguments)...); + result.d->setQObjectShared(result.value, true); +# ifdef QT_SHAREDPOINTER_TRACK_POINTERS + internalSafetyCheckAdd(result.d, result.value); +# endif + return result; + } +#else static inline QSharedPointer<T> create() { typedef QtSharedPointer::ExternalRefCountWithContiguousData<T> Private; @@ -408,6 +434,7 @@ public: result.d->setQObjectShared(result.value, true); return result; } +#endif private: explicit QSharedPointer(Qt::Initialization) {} |