YARP
Yet Another Robot Platform
module.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 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
yarp::manager::Module::setXmlFile
void setXmlFile(const char *szFilename)
Definition: module.h:121
yarp::manager::Module::getPrefix
const char * getPrefix()
Definition: module.h:144
yarp::manager::Module::clone
Node * clone() override
Definition: module.cpp:78
yarp::manager::Author::setName
void setName(const char *name)
Definition: module.h:33
yarp::manager::Module::getForced
bool getForced()
Definition: module.h:136
yarp::manager::InputIterator
std::vector< InputData >::iterator InputIterator
Definition: module.h:91
yarp::manager::Module::getVersion
const char * getVersion()
Definition: module.h:133
yarp::manager::Module::setBroker
void setBroker(const char *szBroker)
Definition: module.h:124
t
float t
Definition: FfmpegWriter.cpp:74
yarp::manager::Module::owner
Node * owner()
Definition: module.h:184
yarp::manager::Module::authorCount
int authorCount()
Definition: module.h:160
yarp::manager::Module::operator=
Module & operator=(const Module &rhs)
Definition: module.cpp:35
yarp::manager::Module::setWorkDir
void setWorkDir(const char *szWDir)
Definition: module.h:122
yarp::manager::Author::~Author
~Author()
Definition: module.h:31
yarp::manager::Module::getInputAt
InputData & getInputAt(int index)
Definition: module.h:155
yarp::manager::Module::setDescription
void setDescription(const char *szDesc)
Definition: module.h:116
yarp::manager::Argument::isSwitch
bool isSwitch()
Definition: module.h:71
yarp::manager::Author::setEmail
void setEmail(const char *email)
Definition: module.h:34
yarp::manager::Module::getArgumentAt
Argument & getArgumentAt(int index)
Definition: module.h:151
yarp::manager::Module::setOwner
void setOwner(Node *owner)
Definition: module.h:183
yarp::manager::Module::getStdio
const char * getStdio()
Definition: module.h:142
yarp::manager::Argument::setParam
void setParam(const char *szParam)
Definition: module.h:64
yarp::manager::Module::getDisplay
const char * getDisplay()
Definition: module.h:147
yarp::manager::Argument::getValue
const char * getValue()
Definition: module.h:69
yarp::manager::ResourcePContainer
std::vector< GenericResource * > ResourcePContainer
Definition: resource.h:62
yarp::manager::Module::getParam
const char * getParam()
Definition: module.h:139
yarp::manager::Module::setName
void setName(const char *szName)
Definition: module.h:113
utility.h
yarp::manager::Module::setPostStopWait
void setPostStopWait(double t)
Definition: module.h:175
yarp::manager::Module::getPostExecWait
double getPostExecWait()
Definition: module.h:174
yarp::manager::ArgumentIterator
std::vector< Argument >::iterator ArgumentIterator
Definition: module.h:96
yarp::manager::ModuleInterface
Class ModuleInterface.
Definition: application.h:164
yarp::manager::Module::setForced
void setForced(bool flag)
Definition: module.h:118
yarp::manager::Module::addResource
bool addResource(GenericResource &res)
Definition: module.cpp:142
yarp::manager::ModulePIterator
std::vector< Module * >::iterator ModulePIterator
Definition: module.h:235
yarp::manager::Module::getResourceAt
GenericResource & getResourceAt(int index) const
Definition: module.h:157
yarp::manager::Module::removeAuthor
bool removeAuthor(Author &author)
Definition: module.cpp:161
yarp::manager::Module::setModelBase
void setModelBase(GraphicModel &mdl)
Definition: module.h:191
yarp::manager::Module::removeArgument
bool removeArgument(Argument &argument)
Definition: module.cpp:92
yarp::manager::Module
Class Module.
Definition: module.h:103
yarp::manager::Module::getModelBase
GraphicModel & getModelBase()
Definition: module.h:190
yarp::manager::AuthorContainer
std::vector< Author > AuthorContainer
Definition: module.h:93
yarp::manager::Module::setBasePrefix
void setBasePrefix(const char *szPrefix)
Definition: module.h:127
yarp::manager::Module::addInput
bool addInput(InputData &input)
Definition: module.cpp:122
yarp::manager::OutputData
Definition: data.h:72
yarp::manager::InputContainer
std::vector< InputData > InputContainer
Definition: module.h:89
yarp::manager::OutputContainer
std::vector< OutputData > OutputContainer
Definition: module.h:90
yarp::manager::Argument
Class Argument.
Definition: module.h:50
node.h
yarp::manager::Module::setVersion
void setVersion(const char *szVersion)
Definition: module.h:115
yarp::manager::Module::setDisplay
void setDisplay(const char *szDisplay)
Definition: module.h:129
yarp::manager::Argument::Argument
Argument(const char *szParam, bool required=false, const char *szDesc=nullptr, bool _switch=false)
Definition: module.h:53
yarp::manager::Module::getWorkDir
const char * getWorkDir()
Definition: module.h:141
yarp::manager::Module::resourceCount
int resourceCount() const
Definition: module.h:156
yarp::manager::Node
a Node of a Graph
Definition: node.h:68
yarp::manager::Module::addAuthor
void addAuthor(Author &author)
Definition: module.h:159
yarp::manager::Module::removeOutput
bool removeOutput(OutputData &output)
Definition: module.cpp:110
yarp::manager::Argument::operator==
bool operator==(const Argument &alt) const
Definition: module.h:73
resource.h
yarp::manager::Argument::~Argument
virtual ~Argument()
Definition: module.h:62
yarp::manager::Module::setStdio
void setStdio(const char *szStdio)
Definition: module.h:123
yarp::manager::Module::getHost
const char * getHost()
Definition: module.h:135
ymm-types.h
yarp::manager::Module::clearOutputs
void clearOutputs()
Definition: module.h:180
yarp::manager::Module::getNeedDeployer
bool getNeedDeployer()
Definition: module.h:137
yarp::manager::Argument::setValue
void setValue(const char *szValue)
Definition: module.h:66
yarp::manager::Author::operator==
bool operator==(const Author &alt) const
Definition: module.h:38
yarp::manager::Argument::isRequired
bool isRequired()
Definition: module.h:72
yarp::manager::Module::getXmlFile
const char * getXmlFile()
Definition: module.h:140
yarp::manager::Argument::setDefault
void setDefault(const char *szDefault)
Definition: module.h:65
yarp::manager::Author::getName
const char * getName()
Definition: module.h:35
yarp::manager::Module::clearResources
void clearResources()
Definition: module.h:181
yarp::manager::Argument::getDefault
const char * getDefault()
Definition: module.h:67
yarp::manager::Module::getRank
int getRank()
Definition: module.h:131
yarp::manager::Module::removeInput
bool removeInput(InputData &input)
Definition: module.cpp:130
yarp::manager::Module::clearInputs
void clearInputs()
Definition: module.h:179
yarp::manager::Module::inputCount
int inputCount()
Definition: module.h:154
yarp::manager::Module::setParam
bool setParam(const char *szParam)
Definition: module.cpp:244
yarp::manager::ArgumentContainer
std::vector< Argument > ArgumentContainer
Definition: module.h:95
yarp::manager::GenericResource
Definition: resource.h:21
yarp::manager::Module::setPostExecWait
void setPostExecWait(double t)
Definition: module.h:173
yarp::manager::GraphicModel
Definition: node.h:28
yarp::manager::Module::setEnvironment
void setEnvironment(const char *szEnv)
Definition: module.h:126
yarp::manager::Module::removeResource
bool removeResource(GenericResource &res)
Definition: module.cpp:151
yarp::manager::Module::getOutputAt
OutputData & getOutputAt(int index)
Definition: module.h:153
data.h
yarp::manager::ResourcePIterator
std::vector< GenericResource * >::iterator ResourcePIterator
Definition: resource.h:63
yarp::manager::Module::outputCount
int outputCount()
Definition: module.h:152
yarp::manager::Module::Module
Module()
Definition: module.cpp:19
yarp::manager::Author::getEmail
const char * getEmail()
Definition: module.h:36
yarp::manager::Module::~Module
~Module() override
yarp::manager::Module::getBroker
const char * getBroker()
Definition: module.h:143
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::manager::Module::setNeedDeployer
void setNeedDeployer(bool need)
Definition: module.h:128
yarp::manager::Module::setHost
void setHost(const char *szHost)
Definition: module.h:117
yarp::manager::Module::setRank
void setRank(int rank)
Definition: module.h:114
yarp::manager::Argument::getParam
const char * getParam()
Definition: module.h:68
yarp::manager::Module::addOutput
bool addOutput(OutputData &output)
Definition: module.cpp:102
yarp::manager::ModulePContainer
std::vector< Module * > ModulePContainer
Definition: module.h:234
yarp::manager::Module::getPostStopWait
double getPostStopWait()
Definition: module.h:176
yarp::manager::Author::Author
Author()
Definition: module.h:26
yarp::manager::Module::getBasePrefix
const char * getBasePrefix()
Definition: module.h:146
yarp::manager::Author
Definition: module.h:24
yarp::manager::Module::getDescription
const char * getDescription()
Definition: module.h:134
yarp::manager::AuthorIterator
std::vector< Author >::iterator AuthorIterator
Definition: module.h:94
yarp::manager::Module::getName
const char * getName()
Definition: module.h:132
api.h
yarp::manager::Module::getAuthorAt
Author & getAuthorAt(int index)
Definition: module.h:161
yarp::manager::Module::addArgument
bool addArgument(Argument &arg)
Definition: module.cpp:85
yarp::manager::InputData
Class InputData.
Definition: data.h:25
yarp::manager::OutputIterator
std::vector< OutputData >::iterator OutputIterator
Definition: module.h:92
yarp::manager::Author::Author
Author(const char *name, const char *email)
Definition: module.h:27
yarp::manager::Module::argumentCount
int argumentCount()
Definition: module.h:150
yarp::manager::Argument::getDescription
const char * getDescription()
Definition: module.h:70
yarp::manager::Module::setPrefix
void setPrefix(const char *szPrefix)
Definition: module.h:125
yarp::manager::Module::getEnvironment
const char * getEnvironment()
Definition: module.h:145
yarp::manager::Module::clear
void clear()
Definition: module.cpp:212