CPP-TOOLBOX
Loading...
Searching...
No Matches
config_file_parser.hpp
Go to the documentation of this file.
1#ifndef CONFIGURATION_HPP
2#define CONFIGURATION_HPP
3
5
6#include <filesystem>
7#include <functional>
8#include <optional>
9#include <string>
10#include <unordered_map>
11#include <vector>
12
18 public:
19 using SectionKeyPair = std::pair<std::string, std::string>;
20 using ConfigLogic = std::function<void(const std::string)>;
24 struct PairHash {
25 PairHash() = default;
26 size_t operator()(const SectionKeyPair &pair) const {
27 return std::hash<std::string>()(pair.first) ^ (std::hash<std::string>()(pair.second) << 1);
28 }
29 };
30 using SectionKeyPairToConfigLogic = std::unordered_map<SectionKeyPair, ConfigLogic, PairHash>;
31 using ConfigData = std::unordered_map<std::string, std::unordered_map<std::string, std::string>>;
32
33 Configuration(const std::filesystem::path &config_path, const SectionKeyPairToConfigLogic &config_logic = {},
34 bool apply = true);
35
36 // discards any in-memory changes
38
39 void register_config_handler(const std::string &section, const std::string &key,
40 std::function<void(const std::string)> logic);
41
42 // Live configuration modification methods
43
44 bool set_value(const std::string &section, const std::string &key, const std::string &value,
45 const bool apply = false);
46 std::optional<std::string> get_value(const std::string &section, const std::string &key);
47 bool remove_value(const std::string &section, const std::string &key);
48
49 // Query methods
50
51 bool has_section(const std::string &section) const;
52 bool has_value(const std::string &section, const std::string &key);
53 std::vector<std::string> get_sections() const;
54 std::vector<std::string> get_keys(const std::string &section);
55
56 // File operations
57
58 bool save_to_file();
59 bool save_to_file(const std::filesystem::path &path);
60 bool backup_config(const std::filesystem::path &backup_path);
61
62 void apply_config_logic_for_key(const std::string &section, const std::string &key);
63 void apply_config_logic();
64
65 private:
66 std::filesystem::path config_path;
67 SectionKeyPairToConfigLogic section_key_to_config_logic;
68 std::unordered_map<std::string, std::unordered_map<std::string, std::string>> section_to_key_to_value;
69 Logger console_logger{"configuration"};
70
71 void parse_config_file();
72 static std::string trim(const std::string &str);
73};
74
75#endif // CONFIGURATION_HPP
std::function< void(const std::string)> ConfigLogic
Definition config_file_parser.hpp:20
std::unordered_map< SectionKeyPair, ConfigLogic, PairHash > SectionKeyPairToConfigLogic
Definition config_file_parser.hpp:30
bool remove_value(const std::string &section, const std::string &key)
Definition config_file_parser.cpp:116
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > ConfigData
Definition config_file_parser.hpp:31
void register_config_handler(const std::string &section, const std::string &key, std::function< void(const std::string)> logic)
Definition config_file_parser.cpp:20
void apply_config_logic()
Definition config_file_parser.cpp:72
bool set_value(const std::string &section, const std::string &key, const std::string &value, const bool apply=false)
Definition config_file_parser.cpp:89
std::pair< std::string, std::string > SectionKeyPair
Definition config_file_parser.hpp:19
bool backup_config(const std::filesystem::path &backup_path)
Definition config_file_parser.cpp:206
std::optional< std::string > get_value(const std::string &section, const std::string &key)
Definition config_file_parser.cpp:102
Configuration(const std::filesystem::path &config_path, const SectionKeyPairToConfigLogic &config_logic={}, bool apply=true)
Definition config_file_parser.cpp:4
bool has_value(const std::string &section, const std::string &key)
Definition config_file_parser.cpp:142
bool has_section(const std::string &section) const
Definition config_file_parser.cpp:138
std::vector< std::string > get_sections() const
Definition config_file_parser.cpp:150
bool save_to_file()
Definition config_file_parser.cpp:171
std::vector< std::string > get_keys(const std::string &section)
Definition config_file_parser.cpp:159
void apply_config_logic_for_key(const std::string &section, const std::string &key)
Definition config_file_parser.cpp:217
void reload_config_from_file()
Definition config_file_parser.cpp:14
Definition logger.hpp:22
size_t operator()(const SectionKeyPair &pair) const
Definition config_file_parser.hpp:26