Bitcoin ABC  0.22.13
P2P Digital Currency
settings.h
Go to the documentation of this file.
1 // Copyright (c) 2019 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_UTIL_SETTINGS_H
6 #define BITCOIN_UTIL_SETTINGS_H
7 
8 #include <fs.h>
9 
10 #include <map>
11 #include <string>
12 #include <vector>
13 
14 class UniValue;
15 
16 namespace util {
17 
28 
31 struct Settings {
33  std::map<std::string, SettingsValue> forced_settings;
35  std::map<std::string, std::vector<SettingsValue>> command_line_options;
37  std::map<std::string, SettingsValue> rw_settings;
40  std::map<std::string, std::map<std::string, std::vector<SettingsValue>>>
42 };
43 
45 bool ReadSettings(const fs::path &path,
46  std::map<std::string, SettingsValue> &values,
47  std::vector<std::string> &errors);
48 
50 bool WriteSettings(const fs::path &path,
51  const std::map<std::string, SettingsValue> &values,
52  std::vector<std::string> &errors);
53 
62 SettingsValue GetSetting(const Settings &settings, const std::string &section,
63  const std::string &name,
64  bool ignore_default_section_config,
65  bool get_chain_name);
66 
69 std::vector<SettingsValue> GetSettingsList(const Settings &settings,
70  const std::string &section,
71  const std::string &name,
72  bool ignore_default_section_config);
73 
79 bool OnlyHasDefaultSectionSetting(const Settings &settings,
80  const std::string &section,
81  const std::string &name);
82 
86 struct SettingsSpan {
87  explicit SettingsSpan() = default;
88  explicit SettingsSpan(const SettingsValue &value) noexcept
89  : SettingsSpan(&value, 1) {}
90  explicit SettingsSpan(const SettingsValue *dataIn, size_t sizeIn) noexcept
91  : data(dataIn), size(sizeIn) {}
92  explicit SettingsSpan(const std::vector<SettingsValue> &vec) noexcept;
94  const SettingsValue *begin() const;
96  const SettingsValue *end() const;
98  bool empty() const;
100  bool last_negated() const;
102  size_t negated() const;
103 
104  const SettingsValue *data = nullptr;
105  size_t size = 0;
106 };
107 
109 template <typename Map, typename Key>
110 auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key)) {
111  auto it = map.find(key);
112  return it == map.end() ? nullptr : &it->second;
113 }
114 
115 } // namespace util
116 
117 #endif // BITCOIN_UTIL_SETTINGS_H
std::map< std::string, SettingsValue > rw_settings
Map of setting name to read-write file setting value.
Definition: settings.h:37
Stored settings.
Definition: settings.h:31
bool OnlyHasDefaultSectionSetting(const Settings &settings, const std::string &section, const std::string &name)
Return true if a setting is set in the default config file section, and not overridden by a higher pr...
Definition: settings.cpp:243
Definition: httprpc.h:13
SettingsValue GetSetting(const Settings &settings, const std::string &section, const std::string &name, bool ignore_default_section_config, bool get_chain_name)
Get settings value from combined sources: forced settings, command line arguments, runtime read-write settings, and the read-only config file.
Definition: settings.cpp:130
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
Definition: settings.h:110
std::vector< SettingsValue > GetSettingsList(const Settings &settings, const std::string &section, const std::string &name, bool ignore_default_section_config)
Get combined setting value similar to GetSetting(), except if setting was specified multiple times...
Definition: settings.cpp:191
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
Definition: settings.cpp:110
const char * name
Definition: rest.cpp:43
std::map< std::string, std::vector< SettingsValue > > command_line_options
Map of setting name to list of command line values.
Definition: settings.h:35
Accessor for list of settings that skips negated values when iterated over.
Definition: settings.h:86
std::map< std::string, std::map< std::string, std::vector< SettingsValue > > > ro_config
Map of config section name and setting name to list of config file values.
Definition: settings.h:41
SettingsSpan(const SettingsValue &value) noexcept
Definition: settings.h:88
SettingsSpan(const SettingsValue *dataIn, size_t sizeIn) noexcept
Definition: settings.h:90
std::map< std::string, SettingsValue > forced_settings
Map of setting name to forced setting value.
Definition: settings.h:33
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
Definition: settings.cpp:61