2011-09-14 20:06:36 +03:00
|
|
|
#ifndef _command__hpp__included__
|
|
|
|
#define _command__hpp__included__
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
2011-11-05 20:09:10 +02:00
|
|
|
#include <set>
|
2011-09-14 20:06:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A command.
|
|
|
|
*/
|
|
|
|
class command
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Register a new command.
|
2011-09-15 22:56:33 +03:00
|
|
|
*
|
2011-09-14 20:06:36 +03:00
|
|
|
* parameter cmd: The command to register.
|
|
|
|
* throws std::bad_alloc: Not enough memory.
|
|
|
|
*/
|
|
|
|
command(const std::string& cmd) throw(std::bad_alloc);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deregister a command.
|
|
|
|
*/
|
|
|
|
virtual ~command() throw();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke a command.
|
2011-09-15 22:56:33 +03:00
|
|
|
*
|
2011-09-14 20:06:36 +03:00
|
|
|
* parameter arguments: Arguments to command.
|
|
|
|
* throws std::bad_alloc: Not enough memory.
|
|
|
|
* throws std::runtime_error: Command execution failed.
|
|
|
|
*/
|
2011-09-17 01:05:41 +03:00
|
|
|
virtual void invoke(const std::string& arguments) throw(std::bad_alloc, std::runtime_error) = 0;
|
2011-09-14 20:06:36 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up and invoke a command. The command will undergo alias expansion and recursion checking.
|
2011-09-15 22:56:33 +03:00
|
|
|
*
|
2011-09-14 20:06:36 +03:00
|
|
|
* parameter cmd: Command to exeucte.
|
|
|
|
*/
|
2011-09-17 01:05:41 +03:00
|
|
|
static void invokeC(const std::string& cmd) throw();
|
2011-11-05 20:09:10 +02:00
|
|
|
/**
|
|
|
|
* Get set of aliases.
|
|
|
|
*/
|
|
|
|
static std::set<std::string> get_aliases() throw(std::bad_alloc);
|
|
|
|
/**
|
|
|
|
* Get alias
|
|
|
|
*/
|
|
|
|
static std::string get_alias_for(const std::string& aname) throw(std::bad_alloc);
|
|
|
|
/**
|
|
|
|
* Set alias
|
|
|
|
*/
|
|
|
|
static void set_alias_for(const std::string& aname, const std::string& avalue) throw(std::bad_alloc);
|
|
|
|
/**
|
|
|
|
* Is alias name valid.
|
|
|
|
*/
|
|
|
|
static bool valid_alias_name(const std::string& aname) throw(std::bad_alloc);
|
2011-09-14 20:06:36 +03:00
|
|
|
/**
|
|
|
|
* Get short help for command.
|
|
|
|
*/
|
|
|
|
virtual std::string get_short_help() throw(std::bad_alloc);
|
2011-09-15 22:56:33 +03:00
|
|
|
|
2011-09-14 20:06:36 +03:00
|
|
|
/**
|
|
|
|
* Get long help for command.
|
|
|
|
*/
|
|
|
|
virtual std::string get_long_help() throw(std::bad_alloc);
|
|
|
|
private:
|
|
|
|
command(const command&);
|
|
|
|
command& operator=(const command&);
|
|
|
|
std::string commandname;
|
|
|
|
};
|
|
|
|
|
2011-09-17 12:00:49 +03:00
|
|
|
/**
|
|
|
|
* Mandatory filename
|
|
|
|
*/
|
|
|
|
struct arg_filename
|
|
|
|
{
|
2011-09-26 19:02:43 +03:00
|
|
|
/**
|
|
|
|
* The filename itself.
|
|
|
|
*/
|
2011-09-17 12:00:49 +03:00
|
|
|
std::string v;
|
2011-09-26 19:02:43 +03:00
|
|
|
/**
|
|
|
|
* Return the filename.
|
|
|
|
*
|
|
|
|
* returns: The filename.
|
|
|
|
*/
|
2011-09-17 12:00:49 +03:00
|
|
|
operator std::string() { return v; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-09-26 19:02:43 +03:00
|
|
|
* Run command function helper.
|
|
|
|
*
|
|
|
|
* parameter fn: Function pointer to invoke.
|
|
|
|
* parameter a: The arguments to pass.
|
2011-09-17 12:00:49 +03:00
|
|
|
*/
|
|
|
|
template<typename... args>
|
|
|
|
void invoke_command_fn(void (*fn)(args... arguments), const std::string& a);
|
|
|
|
|
2011-09-17 00:06:20 +03:00
|
|
|
/**
|
|
|
|
* Warp function pointer as command.
|
|
|
|
*/
|
2011-09-17 12:00:49 +03:00
|
|
|
template<typename... args>
|
2011-09-17 00:06:20 +03:00
|
|
|
class function_ptr_command : public command
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create a new command.
|
2011-09-26 19:02:43 +03:00
|
|
|
*
|
2011-09-17 00:06:20 +03:00
|
|
|
* parameter name: Name of the command
|
|
|
|
* parameter description Description for the command
|
|
|
|
* parameter help: Help for the command.
|
|
|
|
* parameter fn: Function to call on command.
|
|
|
|
*/
|
2011-09-17 12:00:49 +03:00
|
|
|
function_ptr_command(const std::string& name, const std::string& _description, const std::string& _help,
|
|
|
|
void (*_fn)(args... arguments)) throw(std::bad_alloc)
|
|
|
|
: command(name)
|
|
|
|
{
|
|
|
|
description = _description;
|
|
|
|
help = _help;
|
|
|
|
fn = _fn;
|
|
|
|
}
|
2011-09-17 00:06:20 +03:00
|
|
|
/**
|
|
|
|
* Destroy a commnad.
|
|
|
|
*/
|
2011-09-17 12:00:49 +03:00
|
|
|
~function_ptr_command() throw()
|
|
|
|
{
|
|
|
|
}
|
2011-09-17 00:06:20 +03:00
|
|
|
/**
|
|
|
|
* Invoke a command.
|
2011-09-26 19:02:43 +03:00
|
|
|
*
|
2011-09-17 12:00:49 +03:00
|
|
|
* parameter a: Arguments to function.
|
2011-09-17 00:06:20 +03:00
|
|
|
*/
|
2011-09-17 12:00:49 +03:00
|
|
|
void invoke(const std::string& a) throw(std::bad_alloc, std::runtime_error)
|
|
|
|
{
|
|
|
|
invoke_command_fn(fn, a);
|
|
|
|
}
|
2011-09-17 00:06:20 +03:00
|
|
|
/**
|
|
|
|
* Get short description.
|
2011-09-26 19:02:43 +03:00
|
|
|
*
|
2011-09-17 00:06:20 +03:00
|
|
|
* returns: Description.
|
|
|
|
* throw std::bad_alloc: Not enough memory.
|
|
|
|
*/
|
2011-09-17 12:00:49 +03:00
|
|
|
std::string get_short_help() throw(std::bad_alloc)
|
|
|
|
{
|
|
|
|
return description;
|
|
|
|
}
|
2011-09-17 00:06:20 +03:00
|
|
|
/**
|
|
|
|
* Get long help.
|
2011-09-26 19:02:43 +03:00
|
|
|
*
|
2011-09-17 00:06:20 +03:00
|
|
|
* returns: help.
|
|
|
|
* throw std::bad_alloc: Not enough memory.
|
|
|
|
*/
|
2011-09-17 12:00:49 +03:00
|
|
|
std::string get_long_help() throw(std::bad_alloc)
|
|
|
|
{
|
|
|
|
return help;
|
|
|
|
}
|
2011-09-17 00:06:20 +03:00
|
|
|
private:
|
2011-09-17 12:00:49 +03:00
|
|
|
void (*fn)(args... arguments);
|
2011-09-17 00:06:20 +03:00
|
|
|
std::string description;
|
|
|
|
std::string help;
|
|
|
|
};
|
2011-09-14 20:06:36 +03:00
|
|
|
|
2011-09-17 00:06:20 +03:00
|
|
|
#endif
|