-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfigManager.cpp
79 lines (72 loc) · 2.36 KB
/
ConfigManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "ConfigManager.hpp"
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <libconfig.h++>
#include <string>
ConfigManager::ConfigManager() {}
ConfigManager::~ConfigManager() = default;
ConfigManager &ConfigManager::get_instance() {
static ConfigManager instance;
return instance;
}
void ConfigManager::init(std::string cfg_file) {
if (!std::filesystem::exists(cfg_file)) {
std::cerr << "Config file not found!" << std::endl;
return;
}
try {
config.readFile(cfg_file);
} catch (const libconfig::ParseException &e) {
std::cerr << e.what() << std::endl;
std::cerr << e.getLine() << std::endl;
std::cerr << e.getError() << std::endl;
exit(1);
}
}
float ConfigManager::get_or_default(const std::string &key, float def) {
try {
float ret{config.lookup(key)};
return ret;
} catch (const libconfig::SettingNotFoundException &e) {
std::cerr << e.what() << std::endl;
std::cerr << key << " not found in config." << std::endl;
return def;
} catch (const libconfig::SettingTypeException &e) {
std::cerr << e.what() << std::endl;
std::cerr << "Expected type \"float\" for key " << key
<< ", falling back to default." << std::endl;
return def;
}
}
int ConfigManager::get_or_default(const std::string &key, int def) {
try {
int ret{config.lookup(key)};
return ret;
} catch (const libconfig::SettingNotFoundException &e) {
std::cerr << e.what() << std::endl;
std::cerr << key << " not found in config." << std::endl;
return def;
} catch (const libconfig::SettingTypeException &e) {
std::cerr << e.what() << std::endl;
std::cerr << "Expected type \"int\" for key " << key
<< ", falling back to default." << std::endl;
return def;
}
}
std::string ConfigManager::get_or_default(const std::string &key,
std::string def) {
try {
std::string ret{config.lookup(key).c_str()};
return ret;
} catch (const libconfig::SettingNotFoundException &e) {
std::cerr << e.what() << std::endl;
std::cerr << key << " not found in config." << std::endl;
return def;
} catch (const libconfig::SettingTypeException &e) {
std::cerr << e.what() << std::endl;
std::cerr << "Expected type \"string\" for key " << key
<< ", falling back to default." << std::endl;
return def;
}
}