From 10fab589699127e02fed9fca18d115dc96f8e8c0 Mon Sep 17 00:00:00 2001 From: Stephan van Veen Date: Fri, 10 May 2019 17:06:28 +0200 Subject: [PATCH] Replace upsert method by std::map::insert_or_assign --- include/argparse.hpp | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index 2b56ff5..2877f9d 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -49,22 +49,6 @@ struct is_specialization : std::false_type {}; template class Ref, typename... Args> struct is_specialization, Ref> : std::true_type {}; -// Upsert into std::map -template -bool upsert(std::map& aMap, KeyType const& aKey, ElementType const& aNewValue) { - auto tResult = aMap.insert(typename std::map::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) bool starts_with(const std::string& haystack, const std::string& needle) { return needle.length() <= haystack.length() @@ -291,8 +275,8 @@ class ArgumentParser { tArgument->mDefaultValue = false; tArgument->mImplicitValue = true; mOptionalArguments.push_back(tArgument); - upsert(mArgumentMap, std::string("-h"), tArgument); - upsert(mArgumentMap, std::string("--help"), tArgument); + mArgumentMap.insert_or_assign(std::string("-h"), tArgument); + mArgumentMap.insert_or_assign(std::string("--help"), tArgument); } // Parameter packing @@ -307,7 +291,7 @@ class ArgumentParser { mOptionalArguments.push_back(tArgument); for (auto& mName : tArgument->mNames) { - upsert(mArgumentMap, mName, tArgument); + mArgumentMap.insert_or_assign(mName, tArgument); } return *tArgument; } @@ -325,7 +309,7 @@ class ArgumentParser { } auto tArgumentMap = tParentParser.mArgumentMap; for (auto&[tKey, tValue] : tArgumentMap) { - upsert(mArgumentMap, tKey, tValue); + mArgumentMap.insert_or_assign(tKey, tValue); } } }