-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisupport.h
74 lines (57 loc) · 1.76 KB
/
isupport.h
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
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
struct ISupport : std::map<std::string,std::string>
{
bool empty(const std::string &key) const { return !count(key) || at(key).empty(); }
bool operator()(const std::string &key) const { return count(key); }
const std::string &operator[](const std::string &key) const; // empty static string
template<class T = std::string> T get(const std::string &key) const; // throws
template<class T = std::string> T get(const std::string &key, T&& def_val) const; // defaults
template<class T = size_t> T get_or_max(const std::string &key) const; // numeric lim
template<class... A> bool find_in(const std::string &key, A&&... val) const;
template<class... A> ISupport(A&&... a): std::map<std::string,std::string>(std::forward<A>(a)...) {}
};
template<class... A>
bool ISupport::find_in(const std::string &key,
A&&... val)
const
{
const auto it(find(key));
if(it == end())
return false;
return it->second.find(std::forward<A>(val)...) != std::string::npos;
}
template<class T>
T ISupport::get_or_max(const std::string &key)
const
{
return get(key,std::numeric_limits<T>::max());
}
template<class T>
T ISupport::get(const std::string &key,
T&& def_val)
const
{
return empty(key)? def_val : get<T>(key);
}
template<class T>
T ISupport::get(const std::string &key)
const
{
return lex_cast<T>(at(key));
}
inline
const std::string &ISupport::operator[](const std::string &key)
const try
{
return at(key);
}
catch(const std::out_of_range &e)
{
static const std::string mt;
return mt;
}