libalcommon  2.1.0.18
/home/opennao/work/master/sdk/libnaoqi/libalcommon/alcommon/almodulecore.h
Go to the documentation of this file.
00001 
00010 #pragma once
00011 #ifndef _LIBALCOMMON_ALCOMMON_ALMODULECORE_H_
00012 #define _LIBALCOMMON_ALCOMMON_ALMODULECORE_H_
00013 
00014 # include <alcommon/api.h>
00015 # include <boost/enable_shared_from_this.hpp>
00016 # include <boost/shared_ptr.hpp>
00017 # include <boost/noncopyable.hpp>
00018 
00019 # include <alcommon/almoduleinfo.h>
00020 # include <alcommon/almethodinfo.h>
00021 # include <alcommon/alfunctor.h>
00022 
00023 # include <alerror/alerror.h>
00024 # include <qi/log.hpp>
00025 # include <map>
00026 
00027 #include <qitype/dynamicobject.hpp>
00028 #include <qitype/signature.hpp>
00029 #include <qitype/anyfunction.hpp>
00030 #include <qitype/dynamicobjectbuilder.hpp>
00031 #include <qitype/dynamicobject.hpp>
00032 
00033 //legacy hell, LEGACY HELL
00034 #define BIND_OBJ_METHOD(objptr, meth) _builder.advertiseMethod(_mBuilder, objptr, &meth)
00035 #define BIND_METHOD(meth) _builder.advertiseMethod(_mBuilder, this, &meth)
00036 
00037 #define BIND_OBJ_METHOD_PTR(objptr, methptr) _builder.advertiseMethod(_mBuilder, objptr, methptr)
00038 #define BIND_METHOD_PTR(methptr) _builder.advertiseMethod(_mBuilder, this, methptr)
00039 
00040 namespace AL
00041 {
00042   class ALBroker;
00043   class ALProxy;
00044   class ALValue;
00045   class ALModuleCorePrivate;
00046 
00058   class ALModuleCore: public ::boost::enable_shared_from_this<ALModuleCore>, public ::boost::noncopyable, public qi::DynamicObject
00059   {
00060   public:
00066     typedef boost::shared_ptr<ALModuleCore>       Ptr;
00072     typedef boost::weak_ptr<ALModuleCore>         WeakPtr;
00073 
00079     enum ModuleType
00080       {
00081         CPP    = 0, 
00082         PYTHON = 1, 
00083         RUBY   = 2, 
00084         LUA    = 3, 
00085         MATLAB = 4, 
00086         URBI        
00087       };
00088 
00098     ALModuleCore(boost::shared_ptr<ALBroker> pBroker,
00099                  const std::string &pName);
00100 
00102     virtual ~ALModuleCore();
00103 
00109     ALMethodInfo& getCurrentMethodDescription();
00110 
00115     boost::shared_ptr<ALModuleCore> getThis();
00120     boost::shared_ptr<const ALModuleCore> getThis() const;
00121 
00126     bool isClosing();
00127 
00133     void initModule(void);
00134 
00142     boost::shared_ptr<AL::ALProxy> getProxy(const std::string &pModuleName);
00143 
00148     std::string getBrokerName();
00149 
00154     ModuleType getModuleType(void);
00155 
00160     void setModuleType(ModuleType pType);
00161 
00170     virtual ALMethodInfo* execute(const std::string &pMethod,
00171                                   const AL::ALValue &pParams,
00172                                   AL::ALValue &pResult);
00173 
00178     std::vector<std::string> getMethodList();
00179 
00199     AL::ALValue getMethodHelp(const std::string &pMethodName);
00200 
00206     ALMethodInfo getMethodHelpObject(const std::string &pMethodName);
00207 
00212     AL::ALValue moduleHelp();
00213 
00218     bool ping(void);
00219 
00224     virtual std::string version();
00225 
00229     virtual void exit();
00230 
00235     const std::string& getName() const;
00236 
00241     boost::shared_ptr<ALModuleInfo> getModuleInfo();
00242 
00247     virtual std::string httpGet();
00248 
00258     ALMethodInfo* getMethodInfoByNameMember(const std::string &pName,
00259                                             const std::vector<std::string> &paramType,
00260                                             bool softCompare = true);
00261 
00267     ALMethodInfo* getMethodInfo(const std::string &pName);
00268 
00275     ALMethodInfo* getMethodInfo(const std::string &pName,
00276                                 std::vector<std::string> pParamsType);
00277 
00284     ALMethodInfo* getMethodInfo(const std::string &pName,
00285                                 const AL::ALValue &pParams);
00286 
00291     ALMethodInfo* getFunctionDesc(const std::string &pName);
00292 
00297     ALMethodInfo* getFunctionDesc(const std::string &pName,
00298                                   std::vector<std::string> pParamsType);
00299 
00304     ALMethodInfo* getFunctionDesc(const std::string &pName,
00305                                   const AL::ALValue &pParams);
00306 
00311     ALMethodInfo* getFunctionDescByNameMember(const std::string &pName,
00312                                               const std::vector<std::string> &paramType,
00313                                               bool softCompare = true);
00314 
00319     boost::shared_ptr<ALBroker> getParentBroker() const;
00320 
00325     void functionStop(int pTaskID);
00326 
00327 
00332     void setModuleDescription(const std::string &pDesc);
00333 
00339     std::string getUsage(const std::string &methodName);
00340 
00347     template <class T>
00348     static boost::shared_ptr<T> createModuleCore(boost::shared_ptr<ALBroker> pBroker)
00349     {
00350       boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker));
00351       module->initModule();  // register module in broker
00352       try
00353       {
00354         // we call init on a ALModule::Ptr as init may be protected
00355         // init is a virtual method that can be reimplemented
00356         (boost::static_pointer_cast<ALModuleCore>(module))->init();
00357       }
00358       catch(const ALError& e)
00359       {
00360         module->exit();
00361         throw(e);
00362       }
00363       return module;
00364     }
00365 
00373     template <class T>
00374     static boost::shared_ptr<T> createModuleCoreNoRegister(boost::shared_ptr<ALBroker> pBroker, const std::string &name)
00375     {
00376       boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker, name));
00377       //module->initModule();  // register module in broker
00378       try
00379       {
00380         // we call init on a ALModule::Ptr as init may be protected
00381         // init is a virtual method that can be reimplemented
00382         (boost::static_pointer_cast<ALModuleCore>(module))->init();
00383       }
00384       catch(const ALError& e)
00385       {
00386         module->exit();
00387         throw(e);
00388       }
00389       return module;
00390     }
00391 
00399     template <class T>
00400     static boost::shared_ptr<T> createModuleCore(boost::shared_ptr<ALBroker> pBroker,
00401                                                  const std::string &name)
00402     {
00403       boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker, name));
00404       module->initModule();
00405       try
00406       {
00407         // init if you redefined it
00408         (boost::static_pointer_cast<ALModuleCore>(module))->init();
00409       }
00410       catch(const ALError& e)
00411       {
00412         module->exit();
00413         throw(e);
00414       }
00415       return module;
00416     }
00417 
00424     template <class T>
00425     static boost::shared_ptr<T> createUrbiModule(boost::shared_ptr<ALBroker> pBroker,
00426                                                  const std::string &name)
00427     {
00428       boost::shared_ptr<T> module = createModuleCore<T>(pBroker, name);
00429       module->setModuleType(AL::ALModuleCore::URBI);
00430       return module;
00431     }
00432 
00437     bool isModuleStopped();
00438 
00443     void setModuleID(int id);
00444 
00449     int getModuleID();
00450 
00455     void bindMethod(boost::shared_ptr<ALFunctorBase> pFunctor);
00456 
00465     void bindMethod(boost::shared_ptr<ALFunctorBase> pFunctor,
00466                     const std::string &pName,
00467                     const std::string &pClass,
00468                     const std::string &pFunctionDescription,
00469                     const ALMethodInfo &pMethodDescription);
00470 
00475     void bindMethodOverload(boost::shared_ptr<ALFunctorBase> pFunctor);
00476 
00485     void functionName(const std::string &pName,
00486                       const std::string &pClass,
00487                       const std::string &pFunctionDescription,
00488                       int pMask = 0);
00489 
00496     void addParam(const std::string &pName,
00497                   const std::string &pDesc);
00498 
00504     void addModuleExample(const std::string &pLanguage,
00505                           const std::string &pExample);
00506 
00512     void addMethodExample(const std::string &pLanguage,
00513                           const std::string &pExample);
00514 
00520     void setReturn(const std::string &pName, const std::string &pDesc);
00521 
00522     virtual qi::Future<qi::AnyReference> metaCall(qi::AnyObject context, unsigned int method, const qi::GenericFunctionParameters &in, qi::MetaCallType callType, qi::Signature returnSignature);
00523 
00524 
00525     qi::AnyObject asObject();
00526 
00531     qi::DynamicObjectBuilder& getBuilder() {return _builder;};
00532 
00533 
00537     int pCall(const qi::AnyArguments& args);
00538 
00539   protected:
00545     virtual void init(void) {}
00546 
00547   private:
00548     int _pCall(const unsigned int &methodId, const std::vector<qi::AnyValue> &args);
00549 
00550   protected:
00551     qi::DynamicObjectBuilder _builder;
00552     friend class baseModule; // for inaoqi that needs to advertise methods
00553   public:
00554     qi::GenericObject _go;
00555     ALModuleCorePrivate *_p;
00556     qi::MetaMethodBuilder _mBuilder;
00557 
00558   };  // !ALModuleCore
00559 }
00560 
00561 #endif  // _LIBALCOMMON_ALCOMMON_ALMODULECORE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines