diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2013-10-05 03:20:56 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-12-06 08:44:31 +0100 |
commit | 03affacaa3d5530e96f869a8a6e900ca5e714918 (patch) | |
tree | 3e060f7b8bac0edd9f210c77d35d49925a868a96 /src/corelib/tools/qcommandlineoption.cpp | |
parent | 689152e7c1c6b0e851937a20de96c99aa7a7ea2b (diff) |
QCommandLineParser: pluck some low-hanging fruit re: exception safety
Make
QCommandLineParser::add{Help,Version}Option()
QCommandLineOption::setDefaultValue()
QCommandLineOptionPrivate::setNames()
have transaction semantics: either they succeed, or they change nothing.
It's trivial to provide this guarantee, so do it.
Add a test for the surprising property that setDefaultValue("") resets
defaultValues() to an empty QStringList instead of one that contains
the empty string.
Change-Id: I61623019de3c7d2e52c24f42cc2e23ec5fddc4da
Reviewed-by: David Faure <david.faure@kdab.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qcommandlineoption.cpp')
-rw-r--r-- | src/corelib/tools/qcommandlineoption.cpp | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/corelib/tools/qcommandlineoption.cpp b/src/corelib/tools/qcommandlineoption.cpp index 9827547c56..86f087674b 100644 --- a/src/corelib/tools/qcommandlineoption.cpp +++ b/src/corelib/tools/qcommandlineoption.cpp @@ -199,7 +199,8 @@ QStringList QCommandLineOption::names() const void QCommandLineOptionPrivate::setNames(const QStringList &nameList) { - names.clear(); + QStringList newNames; + newNames.reserve(nameList.size()); if (nameList.isEmpty()) qWarning("QCommandLineOption: Options must have at least one name"); foreach (const QString &name, nameList) { @@ -214,9 +215,11 @@ void QCommandLineOptionPrivate::setNames(const QStringList &nameList) else if (name.contains(QLatin1Char('='))) qWarning("QCommandLineOption: Option names cannot contain a '='"); else - names.append(name); + newNames.append(name); } } + // commit + names.swap(newNames); } /*! @@ -288,9 +291,13 @@ QString QCommandLineOption::description() const */ void QCommandLineOption::setDefaultValue(const QString &defaultValue) { - d->defaultValues.clear(); - if (!defaultValue.isEmpty()) - d->defaultValues << defaultValue; + QStringList newDefaultValues; + if (!defaultValue.isEmpty()) { + newDefaultValues.reserve(1); + newDefaultValues << defaultValue; + } + // commit: + d->defaultValues.swap(newDefaultValues); } /*! |