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