Skip to content

Commit

Permalink
Fix constness of ArgsManager methods
Browse files Browse the repository at this point in the history
  • Loading branch information
promag committed Jul 24, 2017
1 parent 0c70e84 commit a622a17
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
32 changes: 15 additions & 17 deletions src/util.cpp
Expand Up @@ -419,49 +419,48 @@ void ArgsManager::ParseParameters(int argc, const char* const argv[])
}
}

std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg)
std::vector<std::string> 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;
}
Expand All @@ -478,8 +477,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};
}


Expand Down
21 changes: 14 additions & 7 deletions src/util.h
Expand Up @@ -195,21 +195,28 @@ inline bool IsSwitchChar(char c)
class ArgsManager
{
protected:
CCriticalSection cs_args;
mutable CCriticalSection cs_args;
std::map<std::string, std::string> mapArgs;
std::map<std::string, std::vector<std::string> > mapMultiArgs;
std::map<std::string, std::vector<std::string>> mapMultiArgs;
public:
void ParseParameters(int argc, const char*const argv[]);
void ReadConfigFile(const std::string& confPath);
std::vector<std::string> 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<std::string> GetArgs(const std::string& strArg) const;

/**
* Return true if the given argument has been manually set
*
* @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
Expand All @@ -218,7 +225,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
Expand All @@ -227,7 +234,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
Expand All @@ -236,7 +243,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
Expand Down

0 comments on commit a622a17

Please sign in to comment.