DNS Lookup: Use std::optional instead of casting enum to int

Task-number: QTBUG-108873
Pick-to: 6.5
Change-Id: I0bd5dc004154c1c4026be2feb6187c53e5e77801
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Mårten Nordheim 2023-02-27 17:36:01 +01:00
parent 5e16397a2c
commit b300c6b777

View File

@ -13,7 +13,7 @@
#include <stdio.h> #include <stdio.h>
static int typeFromParameter(QStringView type) static std::optional<QDnsLookup::Type> typeFromParameter(QStringView type)
{ {
if (type.compare(u"a", Qt::CaseInsensitive) == 0) if (type.compare(u"a", Qt::CaseInsensitive) == 0)
return QDnsLookup::A; return QDnsLookup::A;
@ -33,7 +33,7 @@ static int typeFromParameter(QStringView type)
return QDnsLookup::SRV; return QDnsLookup::SRV;
if (type.compare(u"txt", Qt::CaseInsensitive) == 0) if (type.compare(u"txt", Qt::CaseInsensitive) == 0)
return QDnsLookup::TXT; return QDnsLookup::TXT;
return -1; return std::nullopt;
} }
//! [0] //! [0]
@ -79,12 +79,12 @@ CommandLineParseResult parseCommandLine(QCommandLineParser &parser, DnsQuery *qu
if (parser.isSet(typeOption)) { if (parser.isSet(typeOption)) {
const QString typeParameter = parser.value(typeOption); const QString typeParameter = parser.value(typeOption);
const int type = typeFromParameter(typeParameter); if (std::optional<QDnsLookup::Type> type = typeFromParameter(typeParameter)) {
if (type < 0) { query->type = *type;
} else {
*errorMessage = "Bad record type: " + typeParameter; *errorMessage = "Bad record type: " + typeParameter;
return CommandLineError; return CommandLineError;
} }
query->type = static_cast<QDnsLookup::Type>(type);
} }
const QStringList positionalArguments = parser.positionalArguments(); const QStringList positionalArguments = parser.positionalArguments();