dynamic-graph  4.1.0-8-gdab7-dirty
Dynamic graph library
signal-base.h
1 // -*- mode: c++ -*-
2 // Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
3 // JRL, CNRS/AIST.
4 // LAAS, CNRS
5 //
6 
7 #ifndef DYNAMIC_GRAPH_SIGNAL_BASE_H
8 #define DYNAMIC_GRAPH_SIGNAL_BASE_H
9 #include <boost/noncopyable.hpp>
10 #include <sstream>
11 #include <string>
12 #include <typeinfo>
13 
14 #include <dynamic-graph/exception-signal.h>
15 #include <dynamic-graph/fwd.hh>
16 
17 namespace dynamicgraph {
18 
26 template <class Time> class SignalBase : public boost::noncopyable {
27 public:
28  explicit SignalBase(std::string name = "")
29  : name(name), signalTime(0), ready(false) {}
30 
31  virtual ~SignalBase() {}
32 
35  virtual const Time &getTime() const { return signalTime; }
36 
37  virtual void setTime(const Time &t) { signalTime = t; }
38 
39  const bool &getReady() const { return ready; }
40 
41  const std::string &getName() const { return name; }
42 
43  void getClassName(std::string &aClassName) const {
44  aClassName = typeid(this).name();
45  }
46 
47  virtual void setPeriodTime(const Time &) {}
48 
49  virtual Time getPeriodTime() const { return 1; }
50 
52 
55 
56  virtual void addDependency(const SignalBase<Time> &) {}
57 
58  virtual void removeDependency(const SignalBase<Time> &) {}
59 
60  virtual void clearDependencies() {}
61 
62  virtual bool needUpdate(const Time &) const { return ready; }
63 
64  inline void setReady(const bool sready = true) { ready = sready; }
65 
66  virtual std::ostream &writeGraph(std::ostream &os) const { return os; }
67 
68  virtual std::ostream &displayDependencies(std::ostream &os, const int = -1,
69  std::string space = "",
70  std::string next1 = "",
71  std::string = "") const {
72  os << space << next1 << "-- ";
73  display(os);
74  return os;
75  }
76 
78 
81 
82  /* Plug the arg-signal on the <this> object. Plug-in is always
83  * a descending operation (the actual <this> object will call the arg-signal
84  * and not the opposite).
85  */
86  virtual void plug(SignalBase<Time> *sigarg) {
87  DG_THROW ExceptionSignal(
88  ExceptionSignal::PLUG_IMPOSSIBLE,
89  "Plug-in operation not possible with this signal. ",
90  "(while trying to plug %s on %s).", sigarg->getName().c_str(),
91  this->getName().c_str());
92  }
93 
94  virtual void unplug() {
95  DG_THROW ExceptionSignal(
96  ExceptionSignal::PLUG_IMPOSSIBLE,
97  "Plug-in operation not possible with this signal. ",
98  "(while trying to unplug %s).", this->getName().c_str());
99  }
100 
101  virtual bool isPlugged() const { return false; }
102 
103  virtual SignalBase<Time> *getPluged() const { return NULL; }
104 
105  virtual void setConstantDefault() {
106  DG_THROW ExceptionSignal(
107  ExceptionSignal::PLUG_IMPOSSIBLE,
108  "Plug-in operation not possible with this signal. ",
109  "(while trying to save %s).", this->getName().c_str());
110  }
111 
113 
116 
117  /* Generic set function. Should be reimplemented by the specific
118  * Signal. Sets a signal value
119  */
120  virtual void set(std::istringstream &) {
121  DG_THROW ExceptionSignal(ExceptionSignal::SET_IMPOSSIBLE,
122  "Set operation not possible with this signal. ",
123  "(while trying to set %s).",
124  this->getName().c_str());
125  }
126 
127  virtual void get(std::ostream &) const {
128  DG_THROW ExceptionSignal(ExceptionSignal::SET_IMPOSSIBLE,
129  "Get operation not possible with this signal. ",
130  "(while trying to get %s).",
131  this->getName().c_str());
132  }
133 
134  virtual inline void recompute(const Time &) {
135  DG_THROW ExceptionSignal(
136  ExceptionSignal::SET_IMPOSSIBLE,
137  "Recompute operation not possible with this signal. ",
138  "(while trying to recompute %s).", this->getName().c_str());
139  }
140 
141  virtual void trace(std::ostream &) const {
142  DG_THROW ExceptionSignal(ExceptionSignal::SET_IMPOSSIBLE,
143  "Trace operation not possible with this signal. ",
144  "(while trying to trace %s).",
145  this->getName().c_str());
146  }
147 
149 
152 
153  virtual std::ostream &display(std::ostream &os) const {
154  os << "Sig:" << name;
155  return os;
156  }
157 
158  std::string shortName() const {
159  std::istringstream iss(name);
160  const int SIZE = 128;
161  char buffer[SIZE];
162  while (iss.good()) {
163  iss.getline(buffer, SIZE, ':');
164  }
165  const std::string res(buffer);
166  return res;
167  }
169 
172 
173  virtual void ExtractNodeAndLocalNames(std::string &LocalName,
174  std::string &NodeName) const {
175  std::string fullname = this->getName();
176 
177  size_t IdxPosLocalName = fullname.rfind(":");
178  LocalName = fullname.substr(IdxPosLocalName + 1,
179  fullname.length() - IdxPosLocalName + 1);
180  size_t IdxPosNodeNameStart = fullname.find("(");
181  size_t IdxPosNodeNameEnd = fullname.find(")");
182  NodeName = fullname.substr(IdxPosNodeNameStart + 1,
183  IdxPosNodeNameEnd - IdxPosNodeNameStart - 1);
184  }
185 
187 
190  virtual void checkCompatibility() {
191  DG_THROW ExceptionSignal(ExceptionSignal::PLUG_IMPOSSIBLE,
192  "Abstract signal not compatible with anything.",
193  "(while trying to plug <%s>).",
194  this->getName().c_str());
195  }
197 
198 protected:
199  std::string name;
200  Time signalTime;
201  bool ready;
202 };
203 
205 template <class Time>
206 std::ostream &operator<<(std::ostream &os, const SignalBase<Time> &sig) {
207  return sig.display(os);
208 }
209 } // end of namespace dynamicgraph.
210 
211 #endif