libalcommon
1.14.5
|
00001 00010 #pragma once 00011 #ifndef _LIBALCOMMON_ALCOMMON_ALMODULECORE_H_ 00012 #define _LIBALCOMMON_ALCOMMON_ALMODULECORE_H_ 00013 00014 # include <boost/enable_shared_from_this.hpp> 00015 # include <boost/shared_ptr.hpp> 00016 # include <boost/noncopyable.hpp> 00017 00018 # include <alcommon/almoduleinfo.h> 00019 # include <alcommon/almethodinfo.h> 00020 # include <alcommon/alparamtype.h> 00021 # include <alerror/alerror.h> 00022 # include <alcommon/alfunctor.h> 00023 # include <map> 00024 00029 # define BIND_OBJ_METHOD(objptr, meth) \ 00030 { \ 00031 completeAndCheck(&meth, getCurrentMethodDescription()); \ 00032 bindMethod(createFunctor(objptr, &meth)); \ 00033 } 00034 00041 # define BIND_METHOD(x) BIND_OBJ_METHOD(this, x) 00042 00043 namespace AL 00044 { 00045 class ALModuleCoreImpl; 00046 class ALBroker; 00047 class ALProxy; 00048 class ALValue; 00049 00061 class ALModuleCore: public ::boost::enable_shared_from_this<ALModuleCore>, public ::boost::noncopyable 00062 { 00063 friend class baseModule; 00064 private: 00066 boost::shared_ptr<ALModuleCoreImpl> fImpl; 00067 00068 public: 00074 typedef boost::shared_ptr<ALModuleCore> Ptr; 00080 typedef boost::weak_ptr<ALModuleCore> WeakPtr; 00081 00086 enum ModuleType 00087 { 00088 CPP = 0, 00089 PYTHON = 1, 00090 RUBY = 2, 00091 LUA = 3, 00092 MATLAB = 4, 00093 URBI 00094 }; 00095 00105 ALModuleCore(boost::shared_ptr<ALBroker> pBroker, 00106 const std::string &pName); 00107 00109 virtual ~ALModuleCore(); 00110 00116 ALMethodInfo& getCurrentMethodDescription(); 00117 00122 boost::shared_ptr<ALModuleCore> getThis(); 00127 boost::shared_ptr<const ALModuleCore> getThis() const; 00128 00133 bool isClosing(); 00134 00140 void initModule(void); 00141 00149 boost::shared_ptr<AL::ALProxy> getProxy(const std::string &pModuleName); 00150 00155 std::string getBrokerName(); 00156 00161 ModuleType getModuleType(void); 00162 00167 void setModuleType(ModuleType pType); 00168 00177 virtual ALMethodInfo* execute(const std::string &pMethod, 00178 const AL::ALValue &pParams, 00179 AL::ALValue &pResult); 00180 00185 std::vector<std::string> getMethodList(); 00186 00192 AL::ALValue getMethodHelp(const std::string &pMethodName); 00193 00199 ALMethodInfo getMethodHelpObject(const std::string &pMethodName); 00200 00205 AL::ALValue moduleHelp(); 00206 00211 bool ping(void); 00212 00217 virtual std::string version(); 00218 00222 virtual void exit(); 00223 00228 const std::string& getName() const; 00229 00234 boost::shared_ptr<ALModuleInfo> getModuleInfo(); 00235 00240 virtual std::string httpGet(); 00241 00251 ALMethodInfo* getMethodInfoByNameMember(const std::string &pName, 00252 const std::vector<std::string> ¶mType, 00253 bool softCompare = true); 00254 00260 ALMethodInfo* getMethodInfo(const std::string &pName); 00261 00268 ALMethodInfo* getMethodInfo(const std::string &pName, 00269 std::vector<std::string> pParamsType); 00270 00277 ALMethodInfo* getMethodInfo(const std::string &pName, 00278 const AL::ALValue &pParams); 00279 00284 ALMethodInfo* getFunctionDesc(const std::string &pName); 00285 00290 ALMethodInfo* getFunctionDesc(const std::string &pName, 00291 std::vector<std::string> pParamsType); 00292 00297 ALMethodInfo* getFunctionDesc(const std::string &pName, 00298 const AL::ALValue &pParams); 00299 00304 ALMethodInfo* getFunctionDescByNameMember(const std::string &pName, 00305 const std::vector<std::string> ¶mType, 00306 bool softCompare = true); 00307 00312 boost::shared_ptr<ALBroker> getParentBroker() const; 00313 00318 void functionStop(int pTaskID); 00319 00320 00325 void setModuleDescription(const std::string &pDesc); 00326 00332 std::string getUsage(const std::string &methodName); 00333 00340 template <class T> 00341 static boost::shared_ptr<T> createModuleCore(boost::shared_ptr<ALBroker> pBroker) 00342 { 00343 boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker)); 00344 module->initModule(); // register module in broker 00345 try 00346 { 00347 // we call init on a ALModule::Ptr as init may be protected 00348 // init is a virtual method that can be reimplemented 00349 (boost::static_pointer_cast<ALModuleCore>(module))->init(); 00350 } 00351 catch(const ALError& e) 00352 { 00353 module->exit(); 00354 throw(e); 00355 } 00356 return module; 00357 } 00358 00366 template <class T> 00367 static boost::shared_ptr<T> createModuleCore(boost::shared_ptr<ALBroker> pBroker, 00368 const std::string &name) 00369 { 00370 boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker, name)); 00371 module->initModule(); 00372 try 00373 { 00374 // init if you redefined it 00375 (boost::static_pointer_cast<ALModuleCore>(module))->init(); 00376 } 00377 catch(const ALError& e) 00378 { 00379 module->exit(); 00380 throw(e); 00381 } 00382 return module; 00383 } 00384 00391 template <class T> 00392 static boost::shared_ptr<T> createUrbiModule(boost::shared_ptr<ALBroker> pBroker, 00393 const std::string &name) 00394 { 00395 boost::shared_ptr<T> module = createModuleCore<T>(pBroker, name); 00396 module->setModuleType(AL::ALModuleCore::URBI); 00397 return module; 00398 } 00399 00404 bool isModuleStopped(); 00405 00410 void setModuleID(int id); 00411 00416 int getModuleID(); 00417 00418 protected: 00424 virtual void init(void) {} 00425 00430 void bindMethod(boost::shared_ptr<ALFunctorBase> pFunctor); 00431 00440 void bindMethod(boost::shared_ptr<ALFunctorBase> pFunctor, 00441 const std::string &pName, 00442 const std::string &pClass, 00443 const std::string &pFunctionDescription, 00444 const ALMethodInfo &pMethodDescription); 00445 00450 void bindMethodOverload(boost::shared_ptr<ALFunctorBase> pFunctor); 00451 00460 void functionName(const std::string &pName, 00461 const std::string &pClass, 00462 const std::string &pFunctionDescription, 00463 int pMask = 0); 00464 00471 void addParam(const std::string &pName, 00472 const std::string &pDesc); 00473 00479 void addModuleExample(const std::string &pLanguage, 00480 const std::string &pExample); 00481 00487 void addMethodExample(const std::string &pLanguage, 00488 const std::string &pExample); 00489 00495 void setReturn(const std::string &pName, 00496 const std::string &pDesc); 00497 }; // !ALModuleCore 00498 } 00499 #endif // _LIBALCOMMON_ALCOMMON_ALMODULECORE_H_