Bitcoin Core  27.99.0
P2P Digital Currency
settings.h
Go to the documentation of this file.
1 // Copyright (c) 2019-2022 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_COMMON_SETTINGS_H
6 #define BITCOIN_COMMON_SETTINGS_H
7 
8 #include <util/fs.h>
9 
10 #include <cstddef>
11 #include <map>
12 #include <string>
13 #include <vector>
14 
15 class UniValue;
16 
17 namespace common {
18 
29 
32 struct Settings {
34  std::map<std::string, SettingsValue> forced_settings;
36  std::map<std::string, std::vector<SettingsValue>> command_line_options;
38  std::map<std::string, SettingsValue> rw_settings;
40  std::map<std::string, std::map<std::string, std::vector<SettingsValue>>> ro_config;
41 };
42 
44 bool ReadSettings(const fs::path& path,
45  std::map<std::string, SettingsValue>& values,
46  std::vector<std::string>& errors);
47 
49 bool WriteSettings(const fs::path& path,
50  const std::map<std::string, SettingsValue>& values,
51  std::vector<std::string>& errors);
52 
66 SettingsValue GetSetting(const Settings& settings,
67  const std::string& section,
68  const std::string& name,
69  bool ignore_default_section_config,
70  bool ignore_nonpersistent,
71  bool get_chain_type);
72 
75 std::vector<SettingsValue> GetSettingsList(const Settings& settings,
76  const std::string& section,
77  const std::string& name,
78  bool ignore_default_section_config);
79 
85 bool OnlyHasDefaultSectionSetting(const Settings& settings, const std::string& section, const std::string& name);
86 
90 struct SettingsSpan {
91  explicit SettingsSpan() = default;
92  explicit SettingsSpan(const SettingsValue& value) noexcept : SettingsSpan(&value, 1) {}
93  explicit SettingsSpan(const SettingsValue* data, size_t size) noexcept : data(data), size(size) {}
94  explicit SettingsSpan(const std::vector<SettingsValue>& vec) noexcept;
95  const SettingsValue* begin() const;
96  const SettingsValue* end() const;
97  bool empty() const;
98  bool last_negated() const;
99  size_t negated() const;
100 
101  const SettingsValue* data = nullptr;
102  size_t size = 0;
103 };
104 
106 template <typename Map, typename Key>
107 auto FindKey(Map&& map, Key&& key) -> decltype(&map.at(key))
108 {
109  auto it = map.find(key);
110  return it == map.end() ? nullptr : &it->second;
111 }
112 
113 } // namespace common
114 
115 #endif // BITCOIN_COMMON_SETTINGS_H
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
Definition: args.cpp:833
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
Definition: settings.cpp:125
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
Definition: settings.cpp:74
SettingsValue GetSetting(const Settings &settings, const std::string &section, const std::string &name, bool ignore_default_section_config, bool ignore_nonpersistent, bool get_chain_type)
Get settings value from combined sources: forced settings, command line arguments,...
Definition: settings.cpp:148
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
Definition: settings.h:107
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:205
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:250
const char * name
Definition: rest.cpp:50
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Stored settings.
Definition: settings.h:32
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:40
std::map< std::string, SettingsValue > forced_settings
Map of setting name to forced setting value.
Definition: settings.h:34
std::map< std::string, SettingsValue > rw_settings
Map of setting name to read-write file setting value.
Definition: settings.h:38
std::map< std::string, std::vector< SettingsValue > > command_line_options
Map of setting name to list of command line values.
Definition: settings.h:36
Accessor for list of settings that skips negated values when iterated over.
Definition: settings.h:90
bool last_negated() const
True if the last value is negated.
Definition: settings.cpp:269
const SettingsValue * begin() const
Pointer to first non-negated value.
Definition: settings.cpp:266
const SettingsValue * end() const
Pointer to end of values.
Definition: settings.cpp:267
SettingsSpan(const SettingsValue *data, size_t size) noexcept
Definition: settings.h:93
bool empty() const
True if there are any non-negated values.
Definition: settings.cpp:268
SettingsSpan(const SettingsValue &value) noexcept
Definition: settings.h:92
size_t negated() const
Number of negated values.
Definition: settings.cpp:270
const SettingsValue * data
Definition: settings.h:101