Allow variable arguments to command functions
[lsnes.git] / command.hpp
blob853bd37e5d1fc04cbbfbcb42387251856476c15a
1 #ifndef _command__hpp__included__
2 #define _command__hpp__included__
4 #include <stdexcept>
5 #include <string>
8 /**
9 * A command.
11 class command
13 public:
14 /**
15 * Register a new command.
17 * parameter cmd: The command to register.
18 * throws std::bad_alloc: Not enough memory.
20 command(const std::string& cmd) throw(std::bad_alloc);
22 /**
23 * Deregister a command.
25 virtual ~command() throw();
27 /**
28 * Invoke a command.
30 * parameter arguments: Arguments to command.
31 * throws std::bad_alloc: Not enough memory.
32 * throws std::runtime_error: Command execution failed.
34 virtual void invoke(const std::string& arguments) throw(std::bad_alloc, std::runtime_error) = 0;
36 /**
37 * Look up and invoke a command. The command will undergo alias expansion and recursion checking.
39 * parameter cmd: Command to exeucte.
41 static void invokeC(const std::string& cmd) throw();
43 /**
44 * Get short help for command.
46 virtual std::string get_short_help() throw(std::bad_alloc);
48 /**
49 * Get long help for command.
51 virtual std::string get_long_help() throw(std::bad_alloc);
52 private:
53 command(const command&);
54 command& operator=(const command&);
55 std::string commandname;
58 /**
59 * Splits string to fields on ' ' and '\t', with multiple whitespace collapsed into one.
61 class tokensplitter
63 public:
64 /**
65 * Create a new splitter.
67 * parameter _line: The line to start splitting.
68 * throws std::bad_alloc: Not enough memory.
70 tokensplitter(const std::string& _line) throw(std::bad_alloc);
71 /**
72 * Is there more coming?
74 * returns: True if there is more, false otherwise.
75 * throws std::bad_alloc: Not enough memory.
77 operator bool() throw();
78 /**
79 * Get the next token.
81 * returns: The next token from line. If there is no more tokens, returns "".
82 * throws std::bad_alloc: Not enough memory.
84 operator std::string() throw(std::bad_alloc);
85 /**
86 * Get all that is remaining.
88 * returns: All remaining parts of line as-is.
89 * throws std::bad_alloc: Not enough memory.
91 std::string tail() throw(std::bad_alloc);
92 private:
93 std::string line;
94 size_t position;
97 /**
98 * Mandatory filename
100 struct arg_filename
102 std::string v;
103 operator std::string() { return v; }
107 * Run command function.
109 template<typename... args>
110 void invoke_command_fn(void (*fn)(args... arguments), const std::string& a);
113 * Warp function pointer as command.
115 template<typename... args>
116 class function_ptr_command : public command
118 public:
120 * Create a new command.
122 * parameter name: Name of the command
123 * parameter description Description for the command
124 * parameter help: Help for the command.
125 * parameter fn: Function to call on command.
127 function_ptr_command(const std::string& name, const std::string& _description, const std::string& _help,
128 void (*_fn)(args... arguments)) throw(std::bad_alloc)
129 : command(name)
131 description = _description;
132 help = _help;
133 fn = _fn;
136 * Destroy a commnad.
138 ~function_ptr_command() throw()
142 * Invoke a command.
144 * parameter a: Arguments to function.
146 void invoke(const std::string& a) throw(std::bad_alloc, std::runtime_error)
148 invoke_command_fn(fn, a);
151 * Get short description.
153 * returns: Description.
154 * throw std::bad_alloc: Not enough memory.
156 std::string get_short_help() throw(std::bad_alloc)
158 return description;
161 * Get long help.
163 * returns: help.
164 * throw std::bad_alloc: Not enough memory.
166 std::string get_long_help() throw(std::bad_alloc)
168 return help;
170 private:
171 void (*fn)(args... arguments);
172 std::string description;
173 std::string help;
176 #endif