charon-core  0.3.1
Slots.cpp
Go to the documentation of this file.
1 /* This file is part of Charon.
2 
3  Charon is free software: you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as published by
5  the Free Software Foundation, either version 3 of the License, or
6  (at your option) any later version.
7 
8  Charon is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU Lesser General Public License for more details.
12 
13  You should have received a copy of the GNU Lesser General Public License
14  along with Charon. If not, see <http://www.gnu.org/licenses/>.
15  */
22 #include <cassert>
24 #include <charon-core/StringTool.h>
26 #include "../include/charon-core/Slots.hxx"
27 #include "../include/charon-core/SlotBundleInterfaces.h"
28 
29 // ========================== class Slot ================================
30 
32  _parent(0) {
33 }
34 
35 void Slot::init(ParameteredObject* parent, std::string name,
36  std::string type) {
37 
38  if(_parent)
39  throw std::runtime_error(std::string("Parameter \"") + name + "\" already assigned");
40  if(!parent)
41  throw std::runtime_error("Setting invalid parent object!");
42  if(!name.length())
43  throw std::runtime_error("Invalid name: length 0");
44 
45  _parent = parent;
46 
48  if(_type!="virtual")
49  {
50  _name = name;
51 
52  }
53  _displayName=name;
54 }
55 
56 void Slot::init(ParameteredObject* parent, std::string name,std::string displayname,
57  std::string type) {
58  Slot::init(parent,name,type);
59  _displayName=displayname;
60 }
61 
62 Slot::~Slot() {
63 }
64 
66  return *_parent;
67 }
68 
70  return *_parent;
71 }
72 
73 std::string Slot::getName() const {
74  return _name;
75 }
76 
77 bool Slot::getMulti() const {
78  return _multiSlot;
79 }
80 
81 bool Slot::getOptional() const {
82  return _optional;
83 }
84 
85 bool Slot::connected() const {
86  return (getTargets().size() > 0);
87 }
88 
89 bool Slot::connected(Slot& target) const {
90  std::set<Slot *> targets = getTargets();
91  return (targets.find(&target) != targets.end());
92 }
93 
94 bool Slot::connect(Slot& target) {
95  if (!&target) {
96  raise("Slot::connect(): invalid target slot given (null)");
97  }
98  bool ret = true;
99  ret &= _addTarget(&target);
100  ret &= target._addTarget(this);
101  return ret;
102 }
103 
105  bool res = true;
106  std::set<Slot*>::iterator tIter;
107  std::set<Slot*> targets = getTargets();
108  for (tIter = targets.begin(); tIter != targets.end(); tIter++) {
109  res &= (*tIter)->_removeTarget(this);
110  res &= _removeTarget(*tIter);
111  }
112  return res;
113 }
114 
115 bool Slot::disconnect(Slot& target) {
116  std::set<Slot*> targets = getTargets();
117  if (targets.find(&target) == targets.end())
118  return false;
119  bool res = true;
120  res &= target._removeTarget(this);
121  res &= _removeTarget(&target);
122  return res;
123 }
124 
125 void Slot::save(ParameterFile& /*pf*/) const {
126  //Does nothing, erasing the parameter file is not required
127 }
128 
129 void Slot::printInfo(const std::string& msg) const {
130  sout << "(II) Slot \"" << _parent->getName() << "." << _name
131  << "\": " << msg << std::endl;
132 }
133 
134 void Slot::printWarning(const std::string& msg) const {
135  sout << "(WW) Slot \"" << _parent->getName() << "." << _name
136  << "\": " << msg << std::endl;
137 }
138 
139 void Slot::printError(const std::string& msg) const {
140  sout << "(EE) Slot \"" << _parent->getName() << "." << _name
141  << "\" (type " << getType() << "): " << msg << std::endl;
142 }
143 
144 void Slot::raise(const std::string& msg) const {
145  throw std::runtime_error(
146  "Error in Slot \"" + _parent->getName() + "." + _name
147  + "\" (type " + getType() + "): "
148  + msg);
149 }
150 
151 // ======================= class VirtualSlot ==============================
152 
153 VirtualSlot::VirtualSlot(std::string virtType,int num) {
154  _type="virtual";
155  std::stringstream vname;
156  vname<<"Virtual"<<virtType<<num;
157  _name=vname.str();
159  _partner=0;
160  _optional=true;
161 }
162 
164 {
165  assert(target);
166  if(target) {
167  if(!isValidTarget(target)) {
168  return false;
169  }
170  // if(_target.size()>0)
171  // return false;
172 
173  // _displayName=target->getDisplayName();
174  // _type=StringTool::toLowerCase(target->getType());
175 
176  // _partner->setDisplayNameAndType(target->getName(),target->getType());
177  // _target.insert(target);
178  onAddTarget(target);
179  }
180  else {
181  return false;
182  }
183 
184  return true;
185 }
186 
187 const std::string& VirtualOutputSlot::getConfig() const {
188  return _managerconfig;
189 }
190 
192  return onRemoveTarget(target);
193 }
194 
196 {
197  Slot::save(pf);
198  std::vector<std::string> targetList;
199  std::set<Slot*>::const_iterator slot;
200  for (slot = _target.begin(); slot != _target.end(); slot++)
201  targetList.push_back((*slot)->getParent().getName() + "."
202  + (*slot)->getName());
203  if (targetList.size())
204  pf.set<std::string> (_parent->getName() + "." + _name, targetList);
205  else if (pf.isSet(_parent->getName() + "." + _name))
206  pf.erase(_parent->getName() + "." + _name);
207 
208  pf.set<std::string> (_parent->getName() + "." + _name+".displayName", _displayName);
209  pf.set<std::string> (_parent->getName() + "." + _name+".typeSlot", _type);
210  onSave(pf);
211 }
212 
214  if(_loop) {
215  if(_loopPartner) {
216  OutputSlotIntf* sl= dynamic_cast<OutputSlotIntf*>(
217  *(_loopPartner->getTargets().begin()));
218  return sl->getDataSlot();
219  }
220  else {
221  raise("_loopset to true but no _loopPartner!!");
222  }
223  }
224  else if(_partner) {
225  if(_partner->getTargets().size()) {
226  OutputSlotIntf* sl=dynamic_cast<OutputSlotIntf*>(
227  *(_partner->getTargets().begin()));
228  return sl->getDataSlot();
229  }
230  //Slot::raise(_partner->getName()+" not connected!!");
231  }
232  return this;
233 }
234 
236 {
237  // disconnect from other slots
239 
240  if (!pf.isSet(Slot::_parent->getName() + "." + Slot::_name))
241  // nothing to do
242  return;
243 
244  // get all targets from parameter file
245  std::vector<std::string> targetList = pf.getList<std::string> (
246  Slot::_parent->getName() + "." + Slot::_name);
247 
248  // only multislots can be connected to more than one source!
249  if (!(Slot::_multiSlot || (targetList.size() < 2))) {
250  Slot::raise("multiple targets but not a multi-slot");
251  }
252 
253  if (!targetList.size())
254  // nothing to do
255  return;
256 
257  std::vector<std::string>::const_iterator tStrIter;
258 
259  // and add corresponding slots to _targets
260  for (tStrIter = targetList.begin(); tStrIter != targetList.end(); tStrIter++) {
261  ParameteredObject* targObj = man->getInstance(tStrIter->substr(0,
262  tStrIter->find(".")));
263 
264  Slot * targetSlot = targObj->getSlot(
265  tStrIter->substr(tStrIter->find(".") + 1));
266  Slot::connect(*targetSlot);
267  }
268 
269  //if(pf.isSet(_parent->getName() + "." + _name+".displayName"))
270  //_displayName=pf.get<std::string>(_parent->getName() + "." + _name+".displayName");
271  //if(pf.isSet(_parent->getName() + "." + _name+".typeSlot"))
272  //_type=pf.get<std::string>(_parent->getName() + "." + _name+".typeSlot");
273  onLoad(pf,man);
274 
275 }
276 
278 {
279  if(_partner!=insl) {
280  if(isValidPartner(insl)) {
281  _partner=insl;
282  insl->setVirtualPartnerSlot(this);
283  }
284  else {
285  Slot::raise("Invalid VirtualSlot pairing!!");
286  }
287  }
288 }
289 
291  _cacheType=type;
292 }
293 
295  return _cacheType;
296 }
297 
299 }
300 
302 }
303 
304 std::string VirtualSlot::guessType() const {
305  return getType();
306 }
307 
308 std::set<Slot *> VirtualSlot::getTargets() const {
309  return _target;
310 }
311 
312 VirtualInputSlot::VirtualInputSlot(int num)
313  :VirtualSlot("Slot-in",num)
314 {
315  _multiSlot=false;
316 }
317 
318 std::string VirtualInputSlot::getType() const {
319  return VirtualSlot::getType();
320 }
321 
322 std::string VirtualInputSlot::getName() const {
323  return VirtualSlot::getName();
324 }
325 
326 VirtualOutputSlot::VirtualOutputSlot(int num)
327  :VirtualSlot("Slot-out",num)
328 {
329  _multiSlot=true;
330  _loopPartner=0;
331  _loop=0;
332 }
333 
334 bool VirtualOutputSlot::isValidPartner(VirtualSlot *insl) {
335  return (dynamic_cast<VirtualInputSlot*>(insl) != 0);
336 }
337 
338 bool VirtualInputSlot::isValidPartner(VirtualSlot *insl) {
339  return (dynamic_cast<VirtualOutputSlot*>(insl) != 0);
340 }
341 
342 void VirtualSlot::setDisplayNameAndType(std::string name, std::string type) {
343  _displayName=name;
345 }
346 
347 bool VirtualSlot::onAddTarget(Slot*) {
348  return true;
349 }
350 
351 bool VirtualSlot::onRemoveTarget(Slot*) {
352  return true;
353 }
354 
355 bool VirtualInputSlot::onAddTarget(Slot *target) {
356  if(dynamic_cast<SlotBundleIntf*>(_parent))
357  {
358 
359  _partner->_displayName=target->getParent().getName()+"."+target->getDisplayName();
360  }
361 
362  if(_target.size()>0) {
363  return false;
364  }
365  if(StringTool::toLowerCase(_type)=="virtual") {
366  _type=target->getType();
367  _partner->_type=_type;
368 
369  }
370  _target.insert(target);
371  //_displayName=target->getName();
372  //_partner->setDisplayNameAndType(_displayName,_type);
373  //target->_addTarget(dynamic_cast<Slot*>(this));
374  OutputSlotIntf* sl=dynamic_cast<OutputSlotIntf*>(target);
375  VirtualOutputSlot* partner=dynamic_cast<VirtualOutputSlot*>(_partner);
376 #pragma message ("This is ugly, OutputSlot should not have to deal \
377 with DataManager. Maybe put DataManager into VirtualSlot??")
378  if(sl) {
379  sl->setConfig(_parent->getName()+"."+_name);
380  partner->_managerconfig=sl->getConfig();
381  partner->_cacheType=sl->getCacheType();
382 
383  return true;
384  }
385  return false;
386 }
387 
388 bool VirtualInputSlot::onRemoveTarget(Slot *target) {
389  if(_target.find(target)==_target.end()) {
390  return false;
391  }
392  OutputSlotIntf* sl=dynamic_cast<OutputSlotIntf*>(target);
393  VirtualOutputSlot* partner=dynamic_cast<VirtualOutputSlot*>(_partner);
394  if(sl) {
395  sl->setConfig("");
396  partner->_managerconfig="";
397  partner->_cacheType=CACHE_INVALID;
398  if(_partner->_target.size()==0) {
399  _partner->_type="virtual";
400  }
401  if(dynamic_cast<SlotBundleIntf*>(_parent))
402  {
403  _partner->_displayName=_partner->_name;
404  }
405  _target.erase(target);
406  return true;
407  }
408  return false;
409 }
410 
411 bool VirtualOutputSlot::isValidTarget(Slot *target) {
412  InputSlotIntf *sl=dynamic_cast<InputSlotIntf*>(target);
413  return (sl != 0);
414 }
415 
416 bool VirtualInputSlot::isValidTarget(Slot *target) {
417  OutputSlotIntf* sl=dynamic_cast<OutputSlotIntf*>(target);
418  return (sl != 0);
419 }
420 
421 std::string VirtualOutputSlot::getType() const {
422  return VirtualSlot::getType();
423 }
424 
425 std::string VirtualOutputSlot::getName() const {
426  return VirtualSlot::getName();
427 }
428 
429 std::string VirtualSlot::getType() const {
430  return _type;
431 }
432 
433 void VirtualSlot::onLoad(
434  const ParameterFile&, const PluginManagerInterface*) {
435 }
436 
437 void VirtualSlot::onSave(ParameterFile&) const {
438 }
439 
440 void VirtualOutputSlot::onLoad(
441  const ParameterFile &pf, const PluginManagerInterface*) {
442  if(pf.isSet(_parent->getName() + "." + _name+".config"))
443  _managerconfig=pf.get<std::string>(_parent->getName() + "." + _name+".config");
444  if(pf.isSet(_parent->getName()+"."+_name+".cache_type"))
445  {
446  std::string cType=pf.get<std::string>(_parent->getName()+"."+_name+".cache_type");
447  if(cType=="Cache_mem") {
448  _cacheType=CACHE_MEM;
449  }
450  else if(cType=="Cache_managed") {
451  _cacheType=CACHE_MANAGED;
452  }
453  else {
454  _cacheType=CACHE_INVALID;
455  }
456  }
457 }
458 
459 void VirtualOutputSlot::onSave(ParameterFile &pf) const {
460  pf.set<std::string> (
461  _parent->getName() + "." + _name+".config", _managerconfig);
462  std::string cType;
463  switch (_cacheType) {
464  case CACHE_MEM:
465  cType="Cache_mem";
466  break;
467  case CACHE_MANAGED:
468  cType="Cache_managed";
469  break;
470  case CACHE_INVALID:
471  cType="Cache_invalid";
472  break;
473  default:
474  cType="Cache_mem";
475  }
476 
477  pf.set<std::string>(_parent->getName()+"."+_name+".cache_type",cType);
478 }
479 
480 std::string VirtualSlot::getName() const {
481  return _name;
482 }
483 
484 std::string Slot::getDisplayName() const {
485  return _displayName;
486 }
487 
488 void VirtualOutputSlot::setConfig(std::string conf) {
489  if(_partner) {
490  if(_partner->getTargets().size()) {
491  OutputSlotIntf* sl=dynamic_cast<OutputSlotIntf*>(
492  *(_partner->getTargets().begin()));
493  sl->setConfig(conf);
494  }
495  //Slot::raise(_partner->getName()+" not connected!!");
496  }
497  _managerconfig=conf;
498 }
499 
500 void VirtualOutputSlot::setLoop(bool loop) {
501  _loop=loop;
502 }
503 
504 void VirtualOutputSlot::setLoopPartner(VirtualInputSlot *loopPartner) {
505  if(_loopPartner) {
506  if(loopPartner) {
507  raise("loopPartner already set");
508  }
509  else {
510  _loopPartner=0;
511  }
512  }
513  else {
514  _loopPartner=loopPartner;
515  }
516 }
517 
518 bool VirtualOutputSlot::onAddTarget(Slot *target) {
519  if(dynamic_cast<SlotBundleIntf*>(_parent))
520  {
521  _partner->_displayName=target->getParent().getName()+"."+target->getDisplayName();
522  }
523  if(StringTool::toLowerCase(_type)=="virtual") {
524  _type=target->getType();
525  _partner->_type=_type;
526 
527  }
528  _target.insert(target);
529 
530  return true;
531 }
532 
533 bool VirtualOutputSlot::onRemoveTarget(Slot *target) {
534  if(_target.find(target)!=_target.end()) {
535  _target.erase(target);
536 
537  if(_target.size()==0) {
538  if(_partner->_target.size()==0)
539  {
540  _type="virtual";
542  _partner->_type=_type;
543  }
544  }
545  return true;
546  }
547  return false;
548 }
virtual const std::string & getConfig() const =0
get manager configuration string
std::string _displayName
Slot display name.
Definition: Slots.h:74
stay in memory after execution
Definition: Slots.h:206
virtual const OutputSlotIntf * getDataSlot() const =0
Return a pointer to a real slot.
virtual std::string getType() const =0
Get slot type.
Interface for a plugin manager.
virtual std::set< Slot * > getTargets() const
Get pointers to the connected targets.
Definition: Slots.cpp:308
virtual void setConfig(std::string conf)
set manager configuration string
Definition: Slots.cpp:488
virtual Slot::CacheType getCacheType() const =0
query data cache type
virtual void setConfig(std::string conf)=0
set manager configuration string
std::string getType() const
overloaded getType
Definition: Slots.cpp:318
This class serves to store parameters used within the Charon Project.
Definition: ParameterFile.h:68
std::string getName() const
Get slot name.
Definition: Slots.cpp:480
bool getOptional() const
return value of _optional;
Definition: Slots.cpp:81
void init(ParameteredObject *parent, std::string name, std::string type)
initialize parent and name
Definition: Slots.cpp:35
void setVirtualPartnerSlot(VirtualSlot *insl)
Set corresponding partner VirtualSlot.
Definition: Slots.cpp:277
SplitStream sout
Dummy instance for usage in other files (for interface too).
virtual void load(const ParameterFile &pf, const PluginManagerInterface *man)
overload Slot functions. Load and save the config string
Definition: Slots.cpp:235
void erase(std::string parameter)
Delete a parameter from the parameter list.
bool disconnect()
Remove all slot targets.
Definition: Slots.cpp:104
Base class for serializable objects.
Slot::CacheType getCacheType() const
get the cache type from _slot
Definition: Slots.cpp:294
void printInfo(const std::string &msg) const
print info message with slot name to sout
Definition: Slots.cpp:129
bool _optional
flag to mark this slot as optional
Definition: Slots.h:80
const OutputSlotIntf * getDataSlot() const
Return a pointer to a real slot.
Definition: Slots.cpp:213
virtual bool _addTarget(Slot *target)=0
Add slot target.
bool connected() const
Check if slot is connected.
Definition: Slots.cpp:85
dumped after execution, not recommended
Definition: Slots.h:205
std::vector< T > getList(std::string parameter) const
If multiple values are set, return a list containing these values.
virtual ParameteredObject * getInstance(const std::string &instanceName) const =0
Get an existing instance of a loaded plugin.
virtual void save(ParameterFile &pf) const
Save slot connections This function disconnects already established connections in the parameterFile ...
Definition: Slots.cpp:195
VirtualOutputSlot This class holds a pointer to an output if the output is CACHE_MEM, otherwise it loads a config string from a given parameterfile.
Definition: Slots.h:478
ParameteredObject & getParent()
get parent object
Definition: Slots.cpp:65
virtual void finalize()
finalize slot
Definition: Slots.cpp:301
std::string getType() const
Get slot type.
Definition: Slots.cpp:429
std::string getName() const
get the Name
Definition: Slots.cpp:425
std::string getType() const
overloaded getType
Definition: Slots.cpp:421
virtual std::string getName() const
Get slot name.
Definition: Slots.cpp:73
virtual std::set< Slot * > getTargets() const =0
Get pointers to the connected targets.
void setCacheType(Slot::CacheType type)
set the cache type of _slot
Definition: Slots.cpp:290
bool _removeTarget(Slot *target)
remove target
Definition: Slots.cpp:191
void set(std::string parameter, const T &value=T())
Set a parameter to the given single value.
abstract interface class for output slots
Definition: Slots.h:373
Declaration of StringTool methods.
bool getMulti() const
return value of _multiSlot;
Definition: Slots.cpp:77
std::string guessType() const
Try to guess slot type.
Definition: Slots.cpp:304
bool _multiSlot
flag to mark this slot as a multislot, that can have multiple sources/targets.
Definition: Slots.h:84
T get(std::string parameter) const
Get the value of a specified parameter.
bool isSet(std::string parameter) const
Check if a givem parameter has already been set.
CacheType
slot data cache type
Definition: Slots.h:204
Implementation of the template class ParameterFile.
Abstract interface class for input slots.
Definition: Slots.h:293
Slot * getSlot(const std::string &slotName) const
Get pointer to some slot (by name)
bool _addTarget(Slot *target)
set _slot to Casted target
Definition: Slots.cpp:163
std::string _type
Slot type.
Definition: Slots.h:77
charon_core_DLL_PUBLIC std::string type(const std::string &typeInfo)
Get type representation.
Commom properties of slot objects.
Definition: Slots.h:47
Declaration of class SplitStream.
void printWarning(const std::string &msg) const
print warning message with slot name to sout
Definition: Slots.cpp:134
void raise(const std::string &msg) const
throw runtime error with slot name and type info
Definition: Slots.cpp:144
std::string _name
Slot name.
Definition: Slots.h:71
const std::string & getName() const
instance name
virtual std::string getDisplayName() const
Get slot display name.
Definition: Slots.cpp:484
virtual void save(ParameterFile &pf) const
Save slot connections This function disconnects already established connections in the parameterFile ...
Definition: Slots.cpp:125
Slot()
default constructor
Definition: Slots.cpp:31
void printError(const std::string &msg) const
print error with slot name and type info to sout
Definition: Slots.cpp:139
std::string charon_core_DLL_PUBLIC toLowerCase(std::string s)
Convert a string to lowercase.
Definition: StringTool.cpp:55
virtual void prepare()
prepare slot
Definition: Slots.cpp:298
const std::string & getConfig() const
get the manager config
Definition: Slots.cpp:187
std::string getName() const
get the Name
Definition: Slots.cpp:322
cached on disk after execution, save memory
Definition: Slots.h:207
ParameteredObject * _parent
Pointer to parent object.
Definition: Slots.h:68
virtual bool _removeTarget(Slot *target)=0
Remove slot target.
bool connect(Slot &target)
Connect with given slot.
Definition: Slots.cpp:94