libalcommon  2.4.3.28-r2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
almodulecore.h
Go to the documentation of this file.
1 
10 #pragma once
11 #ifndef _LIBALCOMMON_ALCOMMON_ALMODULECORE_H_
12 #define _LIBALCOMMON_ALCOMMON_ALMODULECORE_H_
13 
14 # include <alcommon/api.h>
15 # include <boost/enable_shared_from_this.hpp>
16 # include <boost/shared_ptr.hpp>
17 # include <boost/noncopyable.hpp>
18 
19 # include <alcommon/almoduleinfo.h>
20 # include <alcommon/almethodinfo.h>
21 # include <alcommon/alfunctor.h>
22 
23 # include <alerror/alerror.h>
24 # include <qi/log.hpp>
25 # include <map>
26 
27 #include <qi/anyfunction.hpp>
28 #include <qi/signature.hpp>
29 #include <qi/type/dynamicobject.hpp>
30 #include <qi/type/dynamicobjectbuilder.hpp>
31 
32 //legacy hell, LEGACY HELL
33 #define BIND_OBJ_METHOD(objptr, meth) _builder.advertiseMethod(_mBuilder, objptr, &meth)
34 #define BIND_METHOD(meth) _builder.advertiseMethod(_mBuilder, this, &meth)
35 
36 #define BIND_OBJ_METHOD_PTR(objptr, methptr) _builder.advertiseMethod(_mBuilder, objptr, methptr)
37 #define BIND_METHOD_PTR(methptr) _builder.advertiseMethod(_mBuilder, this, methptr)
38 
39 namespace AL
40 {
41  class ALBroker;
42  class ALProxy;
43  class ALValue;
44  class ALModuleCorePrivate;
45 
57  class ALModuleCore: public ::boost::enable_shared_from_this<ALModuleCore>, public ::boost::noncopyable, public qi::DynamicObject
58  {
59  public:
65  typedef boost::shared_ptr<ALModuleCore> Ptr;
71  typedef boost::weak_ptr<ALModuleCore> WeakPtr;
72 
79  {
80  CPP = 0,
81  PYTHON = 1,
82  RUBY = 2,
83  LUA = 3,
84  MATLAB = 4,
86  };
87 
97  ALModuleCore(boost::shared_ptr<ALBroker> pBroker,
98  const std::string &pName);
99 
101  virtual ~ALModuleCore();
102 
109 
114  boost::shared_ptr<ALModuleCore> getThis();
119  boost::shared_ptr<const ALModuleCore> getThis() const;
120 
125  bool isClosing();
126 
132  void initModule(void);
133 
141  boost::shared_ptr<AL::ALProxy> getProxy(const std::string &pModuleName);
142 
147  std::string getBrokerName();
148 
154 
159  void setModuleType(ModuleType pType);
160 
169  virtual ALMethodInfo* execute(const std::string &pMethod,
170  const AL::ALValue &pParams,
171  AL::ALValue &pResult);
172 
177  std::vector<std::string> getMethodList();
178 
198  AL::ALValue getMethodHelp(const std::string &pMethodName);
199 
205  ALMethodInfo getMethodHelpObject(const std::string &pMethodName);
206 
212 
217  bool ping(void);
218 
223  virtual std::string version();
224 
228  virtual void exit();
229 
234  const std::string& getName() const;
235 
240  boost::shared_ptr<ALModuleInfo> getModuleInfo();
241 
246  virtual std::string httpGet();
247 
257  ALMethodInfo* getMethodInfoByNameMember(const std::string &pName,
258  const std::vector<std::string> &paramType,
259  bool softCompare = true);
260 
266  ALMethodInfo* getMethodInfo(const std::string &pName);
267 
274  ALMethodInfo* getMethodInfo(const std::string &pName,
275  std::vector<std::string> pParamsType);
276 
283  ALMethodInfo* getMethodInfo(const std::string &pName,
284  const AL::ALValue &pParams);
285 
290  ALMethodInfo* getFunctionDesc(const std::string &pName);
291 
296  ALMethodInfo* getFunctionDesc(const std::string &pName,
297  std::vector<std::string> pParamsType);
298 
303  ALMethodInfo* getFunctionDesc(const std::string &pName,
304  const AL::ALValue &pParams);
305 
310  ALMethodInfo* getFunctionDescByNameMember(const std::string &pName,
311  const std::vector<std::string> &paramType,
312  bool softCompare = true);
313 
318  boost::shared_ptr<ALBroker> getParentBroker() const;
319 
324  void functionStop(int pTaskID);
325 
326 
331  void setModuleDescription(const std::string &pDesc);
332 
338  std::string getUsage(const std::string &methodName);
339 
346  template <class T>
347  static boost::shared_ptr<T> createModuleCore(boost::shared_ptr<ALBroker> pBroker)
348  {
349  boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker));
350  module->initModule(); // register module in broker
351  try
352  {
353  // we call init on a ALModule::Ptr as init may be protected
354  // init is a virtual method that can be reimplemented
355  (boost::static_pointer_cast<ALModuleCore>(module))->init();
356  }
357  catch(const ALError& e)
358  {
359  module->exit();
360  throw(e);
361  }
362  return module;
363  }
364 
372  template <class T>
373  static boost::shared_ptr<T> createModuleCoreNoRegister(boost::shared_ptr<ALBroker> pBroker, const std::string &name)
374  {
375  boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker, name));
376  //module->initModule(); // register module in broker
377  try
378  {
379  // we call init on a ALModule::Ptr as init may be protected
380  // init is a virtual method that can be reimplemented
381  (boost::static_pointer_cast<ALModuleCore>(module))->init();
382  }
383  catch(const ALError& e)
384  {
385  module->exit();
386  throw(e);
387  }
388  return module;
389  }
390 
398  template <class T>
399  static boost::shared_ptr<T> createModuleCore(boost::shared_ptr<ALBroker> pBroker,
400  const std::string &name)
401  {
402  boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker, name));
403  module->initModule();
404  try
405  {
406  // init if you redefined it
407  (boost::static_pointer_cast<ALModuleCore>(module))->init();
408  }
409  catch(const ALError& e)
410  {
411  module->exit();
412  throw(e);
413  }
414  return module;
415  }
416 
423  template <class T>
424  static boost::shared_ptr<T> createUrbiModule(boost::shared_ptr<ALBroker> pBroker,
425  const std::string &name)
426  {
427  boost::shared_ptr<T> module = createModuleCore<T>(pBroker, name);
428  module->setModuleType(AL::ALModuleCore::URBI);
429  return module;
430  }
431 
436  bool isModuleStopped();
437 
442  void setModuleID(int id);
443 
448  int getModuleID();
449 
454  void bindMethod(boost::shared_ptr<ALFunctorBase> pFunctor);
455 
464  void bindMethod(boost::shared_ptr<ALFunctorBase> pFunctor,
465  const std::string &pName,
466  const std::string &pClass,
467  const std::string &pFunctionDescription,
468  const ALMethodInfo &pMethodDescription);
469 
474  void bindMethodOverload(boost::shared_ptr<ALFunctorBase> pFunctor);
475 
484  void functionName(const std::string &pName,
485  const std::string &pClass,
486  const std::string &pFunctionDescription,
487  int pMask = 0);
488 
495  void addParam(const std::string &pName,
496  const std::string &pDesc);
497 
503  void addModuleExample(const std::string &pLanguage,
504  const std::string &pExample);
505 
511  void addMethodExample(const std::string &pLanguage,
512  const std::string &pExample);
513 
519  void setReturn(const std::string &pName, const std::string &pDesc);
520 
521  virtual qi::Future<qi::AnyReference> metaCall(qi::AnyObject context, unsigned int method, const qi::GenericFunctionParameters &in, qi::MetaCallType callType, qi::Signature returnSignature);
522 
523 
524  qi::AnyObject asObject();
525 
530  qi::DynamicObjectBuilder& getBuilder() {return _builder;}
531 
532 
536  int pCall(const qi::AnyArguments& args);
537 
538  protected:
544  virtual void init(void) {}
545 
546  private:
547  int _pCall(const unsigned int &methodId, const std::vector<qi::AnyValue> &args);
548 
549  protected:
550  qi::DynamicObjectBuilder _builder;
551  friend class baseModule; // for inaoqi that needs to advertise methods
552  public:
553  qi::GenericObject _go;
554  ALModuleCorePrivate *_p;
555  qi::MetaMethodBuilder _mBuilder;
556 
557  }; // !ALModuleCore
558 }
559 
560 #endif // _LIBALCOMMON_ALCOMMON_ALMODULECORE_H_
ALMethodInfo * getMethodInfo(const std::string &pName)
Call by a proxy to check function's parameter.
boost::shared_ptr< AL::ALProxy > getProxy(const std::string &pModuleName)
Get access to a module.
qi::MetaMethodBuilder _mBuilder
Definition: almodulecore.h:555
void setReturn(const std::string &pName, const std::string &pDesc)
Sets the description of the return value.
ModuleType
Module type (Ruby, lua, and matlab are not currently used)
Definition: almodulecore.h:78
ALMethodInfo * getFunctionDescByNameMember(const std::string &pName, const std::vector< std::string > &paramType, bool softCompare=true)
deprecated
static boost::shared_ptr< T > createModuleCoreNoRegister(boost::shared_ptr< ALBroker > pBroker, const std::string &name)
Create a module core, do not register on the broker.
Definition: almodulecore.h:373
void bindMethod(boost::shared_ptr< ALFunctorBase > pFunctor)
Bind a method.
ALMethodInfo & getCurrentMethodDescription()
Get a reference to a temporary object used to construct method help.
bool ping(void)
Just a ping. Used to test connectivity to a module.
void addMethodExample(const std::string &pLanguage, const std::string &pExample)
Add a method example.
ALModuleCore is the superclass of user modules.
Definition: almodulecore.h:57
boost::shared_ptr< ALModuleCore > getThis()
Getter to the class.
boost::weak_ptr< ALModuleCore > WeakPtr
Weak pointer to ALModuleCore.
Definition: almodulecore.h:71
static boost::shared_ptr< T > createModuleCore(boost::shared_ptr< ALBroker > pBroker, const std::string &name)
Create a module core link to a broker.
Definition: almodulecore.h:399
void initModule(void)
Register a module to a broker.
ALMethodInfo getMethodHelpObject(const std::string &pMethodName)
Get a method's description string.
qi::DynamicObjectBuilder _builder
Definition: almodulecore.h:550
virtual ~ALModuleCore()
Destructor.
boost::shared_ptr< ALBroker > getParentBroker() const
Get a pointer to the broker context.
ALModuleCore(boost::shared_ptr< ALBroker > pBroker, const std::string &pName)
Constructor.
virtual std::string httpGet()
Called by the broker webpage to detail the module.
static boost::shared_ptr< T > createUrbiModule(boost::shared_ptr< ALBroker > pBroker, const std::string &name)
Create a URBI module core link to a broker.
Definition: almodulecore.h:424
ALMethodInfo * getMethodInfoByNameMember(const std::string &pName, const std::vector< std::string > &paramType, bool softCompare=true)
Find method information by name and argument type not only by map key.
virtual void init(void)
Call at every module creation.
Definition: almodulecore.h:544
std::string getBrokerName()
Get the name of the registered broker.
ALMethodInfo is the introspection container.
Definition: almethodinfo.h:33
int pCall(const qi::AnyArguments &args)
void bindMethodOverload(boost::shared_ptr< ALFunctorBase > pFunctor)
Use to define a bound method.
int getModuleID()
Get the module id given to it be the broker.
std::string getUsage(const std::string &methodName)
Get the usage of a method as a string.
ALModuleCorePrivate * _p
Definition: almodulecore.h:554
friend class baseModule
Definition: almodulecore.h:551
qi::GenericObject _go
Definition: almodulecore.h:553
void setModuleDescription(const std::string &pDesc)
Set the description of the module.
boost::shared_ptr< ALModuleCore > Ptr
Shared pointer to ALModuleCore.
Definition: almodulecore.h:65
void functionName(const std::string &pName, const std::string &pClass, const std::string &pFunctionDescription, int pMask=0)
Define the name of a bound method.
const std::string & getName() const
Get the name of the module given when constructing.
ModuleType getModuleType(void)
Get the type of the module.
AL::ALValue moduleHelp()
Get the module's description.
qi::AnyObject asObject()
void functionStop(int pTaskID)
Local stop. Use stop(id) for remote/local compatibility.
void setModuleID(int id)
Set the id of the module used to determine the shutdown order.
AL::ALValue getMethodHelp(const std::string &pMethodName)
Get a method's description string.
static boost::shared_ptr< T > createModuleCore(boost::shared_ptr< ALBroker > pBroker)
Create a module core link to a broker.
Definition: almodulecore.h:347
bool isClosing()
Check if someone want to exit the module.
void addModuleExample(const std::string &pLanguage, const std::string &pExample)
Add a module example.
std::vector< std::string > getMethodList()
Get the module method list.
void setModuleType(ModuleType pType)
Set the type of the module.
bool isModuleStopped()
Know if program or module termination is asked.
void addParam(const std::string &pName, const std::string &pDesc)
Add a documented parameter to a method.
qi::DynamicObjectBuilder & getBuilder()
Definition: almodulecore.h:530
virtual ALMethodInfo * execute(const std::string &pMethod, const AL::ALValue &pParams, AL::ALValue &pResult)
Execute a method with some arguments and store the result.
ALMethodInfo * getFunctionDesc(const std::string &pName)
deprecated
virtual std::string version()
Get module's version.
virtual qi::Future< qi::AnyReference > metaCall(qi::AnyObject context, unsigned int method, const qi::GenericFunctionParameters &in, qi::MetaCallType callType, qi::Signature returnSignature)
boost::shared_ptr< ALModuleInfo > getModuleInfo()
Get information about the module.
virtual void exit()
Exit the module and unregister it.