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