LCOV - code coverage report
Current view: top level - src/util - settings.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 7 7 100.0 %
Date: 2020-09-26 01:30:44 Functions: 13 13 100.0 %

          Line data    Source code
       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             : 
      14             : class UniValue;
      15             : 
      16             : namespace util {
      17             : 
      18             : //! Settings value type (string/integer/boolean/null variant).
      19             : //!
      20             : //! @note UniValue is used here for convenience and because it can be easily
      21             : //!       serialized in a readable format. But any other variant type that can
      22             : //!       be assigned strings, int64_t, and bool values and has get_str(),
      23             : //!       get_int64(), get_bool(), isNum(), isBool(), isFalse(), isTrue() and
      24             : //!       isNull() methods can be substituted if there's a need to move away
      25             : //!       from UniValue. (An implementation with boost::variant was posted at
      26             : //!       https://github.com/bitcoin/bitcoin/pull/15934/files#r337691812)
      27             : using SettingsValue = UniValue;
      28             : 
      29             : //! Stored settings. This struct combines settings from the command line, a
      30             : //! read-only configuration file, and a read-write runtime settings file.
      31      116928 : struct Settings {
      32             :     //! Map of setting name to forced setting value.
      33             :     std::map<std::string, SettingsValue> forced_settings;
      34             :     //! Map of setting name to list of command line values.
      35             :     std::map<std::string, std::vector<SettingsValue>> command_line_options;
      36             :     //! Map of setting name to read-write file setting value.
      37             :     std::map<std::string, SettingsValue> rw_settings;
      38             :     //! Map of config section name and setting name to list of config file values.
      39             :     std::map<std::string, std::map<std::string, std::vector<SettingsValue>>> ro_config;
      40             : };
      41             : 
      42             : //! Read settings file.
      43             : bool ReadSettings(const fs::path& path,
      44             :     std::map<std::string, SettingsValue>& values,
      45             :     std::vector<std::string>& errors);
      46             : 
      47             : //! Write settings file.
      48             : bool WriteSettings(const fs::path& path,
      49             :     const std::map<std::string, SettingsValue>& values,
      50             :     std::vector<std::string>& errors);
      51             : 
      52             : //! Get settings value from combined sources: forced settings, command line
      53             : //! arguments, runtime read-write settings, and the read-only config file.
      54             : //!
      55             : //! @param ignore_default_section_config - ignore values in the default section
      56             : //!                                        of the config file (part before any
      57             : //!                                        [section] keywords)
      58             : //! @param get_chain_name - enable special backwards compatible behavior
      59             : //!                         for GetChainName
      60             : SettingsValue 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             : 
      66             : //! Get combined setting value similar to GetSetting(), except if setting was
      67             : //! specified multiple times, return a list of all the values specified.
      68             : std::vector<SettingsValue> GetSettingsList(const Settings& settings,
      69             :     const std::string& section,
      70             :     const std::string& name,
      71             :     bool ignore_default_section_config);
      72             : 
      73             : //! Return true if a setting is set in the default config file section, and not
      74             : //! overridden by a higher priority command-line or network section value.
      75             : //!
      76             : //! This is used to provide user warnings about values that might be getting
      77             : //! ignored unintentionally.
      78             : bool OnlyHasDefaultSectionSetting(const Settings& settings, const std::string& section, const std::string& name);
      79             : 
      80             : //! Accessor for list of settings that skips negated values when iterated over.
      81             : //! The last boolean `false` value in the list and all earlier values are
      82             : //! considered negated.
      83             : struct SettingsSpan {
      84             :     explicit SettingsSpan() = default;
      85       57324 :     explicit SettingsSpan(const SettingsValue& value) noexcept : SettingsSpan(&value, 1) {}
      86      801514 :     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; //!< Pointer to first non-negated value.
      89             :     const SettingsValue* end() const;   //!< Pointer to end of values.
      90             :     bool empty() const;                 //!< True if there are any non-negated values.
      91             :     bool last_negated() const;          //!< True if the last value is negated.
      92             :     size_t negated() const;             //!< Number of negated values.
      93             : 
      94             :     const SettingsValue* data = nullptr;
      95             :     size_t size = 0;
      96             : };
      97             : 
      98             : //! Map lookup helper.
      99             : template <typename Map, typename Key>
     100    11138039 : auto FindKey(Map&& map, Key&& key) -> decltype(&map.at(key))
     101             : {
     102    11138039 :     auto it = map.find(key);
     103    11138039 :     return it == map.end() ? nullptr : &it->second;
     104    11138039 : }
     105             : 
     106             : } // namespace util
     107             : 
     108             : #endif // BITCOIN_UTIL_SETTINGS_H

Generated by: LCOV version 1.15