Line | Branch | Exec | Source |
---|---|---|---|
1 | // This file is part of INSTINCT, the INS Toolkit for Integrated | ||
2 | // Navigation Concepts and Training by the Institute of Navigation of | ||
3 | // the University of Stuttgart, Germany. | ||
4 | // | ||
5 | // This Source Code Form is subject to the terms of the Mozilla Public | ||
6 | // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
7 | // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
8 | |||
9 | /// @file ConfigManager.hpp | ||
10 | /// @brief Config management for the Project | ||
11 | /// @author T. Topp (topp@ins.uni-stuttgart.de) | ||
12 | /// @date 2020-03-11 | ||
13 | |||
14 | #pragma once | ||
15 | |||
16 | #include <vector> | ||
17 | #include <string> | ||
18 | #include <deque> | ||
19 | #include <memory> | ||
20 | |||
21 | #include <boost/program_options/options_description.hpp> | ||
22 | #include <boost/program_options/variables_map.hpp> | ||
23 | |||
24 | #include <fmt/core.h> | ||
25 | |||
26 | namespace NAV::ConfigManager | ||
27 | { | ||
28 | /// Map which stores all options | ||
29 | extern boost::program_options::variables_map vm; | ||
30 | |||
31 | /// @brief Initializes the config manager. Call this function before using other functions | ||
32 | void initialize(); | ||
33 | |||
34 | /// @brief Deinitializes the config manager. Call this if you want to Fetch config again | ||
35 | void deinitialize(); | ||
36 | |||
37 | /// @brief Get the Program Options object | ||
38 | /// @return The object | ||
39 | [[nodiscard]] const boost::program_options::options_description& GetProgramOptions(); | ||
40 | |||
41 | /// @brief Fetches the configs from the command line parameters | ||
42 | /// @param[in] argc Number of command line parameters | ||
43 | /// @param[in] argv Array of the command line parameters | ||
44 | /// @return List of config files which failed to be read (for error reporting) | ||
45 | std::vector<std::string> FetchConfigs(const int argc, const char* argv[]); // NOLINT | ||
46 | |||
47 | /// @brief Writes all command line options into the log | ||
48 | /// @param[in] argc Number of command line parameters | ||
49 | /// @param[in] argv Array of the command line parameters | ||
50 | void CheckOptions(const int argc, const char* argv[]); // NOLINT | ||
51 | |||
52 | /// @brief Retrieves the value of a corresponding key from the configuration, if one exists. | ||
53 | /// @tparam T Return value type | ||
54 | /// @param[in] key Key to search for | ||
55 | /// @param[in] defaultValue If key is not found, the default value is returned | ||
56 | /// @return The value found with the key or the default value | ||
57 | template<typename T> | ||
58 | 1203 | const T& Get(const std::string& key, const T&& defaultValue) | |
59 | { | ||
60 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
1203 | if (vm.count(key)) |
61 | { | ||
62 | 1199 | return vm[key].as<T>(); | |
63 | } | ||
64 | |||
65 | 4 | return defaultValue; | |
66 | } | ||
67 | |||
68 | /// @brief Retrieves the value of a corresponding key from the configuration, if it does not exists, throws an exception | ||
69 | /// @tparam T Return value type | ||
70 | /// @param[in] key Key to search for | ||
71 | /// @return The value found with the key | ||
72 | template<typename T> | ||
73 | 1462332 | const T& Get(const std::string& key) | |
74 | { | ||
75 |
1/2✓ Branch 1 taken 211290 times.
✗ Branch 2 not taken.
|
1462332 | if (vm.count(key)) |
76 | { | ||
77 | 1462179 | return vm[key].as<T>(); | |
78 | } | ||
79 | |||
80 | ✗ | throw std::runtime_error(fmt::format("The key '{}' does not exist.", key)); | |
81 | } | ||
82 | |||
83 | /// Checks if a corresponding key exists in the configuration. | ||
84 | bool HasKey(const std::string& key); | ||
85 | |||
86 | /// Returns all keys in the configuration, as a vector. | ||
87 | std::vector<std::string> GetKeys(); | ||
88 | |||
89 | /// @brief Saves the global settings | ||
90 | void SaveGlobalSettings(); | ||
91 | |||
92 | /// @brief Loads the global settings | ||
93 | void LoadGlobalSettings(); | ||
94 | |||
95 | } // namespace NAV::ConfigManager | ||
96 |