libalcommon  1.14
 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 <boost/enable_shared_from_this.hpp>
15 # include <boost/shared_ptr.hpp>
16 # include <boost/noncopyable.hpp>
17 
18 # include <alcommon/almoduleinfo.h>
19 # include <alcommon/almethodinfo.h>
20 # include <alcommon/alparamtype.h>
21 # include <alerror/alerror.h>
22 # include <alcommon/alfunctor.h>
23 # include <map>
24 
29 # define BIND_OBJ_METHOD(objptr, meth) \
30  { \
31  completeAndCheck(&meth, getCurrentMethodDescription()); \
32  bindMethod(createFunctor(objptr, &meth)); \
33  }
34 
41 # define BIND_METHOD(x) BIND_OBJ_METHOD(this, x)
42 
43 namespace AL
44 {
45  class ALModuleCoreImpl;
46  class ALBroker;
47  class ALProxy;
48  class ALValue;
49 
61  class ALModuleCore: public ::boost::enable_shared_from_this<ALModuleCore>, public ::boost::noncopyable
62  {
63  friend class baseModule;
64  private:
66  boost::shared_ptr<ALModuleCoreImpl> fImpl;
67 
68  public:
74  typedef boost::shared_ptr<ALModuleCore> Ptr;
80  typedef boost::weak_ptr<ALModuleCore> WeakPtr;
81 
87  {
88  CPP = 0,
89  PYTHON = 1,
90  RUBY = 2,
91  LUA = 3,
92  MATLAB = 4,
94  };
95 
105  ALModuleCore(boost::shared_ptr<ALBroker> pBroker,
106  const std::string &pName);
107 
109  virtual ~ALModuleCore();
110 
117 
122  boost::shared_ptr<ALModuleCore> getThis();
127  boost::shared_ptr<const ALModuleCore> getThis() const;
128 
133  bool isClosing();
134 
140  void initModule(void);
141 
149  boost::shared_ptr<AL::ALProxy> getProxy(const std::string &pModuleName);
150 
155  std::string getBrokerName();
156 
162 
167  void setModuleType(ModuleType pType);
168 
177  virtual ALMethodInfo* execute(const std::string &pMethod,
178  const AL::ALValue &pParams,
179  AL::ALValue &pResult);
180 
185  std::vector<std::string> getMethodList();
186 
192  AL::ALValue getMethodHelp(const std::string &pMethodName);
193 
199  ALMethodInfo getMethodHelpObject(const std::string &pMethodName);
200 
206 
211  bool ping(void);
212 
217  virtual std::string version();
218 
222  virtual void exit();
223 
228  const std::string& getName() const;
229 
234  boost::shared_ptr<ALModuleInfo> getModuleInfo();
235 
240  virtual std::string httpGet();
241 
251  ALMethodInfo* getMethodInfoByNameMember(const std::string &pName,
252  const std::vector<std::string> &paramType,
253  bool softCompare = true);
254 
260  ALMethodInfo* getMethodInfo(const std::string &pName);
261 
268  ALMethodInfo* getMethodInfo(const std::string &pName,
269  std::vector<std::string> pParamsType);
270 
277  ALMethodInfo* getMethodInfo(const std::string &pName,
278  const AL::ALValue &pParams);
279 
284  ALMethodInfo* getFunctionDesc(const std::string &pName);
285 
290  ALMethodInfo* getFunctionDesc(const std::string &pName,
291  std::vector<std::string> pParamsType);
292 
297  ALMethodInfo* getFunctionDesc(const std::string &pName,
298  const AL::ALValue &pParams);
299 
304  ALMethodInfo* getFunctionDescByNameMember(const std::string &pName,
305  const std::vector<std::string> &paramType,
306  bool softCompare = true);
307 
312  boost::shared_ptr<ALBroker> getParentBroker() const;
313 
318  void functionStop(int pTaskID);
319 
320 
325  void setModuleDescription(const std::string &pDesc);
326 
332  std::string getUsage(const std::string &methodName);
333 
340  template <class T>
341  static boost::shared_ptr<T> createModuleCore(boost::shared_ptr<ALBroker> pBroker)
342  {
343  boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker));
344  module->initModule(); // register module in broker
345  try
346  {
347  // we call init on a ALModule::Ptr as init may be protected
348  // init is a virtual method that can be reimplemented
349  (boost::static_pointer_cast<ALModuleCore>(module))->init();
350  }
351  catch(const ALError& e)
352  {
353  module->exit();
354  throw(e);
355  }
356  return module;
357  }
358 
366  template <class T>
367  static boost::shared_ptr<T> createModuleCore(boost::shared_ptr<ALBroker> pBroker,
368  const std::string &name)
369  {
370  boost::shared_ptr<T> module = boost::shared_ptr<T>(new T(pBroker, name));
371  module->initModule();
372  try
373  {
374  // init if you redefined it
375  (boost::static_pointer_cast<ALModuleCore>(module))->init();
376  }
377  catch(const ALError& e)
378  {
379  module->exit();
380  throw(e);
381  }
382  return module;
383  }
384 
391  template <class T>
392  static boost::shared_ptr<T> createUrbiModule(boost::shared_ptr<ALBroker> pBroker,
393  const std::string &name)
394  {
395  boost::shared_ptr<T> module = createModuleCore<T>(pBroker, name);
396  module->setModuleType(AL::ALModuleCore::URBI);
397  return module;
398  }
399 
404  bool isModuleStopped();
405 
410  void setModuleID(int id);
411 
416  int getModuleID();
417 
418  protected:
424  virtual void init(void) {}
425 
430  void bindMethod(boost::shared_ptr<ALFunctorBase> pFunctor);
431 
440  void bindMethod(boost::shared_ptr<ALFunctorBase> pFunctor,
441  const std::string &pName,
442  const std::string &pClass,
443  const std::string &pFunctionDescription,
444  const ALMethodInfo &pMethodDescription);
445 
450  void bindMethodOverload(boost::shared_ptr<ALFunctorBase> pFunctor);
451 
460  void functionName(const std::string &pName,
461  const std::string &pClass,
462  const std::string &pFunctionDescription,
463  int pMask = 0);
464 
471  void addParam(const std::string &pName,
472  const std::string &pDesc);
473 
479  void addModuleExample(const std::string &pLanguage,
480  const std::string &pExample);
481 
487  void addMethodExample(const std::string &pLanguage,
488  const std::string &pExample);
489 
495  void setReturn(const std::string &pName,
496  const std::string &pDesc);
497  }; // !ALModuleCore
498 }
499 #endif // _LIBALCOMMON_ALCOMMON_ALMODULECORE_H_