1 #ifndef _library_command__hpp__included__
2 #define _library_command__hpp__included__
24 * Set add/drop listener.
36 virtual void create(set
& s
, const std::string
& name
, factory_base
& cmd
) = 0;
38 * Deleted item from set.
40 virtual void destroy(set
& s
, const std::string
& name
) = 0;
42 * Destroyed the entiere set.
44 virtual void kill(set
& s
) = 0;
49 set() throw(std::bad_alloc
);
55 * Add a command to set.
57 void do_register(const std::string
& name
, factory_base
& cmd
) throw(std::bad_alloc
);
59 * Remove a command from set.
61 void do_unregister(const std::string
& name
, factory_base
& cmd
) throw(std::bad_alloc
);
63 * Add a notification callback and call ccb on all.
65 * Parameter listener: The listener to add.
67 void add_callback(listener
& listener
) throw(std::bad_alloc
);
69 * Drop a notification callback and call dcb on all.
71 * Parameter listener: The listener to drop.
73 void drop_callback(listener
& listener
) throw();
79 * A group of commands (with aliases).
85 * Create a new command group. This also places some builtin commands in that new group.
87 group() throw(std::bad_alloc
);
93 * Look up and invoke a command. The command will undergo alias expansion and recursion checking.
95 * parameter cmd: Command to exeucte.
97 void invoke(const std::string
& cmd
) throw();
99 * Look up and invoke a command. No alias expansion is performed, but recursion checking is.
101 * parameter cmd: Command to execute.
102 * parameter args: The parameters for command.
104 void invoke(const std::string
& cmd
, const std::string
& args
) throw();
106 * Get set of aliases.
108 std::set
<std::string
> get_aliases() throw(std::bad_alloc
);
112 std::string
get_alias_for(const std::string
& aname
) throw(std::bad_alloc
);
116 void set_alias_for(const std::string
& aname
, const std::string
& avalue
) throw(std::bad_alloc
);
118 * Is alias name valid.
120 bool valid_alias_name(const std::string
& aname
) throw(std::bad_alloc
);
122 * Register a command.
124 void do_register(const std::string
& name
, base
& cmd
) throw(std::bad_alloc
);
126 * Unregister a command.
128 void do_unregister(const std::string
& name
, base
& cmd
) throw(std::bad_alloc
);
130 * Add all commands (including future ones) in given set.
132 void add_set(set
& s
) throw(std::bad_alloc
);
134 * Drop a set of commands.
136 void drop_set(set
& s
) throw();
138 * Set the output stream.
140 void set_output(std::ostream
& s
);
142 * Set the OOM panic routine.
144 void set_oom_panic(void (*fn
)());
146 class listener
: public set::listener
149 listener(group
& _grp
);
151 void create(set
& s
, const std::string
& name
, factory_base
& cmd
);
152 void destroy(set
& s
, const std::string
& name
);
157 std::set
<std::string
> command_stack
;
158 std::map
<std::string
, std::list
<std::string
>> aliases
;
159 std::ostream
* output
;
160 void (*oom_panic_routine
)();
171 * Register a new command.
173 * parameter group: The group command will be part of.
174 * parameter cmd: The command to register.
175 * parameter dynamic: Should the object be freed when its parent group dies?
176 * throws std::bad_alloc: Not enough memory.
178 base(group
& group
, const std::string
& cmd
, bool dynamic
) throw(std::bad_alloc
);
181 * Deregister a command.
183 virtual ~base() throw();
188 * parameter arguments: Arguments to command.
189 * throws std::bad_alloc: Not enough memory.
190 * throws std::runtime_error: Command execution failed.
192 virtual void invoke(const std::string
& arguments
) throw(std::bad_alloc
, std::runtime_error
) = 0;
194 * Get short help for command.
196 virtual std::string
get_short_help() throw(std::bad_alloc
);
199 * Get long help for command.
201 virtual std::string
get_long_help() throw(std::bad_alloc
);
203 * Get name of command.
205 const std::string
& get_name() { return commandname
; }
207 * Notify that the parent group died.
209 * Note: Assumed to be called with global lock held.
211 void group_died() throw();
214 base
& operator=(const base
&);
215 std::string commandname
;
226 factory_base() throw() {}
228 * Register a new command.
230 * parameter _set: The set command will be part of.
231 * parameter cmd: The command to register.
232 * throws std::bad_alloc: Not enough memory.
234 void _factory_base(set
& _set
, const std::string
& cmd
) throw(std::bad_alloc
);
238 virtual ~factory_base() throw();
240 * Make a new command.
242 virtual base
* make(group
& grp
) = 0;
244 * Notify that the parent set died.
246 * Note: Assumed to be called with global lock held.
248 void set_died() throw();
250 factory_base(const factory_base
&);
251 factory_base
& operator=(const factory_base
&);
252 std::string commandname
;
257 * Name, Description and help for command.
266 * The short description.
281 * The filename itself.
285 * Return the filename.
287 * returns: The filename.
289 operator std::string() { return v
; }
293 * Run command function helper.
295 * parameter fn: Function pointer to invoke.
296 * parameter a: The arguments to pass.
298 template<typename
... args
>
299 void invoke_fn(std::function
<void(args
... arguments
)> fn
, const std::string
& a
);
302 * Warp function pointer as command.
304 template<typename
... args
>
305 class _fnptr
: public base
309 * Create a new command.
311 * parameter group: The group command will be part of.
312 * parameter name: Name of the command
313 * parameter description Description for the command
314 * parameter help: Help for the command.
315 * parameter fn: Function to call on command.
316 * parameter dynamic: Should the object be freed when its parent group dies?
318 _fnptr(group
& group
, const std::string
& name
, const std::string
& _description
,
319 const std::string
& _help
, void (*_fn
)(args
... arguments
), bool dynamic
= false) throw(std::bad_alloc
)
320 : base(group
, name
, dynamic
)
322 shorthelp
= _description
;
327 * Create a new command.
329 * parameter _set: The set command will be part of.
330 * parameter name: Name of the command
331 * parameter fn: Function to call on command.
332 * parameter description Description&Help for the command
334 _fnptr(group
& _group
, stub _name
, std::function
<void(args
... arguments
)> _fn
) throw(std::bad_alloc
)
335 : base(_group
, _name
.name
, false)
337 shorthelp
= _name
.desc
;
350 * parameter a: Arguments to function.
352 void invoke(const std::string
& a
) throw(std::bad_alloc
, std::runtime_error
)
357 * Get short description.
359 * returns: Description.
360 * throw std::bad_alloc: Not enough memory.
362 std::string
get_short_help() throw(std::bad_alloc
)
370 * throw std::bad_alloc: Not enough memory.
372 std::string
get_long_help() throw(std::bad_alloc
)
377 std::function
<void(args
... arguments
)> fn
;
378 std::string shorthelp
;
383 * Function pointer command factory.
385 template<typename
... args
>
386 class fnptr
: public factory_base
390 * Create a new command.
392 * parameter _set: The set command will be part of.
393 * parameter desc: Command descriptor.
394 * parameter fn: Function to call on command.
396 fnptr(set
& _set
, stub _name
, void (*_fn
)(args
... arguments
)) throw(std::bad_alloc
)
398 shorthelp
= _name
.desc
;
402 _factory_base(_set
, name
);
405 * Create a new command.
407 * parameter _set: The set command will be part of.
408 * parameter name: Name of the command
409 * parameter description Description for the command
410 * parameter help: Help for the command.
411 * parameter fn: Function to call on command.
413 fnptr(set
& _set
, const std::string
& _name
, const std::string
& _description
,
414 const std::string
& _help
, void (*_fn
)(args
... arguments
)) throw(std::bad_alloc
)
416 shorthelp
= _description
;
420 _factory_base(_set
, name
);
431 base
* make(group
& grp
) throw(std::bad_alloc
)
433 return new _fnptr
<args
...>(grp
, name
, shorthelp
, help
, fn
, true);
436 void (*fn
)(args
... arguments
);
437 std::string shorthelp
;
443 * Generic byname factory.
446 class byname_factory
: public factory_base
450 * Create a new factory.
452 byname_factory(set
& s
, const std::string
& _name
)
455 _factory_base(s
, name
);
460 ~byname_factory() throw()
466 base
* make(group
& grp
) throw(std::bad_alloc
)
468 return new T(grp
, name
);