Bitcoin Core 22.99.0
P2P Digital Currency
settings.h
Go to the documentation of this file.
1// Copyright (c) 2019-2020 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
14class UniValue;
15
16namespace util {
17
28
31struct 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;
39 std::map<std::string, std::map<std::string, std::vector<SettingsValue>>> ro_config;
40};
41
43bool ReadSettings(const fs::path& path,
44 std::map<std::string, SettingsValue>& values,
45 std::vector<std::string>& errors);
46
48bool WriteSettings(const fs::path& path,
49 const std::map<std::string, SettingsValue>& values,
50 std::vector<std::string>& errors);
51
60SettingsValue GetSetting(const Settings& settings,
61 const std::string& section,
62 const std::string& name,
63 bool ignore_default_section_config,
64 bool get_chain_name);
65
68std::vector<SettingsValue> GetSettingsList(const Settings& settings,
69 const std::string& section,
70 const std::string& name,
71 bool ignore_default_section_config);
72
78bool OnlyHasDefaultSectionSetting(const Settings& settings, const std::string& section, const std::string& name);
79
84 explicit SettingsSpan() = default;
85 explicit SettingsSpan(const SettingsValue& value) noexcept : SettingsSpan(&value, 1) {}
86 explicit SettingsSpan(const SettingsValue* data, size_t size) noexcept : data(data), size(size) {}
87 explicit SettingsSpan(const std::vector<SettingsValue>& vec) noexcept;
88 const SettingsValue* begin() const;
89 const SettingsValue* end() const;
90 bool empty() const;
91 bool last_negated() const;
92 size_t negated() const;
93
94 const SettingsValue* data = nullptr;
95 size_t size = 0;
96};
97
99template <typename Map, typename Key>
100auto FindKey(Map&& map, Key&& key) -> decltype(&map.at(key))
101{
102 auto it = map.find(key);
103 return it == map.end() ? nullptr : &it->second;
104}
105
106} // namespace util
107
108#endif // BITCOIN_UTIL_SETTINGS_H
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:34
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:173
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
Definition: settings.cpp:58
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:218
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
Definition: settings.cpp:101
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
Definition: settings.h:100
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,...
Definition: settings.cpp:120
const char * name
Definition: rest.cpp:43
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Stored settings.
Definition: settings.h:31
std::map< std::string, SettingsValue > rw_settings
Map of setting name to read-write file setting value.
Definition: settings.h:37
std::map< std::string, SettingsValue > forced_settings
Map of setting name to forced setting value.
Definition: settings.h:33
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:39
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:83
size_t negated() const
Number of negated values.
Definition: settings.cpp:238
SettingsSpan(const SettingsValue *data, size_t size) noexcept
Definition: settings.h:86
SettingsSpan(const SettingsValue &value) noexcept
Definition: settings.h:85
const SettingsValue * end() const
Pointer to end of values.
Definition: settings.cpp:235
bool empty() const
True if there are any non-negated values.
Definition: settings.cpp:236
SettingsSpan()=default
const SettingsValue * data
Definition: settings.h:94
bool last_negated() const
True if the last value is negated.
Definition: settings.cpp:237
const SettingsValue * begin() const
Pointer to first non-negated value.
Definition: settings.cpp:234