]> git.stg.codes - stg.git/blob - projects/sgconf/api_action.h
Set LC_NUMERIC to C, changes service serialization format.
[stg.git] / projects / sgconf / api_action.h
1 #ifndef __STG_SGCONF_API_ACTION_H__
2 #define __STG_SGCONF_API_ACTION_H__
3
4 #include "action.h"
5
6 #include "options.h"
7
8 #include <string>
9 #include <map>
10 #include <vector>
11
12 namespace SGCONF
13 {
14
15 typedef bool (* API_FUNCTION) (const CONFIG &,
16                                const std::string &,
17                                const std::map<std::string, std::string> &);
18
19 class COMMAND
20 {
21     public:
22         COMMAND(API_FUNCTION funPtr,
23                 const std::string & arg,
24                 const std::map<std::string, std::string> & options)
25             : m_funPtr(funPtr),
26               m_arg(arg),
27               m_options(options)
28         {}
29         bool Execute(const SGCONF::CONFIG & config) const
30         {
31             return m_funPtr(config, m_arg, m_options);
32         }
33
34     private:
35         API_FUNCTION m_funPtr;
36         std::string m_arg;
37         std::map<std::string, std::string> m_options;
38 };
39
40 class COMMANDS
41 {
42     public:
43         void Add(API_FUNCTION funPtr,
44                  const std::string & arg,
45                  const std::map<std::string, std::string> & options) { m_commands.push_back(COMMAND(funPtr, arg, options)); }
46         bool Execute(const SGCONF::CONFIG & config) const
47         {
48             std::vector<COMMAND>::const_iterator it(m_commands.begin());
49             bool res = true;
50             while (it != m_commands.end() && res)
51             {
52                 res = res && it->Execute(config);
53                 ++it;
54             }
55             return res;
56         }
57     private:
58         std::vector<COMMAND> m_commands;
59 };
60
61 class API_ACTION : public ACTION
62 {
63     public:
64         struct PARAM
65         {
66             PARAM(const std::string & n,
67                   const std::string & s,
68                   const std::string & l)
69                 : name(n),
70                   shortDescr(s),
71                   longDescr(l)
72             {}
73             std::string name;
74             std::string shortDescr;
75             std::string longDescr;
76         };
77
78         API_ACTION(COMMANDS & commands,
79                    const std::string & paramDescription,
80                    bool needArgument,
81                    const std::vector<PARAM> & params,
82                    API_FUNCTION funPtr);
83         API_ACTION(COMMANDS & commands,
84                    const std::string & paramDescription,
85                    bool needArgument,
86                    API_FUNCTION funPtr)
87             : m_commands(commands),
88               m_description(paramDescription),
89               m_argument(needArgument ? "1" : ""), // Hack
90               m_funPtr(funPtr)
91         {}
92
93         virtual ACTION * Clone() const { return new API_ACTION(*this); }
94
95         virtual std::string ParamDescription() const { return m_description; }
96         virtual std::string DefaultDescription() const { return ""; }
97         virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
98         virtual PARSER_STATE Parse(int argc, char ** argv, void * /*data*/);
99
100     private:
101         COMMANDS & m_commands;
102         std::string m_description;
103         std::string m_argument;
104         OPTION_BLOCK m_suboptions;
105         std::map<std::string, std::string> m_params;
106         API_FUNCTION m_funPtr;
107 };
108
109 inline
110 ACTION * MakeAPIAction(COMMANDS & commands,
111                        const std::string & paramDescription,
112                        const std::vector<API_ACTION::PARAM> & params,
113                        API_FUNCTION funPtr)
114 {
115 return new API_ACTION(commands, paramDescription, true, params, funPtr);
116 }
117
118 inline
119 ACTION * MakeAPIAction(COMMANDS & commands,
120                        const std::vector<API_ACTION::PARAM> & params,
121                        API_FUNCTION funPtr)
122 {
123 return new API_ACTION(commands, "", false, params, funPtr);
124 }
125
126 inline
127 ACTION * MakeAPIAction(COMMANDS & commands,
128                        const std::string & paramDescription,
129                        API_FUNCTION funPtr)
130 {
131 return new API_ACTION(commands, paramDescription, true, funPtr);
132 }
133
134 inline
135 ACTION * MakeAPIAction(COMMANDS & commands,
136                        API_FUNCTION funPtr)
137 {
138 return new API_ACTION(commands, "", false, funPtr);
139 }
140
141 }
142
143 #endif