lsnes/generic/command.hpp

187 lines
3.7 KiB
C++
Raw Normal View History

#ifndef _command__hpp__included__
#define _command__hpp__included__
#include <stdexcept>
#include <string>
/**
* A command.
*/
class command
{
public:
/**
* Register a new command.
2011-09-15 22:56:33 +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
*
* parameter arguments: Arguments to command.
* throws std::bad_alloc: Not enough memory.
* throws std::runtime_error: Command execution failed.
*/
virtual void invoke(const std::string& arguments) throw(std::bad_alloc, std::runtime_error) = 0;
/**
* Look up and invoke a command. The command will undergo alias expansion and recursion checking.
2011-09-15 22:56:33 +03:00
*
* parameter cmd: Command to exeucte.
*/
static void invokeC(const std::string& cmd) throw();
/**
* Get short help for command.
*/
virtual std::string get_short_help() throw(std::bad_alloc);
2011-09-15 22:56:33 +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;
};
/**
* Splits string to fields on ' ' and '\t', with multiple whitespace collapsed into one.
*/
class tokensplitter
{
public:
/**
* Create a new splitter.
2011-09-15 22:56:33 +03:00
*
* parameter _line: The line to start splitting.
* throws std::bad_alloc: Not enough memory.
*/
tokensplitter(const std::string& _line) throw(std::bad_alloc);
/**
2011-09-26 19:02:43 +03:00
* Are there more tokens coming?
2011-09-15 22:56:33 +03:00
*
2011-09-26 19:02:43 +03:00
* returns: True if there is at least one token coming, false otherwise.
*/
operator bool() throw();
/**
* Get the next token.
2011-09-15 22:56:33 +03:00
*
2011-09-26 19:02:43 +03:00
* returns: The next token from line. If there are no more tokens, returns "".
* throws std::bad_alloc: Not enough memory.
*/
operator std::string() throw(std::bad_alloc);
/**
2011-09-26 19:02:43 +03:00
* Get all remaining line in one go.
2011-09-15 22:56:33 +03:00
*
* returns: All remaining parts of line as-is.
* throws std::bad_alloc: Not enough memory.
*/
std::string tail() throw(std::bad_alloc);
private:
std::string line;
size_t position;
};
/**
* Mandatory filename
*/
struct arg_filename
{
2011-09-26 19:02:43 +03:00
/**
* The filename itself.
*/
std::string v;
2011-09-26 19:02:43 +03:00
/**
* Return the filename.
*
* returns: The filename.
*/
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.
*/
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.
*/
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.
*/
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.
*/
~function_ptr_command() throw()
{
}
2011-09-17 00:06:20 +03:00
/**
* Invoke a command.
2011-09-26 19:02:43 +03:00
*
* parameter a: Arguments to function.
2011-09-17 00:06:20 +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.
*/
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.
*/
std::string get_long_help() throw(std::bad_alloc)
{
return help;
}
2011-09-17 00:06:20 +03:00
private:
void (*fn)(args... arguments);
2011-09-17 00:06:20 +03:00
std::string description;
std::string help;
};
2011-09-17 00:06:20 +03:00
#endif