6 #ifndef DYNAMIC_GRAPH_SIGNAL_T_CPP 7 #define DYNAMIC_GRAPH_SIGNAL_T_CPP 8 #include <dynamic-graph/signal-caster.h> 9 #include <dynamic-graph/signal.h> 11 #undef VP_TEMPLATE_DEBUG_MODE 12 #define VP_TEMPLATE_DEBUG_MODE 0 13 #include <dynamic-graph/debug.h> 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) 23 template <
class T,
class Time>
24 Signal<T, Time>::Signal(std::string name)
25 : __SIGNAL_INIT(name, T(), NULL, NULL, NULL) {
31 template <
class T,
class Time>
32 void Signal<T, Time>::set(std::istringstream &stringValue) {
33 (*this) = signal_cast<T>(stringValue);
36 template <
class T,
class Time>
37 void Signal<T, Time>::get(std::ostream &os)
const {
38 signal_disp<T>(this->accessCopy(), os);
41 template <
class T,
class Time>
42 void Signal<T, Time>::trace(std::ostream &os)
const {
44 signal_trace<T>(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());
55 template <
class T,
class Time>
const T &Signal<T, Time>::setTcopy(
const T &t) {
56 if (Tcopy == &Tcopy1) {
69 template <
class T,
class Time> T &Signal<T, Time>::getTwork() {
76 template <
class T,
class Time>
const T &Signal<T, Time>::getTwork()
const {
83 template <
class T,
class Time>
const T &Signal<T, Time>::switchTcopy() {
84 if (Tcopy == &Tcopy1) {
93 template <
class T,
class Time>
void Signal<T, Time>::setConstant(
const T &t) {
94 signalType = CONSTANT;
99 template <
class T,
class Time>
100 void Signal<T, Time>::setReference(
const T *t, Mutex *mutexref) {
101 signalType = REFERENCE;
103 providerMutex = mutexref;
108 template <
class T,
class Time>
109 void Signal<T, Time>::setReferenceNonConstant(T *t, Mutex *mutexref) {
110 signalType = REFERENCE_NON_CONST;
112 TreferenceNonConst = t;
113 providerMutex = mutexref;
118 template <
class T,
class Time>
119 void Signal<T, Time>::setFunction(boost::function2<T &, T &, Time> t,
121 signalType = FUNCTION;
123 providerMutex = mutexref;
128 template <
class T,
class Time>
const T &Signal<T, Time>::accessCopy()
const {
132 template <
class T,
class Time>
const T &Signal<T, Time>::access(
const Time &t) {
133 switch (signalType) {
135 case REFERENCE_NON_CONST: {
136 if (NULL == providerMutex) {
139 return setTcopy(*Treference);
142 #ifdef HAVE_LIBBOOST_THREAD 143 boost::try_mutex::scoped_try_lock lock(*providerMutex);
147 return setTcopy(*Treference);
148 }
catch (
const MutexError &) {
157 if (NULL == providerMutex) {
159 Tfunction(getTwork(), t);
161 return switchTcopy();
164 #ifdef HAVE_LIBBOOST_THREAD 165 boost::try_mutex::scoped_try_lock lock(*providerMutex);
168 Tfunction(getTwork(), t);
170 return switchTcopy();
171 }
catch (
const MutexError &) {
179 if (this->getReady()) {
187 template <
class T,
class Time>
188 Signal<T, Time> &Signal<T, Time>::operator=(
const T &t) {
189 if (keepReference && (REFERENCE_NON_CONST == signalType) &&
190 (NULL != TreferenceNonConst)) {
191 if (NULL == providerMutex) {
193 (*TreferenceNonConst) = t;
196 #ifdef HAVE_LIBBOOST_THREAD 197 boost::try_mutex::scoped_try_lock lock(*providerMutex);
200 (*TreferenceNonConst) = t;
201 }
catch (
const MutexError &) {
210 template <
class T,
class Time>
211 std::ostream &Signal<T, Time>::display(std::ostream &os)
const {
212 os <<
"Sig:" << this->name <<
" (Type ";
213 switch (this->signalType) {
214 case Signal<T, Time>::CONSTANT:
217 case Signal<T, Time>::REFERENCE:
220 case Signal<T, Time>::REFERENCE_NON_CONST:
223 case Signal<T, Time>::FUNCTION: