libalcommon  2.1.4.13
 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 <qitype/dynamicobject.hpp>
28 #include <qitype/signature.hpp>
29 #include <qitype/anyfunction.hpp>
30 #include <qitype/dynamicobjectbuilder.hpp>
31 #include <qitype/dynamicobject.hpp>
32 
33 //legacy hell, LEGACY HELL
34 #define BIND_OBJ_METHOD(objptr, meth) _builder.advertiseMethod(_mBuilder, objptr, &meth)
35 #define BIND_METHOD(meth) _builder.advertiseMethod(_mBuilder, this, &meth)
36 
37 #define BIND_OBJ_METHOD_PTR(objptr, methptr) _builder.advertiseMethod(_mBuilder, objptr, methptr)
38 #define BIND_METHOD_PTR(methptr) _builder.advertiseMethod(_mBuilder, this, methptr)
39 
40 namespace AL
41 {
42  class ALBroker;
43  class ALProxy;
44  class ALValue;
45  class ALModuleCorePrivate;
46 
58  class ALModuleCore: public ::boost::enable_shared_from_this<ALModuleCore>, public ::boost::noncopyable, public qi::DynamicObject
59  {
60  public:
66  typedef boost::shared_ptr<ALModuleCore> Ptr;
72  typedef boost::weak_ptr<ALModuleCore> WeakPtr;
73 
80  {
81  CPP = 0,
82  PYTHON = 1,
83  RUBY = 2,
84  LUA = 3,
85  MATLAB = 4,
87  };
88 
98  ALModuleCore(boost::shared_ptr<ALBroker> pBroker,
99  const std::string &pName);
100 
102  virtual ~ALModuleCore();
103 
110 
115  boost::shared_ptr<ALModuleCore> getThis();
120  boost::shared_ptr<const ALModuleCore> getThis() const;
121 
126  bool isClosing();
127 
133  void initModule(void);
134 
142  boost::shared_ptr<AL::ALProxy> getProxy(const std::string &pModuleName);
143 
148  std::string getBrokerName();
149 
155 
160  void setModuleType(ModuleType pType);
161 
170  virtual ALMethodInfo* execute(const std::string &pMethod,
171  const AL::ALValue &pParams,
172  AL::ALValue &pResult);
173 
178  std::vector<std::string> getMethodList();
179 
199  AL::ALValue getMethodHelp(const std::string &pMethodName);
200 
206  ALMethodInfo getMethodHelpObject(const std::string &pMethodName);
207 
213 
218  bool ping(void);
219 
224  virtual std::string version();
225 
229  virtual void exit();
230 
235  const std::string& getName() const;
236 
241  boost::shared_ptr<ALModuleInfo> getModuleInfo();
242 
247  virtual std::string httpGet();
248 
258  ALMethodInfo* getMethodInfoByNameMember(const std::string &pName,
259  const std::vector<std::string> &paramType,
260  bool softCompare = true);
261 
267  ALMethodInfo* getMethodInfo(const std::string &pName);
268 
275  ALMethodInfo* getMethodInfo(const std::string &pName,
276  std::vector<std::string> pParamsType);
277 
284  ALMethodInfo* getMethodInfo(const std::string &pName,
285  const AL::ALValue &pParams);
286 
291  ALMethodInfo* getFunctionDesc(const std::string &pName);
292 
297  ALMethodInfo* getFunctionDesc(const std::string &pName,
298  std::vector<std::string> pParamsType);
299 
304  ALMethodInfo* getFunctionDesc(const std::string &pName,
305  const AL::ALValue &pParams);
306 
311  ALMethodInfo* getFunctionDescByNameMember(const std::string &pName,
312  const std::vector<std::string> &paramType,
313  bool softCompare = true);
314 
319  boost::shared_ptr<ALBroker> getParentBroker() const;
320 
325  void functionStop(int pTaskID);
326 
327 
332  void setModuleDescription(const std::string &pDesc);
333 
339  std::string getUsage(const std::string &methodName);
340 
347  template <class T>
348  static boost::shared_ptr<T> createModuleCore(boost::shared_ptr<ALBroker> pBroker)
349  {
350  boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker));
351  module->initModule(); // register module in broker
352  try
353  {
354  // we call init on a ALModule::Ptr as init may be protected
355  // init is a virtual method that can be reimplemented
356  (boost::static_pointer_cast<ALModuleCore>(module))->init();
357  }
358  catch(const ALError& e)
359  {
360  module->exit();
361  throw(e);
362  }
363  return module;
364  }
365 
373  template <class T>
374  static boost::shared_ptr<T> createModuleCoreNoRegister(boost::shared_ptr<ALBroker> pBroker, const std::string &name)
375  {
376  boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker, name));
377  //module->initModule(); // register module in broker
378  try
379  {
380  // we call init on a ALModule::Ptr as init may be protected
381  // init is a virtual method that can be reimplemented
382  (boost::static_pointer_cast<ALModuleCore>(module))->init();
383  }
384  catch(const ALError& e)
385  {
386  module->exit();
387  throw(e);
388  }
389  return module;
390  }
391 
399  template <class T>
400  static boost::shared_ptr<T> createModuleCore(boost::shared_ptr<ALBroker> pBroker,
401  const std::string &name)
402  {
403  boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker, name));
404  module->initModule();
405  try
406  {
407  // init if you redefined it
408  (boost::static_pointer_cast<ALModuleCore>(module))->init();
409  }
410  catch(const ALError& e)
411  {
412  module->exit();
413  throw(e);
414  }
415  return module;
416  }
417 
424  template <class T>
425  static boost::shared_ptr<T> createUrbiModule(boost::shared_ptr<ALBroker> pBroker,
426  const std::string &name)
427  {
428  boost::shared_ptr<T> module = createModuleCore<T>(pBroker, name);
429  module->setModuleType(AL::ALModuleCore::URBI);
430  return module;
431  }
432 
437  bool isModuleStopped();
438 
443  void setModuleID(int id);
444 
449  int getModuleID();
450 
455  void bindMethod(boost::shared_ptr<ALFunctorBase> pFunctor);
456 
465  void bindMethod(boost::shared_ptr<ALFunctorBase> pFunctor,
466  const std::string &pName,
467  const std::string &pClass,
468  const std::string &pFunctionDescription,
469  const ALMethodInfo &pMethodDescription);
470 
475  void bindMethodOverload(boost::shared_ptr<ALFunctorBase> pFunctor);
476 
485  void functionName(const std::string &pName,
486  const std::string &pClass,
487  const std::string &pFunctionDescription,
488  int pMask = 0);
489 
496  void addParam(const std::string &pName,
497  const std::string &pDesc);
498 
504  void addModuleExample(const std::string &pLanguage,
505  const std::string &pExample);
506 
512  void addMethodExample(const std::string &pLanguage,
513  const std::string &pExample);
514 
520  void setReturn(const std::string &pName, const std::string &pDesc);
521 
522  virtual qi::Future<qi::AnyReference> metaCall(qi::AnyObject context, unsigned int method, const qi::GenericFunctionParameters &in, qi::MetaCallType callType, qi::Signature returnSignature);
523 
524 
525  qi::AnyObject asObject();
526 
531  qi::DynamicObjectBuilder& getBuilder() {return _builder;};
532 
533 
537  int pCall(const qi::AnyArguments& args);
538 
539  protected:
545  virtual void init(void) {}
546 
547  private:
548  int _pCall(const unsigned int &methodId, const std::vector<qi::AnyValue> &args);
549 
550  protected:
551  qi::DynamicObjectBuilder _builder;
552  friend class baseModule; // for inaoqi that needs to advertise methods
553  public:
554  qi::GenericObject _go;
555  ALModuleCorePrivate *_p;
556  qi::MetaMethodBuilder _mBuilder;
557 
558  }; // !ALModuleCore
559 }
560 
561 #endif // _LIBALCOMMON_ALCOMMON_ALMODULECORE_H_
ModuleType
Module type (Ruby, lua, and matlab are not currently used)
Definition: almodulecore.h:79
boost::shared_ptr< ALBroker > getParentBroker() const
Get a pointer to the broker context.
boost::shared_ptr< ALModuleCore > getThis()
Getter to the class.
boost::shared_ptr< ALModuleCore > Ptr
Shared pointer to ALModuleCore.
Definition: almodulecore.h:66
boost::shared_ptr< AL::ALProxy > getProxy(const std::string &pModuleName)
Get access to a module.
virtual qi::Future< qi::AnyReference > metaCall(qi::AnyObject context, unsigned int method, const qi::GenericFunctionParameters &in, qi::MetaCallType callType, qi::Signature returnSignature)
AL::ALValue getMethodHelp(const std::string &pMethodName)
Get a method's description string.
boost::shared_ptr< ALModuleInfo > getModuleInfo()
Get information about the module.
void functionStop(int pTaskID)
Local stop. Use stop(id) for remote/local compatibility.
void addMethodExample(const std::string &pLanguage, const std::string &pExample)
Add a method example.
ALMethodInfo getMethodHelpObject(const std::string &pMethodName)
Get a method's description string.
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.
void bindMethod(boost::shared_ptr< ALFunctorBase > pFunctor)
Bind a method.
std::string getBrokerName()
Get the name of the registered broker.
void functionName(const std::string &pName, const std::string &pClass, const std::string &pFunctionDescription, int pMask=0)
Define the name of a bound method.
void bindMethodOverload(boost::shared_ptr< ALFunctorBase > pFunctor)
Use to define a bound method.
void initModule(void)
Register a module to a broker.
ALModuleCorePrivate * _p
Definition: almodulecore.h:555
ALMethodInfo * getFunctionDescByNameMember(const std::string &pName, const std::vector< std::string > &paramType, bool softCompare=true)
deprecated
qi::DynamicObjectBuilder _builder
Definition: almodulecore.h:551
qi::GenericObject _go
Definition: almodulecore.h:554
void setModuleDescription(const std::string &pDesc)
Set the description of the module.
ALModuleCore(boost::shared_ptr< ALBroker > pBroker, const std::string &pName)
Constructor.
ALModuleCore is the superclass of user modules.
Definition: almodulecore.h:58
void setModuleType(ModuleType pType)
Set the type of the module.
const std::string & getName() const
Get the name of the module given when constructing.
ModuleType getModuleType(void)
Get the type of the module.
virtual ALMethodInfo * execute(const std::string &pMethod, const AL::ALValue &pParams, AL::ALValue &pResult)
Execute a method with some arguments and store the result.
int getModuleID()
Get the module id given to it be the broker.
virtual std::string httpGet()
Called by the broker webpage to detail the module.
ALMethodInfo * getMethodInfo(const std::string &pName)
Call by a proxy to check function's parameter.
friend class baseModule
Definition: almodulecore.h:552
virtual void exit()
Exit the module and unregister it.
bool ping(void)
Just a ping. Used to test connectivity to a module.
virtual ~ALModuleCore()
Destructor.
qi::DynamicObjectBuilder & getBuilder()
Definition: almodulecore.h:531
ALMethodInfo & getCurrentMethodDescription()
Get a reference to a temporary object used to construct method help.
void addParam(const std::string &pName, const std::string &pDesc)
Add a documented parameter to a method.
AL::ALValue moduleHelp()
Get the module's description.
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:400
qi::MetaMethodBuilder _mBuilder
Definition: almodulecore.h:556
virtual void init(void)
Call at every module creation.
Definition: almodulecore.h:545
qi::AnyObject asObject()
std::vector< std::string > getMethodList()
Get the module method list.
virtual std::string version()
Get module's version.
static boost::shared_ptr< T > createModuleCore(boost::shared_ptr< ALBroker > pBroker)
Create a module core link to a broker.
Definition: almodulecore.h:348
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:425
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:374
bool isClosing()
Check if someone want to exit the module.
void addModuleExample(const std::string &pLanguage, const std::string &pExample)
Add a module example.
bool isModuleStopped()
Know if program or module termination is asked.
int pCall(const qi::AnyArguments &args)
ALMethodInfo * getFunctionDesc(const std::string &pName)
deprecated
ALMethodInfo is the introspection container.
Definition: almethodinfo.h:33
void setReturn(const std::string &pName, const std::string &pDesc)
Sets the description of the return value.
void setModuleID(int id)
Set the id of the module used to determine the shutdown order.
boost::weak_ptr< ALModuleCore > WeakPtr
Weak pointer to ALModuleCore.
Definition: almodulecore.h:72
std::string getUsage(const std::string &methodName)
Get the usage of a method as a string.