Skip to content

Commit 22e301a

Browse files
author
MarcoFalke
committed
Merge #10901: Fix constness of ArgsManager methods
a622a17 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
2 parents c484ec6 + a622a17 commit 22e301a

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

src/util.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -419,49 +419,48 @@ void ArgsManager::ParseParameters(int argc, const char* const argv[])
419419
}
420420
}
421421

422-
std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg)
422+
std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
423423
{
424424
LOCK(cs_args);
425-
if (IsArgSet(strArg))
426-
return mapMultiArgs.at(strArg);
425+
auto it = mapMultiArgs.find(strArg);
426+
if (it != mapMultiArgs.end()) return it->second;
427427
return {};
428428
}
429429

430-
bool ArgsManager::IsArgSet(const std::string& strArg)
430+
bool ArgsManager::IsArgSet(const std::string& strArg) const
431431
{
432432
LOCK(cs_args);
433433
return mapArgs.count(strArg);
434434
}
435435

436-
std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault)
436+
std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
437437
{
438438
LOCK(cs_args);
439-
if (mapArgs.count(strArg))
440-
return mapArgs[strArg];
439+
auto it = mapArgs.find(strArg);
440+
if (it != mapArgs.end()) return it->second;
441441
return strDefault;
442442
}
443443

444-
int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault)
444+
int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault) const
445445
{
446446
LOCK(cs_args);
447-
if (mapArgs.count(strArg))
448-
return atoi64(mapArgs[strArg]);
447+
auto it = mapArgs.find(strArg);
448+
if (it != mapArgs.end()) return atoi64(it->second);
449449
return nDefault;
450450
}
451451

452-
bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault)
452+
bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
453453
{
454454
LOCK(cs_args);
455-
if (mapArgs.count(strArg))
456-
return InterpretBool(mapArgs[strArg]);
455+
auto it = mapArgs.find(strArg);
456+
if (it != mapArgs.end()) return InterpretBool(it->second);
457457
return fDefault;
458458
}
459459

460460
bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
461461
{
462462
LOCK(cs_args);
463-
if (mapArgs.count(strArg))
464-
return false;
463+
if (IsArgSet(strArg)) return false;
465464
ForceSetArg(strArg, strValue);
466465
return true;
467466
}
@@ -478,8 +477,7 @@ void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strV
478477
{
479478
LOCK(cs_args);
480479
mapArgs[strArg] = strValue;
481-
mapMultiArgs[strArg].clear();
482-
mapMultiArgs[strArg].push_back(strValue);
480+
mapMultiArgs[strArg] = {strValue};
483481
}
484482

485483

src/util.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,28 @@ inline bool IsSwitchChar(char c)
195195
class ArgsManager
196196
{
197197
protected:
198-
CCriticalSection cs_args;
198+
mutable CCriticalSection cs_args;
199199
std::map<std::string, std::string> mapArgs;
200-
std::map<std::string, std::vector<std::string> > mapMultiArgs;
200+
std::map<std::string, std::vector<std::string>> mapMultiArgs;
201201
public:
202202
void ParseParameters(int argc, const char*const argv[]);
203203
void ReadConfigFile(const std::string& confPath);
204-
std::vector<std::string> GetArgs(const std::string& strArg);
204+
205+
/**
206+
* Return a vector of strings of the given argument
207+
*
208+
* @param strArg Argument to get (e.g. "-foo")
209+
* @return command-line arguments
210+
*/
211+
std::vector<std::string> GetArgs(const std::string& strArg) const;
205212

206213
/**
207214
* Return true if the given argument has been manually set
208215
*
209216
* @param strArg Argument to get (e.g. "-foo")
210217
* @return true if the argument has been set
211218
*/
212-
bool IsArgSet(const std::string& strArg);
219+
bool IsArgSet(const std::string& strArg) const;
213220

214221
/**
215222
* Return string argument or default value
@@ -218,7 +225,7 @@ class ArgsManager
218225
* @param strDefault (e.g. "1")
219226
* @return command-line argument or default value
220227
*/
221-
std::string GetArg(const std::string& strArg, const std::string& strDefault);
228+
std::string GetArg(const std::string& strArg, const std::string& strDefault) const;
222229

223230
/**
224231
* Return integer argument or default value
@@ -227,7 +234,7 @@ class ArgsManager
227234
* @param nDefault (e.g. 1)
228235
* @return command-line argument (0 if invalid number) or default value
229236
*/
230-
int64_t GetArg(const std::string& strArg, int64_t nDefault);
237+
int64_t GetArg(const std::string& strArg, int64_t nDefault) const;
231238

232239
/**
233240
* Return boolean argument or default value
@@ -236,7 +243,7 @@ class ArgsManager
236243
* @param fDefault (true or false)
237244
* @return command-line argument or default value
238245
*/
239-
bool GetBoolArg(const std::string& strArg, bool fDefault);
246+
bool GetBoolArg(const std::string& strArg, bool fDefault) const;
240247

241248
/**
242249
* Set an argument if it doesn't already have a value

0 commit comments

Comments
 (0)