YARP
Yet Another Robot Platform
module.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_MANAGER_MODULE_H
10 #define YARP_MANAGER_MODULE_H
11 
12 
13 #include <yarp/manager/ymm-types.h>
14 #include <yarp/manager/node.h>
15 #include <yarp/manager/data.h>
16 #include <yarp/manager/utility.h>
17 #include <yarp/manager/resource.h>
18 #include <yarp/conf/api.h>
19 
20 namespace yarp {
21 namespace manager {
22 
23 
24 class Author {
25 public:
26  Author() {}
27  Author(const char* name, const char* email) {
28  if(name) { strName = name;}
29  if(email) { strEmail = email; }
30  }
31  ~Author() {}
32 
33  void setName(const char* name) { if(name) { strName = name; } }
34  void setEmail(const char* email) { if(email) { strEmail = email; } }
35  const char* getName() { return strName.c_str(); }
36  const char* getEmail() { return strEmail.c_str(); }
37 
38  bool operator==(const Author& alt) const {
39  return (strName == alt.strName);
40  }
41 
42 private:
43  std::string strName;
44  std::string strEmail;
45 };
46 
50 class Argument{
51 
52 public:
53  Argument(const char* szParam, bool required=false,
54  const char* szDesc=nullptr, bool _switch=false) {
55  if(szParam) { strParam = szParam; }
56  if(szDesc) { strDesc = szDesc; }
57  bRequired = required;
58  bSwitch = _switch;
59  if(bSwitch) { strDefault = "off"; }
60  strValue = strDefault;
61  }
62  virtual ~Argument(){}
63 
64  void setParam(const char* szParam) { if(szParam) { strParam = szParam; } }
65  void setDefault(const char* szDefault) {if(szDefault) { strDefault = szDefault; } }
66  void setValue(const char* szValue) {if(szValue) { strValue = szValue; } }
67  const char* getDefault() { return strDefault.c_str(); }
68  const char* getParam() { return strParam.c_str(); }
69  const char* getValue() { return strValue.c_str(); }
70  const char* getDescription() { return strDesc.c_str(); }
71  bool isSwitch() { return bSwitch; }
72  bool isRequired() {return bRequired; }
73  bool operator==(const Argument& alt) const {
74  return (strParam == alt.strParam);
75  }
76 
77 protected:
78 
79 private:
80  std::string strParam;
81  std::string strDesc;
82  std::string strDefault;
83  std::string strValue;
84  bool bRequired;
85  bool bSwitch;
86 };
87 
88 
89 typedef std::vector<InputData> InputContainer;
90 typedef std::vector<OutputData> OutputContainer;
91 typedef std::vector<InputData>::iterator InputIterator;
92 typedef std::vector<OutputData>::iterator OutputIterator;
93 typedef std::vector<Author> AuthorContainer;
94 typedef std::vector<Author>::iterator AuthorIterator;
95 typedef std::vector<Argument> ArgumentContainer;
96 typedef std::vector<Argument>::iterator ArgumentIterator;
97 
98 
99 
103 class Module : public Node{
104 
105 public:
106  Module();
107  Module(const char* szName);
108  Module(const Module &mod);
109  ~Module() override;
110  Node* clone() override;
111  Module& operator=(const Module& rhs);
112 
113  void setName(const char* szName) { if(szName) { strName = szName; } }
114  void setRank(int rank) {iRank = rank;}
115  void setVersion(const char* szVersion) { if(szVersion) { strVersion = szVersion; } }
116  void setDescription(const char* szDesc) { if(szDesc) { strDescription = szDesc; } }
117  void setHost(const char* szHost) { if(szHost) { strHost = szHost; } }
118  void setForced(bool flag) { bForced = flag; }
119 
120  bool setParam(const char* szParam);
121  void setXmlFile(const char* szFilename) { if(szFilename) { strXmlFile = szFilename;} }
122  void setWorkDir(const char* szWDir) { if(szWDir) { strWorkDir = szWDir; } }
123  void setStdio(const char* szStdio) { if(szStdio) { strStdio = szStdio; } }
124  void setBroker(const char* szBroker) { if(szBroker) { strBroker = szBroker; } }
125  void setPrefix(const char* szPrefix) { if(szPrefix) { strPrefix = szPrefix; } }
126  void setEnvironment(const char* szEnv) {if(szEnv) { strEnvironment = szEnv; } }
127  void setBasePrefix(const char* szPrefix) { if(szPrefix) { strBasePrefix = szPrefix; } }
128  void setNeedDeployer(bool need) { bNeedDeployer = need; }
129  void setDisplay(const char* szDisplay) {if(szDisplay) { strDisplay = szDisplay; } }
130 
131  int getRank() { return iRank; }
132  const char* getName() { return strName.c_str(); }
133  const char* getVersion() { return strVersion.c_str(); }
134  const char* getDescription() { return strDescription.c_str(); }
135  const char* getHost() { return strHost.c_str(); }
136  bool getForced() { return bForced; }
137  bool getNeedDeployer() { return bNeedDeployer; }
138 
139  const char* getParam() { return strParam.c_str(); }
140  const char* getXmlFile() { return strXmlFile.c_str(); }
141  const char* getWorkDir() { return strWorkDir.c_str(); }
142  const char* getStdio() { return strStdio.c_str(); }
143  const char* getBroker() { return strBroker.c_str(); }
144  const char* getPrefix() { return strPrefix.c_str(); }
145  const char* getEnvironment() { return strEnvironment.c_str(); }
146  const char* getBasePrefix() { return strBasePrefix.c_str(); }
147  const char* getDisplay() { return strDisplay.c_str(); }
148 
149 
150  int argumentCount() { return arguments.size(); }
151  Argument& getArgumentAt(int index){ return arguments[index]; }
152  int outputCount() { return outputs.size(); }
153  OutputData& getOutputAt(int index) { return outputs[index]; }
154  int inputCount() { return inputs.size(); }
155  InputData& getInputAt(int index) { return inputs[index]; }
156  int resourceCount() const { return resources.size(); }
157  GenericResource& getResourceAt(int index) const { return *(resources[index]); }
158 
159  void addAuthor(Author& author) { authors.push_back(author); }
160  int authorCount() { return authors.size(); }
161  Author& getAuthorAt(int index) { return authors[index]; }
162  bool removeAuthor(Author& author);
163 
164  bool addArgument(Argument& arg);
165  bool removeArgument(Argument& argument);
166  bool addOutput(OutputData& output);
167  bool removeOutput(OutputData& output);
168  bool addInput(InputData& input);
169  bool removeInput(InputData& input);
170  bool addResource(GenericResource& res);
171  bool removeResource(GenericResource& res);
172 
173  void setPostExecWait(double t) { waitStart = t; }
174  double getPostExecWait() { return waitStart; }
175  void setPostStopWait(double t) { waitStop = t; }
176  double getPostStopWait() { return waitStop; }
177 
178 
179  void clearInputs() { inputs.clear(); }
180  void clearOutputs() { outputs.clear(); }
181  void clearResources() { resources.clear(); }
182 
183  void setOwner(Node* owner) { modOwner = owner; }
184  Node* owner() { return modOwner; }
185 
186  void clear();
187  // modelBased is used to keep the graphic and geometric
188  // information which is directly loaded from application
189  // description file.
190  GraphicModel& getModelBase() { return modelBase;}
191  void setModelBase(GraphicModel& mdl) { modelBase = mdl; };
192 
193 
194  friend class ModuleInterface;
195 
196 protected:
197 
198 private:
199  Node* modOwner;
200  std::string strName;
201  std::string strVersion;
202  std::string strDescription;
203  std::string strHost;
204  bool bForced;
205  int iRank;
206  ArgumentContainer arguments;
207  AuthorContainer authors;
208  OutputContainer outputs;
209  InputContainer inputs;
210  ResourcePContainer resources;
211 
212  std::string strParam;
213  std::string strXmlFile;
214  std::string strWorkDir;
215  std::string strStdio;
216  std::string strBroker;
217  bool bNeedDeployer;
218  std::string strPrefix;
219  std::string strEnvironment;
220  std::string strBasePrefix;
221  double waitStart;
222  double waitStop;
223  std::string strDisplay;
224  GraphicModel modelBase;
225 
226  ArgumentIterator findArgument(Argument& argument);
227  InputIterator findInput(InputData& input);
228  OutputIterator findOutput(OutputData& output);
229  ResourcePIterator findResource(GenericResource& output);
230  void swap(const Module &mod);
231  bool getParamValue(const char* key, bool bSwitch, std::string &param);
232 };
233 
234 typedef std::vector<Module*> ModulePContainer;
235 typedef std::vector<Module*>::iterator ModulePIterator;
236 
237 
238 #define PRINT_MODULE(m)\
239  {\
240  cout<<"Name : "<<m->getName()<<endl;\
241  cout<<"Desc : "<<m->getDescription()<<endl;\
242  cout<<"Version : "<<m->getVersion()<<endl;\
243  cout<<"Rank : "<<m->getRank()<<endl;\
244  cout<<"Param : "<<endl;\
245  for(int i=0; i<m->argumentCount(); i++)\
246  {\
247  cout<<" "<<m->getArgumentAt(i).getParam()<<" (";\
248  cout<<m->getArgumentAt(i).getDescription()<<")"<<endl;\
249  }\
250  cout<<"Authors : "<<endl;\
251  for(int i=0; i<m->authorCount(); i++)\
252  cout<<" "<<m->getAuthorAt(i).getName()<<" ("<<m->getAuthorAt(i).getEmail()<<")"<<endl;\
253  /*cout<<"Platforms: "<<endl;\
254  for(int i=0; i<m->platformCount(); i++)\
255  cout<<" "<<m->getPlatformAt(i).getOS()\
256  <<", "<<m->getPlatformAt(i).getDistribution()<<endl;*/\
257  cout<<"Inputs : "<<endl;\
258  for(int i=0; i<m->inputCount(); i++)\
259  {\
260  cout<<" "<<m->getInputAt(i).getName()\
261  <<", "<<m->getInputAt(i).getPort();\
262  if(m->getInputAt(i).isRequired())\
263  cout<<", [REQUIRED]";\
264  if(m->getInputAt(i).withPriority())\
265  cout<<", [PRIORITY]";\
266  cout<<endl;\
267  }\
268  cout<<"Outputs : "<<endl;\
269  for(int i=0; i<m->outputCount(); i++)\
270  cout<<" "<<m->getOutputAt(i).getName()\
271  <<", "<<m->getOutputAt(i).getPort()<<endl;\
272  cout<<endl;\
273  }
274 
275 } // namespace yarp
276 } // namespace manager
277 
278 
279 #endif // YARP_MANAGER_MODULE
float t
Class Argument.
Definition: module.h:50
void setDefault(const char *szDefault)
Definition: module.h:65
const char * getDescription()
Definition: module.h:70
virtual ~Argument()
Definition: module.h:62
Argument(const char *szParam, bool required=false, const char *szDesc=nullptr, bool _switch=false)
Definition: module.h:53
bool operator==(const Argument &alt) const
Definition: module.h:73
const char * getParam()
Definition: module.h:68
const char * getValue()
Definition: module.h:69
const char * getDefault()
Definition: module.h:67
void setValue(const char *szValue)
Definition: module.h:66
void setParam(const char *szParam)
Definition: module.h:64
void setName(const char *name)
Definition: module.h:33
bool operator==(const Author &alt) const
Definition: module.h:38
const char * getName()
Definition: module.h:35
Author(const char *name, const char *email)
Definition: module.h:27
const char * getEmail()
Definition: module.h:36
void setEmail(const char *email)
Definition: module.h:34
Class InputData.
Definition: data.h:25
Class ModuleInterface.
Definition: application.h:164
Class Module.
Definition: module.h:103
void clearResources()
Definition: module.h:181
bool removeInput(InputData &input)
Definition: module.cpp:130
const char * getWorkDir()
Definition: module.h:141
bool removeOutput(OutputData &output)
Definition: module.cpp:110
void setStdio(const char *szStdio)
Definition: module.h:123
bool removeResource(GenericResource &res)
Definition: module.cpp:151
OutputData & getOutputAt(int index)
Definition: module.h:153
const char * getXmlFile()
Definition: module.h:140
void setWorkDir(const char *szWDir)
Definition: module.h:122
void setXmlFile(const char *szFilename)
Definition: module.h:121
InputData & getInputAt(int index)
Definition: module.h:155
const char * getVersion()
Definition: module.h:133
void setEnvironment(const char *szEnv)
Definition: module.h:126
void setHost(const char *szHost)
Definition: module.h:117
const char * getName()
Definition: module.h:132
const char * getBroker()
Definition: module.h:143
Node * clone() override
Definition: module.cpp:78
bool removeAuthor(Author &author)
Definition: module.cpp:161
void setNeedDeployer(bool need)
Definition: module.h:128
void setForced(bool flag)
Definition: module.h:118
const char * getBasePrefix()
Definition: module.h:146
void setDescription(const char *szDesc)
Definition: module.h:116
Argument & getArgumentAt(int index)
Definition: module.h:151
void setPrefix(const char *szPrefix)
Definition: module.h:125
int resourceCount() const
Definition: module.h:156
void setModelBase(GraphicModel &mdl)
Definition: module.h:191
void addAuthor(Author &author)
Definition: module.h:159
const char * getStdio()
Definition: module.h:142
const char * getHost()
Definition: module.h:135
void setRank(int rank)
Definition: module.h:114
bool removeArgument(Argument &argument)
Definition: module.cpp:92
bool setParam(const char *szParam)
Definition: module.cpp:244
void setPostStopWait(double t)
Definition: module.h:175
GraphicModel & getModelBase()
Definition: module.h:190
void setName(const char *szName)
Definition: module.h:113
void setBasePrefix(const char *szPrefix)
Definition: module.h:127
const char * getEnvironment()
Definition: module.h:145
const char * getParam()
Definition: module.h:139
double getPostStopWait()
Definition: module.h:176
void setBroker(const char *szBroker)
Definition: module.h:124
bool addArgument(Argument &arg)
Definition: module.cpp:85
const char * getPrefix()
Definition: module.h:144
Module & operator=(const Module &rhs)
Definition: module.cpp:35
GenericResource & getResourceAt(int index) const
Definition: module.h:157
const char * getDescription()
Definition: module.h:134
double getPostExecWait()
Definition: module.h:174
bool addInput(InputData &input)
Definition: module.cpp:122
Author & getAuthorAt(int index)
Definition: module.h:161
void setOwner(Node *owner)
Definition: module.h:183
bool addResource(GenericResource &res)
Definition: module.cpp:142
void setPostExecWait(double t)
Definition: module.h:173
const char * getDisplay()
Definition: module.h:147
bool addOutput(OutputData &output)
Definition: module.cpp:102
void setVersion(const char *szVersion)
Definition: module.h:115
void setDisplay(const char *szDisplay)
Definition: module.h:129
bool getNeedDeployer()
Definition: module.h:137
a Node of a Graph
Definition: node.h:68
std::vector< InputData > InputContainer
Definition: module.h:89
std::vector< Author >::iterator AuthorIterator
Definition: module.h:94
std::vector< GenericResource * >::iterator ResourcePIterator
Definition: resource.h:63
std::vector< OutputData > OutputContainer
Definition: module.h:90
std::vector< InputData >::iterator InputIterator
Definition: module.h:91
std::vector< Module * > ModulePContainer
Definition: module.h:234
std::vector< Argument > ArgumentContainer
Definition: module.h:95
std::vector< Author > AuthorContainer
Definition: module.h:93
std::vector< GenericResource * > ResourcePContainer
Definition: resource.h:62
std::vector< OutputData >::iterator OutputIterator
Definition: module.h:92
std::vector< Module * >::iterator ModulePIterator
Definition: module.h:235
std::vector< Argument >::iterator ArgumentIterator
Definition: module.h:96
The main, catch-all namespace for YARP.
Definition: environment.h:25