dynamic-graph  4.1.0-8-gdab7-dirty
Dynamic graph library
signal-ptr.t.cpp
1 // -*- mode: c++ -*-
2 // Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
3 // JRL, CNRS/AIST.
4 //
5 
6 #ifndef DYNAMIC_GRAPH_SIGNAL_PTR_T_CPP
7 #define DYNAMIC_GRAPH_SIGNAL_PTR_T_CPP
8 #include <dynamic-graph/signal-ptr.h>
9 
10 #undef VP_TEMPLATE_DEBUG_MODE
11 #define VP_TEMPLATE_DEBUG_MODE 0
12 #include <dynamic-graph/debug.h>
13 
14 namespace dynamicgraph {
15 template <class T, class Time>
16 bool SignalPtr<T, Time>::isAbstractPluged() const {
17  return ((NULL != signalPtr) || (abstractTransmitter));
18 }
19 
20 template <class T, class Time> Signal<T, Time> *SignalPtr<T, Time>::getPtr() {
21  dgTDEBUGIN(25);
22  if (!isPlugged())
23  DG_THROW
24  ExceptionSignal(ExceptionSignal::NOT_INITIALIZED,
25  "In SignalPtr: SIN ptr not set.", " (in signal <%s>)",
26  getName().c_str());
27  dgTDEBUGOUT(25);
28  return signalPtr;
29 }
30 
31 template <class T, class Time>
32 const Signal<T, Time> *SignalPtr<T, Time>::getPtr() const {
33  dgTDEBUGIN(25) << SignalBase<Time>::name << "(" << isPlugged() << ")" << this
34  << "->" << signalPtr << std::endl;
35  dgTDEBUGIN(25);
36  if (!isPlugged()) {
37  DG_THROW ExceptionSignal(ExceptionSignal::NOT_INITIALIZED,
38  "In SignalPtr: SIN ptr not set.",
39  " (in signal <%s>)", getName().c_str());
40  }
41  dgTDEBUGOUT(25);
42  return signalPtr;
43 }
44 
45 template <class T, class Time>
46 SignalBase<Time> *SignalPtr<T, Time>::getAbstractPtr() {
47  if (!isAbstractPluged()) {
48  DG_THROW ExceptionSignal(ExceptionSignal::NOT_INITIALIZED,
49  "In SignalPtr: SIN ptr not set.",
50  " (in signal <%s>)", getName().c_str());
51  }
52  if (NULL != signalPtr)
53  return signalPtr;
54  else
55  return abstractTransmitter;
56 }
57 
58 template <class T, class Time>
59 const SignalBase<Time> *SignalPtr<T, Time>::getAbstractPtr() const {
60  if (!isAbstractPluged()) {
61  DG_THROW ExceptionSignal(ExceptionSignal::NOT_INITIALIZED,
62  "In SignalPtr: SIN ptr not set.",
63  " (in signal <%s>)", getName().c_str());
64  }
65  if (NULL != signalPtr)
66  return signalPtr;
67  else
68  return abstractTransmitter;
69 }
70 
71 template <class T, class Time>
72 void SignalPtr<T, Time>::plug(SignalBase<Time> *unknown_ref) {
73  dgTDEBUGIN(5);
74  if (!unknown_ref) {
75  signalPtr = NULL;
76  transmitAbstract = false;
77  dgTDEBUGOUT(5);
78  return;
79  }
80 
81  dgTDEBUG(5) << "# In T = " << getName() << " ="
82  << typeid(Signal<T, Time>::Tcopy1).name() << "{ " << std::endl;
83 
84  Signal<T, Time> *ref = dynamic_cast<Signal<T, Time> *>(unknown_ref);
85  if (NULL == ref) {
86  try {
87  unknown_ref->checkCompatibility();
88  } catch (T *t) {
89  dgTDEBUG(25) << "Cast THROW ok." << std::endl;
90  Signal<T, Time>::setReference(t);
91  transmitAbstract = true;
92  abstractTransmitter = unknown_ref;
93  transmitAbstractData = t;
94  } catch (...) {
95  dgTDEBUG(25) << "Fatal error." << std::endl;
96  transmitAbstract = false;
97  DG_THROW ExceptionSignal(ExceptionSignal::PLUG_IMPOSSIBLE,
98  "Compl. Uncompatible types for plugin.",
99  "(while trying to plug <%s> on <%s>)"
100  " with types <%s> on <%s>.",
101  unknown_ref->getName().c_str(),
102  this->getName().c_str(), typeid(T).name(),
103  typeid(unknown_ref).name());
104  }
105  } else {
106  dgTDEBUG(25) << "Cast ok." << std::endl;
107  transmitAbstract = false;
108  signalPtr = ref;
109  }
110  dgTDEBUGOUT(5);
111 }
112 
113 template <class T, class Time> void SignalPtr<T, Time>::checkCompatibility() {
114  if (isPlugged() && (!autoref())) {
115  getPtr()->checkCompatibility();
116  } else if (isAbstractPluged() && (!autoref())) {
117  abstractTransmitter->checkCompatibility();
118  } else
120 }
121 
122 template <class T, class Time>
123 bool SignalPtr<T, Time>::needUpdate(const Time &t) const {
124  if ((isAbstractPluged()) && (!autoref())) {
125  return getAbstractPtr()->needUpdate(t);
126  } else
127  return Signal<T, Time>::needUpdate(t);
128 }
129 
130 template <class T, class Time> const Time &SignalPtr<T, Time>::getTime() const {
131  if ((isAbstractPluged()) && (!autoref())) {
132  return getAbstractPtr()->getTime();
133  }
134  return Signal<T, Time>::getTime();
135 }
136 
137 template <class T, class Time>
138 const T &SignalPtr<T, Time>::operator()(const Time &t) {
139  return access(t);
140 }
141 
142 template <class T, class Time>
143 const T &SignalPtr<T, Time>::access(const Time &t) {
144  dgTDEBUGIN(15);
145  if (modeNoThrow && (!isPlugged()) && Signal<T, Time>::copyInit) {
146  dgTDEBUGOUT(15);
148  } else if (autoref()) {
149  dgTDEBUGOUT(15);
150  return Signal<T, Time>::access(t);
151  } else if (transmitAbstract) {
152  abstractTransmitter->recompute(t);
153  dgTDEBUGOUT(15);
154  return *transmitAbstractData;
155  } else {
156  dgTDEBUGOUT(15);
157  return getPtr()->access(t);
158  }
159 }
160 
161 template <class T, class Time> const T &SignalPtr<T, Time>::accessCopy() const {
162  if (modeNoThrow && (!isPlugged()) && Signal<T, Time>::copyInit)
164  else if (autoref())
166  else if (transmitAbstract)
167  return *transmitAbstractData;
168  else
169  return getPtr()->accessCopy();
170 }
171 template <class T, class Time>
172 std::ostream &SignalPtr<T, Time>::writeGraph(std::ostream &os) const {
173  std::string LeaderLocalName;
174  std::string LeaderNodeName;
175  Signal<T, Time>::ExtractNodeAndLocalNames(LeaderLocalName, LeaderNodeName);
176  if (isAbstractPluged() && !autoref()) {
177  std::string itLocalName, itNodeName;
178  getAbstractPtr()->ExtractNodeAndLocalNames(itLocalName, itNodeName);
179  os << "\t\"" << itNodeName << "\" -> \"" << LeaderNodeName << "\""
180  << std::endl
181  << "\t [ headlabel = \"" << LeaderLocalName << "\" , taillabel = \""
182  << itLocalName << "\", fontsize=7, fontcolor=red ]" << std::endl;
183  }
184  return os;
185 }
186 
187 template <class T, class Time>
188 std::ostream &SignalPtr<T, Time>::display(std::ostream &os) const {
189  dgTDEBUGIN(25) << SignalBase<Time>::name << this << "||" << isPlugged()
190  << "||" << signalPtr;
191  { Signal<T, Time>::display(os); }
192 
193  if ((isAbstractPluged()) && (!autoref())) {
194  os << " -->-- PLUGGED";
195  } else {
196  if (!isAbstractPluged())
197  os << " UNPLUGGED";
198  else if (autoref())
199  os << " AUTOPLUGGED";
200  }
201 
202  dgTDEBUGOUT(25);
203  return os;
204 }
205 
206 template <class T, class Time>
207 std::ostream &
208 SignalPtr<T, Time>::displayDependencies(std::ostream &os, const int depth,
209  std::string space, std::string next1,
210  std::string next2) const {
211  dgTDEBUGIN(25);
212  if ((isAbstractPluged()) && (!autoref())) {
213  getAbstractPtr()->displayDependencies(
214  os, depth, space, next1 + "-- " + SignalBase<Time>::name + " -->",
215  next2);
216  } else {
217  SignalBase<Time>::displayDependencies(os, depth, space, next1, next2);
218  }
219  dgTDEBUGOUT(25);
220  return os;
221 }
222 
223 } // end of namespace dynamicgraph.
224 
225 #endif
virtual void checkCompatibility()
This is the only type of signal that can be plugged to, using the plug () command.
Definition: fwd.hh:42
Signals link I/O ports of entities. They can be constant-valued signals, or copy the value of a heap ...
Definition: fwd.hh:34
The base class for signals: not to be used as such.
Definition: fwd.hh:40
virtual void checkCompatibility()
Definition: signal.h:112