]> git.stg.codes - stg.git/blob - projects/sgconf/utils.h
Merge branch 'stg-2.409-radius'
[stg.git] / projects / sgconf / utils.h
1 #ifndef __STG_SGCONF_UTILS_H__
2 #define __STG_SGCONF_UTILS_H__
3
4 #include "stg/common.h"
5 #include "stg/resetable.h"
6
7 #include <string>
8 #include <map>
9
10 namespace SGCONF
11 {
12
13 template <typename T>
14 inline
15 void MaybeSet(const std::map<std::string, std::string> & options, const std::string & name, RESETABLE<T> & res)
16 {
17 std::map<std::string, std::string>::const_iterator it(options.find(name));
18 if (it == options.end())
19     return;
20 T value;
21 if (str2x(it->second, value) < 0)
22     return;
23 res = value;
24 }
25
26 template <typename T, typename F>
27 inline
28 void MaybeSet(const std::map<std::string, std::string> & options, const std::string & name, T & res, F conv)
29 {
30 std::map<std::string, std::string>::const_iterator it(options.find(name));
31 if (it == options.end())
32     return;
33 conv(it->second, res);
34 }
35
36 template <>
37 inline
38 void MaybeSet<std::string>(const std::map<std::string, std::string> & options, const std::string & name, RESETABLE<std::string> & res)
39 {
40 std::map<std::string, std::string>::const_iterator it(options.find(name));
41 if (it == options.end())
42     return;
43 res = it->second;
44 }
45
46 } // namespace SGCONF
47
48 #endif