diff options
Diffstat (limited to 'src/Command.cpp')
-rw-r--r-- | src/Command.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/Command.cpp b/src/Command.cpp new file mode 100644 index 0000000..a074930 --- /dev/null +++ b/src/Command.cpp @@ -0,0 +1,37 @@ +#include "../include/Command.hpp" +#include <unordered_map> + +using namespace std; + +namespace dchat +{ + unordered_map<string, CommandHandlerFunc> commandHandlerFuncs; + + bool Command::add(const string &cmd, CommandHandlerFunc handlerFunc) + { + auto it = commandHandlerFuncs.find(cmd); + if(it != commandHandlerFuncs.end()) + return false; + + commandHandlerFuncs[cmd] = handlerFunc; + return true; + } + + bool Command::call(const string &cmd, const vector<string> &args) + { + auto it = commandHandlerFuncs.find(cmd); + if(it != commandHandlerFuncs.end()) + { + try + { + it->second(args); + } + catch(exception &e) + { + fprintf(stderr, "Failed while executing command %s, reason: %s\n", cmd.c_str(), e.what()); + } + return true; + } + return false; + } +} |