Replace upsert method by std::map::insert_or_assign

This commit is contained in:
Stephan van Veen 2019-05-10 17:06:28 +02:00
parent 91cd2477de
commit 10fab58969

View File

@ -49,22 +49,6 @@ struct is_specialization : std::false_type {};
template<template<typename...> class Ref, typename... Args> template<template<typename...> class Ref, typename... Args>
struct is_specialization<Ref<Args...>, Ref> : std::true_type {}; struct is_specialization<Ref<Args...>, Ref> : std::true_type {};
// Upsert into std::map
template <class KeyType, class ElementType>
bool upsert(std::map<KeyType, ElementType>& aMap, KeyType const& aKey, ElementType const& aNewValue) {
auto tResult = aMap.insert(typename std::map<KeyType, ElementType>::value_type(aKey, aNewValue));
if (!tResult.second) {
if (!(tResult.first->second == aNewValue)) {
tResult.first->second = aNewValue;
return true;
}
else
return false; // it was the same
}
else
return true; // changed cause not existing
}
// Check if string (haystack) starts with a substring (needle) // Check if string (haystack) starts with a substring (needle)
bool starts_with(const std::string& haystack, const std::string& needle) { bool starts_with(const std::string& haystack, const std::string& needle) {
return needle.length() <= haystack.length() return needle.length() <= haystack.length()
@ -291,8 +275,8 @@ class ArgumentParser {
tArgument->mDefaultValue = false; tArgument->mDefaultValue = false;
tArgument->mImplicitValue = true; tArgument->mImplicitValue = true;
mOptionalArguments.push_back(tArgument); mOptionalArguments.push_back(tArgument);
upsert(mArgumentMap, std::string("-h"), tArgument); mArgumentMap.insert_or_assign(std::string("-h"), tArgument);
upsert(mArgumentMap, std::string("--help"), tArgument); mArgumentMap.insert_or_assign(std::string("--help"), tArgument);
} }
// Parameter packing // Parameter packing
@ -307,7 +291,7 @@ class ArgumentParser {
mOptionalArguments.push_back(tArgument); mOptionalArguments.push_back(tArgument);
for (auto& mName : tArgument->mNames) { for (auto& mName : tArgument->mNames) {
upsert(mArgumentMap, mName, tArgument); mArgumentMap.insert_or_assign(mName, tArgument);
} }
return *tArgument; return *tArgument;
} }
@ -325,7 +309,7 @@ class ArgumentParser {
} }
auto tArgumentMap = tParentParser.mArgumentMap; auto tArgumentMap = tParentParser.mArgumentMap;
for (auto&[tKey, tValue] : tArgumentMap) { for (auto&[tKey, tValue] : tArgumentMap) {
upsert(mArgumentMap, tKey, tValue); mArgumentMap.insert_or_assign(tKey, tValue);
} }
} }
} }