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();
101 std::set
<std::string
> get_aliases() throw(std::bad_alloc
);
105 std::string
get_alias_for(const std::string
& aname
) throw(std::bad_alloc
);
109 void set_alias_for(const std::string
& aname
, const std::string
& avalue
) throw(std::bad_alloc
);
111 * Is alias name valid.
113 bool valid_alias_name(const std::string
& aname
) throw(std::bad_alloc
);
115 * Register a command.
117 void do_register(const std::string
& name
, base
& cmd
) throw(std::bad_alloc
);
119 * Unregister a command.
121 void do_unregister(const std::string
& name
, base
& cmd
) throw(std::bad_alloc
);
123 * Add all commands (including future ones) in given set.
125 void add_set(set
& s
) throw(std::bad_alloc
);
127 * Drop a set of commands.
129 void drop_set(set
& s
) throw();
131 * Set the output stream.
133 void set_output(std::ostream
& s
);
135 * Set the OOM panic routine.
137 void set_oom_panic(void (*fn
)());
139 class listener
: public set::listener
142 listener(group
& _grp
);
144 void create(set
& s
, const std::string
& name
, factory_base
& cmd
);
145 void destroy(set
& s
, const std::string
& name
);
150 std::set
<std::string
> command_stack
;
151 std::map
<std::string
, std::list
<std::string
>> aliases
;
152 std::ostream
* output
;
153 void (*oom_panic_routine
)();
164 * Register a new command.
166 * parameter group: The group command will be part of.
167 * parameter cmd: The command to register.
168 * parameter dynamic: Should the object be freed when its parent group dies?
169 * throws std::bad_alloc: Not enough memory.
171 base(group
& group
, const std::string
& cmd
, bool dynamic
) throw(std::bad_alloc
);
174 * Deregister a command.
176 virtual ~base() throw();
181 * parameter arguments: Arguments to command.
182 * throws std::bad_alloc: Not enough memory.
183 * throws std::runtime_error: Command execution failed.
185 virtual void invoke(const std::string
& arguments
) throw(std::bad_alloc
, std::runtime_error
) = 0;
187 * Get short help for command.
189 virtual std::string
get_short_help() throw(std::bad_alloc
);
192 * Get long help for command.
194 virtual std::string
get_long_help() throw(std::bad_alloc
);
196 * Get name of command.
198 const std::string
& get_name() { return commandname
; }
200 * Notify that the parent group died.
202 * Note: Assumed to be called with global lock held.
204 void group_died() throw();
207 base
& operator=(const base
&);
208 std::string commandname
;
219 factory_base() throw() {}
221 * Register a new command.
223 * parameter _set: The set command will be part of.
224 * parameter cmd: The command to register.
225 * throws std::bad_alloc: Not enough memory.
227 void _factory_base(set
& _set
, const std::string
& cmd
) throw(std::bad_alloc
);
231 virtual ~factory_base() throw();
233 * Make a new command.
235 virtual base
* make(group
& grp
) = 0;
237 * Notify that the parent set died.
239 * Note: Assumed to be called with global lock held.
241 void set_died() throw();
243 factory_base(const factory_base
&);
244 factory_base
& operator=(const factory_base
&);
245 std::string commandname
;
250 * Name, Description and help for command.
259 * The short description.
274 * The filename itself.
278 * Return the filename.
280 * returns: The filename.
282 operator std::string() { return v
; }
286 * Run command function helper.
288 * parameter fn: Function pointer to invoke.
289 * parameter a: The arguments to pass.
291 template<typename
... args
>
292 void invoke_fn(std::function
<void(args
... arguments
)> fn
, const std::string
& a
);
295 * Warp function pointer as command.
297 template<typename
... args
>
298 class _fnptr
: public base
302 * Create a new command.
304 * parameter group: The group command will be part of.
305 * parameter name: Name of the command
306 * parameter description Description for the command
307 * parameter help: Help for the command.
308 * parameter fn: Function to call on command.
309 * parameter dynamic: Should the object be freed when its parent group dies?
311 _fnptr(group
& group
, const std::string
& name
, const std::string
& _description
,
312 const std::string
& _help
, void (*_fn
)(args
... arguments
), bool dynamic
= false) throw(std::bad_alloc
)
313 : base(group
, name
, dynamic
)
315 shorthelp
= _description
;
320 * Create a new command.
322 * parameter _set: The set command will be part of.
323 * parameter name: Name of the command
324 * parameter fn: Function to call on command.
325 * parameter description Description&Help for the command
327 _fnptr(group
& _group
, stub _name
, std::function
<void(args
... arguments
)> _fn
) throw(std::bad_alloc
)
328 : base(_group
, _name
.name
, false)
330 shorthelp
= _name
.desc
;
343 * parameter a: Arguments to function.
345 void invoke(const std::string
& a
) throw(std::bad_alloc
, std::runtime_error
)
350 * Get short description.
352 * returns: Description.
353 * throw std::bad_alloc: Not enough memory.
355 std::string
get_short_help() throw(std::bad_alloc
)
363 * throw std::bad_alloc: Not enough memory.
365 std::string
get_long_help() throw(std::bad_alloc
)
370 std::function
<void(args
... arguments
)> fn
;
371 std::string shorthelp
;
376 * Function pointer command factory.
378 template<typename
... args
>
379 class fnptr
: public factory_base
383 * Create a new command.
385 * parameter _set: The set command will be part of.
386 * parameter desc: Command descriptor.
387 * parameter fn: Function to call on command.
389 fnptr(set
& _set
, stub _name
, void (*_fn
)(args
... arguments
)) throw(std::bad_alloc
)
391 shorthelp
= _name
.desc
;
395 _factory_base(_set
, name
);
398 * Create a new command.
400 * parameter _set: The set command will be part of.
401 * parameter name: Name of the command
402 * parameter description Description for the command
403 * parameter help: Help for the command.
404 * parameter fn: Function to call on command.
406 fnptr(set
& _set
, const std::string
& _name
, const std::string
& _description
,
407 const std::string
& _help
, void (*_fn
)(args
... arguments
)) throw(std::bad_alloc
)
409 shorthelp
= _description
;
413 _factory_base(_set
, name
);
424 base
* make(group
& grp
) throw(std::bad_alloc
)
426 return new _fnptr
<args
...>(grp
, name
, shorthelp
, help
, fn
, true);
429 void (*fn
)(args
... arguments
);
430 std::string shorthelp
;
436 * Generic byname factory.
439 class byname_factory
: public factory_base
443 * Create a new factory.
445 byname_factory(set
& s
, const std::string
& _name
)
448 _factory_base(s
, name
);
453 ~byname_factory() throw()
459 base
* make(group
& grp
) throw(std::bad_alloc
)
461 return new T(grp
, name
);