From 3dc7ebfcded9b469efdeaffa25fccb0d8eb4cb31 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 16 Aug 2017 15:59:26 +0200 Subject: [PATCH] Merge #10901: Fix constness of ArgsManager methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit a622a1768 Fix constness of ArgsManager methods (João Barbosa) Pull request description: Make `cs_args` mutex mutable so that const methods can acquire it. There's also tiny performance improvement by avoiding two map lookups when retrieving an argument value. Tree-SHA512: ece58469745f2743b4b643242b51889a3d9c5b76492ed70bb74d4e5b378fff59da79fc129e499da779bf9f488c9435dda17ad1f3a804c1c30f56af422389e8bd --- src/util.cpp | 32 +++++++++++++++----------------- src/util.h | 21 ++++++++++++++------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index 3677a472dfd28f..d2fbf6b50700c2 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -512,49 +512,48 @@ void ArgsManager::ParseParameters(int argc, const char* const argv[]) } } -std::vector ArgsManager::GetArgs(const std::string& strArg) +std::vector ArgsManager::GetArgs(const std::string& strArg) const { LOCK(cs_args); - if (IsArgSet(strArg)) - return mapMultiArgs.at(strArg); + auto it = mapMultiArgs.find(strArg); + if (it != mapMultiArgs.end()) return it->second; return {}; } -bool ArgsManager::IsArgSet(const std::string& strArg) +bool ArgsManager::IsArgSet(const std::string& strArg) const { LOCK(cs_args); return mapArgs.count(strArg); } -std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) +std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const { LOCK(cs_args); - if (mapArgs.count(strArg)) - return mapArgs[strArg]; + auto it = mapArgs.find(strArg); + if (it != mapArgs.end()) return it->second; return strDefault; } -int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault) +int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault) const { LOCK(cs_args); - if (mapArgs.count(strArg)) - return atoi64(mapArgs[strArg]); + auto it = mapArgs.find(strArg); + if (it != mapArgs.end()) return atoi64(it->second); return nDefault; } -bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) +bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const { LOCK(cs_args); - if (mapArgs.count(strArg)) - return InterpretBool(mapArgs[strArg]); + auto it = mapArgs.find(strArg); + if (it != mapArgs.end()) return InterpretBool(it->second); return fDefault; } bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue) { LOCK(cs_args); - if (mapArgs.count(strArg)) - return false; + if (IsArgSet(strArg)) return false; ForceSetArg(strArg, strValue); return true; } @@ -571,8 +570,7 @@ void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strV { LOCK(cs_args); mapArgs[strArg] = strValue; - mapMultiArgs[strArg].clear(); - mapMultiArgs[strArg].push_back(strValue); + mapMultiArgs[strArg] = {strValue}; } void ArgsManager::ForceSetMultiArgs(const std::string& strArg, const std::vector& values) diff --git a/src/util.h b/src/util.h index 4de8b02080d3f3..9ff425d32005ac 100644 --- a/src/util.h +++ b/src/util.h @@ -248,13 +248,20 @@ inline bool IsSwitchChar(char c) class ArgsManager { protected: - CCriticalSection cs_args; + mutable CCriticalSection cs_args; std::map mapArgs; - std::map > mapMultiArgs; + std::map> mapMultiArgs; public: void ParseParameters(int argc, const char*const argv[]); void ReadConfigFile(const std::string& confPath); - std::vector GetArgs(const std::string& strArg); + + /** + * Return a vector of strings of the given argument + * + * @param strArg Argument to get (e.g. "-foo") + * @return command-line arguments + */ + std::vector GetArgs(const std::string& strArg) const; /** * Return true if the given argument has been manually set @@ -262,7 +269,7 @@ class ArgsManager * @param strArg Argument to get (e.g. "-foo") * @return true if the argument has been set */ - bool IsArgSet(const std::string& strArg); + bool IsArgSet(const std::string& strArg) const; /** * Return string argument or default value @@ -271,7 +278,7 @@ class ArgsManager * @param strDefault (e.g. "1") * @return command-line argument or default value */ - std::string GetArg(const std::string& strArg, const std::string& strDefault); + std::string GetArg(const std::string& strArg, const std::string& strDefault) const; /** * Return integer argument or default value @@ -280,7 +287,7 @@ class ArgsManager * @param nDefault (e.g. 1) * @return command-line argument (0 if invalid number) or default value */ - int64_t GetArg(const std::string& strArg, int64_t nDefault); + int64_t GetArg(const std::string& strArg, int64_t nDefault) const; /** * Return boolean argument or default value @@ -289,7 +296,7 @@ class ArgsManager * @param fDefault (true or false) * @return command-line argument or default value */ - bool GetBoolArg(const std::string& strArg, bool fDefault); + bool GetBoolArg(const std::string& strArg, bool fDefault) const; /** * Set an argument if it doesn't already have a value