From 6a823be7d1f61ff1ef57cc60bdbba7821fee2c62 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Sat, 17 Sep 2011 13:15:20 +0300 Subject: [PATCH] Pass tokensplitter& as argument to command handler --- command.cpp | 17 +++++++++++------ keymapper.cpp | 10 ++++------ mainloop.cpp | 15 ++++++--------- moviedata.cpp | 15 ++++++--------- settings.cpp | 15 ++++++--------- 5 files changed, 33 insertions(+), 39 deletions(-) diff --git a/command.cpp b/command.cpp index 4b9c5537..bf5dfff4 100644 --- a/command.cpp +++ b/command.cpp @@ -36,10 +36,9 @@ namespace messages << "alias " << i->first << " " << *j << std::endl; }); - function_ptr_command unalias_command("unalias-command", "unalias a command", + function_ptr_command unalias_command("unalias-command", "unalias a command", "Syntax: unalias-command \nClear expansion of alias \n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { - tokensplitter t(args); + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { std::string aliasname = t; if(t) throw std::runtime_error("This command only takes one argument"); @@ -49,11 +48,10 @@ namespace messages << "Command '" << aliasname << "' unaliased" << std::endl; }); - function_ptr_command alias_command("alias-command", "alias a command", + function_ptr_command alias_command("alias-command", "alias a command", "Syntax: alias-command \nAppend to expansion of alias \n" "Valid alias names can't be empty nor start with '*' or '?'\n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { - tokensplitter t(args); + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { std::string aliasname = t; std::string command = t.tail(); if(command == "") @@ -241,3 +239,10 @@ void invoke_command_fn(void (*fn)(struct arg_filename a), const std::string& arg b.v = args; fn(b); } + +template<> +void invoke_command_fn(void (*fn)(tokensplitter& a), const std::string& args) +{ + tokensplitter t(args); + fn(t); +} diff --git a/keymapper.cpp b/keymapper.cpp index 9f2f249d..6dec340e 100644 --- a/keymapper.cpp +++ b/keymapper.cpp @@ -11,12 +11,11 @@ namespace { - function_ptr_command bind_key("bind-key", "Bind a (pseudo-)key", + function_ptr_command bind_key("bind-key", "Bind a (pseudo-)key", "Syntax: bind-key [/] \nBind command to specified key (with specified " " modifiers)\n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { std::string mod, modmask, keyname, command; - tokensplitter t(args); std::string mod_or_key = t; if(mod_or_key.find_first_of("/") < mod_or_key.length()) { //Mod field. @@ -34,11 +33,10 @@ namespace window::bind(mod, modmask, keyname, command); }); - function_ptr_command unbind_key("unbind-key", "Unbind a (pseudo-)key", + function_ptr_command unbind_key("unbind-key", "Unbind a (pseudo-)key", "Syntax: unbind-key [/] \nUnbind specified key (with specified modifiers)\n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { std::string mod, modmask, keyname, command; - tokensplitter t(args); std::string mod_or_key = t; if(mod_or_key.find_first_of("/") < mod_or_key.length()) { //Mod field. diff --git a/mainloop.cpp b/mainloop.cpp index 4eb6f2d3..5958a545 100644 --- a/mainloop.cpp +++ b/mainloop.cpp @@ -614,10 +614,9 @@ namespace redraw_framebuffer(); }); - function_ptr_command add_watch("add-watch", "Add a memory watch", + function_ptr_command add_watch("add-watch", "Add a memory watch", "Syntax: add-watch \nAdds a new memory watch\n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { - tokensplitter t(args); + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { std::string name = t; if(name == "" || t.tail() == "") throw std::runtime_error("syntax: add-watch "); @@ -626,10 +625,9 @@ namespace update_movie_state(); }); - function_ptr_command remove_watch("remove-watch", "Remove a memory watch", + function_ptr_command remove_watch("remove-watch", "Remove a memory watch", "Syntax: remove-watch \nRemoves a memory watch\n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { - tokensplitter t(args); + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { std::string name = t; if(name == "" || t.tail() != "") { messages << "syntax: remove-watch " << std::endl; @@ -659,10 +657,9 @@ namespace while(1); }); - function_ptr_command mouse_button_handler("mouse_button", "no description available", + function_ptr_command mouse_button_handler("mouse_button", "no description available", "No help available\n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { - tokensplitter t(args); + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { std::string x = t; std::string y = t; std::string b = t; diff --git a/moviedata.cpp b/moviedata.cpp index 2829192c..12c8d13e 100644 --- a/moviedata.cpp +++ b/moviedata.cpp @@ -56,31 +56,28 @@ namespace messages << "End of authors list" << std::endl; }); - function_ptr_command add_author("add-author", "Add an author", + function_ptr_command add_author("add-author", "Add an author", "Syntax: add-author \nSyntax: add-author |\n" "Syntax: add-author |\nAdds a new author\n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { - tokensplitter t(args); + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { auto g = split_author(t.tail()); our_movie.authors.push_back(g); messages << (our_movie.authors.size() - 1) << ": " << g.first << "|" << g.second << std::endl; }); - function_ptr_command remove_author("remove-author", "Remove an author", + function_ptr_command remove_author("remove-author", "Remove an author", "Syntax: remove-author \nRemoves author with ID \n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { - tokensplitter t(args); + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { uint64_t index = parse_value(t.tail()); if(index >= our_movie.authors.size()) throw std::runtime_error("No such author"); our_movie.authors.erase(our_movie.authors.begin() + index); }); - function_ptr_command edit_author("edit-author", "Edit an author", + function_ptr_command edit_author("edit-author", "Edit an author", "Syntax: edit-author \nSyntax: edit-author |\n" "Syntax: edit-author |\nEdits author name\n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { - tokensplitter t(args); + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { uint64_t index = parse_value(t); if(index >= our_movie.authors.size()) throw std::runtime_error("No such author"); diff --git a/settings.cpp b/settings.cpp index 316eebc2..6cf25832 100644 --- a/settings.cpp +++ b/settings.cpp @@ -10,11 +10,10 @@ namespace { std::map* settings; - function_ptr_command set_setting("set-setting", "set a setting", + function_ptr_command set_setting("set-setting", "set a setting", "Syntax: set-setting []\nSet setting to a new value. Omit to set to ''\n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { std::string syntax = "Syntax: set-setting []"; - tokensplitter t(args); std::string settingname = t; std::string settingvalue = t.tail(); if(settingname == "") @@ -24,11 +23,10 @@ namespace << std::endl; }); - function_ptr_command unset_setting("unset-setting", "unset a setting", + function_ptr_command unset_setting("unset-setting", "unset a setting", "Syntax: unset-setting \nTry to unset a setting. Note that not all settings can be unset\n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { std::string syntax = "Syntax: unset-setting "; - tokensplitter t(args); std::string settingname = t; if(settingname == "" || t) throw std::runtime_error("Expected setting name and nothing else"); @@ -36,10 +34,9 @@ namespace messages << "Setting '" << settingname << "' unset" << std::endl; }); - function_ptr_command get_command("get-setting", "get value of a setting", + function_ptr_command get_command("get-setting", "get value of a setting", "Syntax: get-setting \nShow value of setting\n", - [](const std::string& args) throw(std::bad_alloc, std::runtime_error) { - tokensplitter t(args); + [](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) { std::string settingname = t; if(settingname == "" || t.tail() != "") throw std::runtime_error("Expected setting name and nothing else");