Skip to content

Commit

Permalink
Add support for QSharedPointer<cv qualified>::create()
Browse files Browse the repository at this point in the history
[ChangeLog][QtCore][QSharedPointer] Fixed a problem that made create()
on a type with const qualification fail to compile.

Task-number: QTBUG-68300
Change-Id: I0825ff5b5f6f4c85939ffffd152f3e55e5b9caae
Reviewed-by: Ville Voutilainen <[email protected]>
Reviewed-by: Simon Hausmann <[email protected]>
  • Loading branch information
thiagomacieira committed May 17, 2018
1 parent fce6303 commit c359df5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/corelib/tools/qsharedpointer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ namespace QtSharedPointer {
struct ExternalRefCountWithContiguousData: public ExternalRefCountData
{
typedef ExternalRefCountData Parent;
T data;
typedef typename std::remove_cv<T>::type NoCVType;
NoCVType data;

static void deleter(ExternalRefCountData *self)
{
Expand All @@ -262,7 +263,7 @@ namespace QtSharedPointer {
}
static void noDeleter(ExternalRefCountData *) { }

static inline ExternalRefCountData *create(T **ptr, DestroyerFn destroy)
static inline ExternalRefCountData *create(NoCVType **ptr, DestroyerFn destroy)
{
ExternalRefCountWithContiguousData *d =
static_cast<ExternalRefCountWithContiguousData *>(::operator new(sizeof(ExternalRefCountWithContiguousData)));
Expand Down Expand Up @@ -437,10 +438,12 @@ template <class T> class QSharedPointer
# endif
typename Private::DestroyerFn noDestroy = &Private::noDeleter;
QSharedPointer result(Qt::Uninitialized);
result.d = Private::create(&result.value, noDestroy);
typename std::remove_cv<T>::type *ptr;
result.d = Private::create(&ptr, noDestroy);

// now initialize the data
new (result.data()) T(std::forward<Args>(arguments)...);
new (ptr) T(std::forward<Args>(arguments)...);
result.value = ptr;
result.d->destroyer = destroy;
result.d->setQObjectShared(result.value, true);
# ifdef QT_SHAREDPOINTER_TRACK_POINTERS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private slots:
void lambdaCustomDeleter();
#endif
void creating();
void creatingCvQualified();
void creatingVariadic();
void creatingQObject();
void mixTrackingPointerCode();
Expand Down Expand Up @@ -1771,6 +1772,13 @@ void tst_QSharedPointer::creating()
safetyCheck();
}

void tst_QSharedPointer::creatingCvQualified()
{
auto cptr = QSharedPointer<const Data>::create();
auto vptr = QSharedPointer<volatile Data>::create();
auto cvptr = QSharedPointer<const volatile Data>::create();
}

void tst_QSharedPointer::creatingVariadic()
{
int i = 42;
Expand Down

0 comments on commit c359df5

Please sign in to comment.