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.
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.
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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
25 #include "parser_state.h"
27 #include "stg/common.h"
28 #include "stg/optional.h"
37 typedef void (* FUNC0)();
40 class FUNC0_ACTION : public ACTION
43 FUNC0_ACTION(const F & func) : m_func(func) {}
45 virtual ACTION * Clone() const { return new FUNC0_ACTION<F>(*this); }
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*/)
53 return PARSER_STATE(true, argc, argv);
58 OPTION_BLOCK m_suboptions;
63 FUNC0_ACTION<F> * MakeFunc0Action(F func)
65 return new FUNC0_ACTION<F>(func);
69 class PARAM_ACTION : public ACTION
72 PARAM_ACTION(STG::Optional<T> & param,
73 const T & defaultValue,
74 const std::string & paramDescription)
76 m_defaltValue(defaultValue),
77 m_description(paramDescription),
80 PARAM_ACTION(STG::Optional<T> & param)
84 PARAM_ACTION(STG::Optional<T> & param,
85 const std::string & paramDescription)
87 m_description(paramDescription),
91 virtual ACTION * Clone() const { return new PARAM_ACTION<T>(*this); }
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);
100 STG::Optional<T> & m_param;
102 std::string m_description;
104 OPTION_BLOCK m_suboptions;
107 template <typename T>
109 std::string PARAM_ACTION<T>::DefaultDescription() const
111 return m_hasDefault ? " (default: '" + std::to_string(m_defaltValue) + "')"
117 std::string PARAM_ACTION<std::string>::DefaultDescription() const
119 return m_hasDefault ? " (default: '" + m_defaltValue + "')"
123 template <typename T>
125 PARSER_STATE PARAM_ACTION<T>::Parse(int argc, char ** argv, void * /*data*/)
130 throw ERROR("Missing argument.");
132 if (str2x(*argv, value))
133 throw ERROR(std::string("Bad argument: '") + *argv + "'");
135 return PARSER_STATE(false, --argc, ++argv);
140 PARSER_STATE PARAM_ACTION<bool>::Parse(int argc, char ** argv, void * /*data*/)
143 return PARSER_STATE(false, argc, argv);
146 template <typename T>
148 void PARAM_ACTION<T>::ParseValue(const std::string & stringValue)
150 if (stringValue.empty())
151 throw ERROR("Missing value.");
153 if (str2x(stringValue, value))
154 throw ERROR(std::string("Bad value: '") + stringValue + "'");
160 void PARAM_ACTION<std::string>::ParseValue(const std::string & stringValue)
162 m_param = stringValue;
167 PARSER_STATE PARAM_ACTION<std::string>::Parse(int argc, char ** argv, void * /*data*/)
172 throw ERROR("Missing argument.");
174 return PARSER_STATE(false, --argc, ++argv);
177 template <typename T>
179 PARAM_ACTION<T> * MakeParamAction(STG::Optional<T> & param,
180 const T & defaultValue,
181 const std::string & paramDescription)
183 return new PARAM_ACTION<T>(param, defaultValue, paramDescription);
186 template <typename T>
188 PARAM_ACTION<T> * MakeParamAction(STG::Optional<T> & param)
190 return new PARAM_ACTION<T>(param);
193 template <typename T>
195 PARAM_ACTION<T> * MakeParamAction(STG::Optional<T> & param,
196 const std::string & paramDescription)
198 return new PARAM_ACTION<T>(param, paramDescription);
201 class KV_ACTION : public ACTION
204 KV_ACTION(const std::string & name,
205 const std::string & paramDescription)
207 m_description(paramDescription)
210 virtual ACTION * Clone() const { return new KV_ACTION(*this); }
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);
219 std::string m_description;
220 OPTION_BLOCK m_suboptions;
224 PARSER_STATE KV_ACTION::Parse(int argc, char ** argv, void * data)
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);
233 return PARSER_STATE(false, --argc, ++argv);
237 KV_ACTION * MakeKVAction(const std::string & name,
238 const std::string & paramDescription)
240 return new KV_ACTION(name, paramDescription);
243 } // namespace SGCONF