]> git.stg.codes - stg.git/blob - projects/sgconf/actions.h
Merge branch 'master' of gitorious.org:stg/stg
[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 class FUNC0_ACTION : public ACTION
39 {
40     public:
41         FUNC0_ACTION(FUNC0 func) : m_func(func) {}
42
43         virtual std::string ParamDescription() const { return ""; }
44         virtual std::string DefaultDescription() const { return ""; }
45         virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
46         virtual PARSER_STATE Parse(int argc, char ** argv);
47
48     private:
49         FUNC0 m_func;
50         OPTION_BLOCK m_suboptions;
51 };
52
53 inline
54 FUNC0_ACTION * MakeFunc0Action(FUNC0 func)
55 {
56 return new FUNC0_ACTION(func);
57 }
58
59 template <typename T>
60 class PARAM_ACTION: public ACTION
61 {
62     public:
63         PARAM_ACTION(RESETABLE<T> & param,
64                      const T & defaultValue,
65                      const std::string & paramDescription)
66             : m_param(param),
67               m_defaltValue(defaultValue),
68               m_description(paramDescription),
69               m_hasDefault(true)
70         {}
71         PARAM_ACTION(RESETABLE<T> & param,
72                      const std::string & paramDescription)
73             : m_param(param),
74               m_description(paramDescription),
75               m_hasDefault(false)
76         {}
77
78         virtual std::string ParamDescription() const { return m_description; }
79         virtual std::string DefaultDescription() const;
80         virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
81         virtual PARSER_STATE Parse(int argc, char ** argv);
82
83     private:
84         RESETABLE<T> & m_param;
85         T m_defaltValue;
86         std::string m_description;
87         bool m_hasDefault;
88         OPTION_BLOCK m_suboptions;
89 };
90
91 template <typename T>
92 inline
93 std::string PARAM_ACTION<T>::DefaultDescription() const
94 {
95 return m_hasDefault ? " (default: '" + x2str(m_defaltValue) + "')"
96                     : "";
97 }
98
99 template <>
100 inline
101 std::string PARAM_ACTION<std::string>::DefaultDescription() const
102 {
103 return m_hasDefault ? " (default: '" + m_defaltValue + "')"
104                     : "";
105 }
106
107 template <typename T>
108 inline
109 PARSER_STATE PARAM_ACTION<T>::Parse(int argc, char ** argv)
110 {
111 T value;
112 if (str2x(*argv, value))
113     throw ERROR(std::string("Bad argument: '") + *argv + "'");
114 m_param = value;
115 return PARSER_STATE(false, --argc, ++argv);
116 }
117
118 template <>
119 inline
120 PARSER_STATE PARAM_ACTION<std::string>::Parse(int argc, char ** argv)
121 {
122 m_param = *argv;
123 return PARSER_STATE(false, --argc, ++argv);
124 }
125
126 template <typename T>
127 inline
128 PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param,
129                                   const T & defaultValue,
130                                   const std::string & paramDescription)
131 {
132 return new PARAM_ACTION<T>(param, defaultValue, paramDescription);
133 }
134
135 template <typename T>
136 inline
137 PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param,
138                                   const std::string & paramDescription)
139 {
140 return new PARAM_ACTION<T>(param, paramDescription);
141 }
142
143 } // namespace SGCONF
144
145 #endif