Replace confusing member variable with a predicate

The Converter class, in the eponymous example, had a null member
variable that wasn't a nullptr - it pointed to an instance of
NullConverter - so that other converters could test whether a
Converter * they'd been passed was null (in the sense of pointing to a
NullConverter). This, however, was susceptible to misreading - I
misread one such comparison as a nullptr check and thus thought it
redundant with an earlier actual nullptr check. To spare future
readers similar confusion, replace the public static member variable
with a protected (since only other derived classes need it) static
predicate, to at least give the reader a clue that this is using the
word null in a class-specific sense.

Pick-to: 6.5
Task-number: QTBUG-111228
Change-Id: I1e4f494b303d1bf90107f8c6fa3a4a22f6d81b90
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 0b1670134b43ba390f35b52d2458e4269a804de3)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Edward Welbourne 2023-10-31 16:10:55 +01:00 committed by Qt Cherry-pick Bot
parent 371dd6c6bd
commit 2eb1a72f73
4 changed files with 7 additions and 4 deletions

View File

@ -249,7 +249,7 @@ QVariant CborConverter::loadFile(QIODevice *f, const Converter *&outputConverter
if (outputConverter == nullptr) if (outputConverter == nullptr)
outputConverter = &cborDiagnosticDumper; outputConverter = &cborDiagnosticDumper;
else if (outputConverter == null) else if (isNull(outputConverter))
return QVariant(); return QVariant();
else if (!outputConverter->outputOptions().testFlag(SupportsArbitraryMapKeys)) else if (!outputConverter->outputOptions().testFlag(SupportsArbitraryMapKeys))
return contents.toVariant(); return contents.toVariant();

View File

@ -28,9 +28,9 @@ class Converter
{ {
protected: protected:
Converter(); Converter();
static bool isNull(const Converter *converter); // in nullconverter.cpp
public: public:
static Converter *null;
enum class Direction { In = 1, Out = 2, InOut = In | Out }; enum class Direction { In = 1, Out = 2, InOut = In | Out };
Q_DECLARE_FLAGS(Directions, Direction) Q_DECLARE_FLAGS(Directions, Direction)

View File

@ -76,7 +76,7 @@ QVariant JsonConverter::loadFile(QIODevice *f, const Converter *&outputConverter
qFatal("Could not parse JSON content: offset %d: %s", qFatal("Could not parse JSON content: offset %d: %s",
error.offset, qPrintable(error.errorString())); error.offset, qPrintable(error.errorString()));
} }
if (outputConverter == null) if (isNull(outputConverter))
return QVariant(); return QVariant();
return doc.toVariant(); return doc.toVariant();
} }

View File

@ -6,7 +6,10 @@
using namespace Qt::StringLiterals; using namespace Qt::StringLiterals;
static NullConverter nullConverter; static NullConverter nullConverter;
Converter *Converter::null = &nullConverter; bool Converter::isNull(const Converter *converter)
{
return converter == &nullConverter;
}
QString NullConverter::name() const QString NullConverter::name() const
{ {