]> git.stg.codes - stg.git/blob - projects/sgconf/actions.h
Search parameter in map case insensitive
[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 namespace SGCONF
34 {
35
36 typedef void (* FUNC0)();
37
38 template <typename F>
39 class FUNC0_ACTION : public ACTION
40 {
41     public:
42         FUNC0_ACTION(const F & func) : m_func(func) {}
43
44         virtual ACTION * Clone() const { return new FUNC0_ACTION<F>(*this); }
45
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)
50         {
51         m_func();
52         return PARSER_STATE(true, argc, argv);
53         }
54
55     private:
56         F m_func;
57         OPTION_BLOCK m_suboptions;
58 };
59
60 template <typename F>
61 inline
62 FUNC0_ACTION<F> * MakeFunc0Action(F func)
63 {
64 return new FUNC0_ACTION<F>(func);
65 }
66
67 template <typename T>
68 class PARAM_ACTION : public ACTION
69 {
70     public:
71         PARAM_ACTION(RESETABLE<T> & param,
72                      const T & defaultValue,
73                      const std::string & paramDescription)
74             : m_param(param),
75               m_defaltValue(defaultValue),
76               m_description(paramDescription),
77               m_hasDefault(true)
78         {}
79         PARAM_ACTION(RESETABLE<T> & param,
80                      const std::string & paramDescription)
81             : m_param(param),
82               m_description(paramDescription),
83               m_hasDefault(false)
84         {}
85
86         virtual ACTION * Clone() const { return new PARAM_ACTION<T>(*this); }
87
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);
93
94     private:
95         RESETABLE<T> & m_param;
96         T m_defaltValue;
97         std::string m_description;
98         bool m_hasDefault;
99         OPTION_BLOCK m_suboptions;
100 };
101
102 template <typename T>
103 inline
104 std::string PARAM_ACTION<T>::DefaultDescription() const
105 {
106 return m_hasDefault ? " (default: '" + x2str(m_defaltValue) + "')"
107                     : "";
108 }
109
110 template <>
111 inline
112 std::string PARAM_ACTION<std::string>::DefaultDescription() const
113 {
114 return m_hasDefault ? " (default: '" + m_defaltValue + "')"
115                     : "";
116 }
117
118 template <typename T>
119 inline
120 PARSER_STATE PARAM_ACTION<T>::Parse(int argc, char ** argv)
121 {
122 if (argc == 0 ||
123     argv == NULL ||
124     *argv == NULL)
125     throw ERROR("Missing argument.");
126 T value;
127 if (str2x(*argv, value))
128     throw ERROR(std::string("Bad argument: '") + *argv + "'");
129 m_param = value;
130 return PARSER_STATE(false, --argc, ++argv);
131 }
132
133 template <typename T>
134 inline
135 void PARAM_ACTION<T>::ParseValue(const std::string & stringValue)
136 {
137 if (stringValue.empty())
138     throw ERROR("Missing value.");
139 T value;
140 if (str2x(stringValue, value))
141     throw ERROR(std::string("Bad value: '") + stringValue + "'");
142 m_param = value;
143 }
144
145 template <>
146 inline
147 void PARAM_ACTION<std::string>::ParseValue(const std::string & stringValue)
148 {
149 m_param = stringValue;
150 }
151
152 template <>
153 inline
154 PARSER_STATE PARAM_ACTION<std::string>::Parse(int argc, char ** argv)
155 {
156 if (argc == 0 ||
157     argv == NULL ||
158     *argv == NULL)
159     throw ERROR("Missing argument.");
160 m_param = *argv;
161 return PARSER_STATE(false, --argc, ++argv);
162 }
163
164 template <typename T>
165 inline
166 PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param,
167                                   const T & defaultValue,
168                                   const std::string & paramDescription)
169 {
170 return new PARAM_ACTION<T>(param, defaultValue, paramDescription);
171 }
172
173 template <typename T>
174 inline
175 PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param,
176                                   const std::string & paramDescription)
177 {
178 return new PARAM_ACTION<T>(param, paramDescription);
179 }
180
181 } // namespace SGCONF
182
183 #endif