charon-core  0.3.1
Slots.h
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 */
34 #ifndef _SLOTS_H
35 #define _SLOTS_H
36 
37 #include "TypeDetector.h"
38 #include "AbstractData.hxx"
39 #include <set>
40 #include "DllEx.h"
41 
42 class ParameteredObject;
44 class ParameterFile;
45 
48 private:
50  Slot(const Slot&);
51 
52 protected:
58  virtual bool _addTarget(Slot* target) = 0;
59 
65  virtual bool _removeTarget(Slot* target) = 0;
66 
69 
71  std::string _name;
72 
74  std::string _displayName;
75 
77  std::string _type;
78 
80  bool _optional;
81 
84  bool _multiSlot;
85 
86 public:
88  Slot();
89 
95  void init(ParameteredObject* parent, std::string name, std::string type);
96 
103  void init(ParameteredObject* parent, std::string name, std::string displayname, std::string type);
104 
106 
107  virtual void prepare() = 0;
108 
110 
111  virtual void finalize() = 0;
112 
113  virtual ~Slot();
114 
116  ParameteredObject& getParent();
117 
119  const ParameteredObject& getParent() const;
120 
122  virtual std::string getName() const;
123 
125  virtual std::string getDisplayName() const;
126 
128  void printInfo(const std::string& msg) const;
129 
131  void printWarning(const std::string& msg) const;
132 
134  void printError(const std::string& msg) const;
135 
137  void raise(const std::string& msg) const;
138 
146  virtual std::string getType() const = 0;
147 
149  bool getMulti() const;
150 
152  bool getOptional() const;
153 
155  virtual std::string guessType() const = 0;
156 
158  virtual std::set<Slot*> getTargets() const = 0;
159 
161  bool connected() const;
162 
166  bool connected(Slot& target) const;
167 
169 
172  bool connect(Slot& target);
173 
175  bool disconnect();
176 
178  bool disconnect(Slot& target);
179 
188  virtual void save(ParameterFile& pf) const;
189 
191 
198  virtual void load(
199  const ParameterFile& pf,
200  const PluginManagerInterface* man) = 0;
201 
203 
204  enum CacheType {
207  CACHE_MANAGED
208  };
209 
211 
215  template<typename T>
216  class DataManager {
217  public:
218  virtual ~DataManager() {}
220  virtual void setData(const T& data ) = 0;
222  virtual T getData() = 0;
224  virtual std::string getConfig() const = 0;
225  };
226 
228 
232  template <typename T>
234  public:
236 
245  static DataManager<T>* getManager(
246  const Slot& slot, const std::string& config = "");
247  };
248 };
249 
252 template <typename T>
254 protected:
256  std::set<AbstractSlot<T>*> _targets;
257 
258 
264  virtual bool _addTarget(Slot* target);
265 
271  virtual bool _removeTarget(Slot* target);
272 
273 public:
274  AbstractSlot();
275  virtual ~AbstractSlot();
276 
277  // overload Slot functions
278  virtual std::string guessType() const;
279  virtual std::set<Slot*> getTargets() const;
280  virtual void save(ParameterFile& pf) const;
281  virtual std::string getType() const;
282 
284  // @{
286  typename std::set<AbstractSlot<T>*>::const_iterator begin() const;
288  typename std::set<AbstractSlot<T>*>::const_iterator end() const;
289  // @}
290 };
291 
294 {
295 public:
296 
297 
299  virtual std::string getName() const=0;
300 
302  virtual std::string getType() const =0;
303 
304 };
305 
307 
311 template <typename T>
313  public AbstractSlot<T>, public AbstractROData<T>,
314  public AbstractMultiROData<T>, public InputSlotIntf {
315 public:
319  InputSlot(bool optional=false, bool multi=false);
320 
321  virtual ~InputSlot();
322 
323  // overload Slot functions
324  virtual void load(
325  const ParameterFile& pf, const PluginManagerInterface* man);
326  virtual operator T() const;
327  virtual const T& operator()() const;
328  virtual const T& operator[](std::size_t pos) const;
329  virtual std::size_t size() const;
331 
341  virtual void prepare();
342  virtual void finalize();
343 
344  virtual std::string getName() const
345  {
346  return AbstractSlot<T>::getName();
347  }
348 
349  virtual std::string getType() const
350  {
351  return AbstractSlot<T>::getType();
352  }
353 
355 
358  const T& getDataFromOutputSlot(const OutputSlotIntf* slot) const;
359 
361 
364  const T& getDataFromOutputSlot(const Slot* slot) const;
365 
366 private:
368  std::map<std::string, T> _dataCache;
369 };
370 
371 
374 {
375 public:
377  virtual void setConfig(std::string conf)=0;
379  virtual const std::string& getConfig() const=0;
380 
382  virtual void setCacheType(Slot::CacheType type)=0;
383 
385  virtual std::string getName() const =0;
386 
388  virtual std::string getType() const =0;
389 
391  virtual Slot::CacheType getCacheType() const=0;
392 
393 
395  virtual const OutputSlotIntf* getDataSlot() const=0;
396 
397 };
398 
401 template <typename T>
403  public AbstractSlot<T>, public AbstractData<T>,public OutputSlotIntf {
404 
405 private:
407  const T _initVal;
408  T* data;
409  void _check() const;
412  void _prepare();
416  std::string _managerConfig;
417 
418 public:
420 
421  OutputSlot(const T& initval = T());
423  virtual ~OutputSlot();
424 
425  virtual void load(
426  const ParameterFile& pf, const PluginManagerInterface* man);
427 
432  return _cacheType;
433  }
435  virtual const std::string& getConfig() const {
436  return _managerConfig;
437  }
438 
439  // overload Slot functions
440  virtual operator T() const;
441  virtual const T& operator()() const;
442  virtual T& operator()();
443  virtual T& operator= (const T& B);
444  virtual void prepare();
445  virtual void finalize();
446 
447 
448 
451  {
452  return this;
453  }
454 
455  virtual std::string getName() const
456  {
457  return AbstractSlot<T>::getName();
458  }
459 
460  virtual std::string getType() const
461  {
462  return AbstractSlot<T>::getType();
463  }
465  virtual void setConfig(std::string conf)
466  {
467  _managerConfig=conf;
468  }
469 
470 
471 };
472 
473 
477 
479  :public Slot
480 {
481 public:
482  friend class VirtualInputSlot;
483  friend class VirtualOutputSlot;
484  VirtualSlot(std::string virtType,int num=0);
486  virtual void load(
487  const ParameterFile& pf, const PluginManagerInterface* man);
488  virtual void save(ParameterFile& pf) const;
489 
491  bool _addTarget(Slot *target);
492 
494  bool _removeTarget(Slot *target);
495 
496 
497 
498 
499  virtual void prepare();
500 
501  virtual void finalize();
502 
503  std::string guessType() const;
504 
505  std::string getName() const;
506 
507  std::string getType() const;
508 
510  virtual std::set<Slot*> getTargets() const;
511 
514 protected:
515  virtual bool isValidPartner(VirtualSlot* insl)=0;
516  virtual bool isValidTarget(Slot* target)=0;
517 
518  void setDisplayNameAndType(std::string name,std::string type);
519 
520  virtual void onLoad(const ParameterFile& pf, const PluginManagerInterface* man);
521 
522  virtual void onSave(ParameterFile& pf) const;
523  virtual bool onAddTarget(Slot* target);
524  virtual bool onRemoveTarget(Slot* target);
525 
526  VirtualSlot* _partner;
527  std::set<Slot*> _target;
528 
529 
530 
531 
532 
533 };
534 class VirtualInputSlot;
536  :public VirtualSlot,public OutputSlotIntf
537 {
538 public:
539  friend class VirtualInputSlot;
540  VirtualOutputSlot(int num=0);
543 
546 
548  std::string getType() const;
549 
551  std::string getName() const ;
552 
554  const std::string& getConfig() const;
555 
557  const OutputSlotIntf* getDataSlot() const;
558 
560  virtual void setConfig(std::string conf);
561 
562  void setLoopPartner(VirtualInputSlot* loopPartner);
563  void setLoop(bool loop);
564 protected:
565  virtual bool isValidPartner(VirtualSlot *insl);
566  virtual bool isValidTarget(Slot *target);
567  void onLoad(const ParameterFile &pf, const PluginManagerInterface *man);
568  void onSave(ParameterFile &pf) const;
569  virtual bool onAddTarget(Slot *target);
570  virtual bool onRemoveTarget(Slot *target);
571 
572 private:
573  std::string _managerconfig;
574  Slot::CacheType _cacheType;
575  VirtualInputSlot* _loopPartner;
576  bool _loop;
577 
578 
579 };
580 
581 
582 
584  :public VirtualSlot,public InputSlotIntf
585 {
586 public:
587  friend class VirtualOutputSlot;
588  VirtualInputSlot(int num=0);
589 
590 
591 
592 
593 
595  std::string getType() const;
596 
598  std::string getName() const ;
599 
600 protected:
601  virtual bool isValidPartner(VirtualSlot *insl);
602  virtual bool onAddTarget(Slot *target);
603  virtual bool onRemoveTarget(Slot *target);
604  virtual bool isValidTarget(Slot *target);
605 
606 
607 
608 
609 
610 
611 
612 
613 
614 };
615 
616 
617 
618 
619 
620 #endif /* _SLOTS_H */
Encapsulation of slot connection handling (type specific) and common code for input and output slots...
Definition: Slots.h:253
virtual std::string getType() const
Get slot type.
Definition: Slots.h:460
virtual const std::string & getConfig() const =0
get manager configuration string
std::string _managerConfig
config string
Definition: Slots.h:416
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.
Slot::CacheType _cacheType
output slot data cache type
Definition: Slots.h:414
virtual std::string guessType() const =0
Try to guess slot type.
virtual std::string getType() const =0
Get slot type.
Interface for a plugin manager.
Common interface for read-only access to multiple data members like arrays etc.
virtual Slot::CacheType getCacheType() const =0
query data cache type
virtual void setConfig(std::string conf)=0
set manager configuration string
This class serves to store parameters used within the Charon Project.
Definition: ParameterFile.h:68
virtual void setCacheType(Slot::CacheType type)=0
change data cache type
virtual std::string getType() const
Get slot type.
Definition: Slots.hxx:119
Output slot.
Definition: Slots.h:402
std::map< std::string, T > _dataCache
data cache for managed output slots
Definition: Slots.h:368
#define charon_core_PUBLIC
Preprocessor macro to define this class/function as public visible, i.e.
Definition: DllEx.h:81
void setVirtualPartnerSlot(VirtualSlot *insl)
Set corresponding partner VirtualSlot.
Definition: Slots.cpp:277
interface of data management classes
Definition: Slots.h:216
virtual void prepare()=0
prepare slot
Base class for serializable objects.
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.h:450
virtual std::size_t size() const =0
Access to number of members.
Declaration of class TypeDetector.
virtual bool _addTarget(Slot *target)=0
Add slot target.
dumped after execution, not recommended
Definition: Slots.h:205
virtual void load(const ParameterFile &pf, const PluginManagerInterface *man)=0
Load slot connections.
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
virtual T & operator=(const T &B)=0
data assignment operator
std::set< AbstractSlot< T > * > _targets
Pointer to data of connected output slot.
Definition: Slots.h:256
Interface for data assignment and readout.
virtual void setConfig(std::string conf)
set manager configuration string
Definition: Slots.h:465
Interface for data acces.
virtual std::string getName() const
get the name
Definition: Slots.h:455
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.
virtual std::string getType() const
Get slot type.
Definition: Slots.h:349
interface of data management factories
Definition: Slots.h:233
#define charon_core_DLL_PUBLIC
Preprocessor macro to define this class/function as public visible, i.e.
Definition: DllEx.h:82
virtual const T & operator[](std::size_t pos) const =0
Access to specific member (read-only).
abstract interface class for output slots
Definition: Slots.h:373
Dynamic library API import/export prprocessor macros.
bool _multiSlot
flag to mark this slot as a multislot, that can have multiple sources/targets.
Definition: Slots.h:84
T * data
Slot data.
Definition: Slots.h:408
CacheType
slot data cache type
Definition: Slots.h:204
virtual std::string getType() const =0
get the type
Abstract interface class for input slots.
Definition: Slots.h:293
Slot::CacheType getCacheType() const
query data cache type
Definition: Slots.h:431
virtual std::string getName() const
get the name
Definition: Slots.h:344
virtual std::string getName() const =0
get the name
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
Input slot.
Definition: Slots.h:312
virtual std::string getName() const =0
get the name
virtual T & operator()()=0
Call operator.
std::string _name
Slot name.
Definition: Slots.h:71
virtual void save(ParameterFile &pf) const
Save slot connections This function disconnects already established connections in the parameterFile ...
Definition: Slots.cpp:125
virtual void finalize()=0
finalize slot
virtual const std::string & getConfig() const
get manager configuration string
Definition: Slots.h:435
virtual const T & operator()() const =0
Call operator.
ParameteredObject * _parent
Pointer to parent object.
Definition: Slots.h:68
Declaration and implementation of the abstract class AbstractData.
virtual bool _removeTarget(Slot *target)=0
Remove slot target.
const T _initVal
initial value cache
Definition: Slots.h:407
virtual std::string getType() const =0
get the type