QCommandLineOption: optimize ctors
...by moving common code into the Private ctor, and catering for C++11 move semantics a bit. Saves ~1.5KiB in text size on Linux GCC 4.9 C++11 release builds. Change-Id: I52ed7e47f76b69500a871844c0920e27fe51a127 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
8133b80e63
commit
fedb442db3
@ -41,11 +41,17 @@ QT_BEGIN_NAMESPACE
|
||||
class QCommandLineOptionPrivate : public QSharedData
|
||||
{
|
||||
public:
|
||||
inline QCommandLineOptionPrivate()
|
||||
: hidden(false)
|
||||
explicit QCommandLineOptionPrivate(const QString &name)
|
||||
: names(removeInvalidNames(QStringList(name))),
|
||||
hidden(false)
|
||||
{ }
|
||||
|
||||
void setNames(const QStringList &nameList);
|
||||
explicit QCommandLineOptionPrivate(const QStringList &names)
|
||||
: names(removeInvalidNames(names)),
|
||||
hidden(false)
|
||||
{ }
|
||||
|
||||
static QStringList removeInvalidNames(QStringList nameList);
|
||||
|
||||
//! The list of names used for this option.
|
||||
QStringList names;
|
||||
@ -102,9 +108,8 @@ public:
|
||||
\sa setDescription(), setValueName(), setDefaultValues()
|
||||
*/
|
||||
QCommandLineOption::QCommandLineOption(const QString &name)
|
||||
: d(new QCommandLineOptionPrivate)
|
||||
: d(new QCommandLineOptionPrivate(name))
|
||||
{
|
||||
d->setNames(QStringList(name));
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -121,9 +126,8 @@ QCommandLineOption::QCommandLineOption(const QString &name)
|
||||
\sa setDescription(), setValueName(), setDefaultValues()
|
||||
*/
|
||||
QCommandLineOption::QCommandLineOption(const QStringList &names)
|
||||
: d(new QCommandLineOptionPrivate)
|
||||
: d(new QCommandLineOptionPrivate(names))
|
||||
{
|
||||
d->setNames(names);
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -152,9 +156,8 @@ QCommandLineOption::QCommandLineOption(const QStringList &names)
|
||||
QCommandLineOption::QCommandLineOption(const QString &name, const QString &description,
|
||||
const QString &valueName,
|
||||
const QString &defaultValue)
|
||||
: d(new QCommandLineOptionPrivate)
|
||||
: d(new QCommandLineOptionPrivate(name))
|
||||
{
|
||||
d->setNames(QStringList(name));
|
||||
setValueName(valueName);
|
||||
setDescription(description);
|
||||
setDefaultValue(defaultValue);
|
||||
@ -189,9 +192,8 @@ QCommandLineOption::QCommandLineOption(const QString &name, const QString &descr
|
||||
QCommandLineOption::QCommandLineOption(const QStringList &names, const QString &description,
|
||||
const QString &valueName,
|
||||
const QString &defaultValue)
|
||||
: d(new QCommandLineOptionPrivate)
|
||||
: d(new QCommandLineOptionPrivate(names))
|
||||
{
|
||||
d->setNames(names);
|
||||
setValueName(valueName);
|
||||
setDescription(description);
|
||||
setDefaultValue(defaultValue);
|
||||
@ -240,29 +242,44 @@ QStringList QCommandLineOption::names() const
|
||||
return d->names;
|
||||
}
|
||||
|
||||
void QCommandLineOptionPrivate::setNames(const QStringList &nameList)
|
||||
namespace {
|
||||
struct IsInvalidName
|
||||
{
|
||||
typedef bool result_type;
|
||||
typedef QString argument_type;
|
||||
|
||||
result_type operator()(const QString &name) const Q_DECL_NOTHROW
|
||||
{
|
||||
if (name.isEmpty()) {
|
||||
qWarning("QCommandLineOption: Option names cannot be empty");
|
||||
return true;
|
||||
} else {
|
||||
const QChar c = name.at(0);
|
||||
if (c == QLatin1Char('-')) {
|
||||
qWarning("QCommandLineOption: Option names cannot start with a '-'");
|
||||
return true;
|
||||
} else if (c == QLatin1Char('/')) {
|
||||
qWarning("QCommandLineOption: Option names cannot start with a '/'");
|
||||
return true;
|
||||
} else if (name.contains(QLatin1Char('='))) {
|
||||
qWarning("QCommandLineOption: Option names cannot contain a '='");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
} // unnamed namespace
|
||||
|
||||
// static
|
||||
QStringList QCommandLineOptionPrivate::removeInvalidNames(QStringList nameList)
|
||||
{
|
||||
QStringList newNames;
|
||||
newNames.reserve(nameList.size());
|
||||
if (nameList.isEmpty())
|
||||
qWarning("QCommandLineOption: Options must have at least one name");
|
||||
foreach (const QString &name, nameList) {
|
||||
if (name.isEmpty()) {
|
||||
qWarning("QCommandLineOption: Option names cannot be empty");
|
||||
} else {
|
||||
const QChar c = name.at(0);
|
||||
if (c == QLatin1Char('-'))
|
||||
qWarning("QCommandLineOption: Option names cannot start with a '-'");
|
||||
else if (c == QLatin1Char('/'))
|
||||
qWarning("QCommandLineOption: Option names cannot start with a '/'");
|
||||
else if (name.contains(QLatin1Char('=')))
|
||||
qWarning("QCommandLineOption: Option names cannot contain a '='");
|
||||
else
|
||||
newNames.append(name);
|
||||
}
|
||||
}
|
||||
// commit
|
||||
names.swap(newNames);
|
||||
else
|
||||
nameList.erase(std::remove_if(nameList.begin(), nameList.end(), IsInvalidName()),
|
||||
nameList.end());
|
||||
return qMove(nameList);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -133,7 +133,7 @@ void tst_QCommandLineParser::testBooleanOption()
|
||||
QFETCH(bool, expectedIsSet);
|
||||
QCoreApplication app(empty_argc, empty_argv);
|
||||
QCommandLineParser parser;
|
||||
QVERIFY(parser.addOption(QCommandLineOption(QStringLiteral("b"), QStringLiteral("a boolean option"))));
|
||||
QVERIFY(parser.addOption(QCommandLineOption(QStringLiteral("b"))));
|
||||
QVERIFY(parser.parse(args));
|
||||
QCOMPARE(parser.optionNames(), expectedOptionNames);
|
||||
QCOMPARE(parser.isSet("b"), expectedIsSet);
|
||||
|
Loading…
Reference in New Issue
Block a user