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>
21 #ifndef __STG_SGCONF_ACTIONS_H__
22 #define __STG_SGCONF_ACTIONS_H__
26 #include "parser_state.h"
28 #include "stg/common.h"
29 #include "stg/resetable.h"
36 typedef void (* FUNC0)();
39 class FUNC0_ACTION : public ACTION
42 FUNC0_ACTION(const F & func) : m_func(func) {}
44 virtual ACTION * Clone() const { return new FUNC0_ACTION<F>(*this); }
46 virtual std::string ParamDescription() const { return ""; }
47 virtual std::string DefaultDescription() const { return ""; }
48 virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
49 virtual PARSER_STATE Parse(int argc, char ** argv)
52 return PARSER_STATE(true, argc, argv);
57 OPTION_BLOCK m_suboptions;
62 FUNC0_ACTION<F> * MakeFunc0Action(F func)
64 return new FUNC0_ACTION<F>(func);
68 class PARAM_ACTION : public ACTION
71 PARAM_ACTION(RESETABLE<T> & param,
72 const T & defaultValue,
73 const std::string & paramDescription)
75 m_defaltValue(defaultValue),
76 m_description(paramDescription),
79 PARAM_ACTION(RESETABLE<T> & param,
80 const std::string & paramDescription)
82 m_description(paramDescription),
86 virtual ACTION * Clone() const { return new PARAM_ACTION<T>(*this); }
88 virtual std::string ParamDescription() const { return m_description; }
89 virtual std::string DefaultDescription() const;
90 virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
91 virtual PARSER_STATE Parse(int argc, char ** argv);
92 virtual void ParseValue(const std::string & value);
95 RESETABLE<T> & m_param;
97 std::string m_description;
99 OPTION_BLOCK m_suboptions;
102 template <typename T>
104 std::string PARAM_ACTION<T>::DefaultDescription() const
106 return m_hasDefault ? " (default: '" + x2str(m_defaltValue) + "')"
112 std::string PARAM_ACTION<std::string>::DefaultDescription() const
114 return m_hasDefault ? " (default: '" + m_defaltValue + "')"
118 template <typename T>
120 PARSER_STATE PARAM_ACTION<T>::Parse(int argc, char ** argv)
125 throw ERROR("Missing argument.");
127 if (str2x(*argv, value))
128 throw ERROR(std::string("Bad argument: '") + *argv + "'");
130 return PARSER_STATE(false, --argc, ++argv);
133 template <typename T>
135 void PARAM_ACTION<T>::ParseValue(const std::string & stringValue)
137 if (stringValue.empty())
138 throw ERROR("Missing value.");
140 if (str2x(stringValue, value))
141 throw ERROR(std::string("Bad value: '") + stringValue + "'");
147 void PARAM_ACTION<std::string>::ParseValue(const std::string & stringValue)
149 m_param = stringValue;
154 PARSER_STATE PARAM_ACTION<std::string>::Parse(int argc, char ** argv)
159 throw ERROR("Missing argument.");
161 return PARSER_STATE(false, --argc, ++argv);
164 template <typename T>
166 PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param,
167 const T & defaultValue,
168 const std::string & paramDescription)
170 return new PARAM_ACTION<T>(param, defaultValue, paramDescription);
173 template <typename T>
175 PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param,
176 const std::string & paramDescription)
178 return new PARAM_ACTION<T>(param, paramDescription);
181 } // namespace SGCONF