Skip to content

Commit 3e18552

Browse files
committed
refactor: Get rid of ArgsManagerHelper class
Suggested by John Newbery <[email protected]> bitcoin/bitcoin#15934 (comment) This commit does not change behavior.
1 parent dc0f148 commit 3e18552

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

src/util/system.cpp

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,6 @@ static std::string SettingName(const std::string& arg)
167167
return arg.size() > 0 && arg[0] == '-' ? arg.substr(1) : arg;
168168
}
169169

170-
/** Internal helper functions for ArgsManager */
171-
class ArgsManagerHelper {
172-
public:
173-
/** Determine whether to use config settings in the default section,
174-
* See also comments around ArgsManager::ArgsManager() below. */
175-
static inline bool UseDefaultSection(const ArgsManager& am, const std::string& arg) EXCLUSIVE_LOCKS_REQUIRED(am.cs_args)
176-
{
177-
return (am.m_network == CBaseChainParams::MAIN || am.m_network_only_args.count(arg) == 0);
178-
}
179-
180-
static util::SettingsValue Get(const ArgsManager& am, const std::string& arg)
181-
{
182-
LOCK(am.cs_args);
183-
return GetSetting(am.m_settings, am.m_network, SettingName(arg), !UseDefaultSection(am, arg), /* get_chain_name= */ false);
184-
}
185-
};
186-
187170
/**
188171
* Interpret -nofoo as if the user supplied -foo=0.
189172
*
@@ -370,7 +353,7 @@ Optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) const
370353
std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
371354
{
372355
LOCK(cs_args);
373-
bool ignore_default_section_config = !ArgsManagerHelper::UseDefaultSection(*this, strArg);
356+
bool ignore_default_section_config = !UseDefaultSection(strArg);
374357
std::vector<std::string> result;
375358
for (const util::SettingsValue& value :
376359
util::GetSettingsList(m_settings, m_network, SettingName(strArg), ignore_default_section_config)) {
@@ -381,29 +364,29 @@ std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
381364

382365
bool ArgsManager::IsArgSet(const std::string& strArg) const
383366
{
384-
return !ArgsManagerHelper::Get(*this, strArg).isNull();
367+
return !GetSetting(strArg).isNull();
385368
}
386369

387370
bool ArgsManager::IsArgNegated(const std::string& strArg) const
388371
{
389-
return ArgsManagerHelper::Get(*this, strArg).isFalse();
372+
return GetSetting(strArg).isFalse();
390373
}
391374

392375
std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
393376
{
394-
const util::SettingsValue value = ArgsManagerHelper::Get(*this, strArg);
377+
const util::SettingsValue value = GetSetting(strArg);
395378
return value.isNull() ? strDefault : value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str();
396379
}
397380

398381
int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault) const
399382
{
400-
const util::SettingsValue value = ArgsManagerHelper::Get(*this, strArg);
383+
const util::SettingsValue value = GetSetting(strArg);
401384
return value.isNull() ? nDefault : value.isFalse() ? 0 : value.isTrue() ? 1 : value.isNum() ? value.get_int64() : atoi64(value.get_str());
402385
}
403386

404387
bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
405388
{
406-
const util::SettingsValue value = ArgsManagerHelper::Get(*this, strArg);
389+
const util::SettingsValue value = GetSetting(strArg);
407390
return value.isNull() ? fDefault : value.isBool() ? value.get_bool() : InterpretBool(value.get_str());
408391
}
409392

@@ -854,9 +837,9 @@ std::string ArgsManager::GetChainName() const
854837
{
855838
auto get_net = [&](const std::string& arg) {
856839
LOCK(cs_args);
857-
util::SettingsValue value = GetSetting(m_settings, /* section= */ "", SettingName(arg),
858-
/* ignore_default_section_config= */ false,
859-
/* get_chain_name= */ true);
840+
util::SettingsValue value = util::GetSetting(m_settings, /* section= */ "", SettingName(arg),
841+
/* ignore_default_section_config= */ false,
842+
/* get_chain_name= */ true);
860843
return value.isNull() ? false : value.isBool() ? value.get_bool() : InterpretBool(value.get_str());
861844
};
862845

@@ -874,6 +857,18 @@ std::string ArgsManager::GetChainName() const
874857
return GetArg("-chain", CBaseChainParams::MAIN);
875858
}
876859

860+
bool ArgsManager::UseDefaultSection(const std::string& arg) const
861+
{
862+
return m_network == CBaseChainParams::MAIN || m_network_only_args.count(arg) == 0;
863+
}
864+
865+
util::SettingsValue ArgsManager::GetSetting(const std::string& arg) const
866+
{
867+
LOCK(cs_args);
868+
return util::GetSetting(
869+
m_settings, m_network, SettingName(arg), !UseDefaultSection(arg), /* get_chain_name= */ false);
870+
}
871+
877872
bool RenameOver(fs::path src, fs::path dest)
878873
{
879874
#ifdef WIN32

src/util/system.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ class ArgsManager
148148
};
149149

150150
protected:
151-
friend class ArgsManagerHelper;
152-
153151
struct Arg
154152
{
155153
std::string m_help_param;
@@ -166,6 +164,22 @@ class ArgsManager
166164

167165
NODISCARD bool ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys = false);
168166

167+
/**
168+
* Returns true if settings values from the default section should be used,
169+
* depending on the current network and whether the setting is
170+
* network-specific.
171+
*/
172+
bool UseDefaultSection(const std::string& arg) const EXCLUSIVE_LOCKS_REQUIRED(cs_args);
173+
174+
/**
175+
* Get setting value.
176+
*
177+
* Result will be null if setting was unset, true if "-setting" argument was passed
178+
* false if "-nosetting" argument was passed, and a string if a "-setting=value"
179+
* argument was passed.
180+
*/
181+
util::SettingsValue GetSetting(const std::string& arg) const;
182+
169183
public:
170184
ArgsManager();
171185

0 commit comments

Comments
 (0)