Refactor remaining tokensplitters
[lsnes.git] / include / core / command.hpp
blob3d2abe9ef4b0025cd167cb9979bd5cf4455982e1
1 #ifndef _command__hpp__included__
2 #define _command__hpp__included__
4 #include <stdexcept>
5 #include <string>
6 #include <set>
9 /**
10 * A command.
12 class command
14 public:
15 /**
16 * Register a new command.
18 * parameter cmd: The command to register.
19 * throws std::bad_alloc: Not enough memory.
21 command(const std::string& cmd) throw(std::bad_alloc);
23 /**
24 * Deregister a command.
26 virtual ~command() throw();
28 /**
29 * Invoke a command.
31 * parameter arguments: Arguments to command.
32 * throws std::bad_alloc: Not enough memory.
33 * throws std::runtime_error: Command execution failed.
35 virtual void invoke(const std::string& arguments) throw(std::bad_alloc, std::runtime_error) = 0;
37 /**
38 * Look up and invoke a command. The command will undergo alias expansion and recursion checking.
40 * parameter cmd: Command to exeucte.
42 static void invokeC(const std::string& cmd) throw();
43 /**
44 * Get set of aliases.
46 static std::set<std::string> get_aliases() throw(std::bad_alloc);
47 /**
48 * Get alias
50 static std::string get_alias_for(const std::string& aname) throw(std::bad_alloc);
51 /**
52 * Set alias
54 static void set_alias_for(const std::string& aname, const std::string& avalue) throw(std::bad_alloc);
55 /**
56 * Is alias name valid.
58 static bool valid_alias_name(const std::string& aname) throw(std::bad_alloc);
59 /**
60 * Get short help for command.
62 virtual std::string get_short_help() throw(std::bad_alloc);
64 /**
65 * Get long help for command.
67 virtual std::string get_long_help() throw(std::bad_alloc);
68 private:
69 command(const command&);
70 command& operator=(const command&);
71 std::string commandname;
74 /**
75 * Mandatory filename
77 struct arg_filename
79 /**
80 * The filename itself.
82 std::string v;
83 /**
84 * Return the filename.
86 * returns: The filename.
88 operator std::string() { return v; }
91 /**
92 * Run command function helper.
94 * parameter fn: Function pointer to invoke.
95 * parameter a: The arguments to pass.
97 template<typename... args>
98 void invoke_command_fn(void (*fn)(args... arguments), const std::string& a);
101 * Warp function pointer as command.
103 template<typename... args>
104 class function_ptr_command : public command
106 public:
108 * Create a new command.
110 * parameter name: Name of the command
111 * parameter description Description for the command
112 * parameter help: Help for the command.
113 * parameter fn: Function to call on command.
115 function_ptr_command(const std::string& name, const std::string& _description, const std::string& _help,
116 void (*_fn)(args... arguments)) throw(std::bad_alloc)
117 : command(name)
119 description = _description;
120 help = _help;
121 fn = _fn;
124 * Destroy a commnad.
126 ~function_ptr_command() throw()
130 * Invoke a command.
132 * parameter a: Arguments to function.
134 void invoke(const std::string& a) throw(std::bad_alloc, std::runtime_error)
136 invoke_command_fn(fn, a);
139 * Get short description.
141 * returns: Description.
142 * throw std::bad_alloc: Not enough memory.
144 std::string get_short_help() throw(std::bad_alloc)
146 return description;
149 * Get long help.
151 * returns: help.
152 * throw std::bad_alloc: Not enough memory.
154 std::string get_long_help() throw(std::bad_alloc)
156 return help;
158 private:
159 void (*fn)(args... arguments);
160 std::string description;
161 std::string help;
164 #endif