QPrinterInfo - Fix isNull() by fixing constructors

The QPrinterInfo copy and QPrinter constructors and the assignment
operator were not taking the shared_null into account, and so any use
of them resulted in a new null QPrinterInfo different to shared_null,
which lead to isNull() always returning true in anything other than
the simplest use case. While fixing this also make the shared_null a
Q_GLOBAL_STATIC.

Task-number: QTBUG-21087

Change-Id: I0beb24088208e9ed58d21ca26b0c8d00b02e5b8f
Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
This commit is contained in:
John Layt 2013-11-25 20:36:58 +01:00 committed by The Qt Project
parent 42fa59b151
commit db4afbef7d
3 changed files with 31 additions and 20 deletions

View File

@ -35,8 +35,17 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
QPrinterInfoPrivate QPrinterInfoPrivate::shared_null; Q_GLOBAL_STATIC(QPrinterInfoPrivate, shared_null);
class QPrinterInfoPrivateDeleter
{
public:
static inline void cleanup(QPrinterInfoPrivate *d)
{
if (d != shared_null)
delete d;
}
};
/*! /*!
\class QPrinterInfo \class QPrinterInfo
@ -83,7 +92,7 @@ QPrinterInfoPrivate QPrinterInfoPrivate::shared_null;
\sa isNull() \sa isNull()
*/ */
QPrinterInfo::QPrinterInfo() QPrinterInfo::QPrinterInfo()
: d_ptr(&QPrinterInfoPrivate::shared_null) : d_ptr(shared_null)
{ {
} }
@ -91,7 +100,7 @@ QPrinterInfo::QPrinterInfo()
Constructs a copy of \a other. Constructs a copy of \a other.
*/ */
QPrinterInfo::QPrinterInfo(const QPrinterInfo &other) QPrinterInfo::QPrinterInfo(const QPrinterInfo &other)
: d_ptr(new QPrinterInfoPrivate(*other.d_ptr)) : d_ptr((other.d_ptr.data() == shared_null) ? shared_null : new QPrinterInfoPrivate(*other.d_ptr))
{ {
} }
@ -99,12 +108,15 @@ QPrinterInfo::QPrinterInfo(const QPrinterInfo &other)
Constructs a QPrinterInfo object from \a printer. Constructs a QPrinterInfo object from \a printer.
*/ */
QPrinterInfo::QPrinterInfo(const QPrinter &printer) QPrinterInfo::QPrinterInfo(const QPrinter &printer)
: d_ptr(&QPrinterInfoPrivate::shared_null) : d_ptr(shared_null)
{ {
QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get(); QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
if (ps) { if (ps) {
QPrinterInfo pi = ps->printerInfo(printer.printerName()); QPrinterInfo pi = ps->printerInfo(printer.printerName());
d_ptr.reset(new QPrinterInfoPrivate(*pi.d_ptr)); if (pi.d_ptr.data() == shared_null)
d_ptr.reset(shared_null);
else
d_ptr.reset(new QPrinterInfoPrivate(*pi.d_ptr));
} }
} }
@ -130,7 +142,10 @@ QPrinterInfo::~QPrinterInfo()
QPrinterInfo &QPrinterInfo::operator=(const QPrinterInfo &other) QPrinterInfo &QPrinterInfo::operator=(const QPrinterInfo &other)
{ {
Q_ASSERT(d_ptr); Q_ASSERT(d_ptr);
d_ptr.reset(new QPrinterInfoPrivate(*other.d_ptr)); if (other.d_ptr.data() == shared_null)
d_ptr.reset(shared_null);
else
d_ptr.reset(new QPrinterInfoPrivate(*other.d_ptr));
return *this; return *this;
} }
@ -191,7 +206,7 @@ QString QPrinterInfo::makeAndModel() const
bool QPrinterInfo::isNull() const bool QPrinterInfo::isNull() const
{ {
Q_D(const QPrinterInfo); Q_D(const QPrinterInfo);
return d == &QPrinterInfoPrivate::shared_null; return d == shared_null || d->name.isEmpty();
} }
/*! /*!

View File

@ -72,8 +72,6 @@ public:
~QPrinterInfoPrivate() ~QPrinterInfoPrivate()
{} {}
static QPrinterInfoPrivate shared_null;
QString name; QString name;
QString description; QString description;
QString location; QString location;
@ -87,17 +85,6 @@ public:
mutable QList<QPair<QString, QSizeF> > paperNames; mutable QList<QPair<QString, QSizeF> > paperNames;
}; };
class QPrinterInfoPrivateDeleter
{
public:
static inline void cleanup(QPrinterInfoPrivate *d)
{
if (d != &QPrinterInfoPrivate::shared_null)
delete d;
}
};
QT_END_NAMESPACE QT_END_NAMESPACE
#endif // QT_NO_PRINTER #endif // QT_NO_PRINTER

View File

@ -275,6 +275,9 @@ void tst_QPrinterInfo::testConstructors()
QCOMPARE(null.printerName(), QString()); QCOMPARE(null.printerName(), QString());
QVERIFY(null.isNull()); QVERIFY(null.isNull());
QPrinterInfo null2(null);
QVERIFY(null2.isNull());
QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters(); QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
for (int i = 0; i < printers.size(); ++i) { for (int i = 0; i < printers.size(); ++i) {
@ -295,6 +298,12 @@ void tst_QPrinterInfo::testConstructors()
void tst_QPrinterInfo::testAssignment() void tst_QPrinterInfo::testAssignment()
{ {
QPrinterInfo null;
QVERIFY(null.isNull());
QPrinterInfo null2;
null2 = null;
QVERIFY(null2.isNull());
QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters(); QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
for (int i = 0; i < printers.size(); ++i) { for (int i = 0; i < printers.size(); ++i) {