1 /* bzflag 2 * Copyright (c) 1993-2021 Tim Riker 3 * 4 * This package is free software; you can redistribute it and/or 5 * modify it under the terms of the license found in the file 6 * named COPYING that should have accompanied this file. 7 * 8 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 11 */ 12 13 #ifndef __COMMANDMANAGER_H__ 14 #define __COMMANDMANAGER_H__ 15 16 #include "common.h" 17 18 /* system interface headers */ 19 #include <string> 20 #include <map> 21 #include <vector> 22 23 /* common interface headers */ 24 #include "common.h" 25 #include "Singleton.h" 26 27 28 #define CMDMGR (CommandManager::instance()) 29 30 class CommandManager : public Singleton<CommandManager> 31 { 32 33 public: 34 35 CommandManager() = default; 36 ~CommandManager() = default; 37 38 // type of function that implements command. function should return 39 // a string with the output of the command (or the empty string if 40 // there's no output). 41 typedef std::vector<std::string> ArgList; 42 typedef std::string (*CommandFunction)(const std::string& name, const ArgList&, bool* error); 43 typedef void (*Callback)(const std::string& name, void* userData); 44 45 // add/replace a command handler 46 void add(const std::string& name, 47 CommandFunction, const std::string& help); 48 49 // remove a command handler 50 void remove(const std::string& name); 51 52 // get the help string for a command 53 std::string getHelp(const std::string& name) const; 54 55 // execute a command 56 std::string run(const std::string& name, const ArgList& args, bool *ret = NULL) const; 57 58 // parse and execute a command 59 std::string run(const std::string& cmd, bool *ret = NULL) const; 60 61 // invoke the callback for each registered command 62 void iterate(Callback, void* userData) const; 63 64 private: 65 66 static const char* readValue(const char* string, std::string* value); 67 static const char* readUnquoted(const char* string, std::string* value); 68 static const char* readQuoted(const char* string, std::string* value); 69 static const char* skipWhitespace(const char* string); 70 71 struct CmdInfo 72 { 73 public: 74 CommandFunction func; 75 std::string help; 76 }; 77 typedef std::map<std::string, CmdInfo> Commands; 78 79 Commands commands; 80 }; 81 82 83 #endif /* __COMMANDMANAGER_H__ */ 84 85 // Local Variables: *** 86 // mode: C++ *** 87 // tab-width: 4 *** 88 // c-basic-offset: 4 *** 89 // indent-tabs-mode: nil *** 90 // End: *** 91 // ex: shiftwidth=4 tabstop=4 92