Use C-strings where std::string is unneccessary

This commit is contained in:
Martchus 2016-06-11 19:41:40 +02:00
parent db7b02b1db
commit 4f87cc1181
3 changed files with 47 additions and 89 deletions

View File

@ -67,35 +67,6 @@ Argument::Argument(const char *name, const char *abbreviation, const char *descr
Argument::~Argument() Argument::~Argument()
{} {}
///*!
// * \brief Checks whether the argument is ambigious.
// * \returns Returns zero if the argument is not ambigious. Returns 1 if the name
// * is ambiguous and 2 if the abbreviation is ambiguous.
// */
//unsigned char Argument::isAmbiguous(const ArgumentParser &parser) const
//{
// function<unsigned char (const ArgumentVector &args, const Argument *toCheck)> check;
// check = [&check] (const ArgumentVector &args, const Argument *toCheck) -> unsigned char {
// for(const auto *arg : args) {
// if(arg != toCheck) {
// if(!toCheck->name().empty() && arg->name() == toCheck->name()) {
// return 1;
// } else if(!toCheck->abbreviation().empty() && arg->abbreviation() == toCheck->abbreviation()) {
// return 2;
// }
// }
// if(auto res = check(arg->parents(), toCheck)) {
// return res;
// }
// }
// return 0;
// };
// if(auto res = check(parser.mainArguments(), this)) {
// return res;
// }
// return check(parents(), this);
//}
/*! /*!
* \brief Appends the name, the abbreviation and the description of the Argument to the give ostream. * \brief Appends the name, the abbreviation and the description of the Argument to the give ostream.
*/ */
@ -142,7 +113,7 @@ void Argument::printInfo(ostream &os, unsigned char indentionLevel) const
os << endl << "Usage: " << example(); os << endl << "Usage: " << example();
} }
os << endl; os << endl;
for(const auto *arg : secondaryArguments()) { for(const auto *arg : subArguments()) {
arg->printInfo(os, indentionLevel + 1); arg->printInfo(os, indentionLevel + 1);
} }
} }
@ -176,17 +147,17 @@ Argument *firstPresentUncombinableArg(const ArgumentVector &args, const Argument
* \sa addSecondaryArgument() * \sa addSecondaryArgument()
* \sa hasSecondaryArguments() * \sa hasSecondaryArguments()
*/ */
void Argument::setSecondaryArguments(const ArgumentInitializerList &secondaryArguments) void Argument::setSubArguments(const ArgumentInitializerList &secondaryArguments)
{ {
// remove this argument from the parents list of the previous secondary arguments // remove this argument from the parents list of the previous secondary arguments
for(Argument *arg : m_secondaryArgs) { for(Argument *arg : m_subArgs) {
arg->m_parents.erase(remove(arg->m_parents.begin(), arg->m_parents.end(), this), arg->m_parents.end()); arg->m_parents.erase(remove(arg->m_parents.begin(), arg->m_parents.end(), this), arg->m_parents.end());
} }
// assign secondary arguments // assign secondary arguments
m_secondaryArgs.assign(secondaryArguments); m_subArgs.assign(secondaryArguments);
// add this argument to the parents list of the assigned secondary arguments // add this argument to the parents list of the assigned secondary arguments
// and set the parser // and set the parser
for(Argument *arg : m_secondaryArgs) { for(Argument *arg : m_subArgs) {
if(find(arg->m_parents.cbegin(), arg->m_parents.cend(), this) == arg->m_parents.cend()) { if(find(arg->m_parents.cbegin(), arg->m_parents.cend(), this) == arg->m_parents.cend()) {
arg->m_parents.push_back(this); arg->m_parents.push_back(this);
} }
@ -200,10 +171,10 @@ void Argument::setSecondaryArguments(const ArgumentInitializerList &secondaryArg
* \sa setSecondaryArguments() * \sa setSecondaryArguments()
* \sa hasSecondaryArguments() * \sa hasSecondaryArguments()
*/ */
void Argument::addSecondaryArgument(Argument *arg) void Argument::addSubArgument(Argument *arg)
{ {
if(find(m_secondaryArgs.cbegin(), m_secondaryArgs.cend(), arg) == m_secondaryArgs.cend()) { if(find(m_subArgs.cbegin(), m_subArgs.cend(), arg) == m_subArgs.cend()) {
m_secondaryArgs.push_back(arg); m_subArgs.push_back(arg);
if(find(arg->m_parents.cbegin(), arg->m_parents.cend(), this) == arg->m_parents.cend()) { if(find(arg->m_parents.cbegin(), arg->m_parents.cend(), this) == arg->m_parents.cend()) {
arg->m_parents.push_back(this); arg->m_parents.push_back(this);
} }
@ -253,7 +224,7 @@ Argument *Argument::conflictsWithArgument() const
{ {
if(!isCombinable() && isPresent()) { if(!isCombinable() && isPresent()) {
for(Argument *parent : m_parents) { for(Argument *parent : m_parents) {
for(Argument *sibling : parent->secondaryArguments()) { for(Argument *sibling : parent->subArguments()) {
if(sibling != this && sibling->isPresent() && !sibling->isCombinable()) { if(sibling != this && sibling->isPresent() && !sibling->isCombinable()) {
return sibling; return sibling;
} }
@ -360,7 +331,7 @@ Argument *ArgumentParser::findArg(const ArgumentVector &arguments, const Argumen
for(Argument *arg : arguments) { for(Argument *arg : arguments) {
if(predicate(arg)) { if(predicate(arg)) {
return arg; // argument matches return arg; // argument matches
} else if(Argument *subarg = findArg(arg->secondaryArguments(), predicate)) { } else if(Argument *subarg = findArg(arg->subArguments(), predicate)) {
return subarg; // a secondary argument matches return subarg; // a secondary argument matches
} }
} }
@ -418,7 +389,7 @@ void ArgumentParser::verifySetup() const
names.push_back(arg->name()); names.push_back(arg->name());
} }
verifiedArgs.push_back(arg); verifiedArgs.push_back(arg);
checkArguments(arg->secondaryArguments()); checkArguments(arg->subArguments());
} }
}; };
checkArguments(m_mainArgs); checkArguments(m_mainArgs);
@ -456,7 +427,7 @@ void ArgumentParser::parseArgs(int argc, char *argv[])
foreachArg = [&foreachArg] (Argument *parent, const ArgumentVector &args, const function<void (Argument *, Argument *)> &proc) { foreachArg = [&foreachArg] (Argument *parent, const ArgumentVector &args, const function<void (Argument *, Argument *)> &proc) {
for(Argument *arg : args) { for(Argument *arg : args) {
proc(parent, arg); proc(parent, arg);
foreachArg(arg, arg->secondaryArguments(), proc); foreachArg(arg, arg->subArguments(), proc);
} }
}; };
// parse given arguments // parse given arguments
@ -591,7 +562,7 @@ void ArgumentParser::parseArgs(int argc, char *argv[])
// the argument might be flagged as present if its a default argument // the argument might be flagged as present if its a default argument
if(arg->isDefault() && (arg->isMainArgument() || (parent && parent->isPresent()))) { if(arg->isDefault() && (arg->isMainArgument() || (parent && parent->isPresent()))) {
arg->m_present = true; arg->m_present = true;
if(firstPresentUncombinableArg(arg->isMainArgument() ? m_mainArgs : parent->secondaryArguments(), arg)) { if(firstPresentUncombinableArg(arg->isMainArgument() ? m_mainArgs : parent->subArguments(), arg)) {
arg->m_present = false; arg->m_present = false;
} }
} }
@ -645,7 +616,8 @@ void ArgumentParser::parseArgs(int argc, char *argv[])
// only invoke if its a main argument or the parent is present // only invoke if its a main argument or the parent is present
if(arg->isPresent()) { if(arg->isPresent()) {
if(arg->isDefault() && !arg->values().size()) { if(arg->isDefault() && !arg->values().size()) {
arg->m_callbackFunction(arg->defaultValues()); vector<string> defaultValues(arg->defaultValues().cbegin(), arg->defaultValues().cend());
arg->m_callbackFunction(defaultValues);
} else { } else {
arg->m_callbackFunction(arg->values()); arg->m_callbackFunction(arg->values());
} }
@ -667,7 +639,7 @@ void ArgumentParser::parseArgs(int argc, char *argv[])
HelpArgument::HelpArgument(ArgumentParser &parser) : HelpArgument::HelpArgument(ArgumentParser &parser) :
Argument("help", "h", "shows this information") Argument("help", "h", "shows this information")
{ {
setCallback([&parser] (const StringVector &) { setCallback([&parser] (const std::vector<std::string> &) {
CMD_UTILS_START_CONSOLE; CMD_UTILS_START_CONSOLE;
parser.printHelp(cout); parser.printHelp(cout);
}); });

View File

@ -28,8 +28,6 @@ class ArgumentParser;
typedef std::initializer_list<Argument *> ArgumentInitializerList; typedef std::initializer_list<Argument *> ArgumentInitializerList;
typedef std::vector<Argument *> ArgumentVector; typedef std::vector<Argument *> ArgumentVector;
typedef std::vector<std::string> StringVector;
typedef std::list<std::string> StringList;
typedef std::function<bool (Argument *)> ArgumentPredicate; typedef std::function<bool (Argument *)> ArgumentPredicate;
Argument LIB_EXPORT *firstPresentUncombinableArg(const ArgumentVector &args, const Argument *except); Argument LIB_EXPORT *firstPresentUncombinableArg(const ArgumentVector &args, const Argument *except);
@ -39,7 +37,7 @@ class LIB_EXPORT Argument
friend class ArgumentParser; friend class ArgumentParser;
public: public:
typedef std::function <void (const StringVector &)> CallbackFunction; typedef std::function <void (const std::vector<std::string> &)> CallbackFunction;
Argument(const char *name, const char *abbreviation = nullptr, const char *description = nullptr, const char *example = nullptr); Argument(const char *name, const char *abbreviation = nullptr, const char *description = nullptr, const char *example = nullptr);
~Argument(); ~Argument();
@ -48,25 +46,23 @@ public:
void setName(const char *name); void setName(const char *name);
const char *abbreviation() const; const char *abbreviation() const;
void setAbbreviation(const char *abbreviation); void setAbbreviation(const char *abbreviation);
//unsigned char isAmbiguous(const ArgumentParser &parser) const;
const char *description() const; const char *description() const;
void setDescription(const char *description); void setDescription(const char *description);
const char *example() const; const char *example() const;
void setExample(const char *example); void setExample(const char *example);
const StringVector &values() const; const std::vector<std::string> &values() const;
const std::string &value(StringVector::size_type index) const; const std::string &value(std::size_t index) const;
StringVector::size_type valueCount() const; std::size_t valueCount() const;
int requiredValueCount() const; int requiredValueCount() const;
void setRequiredValueCount(int requiredValueCount); void setRequiredValueCount(int requiredValueCount);
const StringList &valueNames() const; const std::list<const char *> &valueNames() const;
void setValueNames(std::initializer_list<std::string> valueNames); void setValueNames(std::initializer_list<const char *> valueNames);
void appendValueName(const char *valueName); void appendValueName(const char *valueName);
void appendValueName(const std::string &valueName);
bool allRequiredValuesPresent() const; bool allRequiredValuesPresent() const;
bool isDefault() const; bool isDefault() const;
void setDefault(bool value); void setDefault(bool value);
const StringVector &defaultValues() const; const std::list<const char *> &defaultValues() const;
void setDefaultValues(const StringVector &defaultValues); void setDefaultValues(const std::list<const char *> &defaultValues);
bool isPresent() const; bool isPresent() const;
bool isRequired() const; bool isRequired() const;
void setRequired(bool value); void setRequired(bool value);
@ -78,10 +74,10 @@ public:
void setDenotesOperation(bool denotesOperation); void setDenotesOperation(bool denotesOperation);
void setCallback(CallbackFunction callback); void setCallback(CallbackFunction callback);
void printInfo(std::ostream &os, unsigned char indentionLevel = 0) const; void printInfo(std::ostream &os, unsigned char indentionLevel = 0) const;
const ArgumentVector &secondaryArguments() const; const ArgumentVector &subArguments() const;
void setSecondaryArguments(const ArgumentInitializerList &secondaryArguments); void setSubArguments(const ArgumentInitializerList &subArguments);
void addSecondaryArgument(Argument *arg); void addSubArgument(Argument *arg);
bool hasSecondaryArguments() const; bool hasSubArguments() const;
const ArgumentVector parents() const; const ArgumentVector parents() const;
bool isMainArgument() const; bool isMainArgument() const;
std::string parentNames() const; std::string parentNames() const;
@ -98,12 +94,12 @@ private:
bool m_implicit; bool m_implicit;
bool m_denotesOperation; bool m_denotesOperation;
int m_requiredValueCount; int m_requiredValueCount;
StringList m_valueNames; std::list<const char *> m_valueNames;
bool m_default; bool m_default;
StringVector m_defaultValues; std::list<const char *> m_defaultValues;
bool m_present; bool m_present;
StringVector m_values; std::vector<std::string> m_values;
ArgumentVector m_secondaryArgs; ArgumentVector m_subArgs;
CallbackFunction m_callbackFunction; CallbackFunction m_callbackFunction;
ArgumentVector m_parents; ArgumentVector m_parents;
bool m_isMainArg; bool m_isMainArg;
@ -227,7 +223,7 @@ inline void Argument::setExample(const char *example)
* *
* These values set by the parser when parsing the command line arguments. * These values set by the parser when parsing the command line arguments.
*/ */
inline const StringVector &Argument::values() const inline const std::vector<std::string> &Argument::values() const
{ {
return m_values; return m_values;
} }
@ -237,7 +233,7 @@ inline const StringVector &Argument::values() const
* *
* These values set by the parser when parsing the command line arguments. * These values set by the parser when parsing the command line arguments.
*/ */
inline const std::string &Argument::value(StringVector::size_type index) const inline const std::string &Argument::value(std::size_t index) const
{ {
return m_values[index]; return m_values[index];
} }
@ -246,7 +242,7 @@ inline const std::string &Argument::value(StringVector::size_type index) const
* Returns the number of values which could be found when parsing * Returns the number of values which could be found when parsing
* the command line arguments. * the command line arguments.
*/ */
inline StringVector::size_type Argument::valueCount() const inline std::size_t Argument::valueCount() const
{ {
return m_values.size(); return m_values.size();
} }
@ -293,7 +289,7 @@ inline void Argument::setRequiredValueCount(int requiredValueCount)
* \sa setValueNames() * \sa setValueNames()
* \sa appendValueNames() * \sa appendValueNames()
*/ */
inline const StringList &Argument::valueNames() const inline const std::list<const char *> &Argument::valueNames() const
{ {
return m_valueNames; return m_valueNames;
} }
@ -311,7 +307,7 @@ inline const StringList &Argument::valueNames() const
* \sa valueNames() * \sa valueNames()
* \sa requiredValueCount() * \sa requiredValueCount()
*/ */
inline void Argument::setValueNames(std::initializer_list<std::string> valueNames) inline void Argument::setValueNames(std::initializer_list<const char *> valueNames)
{ {
m_valueNames.assign(valueNames); m_valueNames.assign(valueNames);
} }
@ -319,24 +315,14 @@ inline void Argument::setValueNames(std::initializer_list<std::string> valueName
/*! /*!
* \brief Appends a value name. The value names names will be shown * \brief Appends a value name. The value names names will be shown
* when printing information about the argument. * when printing information about the argument.
* \sa setValueNames()
* \sa valueNames()
*/ */
inline void Argument::appendValueName(const char *valueName) inline void Argument::appendValueName(const char *valueName)
{ {
m_valueNames.emplace_back(valueName); m_valueNames.emplace_back(valueName);
} }
/*!
* \brief Appends a value name. The value names names will be shown
* when printing information about the argument.
*
* \sa setValueNames()
* \sa valueNames()
*/
inline void Argument::appendValueName(const std::string &valueName)
{
m_valueNames.push_back(valueName);
}
/*! /*!
* \brief Returns an indication whether all required values are present. * \brief Returns an indication whether all required values are present.
*/ */
@ -384,7 +370,7 @@ inline void Argument::setDefault(bool value)
* \sa setDefault() * \sa setDefault()
* \sa setDefaultValues() * \sa setDefaultValues()
*/ */
inline const StringVector &Argument::defaultValues() const inline const std::list<const char *> &Argument::defaultValues() const
{ {
return m_defaultValues; return m_defaultValues;
} }
@ -399,7 +385,7 @@ inline const StringVector &Argument::defaultValues() const
* \sa setDefault() * \sa setDefault()
* \sa defaultValues() * \sa defaultValues()
*/ */
inline void Argument::setDefaultValues(const StringVector &defaultValues) inline void Argument::setDefaultValues(const std::list<const char *> &defaultValues)
{ {
m_defaultValues = defaultValues; m_defaultValues = defaultValues;
} }
@ -528,9 +514,9 @@ inline void Argument::setCallback(Argument::CallbackFunction callback)
* \sa setSecondaryArguments() * \sa setSecondaryArguments()
* \sa hasSecondaryArguments() * \sa hasSecondaryArguments()
*/ */
inline const ArgumentVector &Argument::secondaryArguments() const inline const ArgumentVector &Argument::subArguments() const
{ {
return m_secondaryArgs; return m_subArgs;
} }
/*! /*!
@ -539,9 +525,9 @@ inline const ArgumentVector &Argument::secondaryArguments() const
* \sa secondaryArguments() * \sa secondaryArguments()
* \sa setSecondaryArguments() * \sa setSecondaryArguments()
*/ */
inline bool Argument::hasSecondaryArguments() const inline bool Argument::hasSubArguments() const
{ {
return !m_secondaryArgs.empty(); return !m_subArgs.empty();
} }
/*! /*!

View File

@ -18,7 +18,7 @@ public:
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
std::string workingCopyPath(const std::string &name) const; std::string workingCopyPath(const std::string &name) const;
#endif #endif
const ApplicationUtilities::StringVector &units() const; const std::vector<std::string> &units() const;
static const TestApplication *instance(); static const TestApplication *instance();
private: private:
@ -56,7 +56,7 @@ inline const TestApplication *TestApplication::instance()
/*! /*!
* \brief Returns the specified test units. * \brief Returns the specified test units.
*/ */
inline const ApplicationUtilities::StringVector &TestApplication::units() const inline const std::vector<std::string> &TestApplication::units() const
{ {
return m_unitsArg.values(); return m_unitsArg.values();
} }