Use string_view in getter interface

This commit is contained in:
Zhihao Yuan 2019-11-16 21:58:49 -06:00
parent f7fe9cf439
commit 56c041707a
No known key found for this signature in database
GPG Key ID: A2E474BDAA37E11C

View File

@ -454,19 +454,15 @@ public:
* @throws std::logic_error in case of an invalid argument name * @throws std::logic_error in case of an invalid argument name
* @throws std::logic_error in case of incompatible types * @throws std::logic_error in case of incompatible types
*/ */
template <typename T = std::string> T get(const std::string &aArgumentName) { template <typename T = std::string> T get(std::string_view aArgumentName) {
auto tIterator = mArgumentMap.find(aArgumentName); return (*this)[aArgumentName].get<T>();
if (tIterator != mArgumentMap.end()) {
return tIterator->second->get<T>();
}
throw std::logic_error("No such argument");
} }
/* Indexing operator. Return a reference to an Argument object /* Indexing operator. Return a reference to an Argument object
* Used in conjuction with Argument.operator== e.g., parser["foo"] == true * Used in conjuction with Argument.operator== e.g., parser["foo"] == true
* @throws std::logic_error in case of an invalid argument name * @throws std::logic_error in case of an invalid argument name
*/ */
Argument &operator[](const std::string &aArgumentName) { Argument &operator[](std::string_view aArgumentName) {
auto tIterator = mArgumentMap.find(aArgumentName); auto tIterator = mArgumentMap.find(aArgumentName);
if (tIterator != mArgumentMap.end()) { if (tIterator != mArgumentMap.end()) {
return *(tIterator->second); return *(tIterator->second);