0.4.1
Loading...
Searching...
No Matches
ConfigManager.hpp
Go to the documentation of this file.
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
27{
28/// Map which stores all options
29extern boost::program_options::variables_map vm;
30
31/// @brief Initializes the config manager. Call this function before using other functions
32void initialize();
33
34/// @brief Deinitializes the config manager. Call this if you want to Fetch config again
35void 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)
45std::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
50void 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
57template<typename T>
58const T& Get(const std::string& key, const T&& defaultValue)
59{
60 if (vm.count(key))
61 {
62 return vm[key].as<T>();
63 }
64
65 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
72template<typename T>
73const T& Get(const std::string& key)
74{
75 if (vm.count(key))
76 {
77 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.
84bool HasKey(const std::string& key);
85
86/// Returns all keys in the configuration, as a vector.
87std::vector<std::string> GetKeys();
88
89/// @brief Saves the global settings
91
92/// @brief Loads the global settings
94
95} // namespace NAV::ConfigManager
void LoadGlobalSettings()
Loads the global settings.
void initialize()
Initializes the config manager. Call this function before using other functions.
boost::program_options::variables_map vm
Map which stores all options.
void CheckOptions(const int argc, const char *argv[])
Writes all command line options into the log.
const T & Get(const std::string &key, const T &&defaultValue)
Retrieves the value of a corresponding key from the configuration, if one exists.
bool HasKey(const std::string &key)
Checks if a corresponding key exists in the configuration.
std::vector< std::string > GetKeys()
Returns all keys in the configuration, as a vector.
std::vector< std::string > FetchConfigs(const int argc, const char *argv[])
Fetches the configs from the command line parameters.
const boost::program_options::options_description & GetProgramOptions()
Get the Program Options object.
void deinitialize()
Deinitializes the config manager. Call this if you want to Fetch config again.
void SaveGlobalSettings()
Saves the global settings.