Sirikata
|
00001 // Copyright (c) 2012 Sirikata Authors. All rights reserved. 00002 // Use of this source code is governed by a BSD-style license that can 00003 // be found in the LICENSE file. 00004 00005 #ifndef _SIRIKATA_LIBCORE_COMMAND_COMMANDER_HPP_ 00006 #define _SIRIKATA_LIBCORE_COMMAND_COMMANDER_HPP_ 00007 00008 #include <sirikata/core/command/Command.hpp> 00009 #include <sirikata/core/util/Factory.hpp> 00010 #include <sirikata/core/service/Context.hpp> 00011 #include <boost/thread.hpp> 00012 00013 namespace Sirikata { 00014 namespace Command { 00015 00022 class SIRIKATA_EXPORT Commander { 00023 public: 00024 Commander(); 00025 virtual ~Commander(); 00026 00037 virtual void registerCommand(const CommandKey& name, CommandHandler handler); 00041 virtual void unregisterCommand(const CommandKey& name); 00042 00046 virtual void result(CommandID id, const Result& result) = 0; 00047 00048 protected: 00052 CommandHandler getHandler(const CommandKey& name); 00053 00054 // Meta commands -- commands the commander implements to allow getting data about itself 00055 void commandListCommands(const Command& cmd, Commander* cmdr, CommandID cmdid); 00056 00057 typedef boost::recursive_mutex Mutex; 00058 typedef boost::lock_guard<Mutex> Lock; 00059 Mutex mMutex; 00060 00061 typedef std::tr1::unordered_map<CommandKey, CommandHandler> HandlerMap; 00062 HandlerMap mHandlers; 00063 }; // class Commandable 00064 00065 00066 class DuplicateHandlerException : public std::exception { 00067 public: 00068 DuplicateHandlerException() {} 00069 virtual ~DuplicateHandlerException() throw() {} 00070 00071 char const* what() const throw() { 00072 return "Duplicate handler found during Commander registration"; 00073 } 00074 private: 00075 }; 00076 00077 00078 class SIRIKATA_EXPORT CommanderFactory : 00079 public Factory2<Commander*, Context*, const String&>, 00080 public AutoSingleton<CommanderFactory> 00081 { 00082 public: 00083 static CommanderFactory& getSingleton(); 00084 static void destroy(); 00085 }; 00086 00087 } // namespace Command 00088 } // namespace Sirikata 00089 00090 00091 #endif //_SIRIKATA_LIBCORE_COMMAND_COMMANDER_HPP_