Bitcoin Core 31.99.0
P2P Digital Currency
settings.h
Go to the documentation of this file.
1// Copyright (c) 2019-present 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// Users of this header need to explicitly #include <univalue.h>
16// IWYU pragma: no_include <univalue.h>
17class UniValue; // IWYU pragma: keep
18
19namespace common {
20
31
34struct Settings {
36 std::map<std::string, SettingsValue> forced_settings;
38 std::map<std::string, std::vector<SettingsValue>> command_line_options;
40 std::map<std::string, SettingsValue> rw_settings;
42 std::map<std::string, std::map<std::string, std::vector<SettingsValue>>> ro_config;
43};
44
46bool ReadSettings(const fs::path& path,
47 std::map<std::string, SettingsValue>& values,
48 std::vector<std::string>& errors);
49
51bool WriteSettings(const fs::path& path,
52 const std::map<std::string, SettingsValue>& values,
53 std::vector<std::string>& errors);
54
68SettingsValue GetSetting(const Settings& settings,
69 const std::string& section,
70 const std::string& name,
71 bool ignore_default_section_config,
72 bool ignore_nonpersistent,
73 bool get_chain_type);
74
77std::vector<SettingsValue> GetSettingsList(const Settings& settings,
78 const std::string& section,
79 const std::string& name,
80 bool ignore_default_section_config);
81
87bool OnlyHasDefaultSectionSetting(const Settings& settings, const std::string& section, const std::string& name);
88
93 explicit SettingsSpan() = default;
94 explicit SettingsSpan(const SettingsValue& value) noexcept : SettingsSpan(&value, 1) {}
95 explicit SettingsSpan(const SettingsValue* data, size_t size) noexcept : data(data), size(size) {}
96 explicit SettingsSpan(const std::vector<SettingsValue>& vec) noexcept;
97 const SettingsValue* begin() const;
98 const SettingsValue* end() const;
99 bool empty() const;
100 bool last_negated() const;
101 size_t negated() const;
102
103 const SettingsValue* data = nullptr;
104 size_t size = 0;
105};
106
108template <typename Map, typename Key>
109auto FindKey(Map&& map, Key&& key) -> decltype(&map.at(key))
110{
111 auto it = map.find(key);
112 return it == map.end() ? nullptr : &it->second;
113}
114
115} // namespace common
116
117#endif // BITCOIN_COMMON_SETTINGS_H
Definition: init.cpp:17
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
Definition: settings.cpp:122
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
Definition: settings.cpp:71
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:153
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
Definition: settings.h:109
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:210
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:255
const char * name
Definition: rest.cpp:56
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Stored settings.
Definition: settings.h:34
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:42
std::map< std::string, SettingsValue > forced_settings
Map of setting name to forced setting value.
Definition: settings.h:36
std::map< std::string, SettingsValue > rw_settings
Map of setting name to read-write file setting value.
Definition: settings.h:40
std::map< std::string, std::vector< SettingsValue > > command_line_options
Map of setting name to list of command line values.
Definition: settings.h:38
Accessor for list of settings that skips negated values when iterated over.
Definition: settings.h:92
bool last_negated() const
True if the last value is negated.
Definition: settings.cpp:274
const SettingsValue * begin() const
Pointer to first non-negated value.
Definition: settings.cpp:271
const SettingsValue * end() const
Pointer to end of values.
Definition: settings.cpp:272
SettingsSpan(const SettingsValue *data, size_t size) noexcept
Definition: settings.h:95
bool empty() const
True if there are any non-negated values.
Definition: settings.cpp:273
SettingsSpan(const SettingsValue &value) noexcept
Definition: settings.h:94
size_t negated() const
Number of negated values.
Definition: settings.cpp:275
const SettingsValue * data
Definition: settings.h:103