YARP
Yet Another Robot Platform
module.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include <yarp/manager/module.h>
7 #include <cstdio>
8 #include <string>
9 #include <cstring>
10 
11 
12 using namespace yarp::manager;
13 
14 
16 
17 
18 Module::Module(const char* szName) : Node(MODULE)
19 {
20  clear();
21  setName(szName);
22 }
23 
24 
25 Module::Module(const Module &mod) : Node(mod)
26 {
27  Module::swap(mod);
28 }
29 
30 
32 {
33  Node::operator=(rhs);
34  Module::swap(rhs);
35  return *this;
36 }
37 
38 
39 void Module::swap(const Module &mod)
40 {
41  clear();
42  iRank = mod.iRank;
43  strName = mod.strName;
44  arguments = mod.arguments;
45  strVersion = mod.strVersion;
46  strDescription = mod.strDescription;
47  strHost = mod.strHost;
48  bForced = mod.bForced;
49  authors = mod.authors;
50  outputs = mod.outputs;
51  inputs = mod.inputs;
52  strXmlFile = mod.strXmlFile;
53  strParam = mod.strParam;
54  strWorkDir = mod.strWorkDir;
55  strStdio = mod.strStdio;
56  strBroker = mod.strBroker;
57  bNeedDeployer = mod.bNeedDeployer;
58  strPrefix = mod.strPrefix;
59  strEnvironment = mod.strEnvironment;
60  strBasePrefix = mod.strBasePrefix;
61  strDisplay = mod.strDisplay;
62  modOwner = mod.modOwner;
63  waitStart = mod.waitStart;
64  waitStop = mod.waitStop;
65  // deep copy
66  for (int i = 0; i < mod.resourceCount(); i++) {
67  addResource(mod.getResourceAt(i));
68  }
69 }
70 
71 
72 Module::~Module() = default;
73 
74 
76 {
77  auto* mod = new Module(*this);
78  return mod;
79 }
80 
81 
83 {
84  arguments.push_back(argument);
85  return true;
86 }
87 
88 
90 {
91  auto itr = findArgument(argument);
92  if (itr == arguments.end()) {
93  return true;
94  }
95  arguments.erase(itr);
96  return true;
97 }
98 
99 
101 {
102  //__CHECK_NULLPTR(output);
103  outputs.push_back(output);
104  return true;
105 }
106 
107 
109 {
110  //__CHECK_NULLPTR(output);
111 
112  auto itr = findOutput(output);
113  if (itr == outputs.end()) {
114  return true;
115  }
116  outputs.erase(itr);
117  return true;
118 }
119 
120 
122 {
123  //__CHECK_NULLPTR(input);
124  inputs.push_back(input);
125  return true;
126 }
127 
128 
130 {
131  //__CHECK_NULLPTR(input);
132 
133  auto itr = findInput(input);
134  if (itr == inputs.end()) {
135  return true;
136  }
137  inputs.erase(itr);
138  return true;
139 }
140 
141 
143 {
144  auto* newres = (GenericResource*) res.clone();
145  newres->setOwner(this);
146  resources.push_back(newres);
147  return true;
148 }
149 
150 
152 {
153  auto itr = findResource(res);
154  if (itr == resources.end()) {
155  return true;
156  }
157  resources.erase(itr);
158  delete (*itr);
159  return true;
160 }
161 
163 {
164  AuthorIterator itr;
165  for (itr = authors.begin(); itr < authors.end(); itr++) {
166  if((*itr) == author)
167  {
168  authors.erase(itr);
169  return true;
170  }
171  }
172  return true;
173 }
174 
175 
176 ArgumentIterator Module::findArgument(Argument& argument)
177 {
178  ArgumentIterator itr;
179  for (itr = arguments.begin(); itr < arguments.end(); itr++) {
180  if ((*itr) == argument) {
181  return itr;
182  }
183  }
184  return arguments.end();
185 }
186 
187 
188 InputIterator Module::findInput(InputData& input)
189 {
190  InputIterator itr;
191  for (itr = inputs.begin(); itr < inputs.end(); itr++) {
192  if ((*itr) == input) {
193  return itr;
194  }
195  }
196  return inputs.end();
197 }
198 
199 
200 OutputIterator Module::findOutput(OutputData& output)
201 {
202  OutputIterator itr;
203  for (itr = outputs.begin(); itr < outputs.end(); itr++) {
204  if ((*itr) == output) {
205  return itr;
206  }
207  }
208  return outputs.end();
209 }
210 
211 ResourcePIterator Module::findResource(GenericResource& res)
212 {
213  ResourcePIterator itr;
214  for (itr = resources.begin(); itr < resources.end(); itr++) {
215  if (*(*itr) == res) {
216  return itr;
217  }
218  }
219  return resources.end();
220 }
221 
223 {
224  modOwner = nullptr;
225  iRank = 1;
226  strName.clear();
227  arguments.clear();
228  strVersion.clear();
229  strDescription.clear();
230  strHost.clear();
231  bForced = false;
232  bNeedDeployer = false;
233  authors.clear();
234  outputs.clear();
235  inputs.clear();
236  strXmlFile.clear();
237  strParam.clear();
238  strWorkDir.clear();
239  strStdio.clear();
240  strBroker.clear();
241  strPrefix.clear();
242  strEnvironment.clear();
243  strBasePrefix.clear();
244  strDisplay.clear();
245  for(auto& resource : resources)
246  {
247  delete resource;
248  resource = nullptr;
249  }
250  resources.clear();
251  waitStart = waitStop = 0.0;
252 }
253 
254 bool Module::setParam(const char* szParam)
255 {
256  __CHECK_NULLPTR(szParam);
257 
258  bool bokay = true;
259  strParam = szParam;
261  ArgumentIterator itr;
262  for(itr=arguments.begin(); itr<arguments.end(); itr++)
263  {
264  std::string strVal;
265  bool ret = getParamValue((*itr).getParam(), (*itr).isSwitch(), strVal);
266  if(!ret)
267  {
268  OSTRINGSTREAM msg;
269  msg<<"Error in parsing parameters of "<<getName() \
270  <<". ( '"<< (*itr).getParam()<<"' is not correct. )";
271  logger->addWarning(msg);
272  bokay = false;
273  }
274  else
275  {
276  if ((*itr).isSwitch()) {
277  (*itr).setValue(strVal.c_str());
278  } else {
279  if (strVal != "off") {
280  (*itr).setValue(strVal.c_str());
281  }
282  }
283  }
284  }
285  return bokay;
286 }
287 
288 bool Module::getParamValue(const char* key, bool bSwitch, std::string &param)
289 {
290  if (!key) {
291  return false;
292  }
293 
294  //printf("\n\nparsing '%s' for %s (switch:%d)\n", strParam.c_str(), key, bSwitch);
295  std::string strKey = std::string("--") + std::string(key);
296  size_t pos = strParam.find(strKey);
297  if(pos == std::string::npos)
298  {
299  param = "off";
300  return true;
301  }
302 
303  if(bSwitch)
304  {
305  param = "on";
306  return true;
307  }
308  //printf("%s %d \n", __FILE__, __LINE__);
309 
310  pos += strKey.size();
311  if ((pos >= strParam.length()) || (strParam.at(pos) != ' ')) {
312  return false;
313  }
314 
315  // skip all spaces
316  while(strParam.at(pos++) == ' ')
317  {
318  if (pos >= strParam.length()) {
319  return false;
320  }
321  }
322  pos--;
323 
324  size_t pos2 = pos;
325  while ((pos2 < strParam.length()) && (strParam.at(pos2) != ' ')) {
326  pos2++;
327  }
328  param = strParam.substr(pos, pos2-pos);
329  return true;
330 }
bool ret
Class Argument.
Definition: module.h:47
Singleton class ErrorLogger.
Definition: utility.h:57
void addWarning(const char *szWarning)
Definition: utility.cpp:104
static ErrorLogger * Instance()
Singleton class ErrorLogger.
Definition: utility.cpp:98
Class InputData.
Definition: data.h:22
Class Module.
Definition: module.h:100
bool removeInput(InputData &input)
Definition: module.cpp:129
bool removeOutput(OutputData &output)
Definition: module.cpp:108
bool removeResource(GenericResource &res)
Definition: module.cpp:151
const char * getName()
Definition: module.h:129
Node * clone() override
Definition: module.cpp:75
bool removeAuthor(Author &author)
Definition: module.cpp:162
int resourceCount() const
Definition: module.h:153
bool removeArgument(Argument &argument)
Definition: module.cpp:89
bool setParam(const char *szParam)
Definition: module.cpp:254
void setName(const char *szName)
Definition: module.h:110
bool addArgument(Argument &arg)
Definition: module.cpp:82
Module & operator=(const Module &rhs)
Definition: module.cpp:31
GenericResource & getResourceAt(int index) const
Definition: module.h:154
bool addInput(InputData &input)
Definition: module.cpp:121
bool addResource(GenericResource &res)
Definition: module.cpp:142
bool addOutput(OutputData &output)
Definition: module.cpp:100
a Node of a Graph
Definition: node.h:65
virtual Node * clone()=0
std::vector< Author >::iterator AuthorIterator
Definition: module.h:91
std::vector< GenericResource * >::iterator ResourcePIterator
Definition: resource.h:60
std::vector< InputData >::iterator InputIterator
Definition: module.h:88
std::stringstream OSTRINGSTREAM
Definition: utility.h:49
std::vector< OutputData >::iterator OutputIterator
Definition: module.h:89
std::vector< Argument >::iterator ArgumentIterator
Definition: module.h:93
#define __CHECK_NULLPTR(_ptr)
Definition: ymm-types.h:80