]> git.stg.codes - stg.git/blob - projects/sgconf/parsers.h
Added command line parser prototype.
[stg.git] / projects / sgconf / parsers.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #ifndef __STG_SGCONF_PARSERS_H__
22 #define __STG_SGCONF_PARSERS_H__
23
24 namespace SGCONF
25 {
26
27 typedef void (*FUNC0)();
28
29 template <typename T>
30 struct FUNC1
31 {
32 typedef void (*type)(T);
33 };
34
35 class PARSER
36 {
37     public:
38         virtual PARSER_STATE parse(int, char **, CONFIG&) = 0;
39 };
40
41 template <typename T>
42 class PARAM_PARSER : public PARSER
43 {
44     public:
45         PARAM_PARSER(T& var) : m_var(var) {}
46         virtual PARSER_STATE parse(int argc, char ** argv, CONFIG& config)
47         {
48         std::istringstream stream(argv[0]);
49         stream >> m_var;
50         PARSER_STATE state;
51         state.argc = argc - 1;
52         state.argv = argv + 1;
53         state.config = config;
54         state.result = false;
55         return state;
56         }
57
58     private:
59         T& m_var;
60 };
61
62 class FUNC0_PARSER
63 {
64     public:
65         FUNC0_PARSER(FUNC0 func) : m_func(func) {}
66         virtual PARSER_STATE parse(int argc, char ** argv, CONFIG& config)
67         {
68         m_func();
69         PARSER_STATE state;
70         state.argc = argc - 1;
71         state.argv = argv + 1;
72         state.config = config;
73         state.result = true;
74         return state;
75         }
76
77     private:
78         FUNC0 m_func;
79 };
80
81 template <typename T>
82 class FUNC1_PARSER
83 {
84     public:
85         FUNC1_PARSER(typename FUNC1<T>::type func, const T & arg) : m_func(func), m_arg(arg) {}
86         virtual PARSER_STATE parse(int argc, char ** argv, CONFIG& config)
87         {
88         m_func(m_arg);
89         PARSER_STATE state;
90         state.argc = argc - 1;
91         state.argv = argv + 1;
92         state.config = config;
93         state.result = true;
94         return state;
95         }
96
97     private:
98         typename FUNC1<T>::type m_func;
99         T m_arg;
100 }
101
102 class PARSERS
103 {
104     public:
105         typedef PARSER_STATE (* FUNC)(int, char **, CONFIG&);
106
107         template <typename T>
108         void add(const std::string & shortToken,
109                  const std::string & fullToken,
110                  T& var);
111
112         template <>
113         void add<void>(const std:string & shortToken,
114                        const std::string & fullToken,
115                        FUNC0 func);
116         template <typename V>
117         void add<void>(const std:string & shortToken,
118                        const std::string & fullToken,
119                        FUNC1 func, const V& v);
120
121     private:
122 };
123
124 }