signal.t.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 // Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
3 // JRL, CNRS/AIST.
4 //
5 
6 #ifndef DYNAMIC_GRAPH_SIGNAL_T_CPP
7 #define DYNAMIC_GRAPH_SIGNAL_T_CPP
9 #include <dynamic-graph/signal.h>
10 
11 #undef VP_TEMPLATE_DEBUG_MODE
12 #define VP_TEMPLATE_DEBUG_MODE 0
13 #include <dynamic-graph/debug.h>
14 
15 #define __SIGNAL_INIT(name, Tcpy, Tref, TrefNC, mutex) \
16  SignalBase<Time>(name), signalType(SIGNAL_TYPE_DEFAULT), Tcopy1(Tcpy), \
17  Tcopy2(Tcpy), Tcopy(&Tcopy1), Treference(Tref), \
18  TreferenceNonConst(TrefNC), Tfunction(), \
19  keepReference(KEEP_REFERENCE_DEFAULT), providerMutex(mutex)
20 
21 namespace dynamicgraph {
22 
23 template <class T, class Time>
25  : __SIGNAL_INIT(name, T(), NULL, NULL, NULL) {
26  return;
27 }
28 
29 /* ------------------------------------------------------------------------ */
30 
31 template <class T, class Time>
32 void Signal<T, Time>::set(std::istringstream &stringValue) {
33  (*this) = signal_io<T>::cast(stringValue);
34 }
35 
36 template <class T, class Time>
37 void Signal<T, Time>::get(std::ostream &os) const {
38  signal_io<T>::disp(this->accessCopy(), os);
39 }
40 
41 template <class T, class Time>
42 void Signal<T, Time>::trace(std::ostream &os) const {
43  try {
44  signal_io<T>::trace(this->accessCopy(), os);
45  } catch DG_RETHROW catch (...) {
46  DG_THROW ExceptionSignal(ExceptionSignal::SET_IMPOSSIBLE,
47  "TRACE operation not possible with this signal. ",
48  "(bad cast while getting value from %s).",
49  SignalBase<Time>::getName().c_str());
50  }
51 }
52 
53 /* ------------------------------------------------------------------------ */
54 
55 template <class T, class Time>
56 const T &Signal<T, Time>::setTcopy(const T &t) {
57  if (Tcopy == &Tcopy1) {
58  Tcopy2 = t;
59  copyInit = true;
60  Tcopy = &Tcopy2;
61  return Tcopy2;
62  } else {
63  Tcopy1 = t;
64  copyInit = true;
65  Tcopy = &Tcopy1;
66  return Tcopy1;
67  }
68 }
69 
70 template <class T, class Time>
72  if (Tcopy == &Tcopy1)
73  return Tcopy2;
74  else
75  return Tcopy1;
76 }
77 
78 template <class T, class Time>
79 const T &Signal<T, Time>::getTwork() const {
80  if (Tcopy == &Tcopy1)
81  return Tcopy2;
82  else
83  return Tcopy1;
84 }
85 
86 template <class T, class Time>
88  if (Tcopy == &Tcopy1) {
89  Tcopy = &Tcopy2;
90  return Tcopy2;
91  } else {
92  Tcopy = &Tcopy1;
93  return Tcopy1;
94  }
95 }
96 
97 template <class T, class Time>
98 void Signal<T, Time>::setConstant(const T &t) {
99  signalType = CONSTANT;
100  setTcopy(t);
101  setReady();
102 }
103 
104 template <class T, class Time>
105 void Signal<T, Time>::setReference(const T *t, Mutex *mutexref) {
106  signalType = REFERENCE;
107  Treference = t;
108  providerMutex = mutexref;
109  copyInit = false;
110  setReady();
111 }
112 
113 template <class T, class Time>
115  signalType = REFERENCE_NON_CONST;
116  Treference = t;
117  TreferenceNonConst = t;
118  providerMutex = mutexref;
119  copyInit = false;
120  setReady();
121 }
122 
123 template <class T, class Time>
124 void Signal<T, Time>::setFunction(boost::function2<T &, T &, Time> t,
125  Mutex *mutexref) {
126  signalType = FUNCTION;
127  Tfunction = t;
128  providerMutex = mutexref;
129  copyInit = false;
130  setReady();
131 }
132 
133 template <class T, class Time>
134 const T &Signal<T, Time>::accessCopy() const {
135  return *Tcopy;
136 }
137 
138 template <class T, class Time>
139 const T &Signal<T, Time>::access(const Time &t) {
140  switch (signalType) {
141  case REFERENCE:
142  case REFERENCE_NON_CONST: {
143  if (NULL == providerMutex) {
144  copyInit = true;
145  signalTime = t;
146  return setTcopy(*Treference);
147  } else {
148  try {
149 #ifdef HAVE_LIBBOOST_THREAD
150  boost::try_mutex::scoped_try_lock lock(*providerMutex);
151 #endif
152  copyInit = true;
153  signalTime = t;
154  return setTcopy(*Treference);
155  } catch (const MutexError &) {
156  return accessCopy();
157  }
158  }
159 
160  break;
161  }
162 
163  case FUNCTION: {
164  if (NULL == providerMutex) {
165  signalTime = t;
166  Tfunction(getTwork(), t);
167  copyInit = true;
168  return switchTcopy();
169  } else {
170  try {
171 #ifdef HAVE_LIBBOOST_THREAD
172  boost::try_mutex::scoped_try_lock lock(*providerMutex);
173 #endif
174  signalTime = t;
175  Tfunction(getTwork(), t);
176  copyInit = true;
177  return switchTcopy();
178  } catch (const MutexError &) {
179  return accessCopy();
180  }
181  }
182  break;
183  }
184  case CONSTANT:
185  default:
186  if (this->getReady()) {
187  setReady(false);
188  this->setTime(t);
189  }
190  return accessCopy();
191  };
192 }
193 
194 template <class T, class Time>
196  if (keepReference && (REFERENCE_NON_CONST == signalType) &&
197  (NULL != TreferenceNonConst)) {
198  if (NULL == providerMutex) {
199  setTcopy(t);
200  (*TreferenceNonConst) = t;
201  } else {
202  try {
203 #ifdef HAVE_LIBBOOST_THREAD
204  boost::try_mutex::scoped_try_lock lock(*providerMutex);
205 #endif
206  setTcopy(t);
207  (*TreferenceNonConst) = t;
208  } catch (const MutexError &) { /* TODO ERROR */
209  }
210  }
211  } else {
212  setConstant(t);
213  }
214  return *this;
215 }
216 
217 template <class T, class Time>
218 std::ostream &Signal<T, Time>::display(std::ostream &os) const {
219  os << "Sig:" << this->name << " (Type ";
220  switch (this->signalType) {
222  os << "Cst";
223  break;
225  os << "Ref";
226  break;
228  os << "RefNonCst";
229  break;
231  os << "Fun";
232  break;
233  }
234  return os << ")";
235 }
236 
237 } // end of namespace dynamicgraph.
238 
239 #undef __SIGNAL_INIT
240 #endif
Class used for I/O operations in Signal<T,Time>
Definition: signal-caster.h:56
Signals link I/O ports of entities. They can be constant-valued signals, or copy the value of a heap ...
Definition: fwd.hh:42
Exceptions raised when an error related to signals happen.
The base class for signals: not to be used as such.
Definition: fwd.hh:51
#define DG_RETHROW
#define __SIGNAL_INIT(name, Tcpy, Tref, TrefNC, mutex)
Definition: signal.t.cpp:15
#define DG_THROW
Signal(std::string name)
Definition: signal.t.cpp:24


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:06:03