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