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