Simplify get method

This commit is contained in:
Stephan van Veen 2019-05-12 15:21:11 +02:00
parent 84adf5b2df
commit 15f45c2054

View File

@ -150,7 +150,7 @@ public:
template <typename T> template <typename T>
typename std::enable_if<is_specialization<T, std::vector>::value, bool>::type typename std::enable_if<is_specialization<T, std::vector>::value, bool>::type
operator==(const T& aRhs) const { operator==(const T& aRhs) const {
T tLhs = get_vector<T>(); T tLhs = get<T>();
if (tLhs.size() != aRhs.size()) if (tLhs.size() != aRhs.size())
return false; return false;
else { else {
@ -167,7 +167,7 @@ public:
template <typename T> template <typename T>
typename std::enable_if<is_specialization<T, std::list>::value, bool>::type typename std::enable_if<is_specialization<T, std::list>::value, bool>::type
operator==(const T& aRhs) const { operator==(const T& aRhs) const {
T tLhs = get_list<T>(); T tLhs = get<T>();
if (tLhs.size() != aRhs.size()) if (tLhs.size() != aRhs.size())
return false; return false;
else { else {
@ -188,7 +188,9 @@ public:
// Getter for template types other than std::vector and std::list // Getter for template types other than std::vector and std::list
template <typename T> template <typename T>
T get() const { typename std::enable_if<!is_specialization<T, std::vector>::value &&
!is_specialization<T, std::list>::value, T>::type
get() const {
if (mValues.empty()) { if (mValues.empty()) {
if (mDefaultValue.has_value()) { if (mDefaultValue.has_value()) {
return std::any_cast<T>(mDefaultValue); return std::any_cast<T>(mDefaultValue);
@ -210,7 +212,8 @@ public:
// Getter for std::vector. Here T = std::vector<...> // Getter for std::vector. Here T = std::vector<...>
template <typename T> template <typename T>
T get_vector() const { typename std::enable_if<is_specialization<T, std::vector>::value, T>::type
get() const {
T tResult; T tResult;
if (mValues.empty()) { if (mValues.empty()) {
if (mDefaultValue.has_value()) { if (mDefaultValue.has_value()) {
@ -246,7 +249,8 @@ public:
// Getter for std::list. Here T = std::list<...> // Getter for std::list. Here T = std::list<...>
template <typename T> template <typename T>
T get_list() const { typename std::enable_if<is_specialization<T, std::list>::value, T>::type
get() const {
T tResult; T tResult;
if (mValues.empty()) { if (mValues.empty()) {
if (mDefaultValue.has_value()) { if (mDefaultValue.has_value()) {
@ -366,9 +370,7 @@ class ArgumentParser {
// Getter enabled for all template types other than std::vector and std::list // Getter enabled for all template types other than std::vector and std::list
template <typename T = std::string> template <typename T = std::string>
typename std::enable_if<!is_specialization<T, std::vector>::value && T get(const std::string& aArgumentName) {
!is_specialization<T, std::list>::value, T>::type
get(const char * aArgumentName) {
auto tIterator = mArgumentMap.find(aArgumentName); auto tIterator = mArgumentMap.find(aArgumentName);
if (tIterator != mArgumentMap.end()) { if (tIterator != mArgumentMap.end()) {
return tIterator->second->get<T>(); return tIterator->second->get<T>();
@ -376,28 +378,6 @@ class ArgumentParser {
return T(); return T();
} }
// Getter enabled for std::vector
template <typename T>
typename std::enable_if<is_specialization<T, std::vector>::value, T>::type
get(const char * aArgumentName) {
auto tIterator = mArgumentMap.find(aArgumentName);
if (tIterator != mArgumentMap.end()) {
return tIterator->second->get_vector<T>();
}
return T();
}
// Getter enabled for std::list
template <typename T>
typename std::enable_if<is_specialization<T, std::list>::value, T>::type
get(const char * aArgumentName) {
auto tIterator = mArgumentMap.find(aArgumentName);
if (tIterator != mArgumentMap.end()) {
return tIterator->second->get_list<T>();
}
return T();
}
// 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
Argument& operator[](const std::string& aArgumentName) { Argument& operator[](const std::string& aArgumentName) {