00001 00033 #include "llviewerprecompiledheaders.h" 00034 00035 #include "llcommandhandler.h" 00036 00037 // system includes 00038 #include <boost/tokenizer.hpp> 00039 00040 //--------------------------------------------------------------------------- 00041 // Underlying registry for command handlers, not directly accessible. 00042 //--------------------------------------------------------------------------- 00043 00044 class LLCommandHandlerRegistry 00045 { 00046 public: 00047 static LLCommandHandlerRegistry& instance(); 00048 void add(const char* cmd, LLCommandHandler* handler); 00049 bool dispatch(const std::string& cmd, const std::vector<std::string>& params); 00050 00051 private: 00052 std::map<std::string, LLCommandHandler*> mMap; 00053 }; 00054 00055 // static 00056 LLCommandHandlerRegistry& LLCommandHandlerRegistry::instance() 00057 { 00058 // Force this to be initialized on first call, because we're going 00059 // to be adding items to the std::map before main() and we can't 00060 // rely on a global being initialized in the right order. 00061 static LLCommandHandlerRegistry instance; 00062 return instance; 00063 } 00064 00065 void LLCommandHandlerRegistry::add(const char* cmd, LLCommandHandler* handler) 00066 { 00067 mMap[cmd] = handler; 00068 } 00069 00070 bool LLCommandHandlerRegistry::dispatch(const std::string& cmd, 00071 const std::vector<std::string>& params) 00072 { 00073 std::map<std::string, LLCommandHandler*>::iterator it = mMap.find(cmd); 00074 if (it == mMap.end()) return false; 00075 LLCommandHandler* handler = it->second; 00076 if (!handler) return false; 00077 return handler->handle(params); 00078 } 00079 00080 //--------------------------------------------------------------------------- 00081 // Automatic registration of commands, runs before main() 00082 //--------------------------------------------------------------------------- 00083 00084 LLCommandHandler::LLCommandHandler(const char* cmd) 00085 { 00086 LLCommandHandlerRegistry::instance().add(cmd, this); 00087 } 00088 00089 LLCommandHandler::~LLCommandHandler() 00090 { 00091 // Don't care about unregistering these, all the handlers 00092 // should be static objects. 00093 } 00094 00095 //--------------------------------------------------------------------------- 00096 // Public interface 00097 //--------------------------------------------------------------------------- 00098 00099 // static 00100 bool LLCommandDispatcher::dispatch(const std::string& cmd, const std::vector<std::string>& params) 00101 { 00102 return LLCommandHandlerRegistry::instance().dispatch(cmd, params); 00103 }