From db8167c224348fe828aaa0746701b7fa3040c2df Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 25 Feb 2014 23:07:46 +0100 Subject: QPen: add move constructor After this change, the relation between copy/move constructor calls in QtGui is something like 40/16. A moved-from QPen can only be copied, assigned-to or else destroyed. This required adding a nullptr check to the copy ctor and the dtor and rewriting copy assignment (which used non-nullptr-safe qAtomicAssign) in terms of copy construction and swapping. Extensive tests included. They are implemented such that they work in C++98 as well as C++11 mode, but they naturally test move semantics only in a C++11 build. Change-Id: If68f37c10b8eeefb2478cbae386cd2e38b4f6e19 Reviewed-by: Thiago Macieira --- src/gui/painting/qpen.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/gui/painting/qpen.cpp') diff --git a/src/gui/painting/qpen.cpp b/src/gui/painting/qpen.cpp index c0b3769c2d..b661057f64 100644 --- a/src/gui/painting/qpen.cpp +++ b/src/gui/painting/qpen.cpp @@ -327,17 +327,29 @@ QPen::QPen(const QBrush &brush, qreal width, Qt::PenStyle s, Qt::PenCapStyle c, QPen::QPen(const QPen &p) { d = p.d; - d->ref.ref(); + if (d) + d->ref.ref(); } +/*! + \fn QPen::QPen(QPen &&pen) + \since 5.4 + + Constructs a pen that is moved from the given \a pen. + + The moved-from pen can only be assigned to, copied, or + destroyed. Any other operation (prior to assignment) leads to + undefined behavior. +*/ + /*! Destroys the pen. */ QPen::~QPen() { - if (!d->ref.deref()) + if (d && !d->ref.deref()) delete d; } @@ -373,7 +385,7 @@ void QPen::detach() QPen &QPen::operator=(const QPen &p) { - qAtomicAssign(d, p.d); + QPen(p).swap(*this); return *this; } -- cgit v1.2.3