-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.hpp
49 lines (38 loc) · 903 Bytes
/
config.hpp
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
#ifndef CONFIG_HPP
#define CONFIG_HPP
#include <map>
#include <stdexcept>
#include <string>
#include <sstream>
class Config
{
public:
Config(int argc, char *argv[]);
template<typename T>
T get(std::string const& key) const
{
auto it = _map.find(key);
if(it == std::end(_map))
throw std::runtime_error{"No such key found : " + key};
T val;
std::istringstream s{it->second};
s >> val;
return val;
}
private:
bool process(std::string const& str);
std::map<std::string, std::string> _map;
};
template<>
inline bool Config::get<bool>(std::string const&key) const
{
auto it = _map.find(key);
if(it == std::end(_map))
throw std::runtime_error{"No such key found : " + key};
if(it->second == "true")
return true;
if(it->second == "false")
return false;
throw std::runtime_error{"Invalid boolean string : " + it->second};
}
#endif