]> git.stg.codes - stg.git/blob - projects/sgconf/actions.h
3181a105a31ab2cc9adf0ca950e2ad8d37f75383
[stg.git] / projects / sgconf / actions.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 #pragma once
22
23 #include "action.h"
24 #include "options.h"
25 #include "parser_state.h"
26
27 #include "stg/common.h"
28 #include "stg/optional.h"
29
30 #include <string>
31
32 #include <cassert>
33
34 namespace SGCONF
35 {
36
37 typedef void (* FUNC0)();
38
39 template <typename F>
40 class FUNC0_ACTION : public ACTION
41 {
42     public:
43         FUNC0_ACTION(const F & func) : m_func(func) {}
44
45         virtual ACTION * Clone() const { return new FUNC0_ACTION<F>(*this); }
46
47         virtual std::string ParamDescription() const { return ""; }
48         virtual std::string DefaultDescription() const { return ""; }
49         virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
50         virtual PARSER_STATE Parse(int argc, char ** argv, void * /*data*/)
51         {
52         m_func();
53         return PARSER_STATE(true, argc, argv);
54         }
55
56     private:
57         F m_func;
58         OPTION_BLOCK m_suboptions;
59 };
60
61 template <typename F>
62 inline
63 FUNC0_ACTION<F> * MakeFunc0Action(F func)
64 {
65 return new FUNC0_ACTION<F>(func);
66 }
67
68 template <typename T>
69 class PARAM_ACTION : public ACTION
70 {
71     public:
72         PARAM_ACTION(STG::Optional<T> & param,
73                      const T & defaultValue,
74                      const std::string & paramDescription)
75             : m_param(param),
76               m_defaltValue(defaultValue),
77               m_description(paramDescription),
78               m_hasDefault(true)
79         {}
80         PARAM_ACTION(STG::Optional<T> & param)
81             : m_param(param),
82               m_hasDefault(false)
83         {}
84         PARAM_ACTION(STG::Optional<T> & param,
85                      const std::string & paramDescription)
86             : m_param(param),
87               m_description(paramDescription),
88               m_hasDefault(false)
89         {}
90
91         virtual ACTION * Clone() const { return new PARAM_ACTION<T>(*this); }
92
93         virtual std::string ParamDescription() const { return m_description; }
94         virtual std::string DefaultDescription() const;
95         virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
96         virtual PARSER_STATE Parse(int argc, char ** argv, void * /*data*/);
97         virtual void ParseValue(const std::string & value);
98
99     private:
100         STG::Optional<T> & m_param;
101         T m_defaltValue;
102         std::string m_description;
103         bool m_hasDefault;
104         OPTION_BLOCK m_suboptions;
105 };
106
107 template <typename T>
108 inline
109 std::string PARAM_ACTION<T>::DefaultDescription() const
110 {
111 return m_hasDefault ? " (default: '" + std::to_string(m_defaltValue) + "')"
112                     : "";
113 }
114
115 template <>
116 inline
117 std::string PARAM_ACTION<std::string>::DefaultDescription() const
118 {
119 return m_hasDefault ? " (default: '" + m_defaltValue + "')"
120                     : "";
121 }
122
123 template <typename T>
124 inline
125 PARSER_STATE PARAM_ACTION<T>::Parse(int argc, char ** argv, void * /*data*/)
126 {
127 if (argc == 0 ||
128     argv == NULL ||
129     *argv == NULL)
130     throw ERROR("Missing argument.");
131 T value;
132 if (str2x(*argv, value))
133     throw ERROR(std::string("Bad argument: '") + *argv + "'");
134 m_param = value;
135 return PARSER_STATE(false, --argc, ++argv);
136 }
137
138 template <>
139 inline
140 PARSER_STATE PARAM_ACTION<bool>::Parse(int argc, char ** argv, void * /*data*/)
141 {
142 m_param = true;
143 return PARSER_STATE(false, argc, argv);
144 }
145
146 template <typename T>
147 inline
148 void PARAM_ACTION<T>::ParseValue(const std::string & stringValue)
149 {
150 if (stringValue.empty())
151     throw ERROR("Missing value.");
152 T value;
153 if (str2x(stringValue, value))
154     throw ERROR(std::string("Bad value: '") + stringValue + "'");
155 m_param = value;
156 }
157
158 template <>
159 inline
160 void PARAM_ACTION<std::string>::ParseValue(const std::string & stringValue)
161 {
162 m_param = stringValue;
163 }
164
165 template <>
166 inline
167 PARSER_STATE PARAM_ACTION<std::string>::Parse(int argc, char ** argv, void * /*data*/)
168 {
169 if (argc == 0 ||
170     argv == NULL ||
171     *argv == NULL)
172     throw ERROR("Missing argument.");
173 m_param = *argv;
174 return PARSER_STATE(false, --argc, ++argv);
175 }
176
177 template <typename T>
178 inline
179 PARAM_ACTION<T> * MakeParamAction(STG::Optional<T> & param,
180                                   const T & defaultValue,
181                                   const std::string & paramDescription)
182 {
183 return new PARAM_ACTION<T>(param, defaultValue, paramDescription);
184 }
185
186 template <typename T>
187 inline
188 PARAM_ACTION<T> * MakeParamAction(STG::Optional<T> & param)
189 {
190 return new PARAM_ACTION<T>(param);
191 }
192
193 template <typename T>
194 inline
195 PARAM_ACTION<T> * MakeParamAction(STG::Optional<T> & param,
196                                   const std::string & paramDescription)
197 {
198 return new PARAM_ACTION<T>(param, paramDescription);
199 }
200
201 class KV_ACTION : public ACTION
202 {
203     public:
204         KV_ACTION(const std::string & name,
205                   const std::string & paramDescription)
206             : m_name(name),
207               m_description(paramDescription)
208         {}
209
210         virtual ACTION * Clone() const { return new KV_ACTION(*this); }
211
212         virtual std::string ParamDescription() const { return m_description; }
213         virtual std::string DefaultDescription() const { return ""; }
214         virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
215         virtual PARSER_STATE Parse(int argc, char ** argv, void * data);
216
217     private:
218         std::string m_name;
219         std::string m_description;
220         OPTION_BLOCK m_suboptions;
221 };
222
223 inline
224 PARSER_STATE KV_ACTION::Parse(int argc, char ** argv, void * data)
225 {
226 if (argc == 0 ||
227     argv == NULL ||
228     *argv == NULL)
229     throw ERROR("Missing argument.");
230 assert(data != NULL && "Expecting container pointer.");
231 std::map<std::string, std::string> & kvs = *static_cast<std::map<std::string, std::string>*>(data);
232 kvs[m_name] = *argv;
233 return PARSER_STATE(false, --argc, ++argv);
234 }
235
236 inline
237 KV_ACTION * MakeKVAction(const std::string & name,
238                          const std::string & paramDescription)
239 {
240 return new KV_ACTION(name, paramDescription);
241 }
242
243 } // namespace SGCONF