fgms 0.11.8
The
FlightGear MultiPlayer Server
project
command.hxx
Go to the documentation of this file.
1 //
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License as
4 // published by the Free Software Foundation; either version 2 of the
5 // License, or (at your option) any later version.
6 //
7 // This program is distributed in the hope that it will be useful, but
8 // WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, U$
15 //
16 // derived from libcli by David Parrish (david@dparrish.com)
17 // Copyright (C) 2011 Oliver Schroeder
18 //
19 
20 #ifndef CLI_COMMAND_H
21 #define CLI_COMMAND_H
22 
23 #include "common.hxx"
24 
25 namespace LIBCLI
26 {
27 
28 using namespace std;
29 
30 class CLI;
31 
33 {
34 };
35 
36 template <class C>
37 class Command : public CommandBase
38 {
39 public:
40  friend class CLI;
41  typedef int ( *c_callback_func ) ( C& obj, char* command, char** argv, int argc );
42  typedef int ( C::*cpp_callback_func ) ( char* command, char** argv, int argc );
43  Command ();
44  Command (
45  C* obj,
46  const char* command,
47  PRIVLEVEL level,
48  MODE mode,
49  const char* help
50  );
51  Command (
52  C* obj,
53  const char* command,
54  c_callback_func callback,
55  PRIVLEVEL level,
56  MODE mode,
57  const char* help
58  );
59  Command (
60  C* obj,
61  const char* command,
62  cpp_callback_func callback,
63  PRIVLEVEL level,
64  MODE mode,
65  const char* help
66  );
67  ~Command ();
68  int exec ( char* command, char** argv, int argc );
69  int exec ( C& Instance, char* command, char** argv, int argc );
70 private:
71  char* command;
72  char* help;
75  unsigned int unique_len;
79  C* _obj;
81  c_callback_func c_callback;
82  cpp_callback_func cpp_callback;
83  Command ( const C& obj );
84 }; // class Command
85 
86 template <class C>
88 ()
89 {
90  DEBUG d ( __FUNCTION__,__FILE__,__LINE__ );
91  this->parent = 0;
92  this->command = 0;
93  this->help = 0;
94  this->next = 0;
95  this->children = 0;
96  this->privilege = UNPRIVILEGED;
97  this->mode = MODE_ANY;
98  this->_obj = 0;
99  this->c_callback = 0;
100  this->cpp_callback = 0;
101  this->have_callback = false;
102 }
103 
104 template <class C>
106 (
107  C* obj,
108  const char* command,
109  PRIVLEVEL level,
110  MODE mode,
111  const char* help
112 )
113 {
114  DEBUG d ( __FUNCTION__,__FILE__,__LINE__ );
115  if ( ( command == 0 ) || ( strlen ( command ) == 0 ) )
116  {
117  throw arg_error ( "C1: bad argument" );
118  }
119  if ( command )
120  {
121  this->command = strdup ( command );
122  }
123  if ( help )
124  {
125  this->help = strdup ( help );
126  }
127  this->parent = 0;
128  this->privilege = level;
129  this->mode = mode;
130  this->next = 0;
131  this->children = 0;
132  this->_obj = obj;
133  this->c_callback = 0;
134  this->cpp_callback = 0;
135  this->have_callback = false;
136 }
137 
138 template <class C>
140 (
141  C* obj,
142  const char* command,
143  c_callback_func callback,
144  PRIVLEVEL level,
145  MODE mode,
146  const char* help
147 )
148 {
149  DEBUG d ( __FUNCTION__,__FILE__,__LINE__ );
150  if ( ( command == 0 ) || ( strlen ( command ) == 0 ) )
151  {
152  throw arg_error ( "C2: bad argument" );
153  }
154  if ( command )
155  {
156  this->command = strdup ( command );
157  }
158  if ( help )
159  {
160  this->help = strdup ( help );
161  }
162  this->parent = 0;
163  this->privilege = level;
164  this->mode = mode;
165  this->next = 0;
166  this->children = 0;
167  this->_obj = obj;
168  this->c_callback = callback;
169  this->cpp_callback = 0;
170  if ( this->c_callback )
171  {
172  this->have_callback = true;
173  }
174  else
175  {
176  this->have_callback = false;
177  }
178 }
179 
180 template <class C>
182 (
183  C* obj,
184  const char* command,
185  cpp_callback_func callback,
186  PRIVLEVEL level,
187  MODE mode,
188  const char* help
189 )
190 {
191  DEBUG d ( __FUNCTION__,__FILE__,__LINE__ );
192  if ( ( command == 0 ) || ( strlen ( command ) == 0 ) )
193  {
194  throw arg_error ( "C2: bad argument" );
195  }
196  if ( command )
197  {
198  this->command = strdup ( command );
199  }
200  if ( help )
201  {
202  this->help = strdup ( help );
203  }
204  this->parent = 0;
205  this->privilege = level;
206  this->mode = mode;
207  this->next = 0;
208  this->children = 0;
209  this->_obj = obj;
210  this->c_callback = 0;
211  this->cpp_callback= callback;
212  if ( this->cpp_callback )
213  {
214  this->have_callback = true;
215  }
216  else
217  {
218  this->have_callback = false;
219  }
220 }
221 
222 template <class C>
224 ()
225 {
226  DEBUG d ( __FUNCTION__,__FILE__,__LINE__ );
227  parent = 0;
228  if ( command )
229  {
230  free_z ( command );
231  }
232  if ( help )
233  {
234  free_z ( help );
235  }
236  c_callback = 0;
237  cpp_callback = 0;
238 }
239 
240 template <class C>
241 int
243 (
244  char* command,
245  char** argv,
246  int argc
247 )
248 {
249  DEBUG d ( __FUNCTION__,__FILE__,__LINE__ );
250  if ( ! have_callback )
251  {
252  throw arg_error ( "Command::exec: no callback!" );
253  }
254  if ( c_callback )
255  {
256  return ( this->c_callback ( *_obj, command, argv, argc ) );
257  }
258  else if ( cpp_callback )
259  {
260  return this->exec ( *_obj, command, argv, argc );
261  }
262  throw arg_error ( "BAD: Command::exec: no callback!" );
263 }
264 
265 template <class C>
266 int
268 (
269  C& Instance,
270  char* command,
271  char** argv,
272  int argc
273 )
274 {
275  DEBUG d ( __FUNCTION__,__FILE__,__LINE__ );
276  if ( ! this->cpp_callback )
277  {
278  throw arg_error ( "Command::exec: no cpp_callback" );
279  }
280  return ( CALL_MEMBER_FN ( Instance, this->cpp_callback ) ( command, argv, argc ) );
281 }
282 
283 }; // namespace LIBCLI
284 
285 #endif
unsigned int unique_len
Definition: command.hxx:75
#define free_z(p)
Definition: common.hxx:37
cpp_callback_func cpp_callback
Definition: command.hxx:82
PRIVLEVEL
Definition: common.hxx:49
c_callback_func c_callback
Definition: command.hxx:81
bool have_callback
Definition: command.hxx:80
Command * next
Definition: command.hxx:76
static char C[28]
Definition: crypt-win.c:86
int exec(char *command, char **argv, int argc)
Definition: command.hxx:243
Command * children
Definition: command.hxx:77
Definition: debug.hxx:31
PRIVLEVEL privilege
Definition: command.hxx:73
#define CALL_MEMBER_FN(object, ptrToMember)
Definition: common.hxx:36
Command * parent
Definition: command.hxx:78
char * command
Definition: command.hxx:71