llcommandhandler.cpp

Go to the documentation of this file.
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 struct LLCommandHandlerInfo
00044 {
00045         bool mAllowFromExternalBrowser;
00046         LLCommandHandler* mHandler;     // safe, all of these are static objects
00047 };
00048 
00049 class LLCommandHandlerRegistry
00050 {
00051 public:
00052         static LLCommandHandlerRegistry& instance();
00053         void add(const char* cmd, bool allow_from_external_browser, LLCommandHandler* handler);
00054         bool dispatch(const std::string& cmd, bool from_external_browser, const LLSD& params, const LLSD& queryMap);
00055 
00056 private:
00057         std::map<std::string, LLCommandHandlerInfo> mMap;
00058 };
00059 
00060 // static 
00061 LLCommandHandlerRegistry& LLCommandHandlerRegistry::instance()
00062 {
00063         // Force this to be initialized on first call, because we're going
00064         // to be adding items to the std::map before main() and we can't
00065         // rely on a global being initialized in the right order.
00066         static LLCommandHandlerRegistry instance;
00067         return instance;
00068 }
00069 
00070 void LLCommandHandlerRegistry::add(const char* cmd, bool allow_from_external_browser, LLCommandHandler* handler)
00071 {
00072         LLCommandHandlerInfo info;
00073         info.mAllowFromExternalBrowser = allow_from_external_browser;
00074         info.mHandler = handler;
00075 
00076         mMap[cmd] = info;
00077 }
00078 
00079 bool LLCommandHandlerRegistry::dispatch(const std::string& cmd,
00080                                                                                 bool from_external_browser,
00081                                                                                 const LLSD& params,
00082                                                                                 const LLSD& queryMap)
00083 {
00084         std::map<std::string, LLCommandHandlerInfo>::iterator it = mMap.find(cmd);
00085         if (it == mMap.end()) return false;
00086         const LLCommandHandlerInfo& info = it->second;
00087         if (from_external_browser && !info.mAllowFromExternalBrowser)
00088         {
00089                 // block request from external browser, but report as
00090                 // "handled" because it was well formatted.
00091                 return true;
00092         }
00093         if (!info.mHandler) return false;
00094         return info.mHandler->handle(params, queryMap);
00095 }
00096 
00097 //---------------------------------------------------------------------------
00098 // Automatic registration of commands, runs before main()
00099 //---------------------------------------------------------------------------
00100 
00101 LLCommandHandler::LLCommandHandler(const char* cmd, bool allow_from_external_browser)
00102 {
00103         LLCommandHandlerRegistry::instance().add(cmd, allow_from_external_browser, this);
00104 }
00105 
00106 LLCommandHandler::~LLCommandHandler()
00107 {
00108         // Don't care about unregistering these, all the handlers
00109         // should be static objects.
00110 }
00111 
00112 //---------------------------------------------------------------------------
00113 // Public interface
00114 //---------------------------------------------------------------------------
00115 
00116 // static
00117 bool LLCommandDispatcher::dispatch(const std::string& cmd,
00118                                                                    bool from_external_browser,
00119                                                                    const LLSD& params, const LLSD& queryMap)
00120 {
00121         return LLCommandHandlerRegistry::instance().dispatch(
00122                 cmd, from_external_browser, params, queryMap);
00123 }

Generated on Fri May 16 08:33:16 2008 for SecondLife by  doxygen 1.5.5