dynamic-graph  4.4.3
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>
21 Signal<T, Time> *SignalPtr<T, Time>::getPtr() {
22  dgTDEBUGIN(25);
23  if (!isPlugged()) 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>
114 void SignalPtr<T, Time>::checkCompatibility() {
115  if (isPlugged() && (!autoref())) {
116  getPtr()->checkCompatibility();
117  } else if (isAbstractPluged() && (!autoref())) {
118  abstractTransmitter->checkCompatibility();
119  } else
121 }
122 
123 template <class T, class Time>
124 bool SignalPtr<T, Time>::needUpdate(const Time &t) const {
125  if ((isAbstractPluged()) && (!autoref())) {
126  return getAbstractPtr()->needUpdate(t);
127  } else
128  return Signal<T, Time>::needUpdate(t);
129 }
130 
131 template <class T, class Time>
132 const Time &SignalPtr<T, Time>::getTime() const {
133  if ((isAbstractPluged()) && (!autoref())) {
134  return getAbstractPtr()->getTime();
135  }
136  return Signal<T, Time>::getTime();
137 }
138 
139 template <class T, class Time>
140 const T &SignalPtr<T, Time>::operator()(const Time &t) {
141  return access(t);
142 }
143 
144 template <class T, class Time>
145 const T &SignalPtr<T, Time>::access(const Time &t) {
146  dgTDEBUGIN(15);
147  if (modeNoThrow && (!isPlugged()) && Signal<T, Time>::copyInit) {
148  dgTDEBUGOUT(15);
149  return Signal<T, Time>::accessCopy();
150  } else if (autoref()) {
151  dgTDEBUGOUT(15);
152  return Signal<T, Time>::access(t);
153  } else if (transmitAbstract) {
154  abstractTransmitter->recompute(t);
155  dgTDEBUGOUT(15);
156  return *transmitAbstractData;
157  } else {
158  dgTDEBUGOUT(15);
159  return getPtr()->access(t);
160  }
161 }
162 
163 template <class T, class Time>
164 const T &SignalPtr<T, Time>::accessCopy() const {
165  if (modeNoThrow && (!isPlugged()) && Signal<T, Time>::copyInit)
166  return Signal<T, Time>::accessCopy();
167  else if (autoref())
168  return Signal<T, Time>::accessCopy();
169  else if (transmitAbstract)
170  return *transmitAbstractData;
171  else
172  return getPtr()->accessCopy();
173 }
174 template <class T, class Time>
175 std::ostream &SignalPtr<T, Time>::writeGraph(std::ostream &os) const {
176  std::string LeaderLocalName;
177  std::string LeaderNodeName;
178  Signal<T, Time>::ExtractNodeAndLocalNames(LeaderLocalName, LeaderNodeName);
179  if (isAbstractPluged() && !autoref()) {
180  std::string itLocalName, itNodeName;
181  getAbstractPtr()->ExtractNodeAndLocalNames(itLocalName, itNodeName);
182  os << "\t\"" << itNodeName << "\" -> \"" << LeaderNodeName << "\""
183  << std::endl
184  << "\t [ headlabel = \"" << LeaderLocalName << "\" , taillabel = \""
185  << itLocalName << "\", fontsize=7, fontcolor=red ]" << std::endl;
186  }
187  return os;
188 }
189 
190 template <class T, class Time>
191 std::ostream &SignalPtr<T, Time>::display(std::ostream &os) const {
192  dgTDEBUGIN(25) << SignalBase<Time>::name << this << "||" << isPlugged()
193  << "||" << signalPtr;
194  { Signal<T, Time>::display(os); }
195 
196  if ((isAbstractPluged()) && (!autoref())) {
197  os << " -->-- PLUGGED";
198  } else {
199  if (!isAbstractPluged())
200  os << " UNPLUGGED";
201  else if (autoref())
202  os << " AUTOPLUGGED";
203  }
204 
205  dgTDEBUGOUT(25);
206  return os;
207 }
208 
209 template <class T, class Time>
210 std::ostream &SignalPtr<T, Time>::displayDependencies(std::ostream &os,
211  const int depth,
212  std::string space,
213  std::string next1,
214  std::string next2) const {
215  dgTDEBUGIN(25);
216  if ((isAbstractPluged()) && (!autoref())) {
217  getAbstractPtr()->displayDependencies(
218  os, depth, space, next1 + "-- " + SignalBase<Time>::name + " -->",
219  next2);
220  } else {
221  SignalBase<Time>::displayDependencies(os, depth, space, next1, next2);
222  }
223  dgTDEBUGOUT(25);
224  return os;
225 }
226 
227 } // end of namespace dynamicgraph.
228 
229 #endif
dynamicgraph
Definition: command-bind.h:30
dynamicgraph::Signal::checkCompatibility
virtual void checkCompatibility()
Definition: signal.h:112