GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/dynamic-graph/signal-ptr.t.cpp Lines: 80 101 79.2 %
Date: 2023-03-15 12:04:10 Branches: 71 162 43.8 %

Line Branch Exec Source
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
45
bool SignalPtr<T, Time>::isAbstractPluged() const {
17

45
  return ((NULL != signalPtr) || (abstractTransmitter));
18
}
19
20
template <class T, class Time>
21
16
Signal<T, Time> *SignalPtr<T, Time>::getPtr() {
22
  dgTDEBUGIN(25);
23
16
  if (!isPlugged()) DG_THROW
24
  ExceptionSignal(ExceptionSignal::NOT_INITIALIZED,
25
                  "In SignalPtr: SIN ptr not set.", " (in signal <%s>)",
26

4
                  getName().c_str());
27
  dgTDEBUGOUT(25);
28
12
  return signalPtr;
29
}
30
31
template <class T, class Time>
32
10
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
10
  if (!isPlugged()) {
37
    DG_THROW ExceptionSignal(ExceptionSignal::NOT_INITIALIZED,
38
                             "In SignalPtr: SIN ptr not set.",
39

2
                             " (in signal <%s>)", getName().c_str());
40
  }
41
  dgTDEBUGOUT(25);
42
8
  return signalPtr;
43
}
44
45
template <class T, class Time>
46
2
SignalBase<Time> *SignalPtr<T, Time>::getAbstractPtr() {
47
2
  if (!isAbstractPluged()) {
48
    DG_THROW ExceptionSignal(ExceptionSignal::NOT_INITIALIZED,
49
                             "In SignalPtr: SIN ptr not set.",
50

1
                             " (in signal <%s>)", getName().c_str());
51
  }
52
1
  if (NULL != signalPtr)
53
1
    return signalPtr;
54
  else
55
    return abstractTransmitter;
56
}
57
58
template <class T, class Time>
59
6
const SignalBase<Time> *SignalPtr<T, Time>::getAbstractPtr() const {
60
6
  if (!isAbstractPluged()) {
61
    DG_THROW ExceptionSignal(ExceptionSignal::NOT_INITIALIZED,
62
                             "In SignalPtr: SIN ptr not set.",
63

2
                             " (in signal <%s>)", getName().c_str());
64
  }
65
4
  if (NULL != signalPtr)
66
4
    return signalPtr;
67
  else
68
    return abstractTransmitter;
69
}
70
71
template <class T, class Time>
72
26
void SignalPtr<T, Time>::plug(SignalBase<Time> *unknown_ref) {
73
  dgTDEBUGIN(5);
74
26
  if (!unknown_ref) {
75
6
    signalPtr = NULL;
76
6
    transmitAbstract = false;
77
    dgTDEBUGOUT(5);
78
6
    return;
79
  }
80
81
  dgTDEBUG(5) << "# In  T = " << getName() << " ="
82
              << typeid(Signal<T, Time>::Tcopy1).name() << "{ " << std::endl;
83
84
20
  Signal<T, Time> *ref = dynamic_cast<Signal<T, Time> *>(unknown_ref);
85
20
  if (NULL == ref) {
86
    try {
87
2
      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
4
    } catch (...) {
95
      dgTDEBUG(25) << "Fatal error." << std::endl;
96
2
      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
2
                               unknown_ref->getName().c_str(),
102
2
                               this->getName().c_str(), typeid(T).name(),
103

6
                               typeid(unknown_ref).name());
104
    }
105
  } else {
106
    dgTDEBUG(25) << "Cast ok." << std::endl;
107
18
    transmitAbstract = false;
108
18
    signalPtr = ref;
109
  }
110
  dgTDEBUGOUT(5);
111
}
112
113
template <class T, class Time>
114
4
void SignalPtr<T, Time>::checkCompatibility() {
115

4
  if (isPlugged() && (!autoref())) {
116
2
    getPtr()->checkCompatibility();
117

2
  } else if (isAbstractPluged() && (!autoref())) {
118
    abstractTransmitter->checkCompatibility();
119
  } else
120
2
    Signal<T, Time>::checkCompatibility();
121
}
122
123
template <class T, class Time>
124
4
bool SignalPtr<T, Time>::needUpdate(const Time &t) const {
125

4
  if ((isAbstractPluged()) && (!autoref())) {
126
2
    return getAbstractPtr()->needUpdate(t);
127
  } else
128
2
    return Signal<T, Time>::needUpdate(t);
129
}
130
131
template <class T, class Time>
132
4
const Time &SignalPtr<T, Time>::getTime() const {
133

4
  if ((isAbstractPluged()) && (!autoref())) {
134
2
    return getAbstractPtr()->getTime();
135
  }
136
2
  return Signal<T, Time>::getTime();
137
}
138
139
template <class T, class Time>
140
2
const T &SignalPtr<T, Time>::operator()(const Time &t) {
141
2
  return access(t);
142
}
143
144
template <class T, class Time>
145
12
const T &SignalPtr<T, Time>::access(const Time &t) {
146
  dgTDEBUGIN(15);
147


12
  if (modeNoThrow && (!isPlugged()) && Signal<T, Time>::copyInit) {
148
    dgTDEBUGOUT(15);
149
    return Signal<T, Time>::accessCopy();
150
12
  } else if (autoref()) {
151
    dgTDEBUGOUT(15);
152
6
    return Signal<T, Time>::access(t);
153
6
  } else if (transmitAbstract) {
154
    abstractTransmitter->recompute(t);
155
    dgTDEBUGOUT(15);
156
    return *transmitAbstractData;
157
  } else {
158
    dgTDEBUGOUT(15);
159
6
    return getPtr()->access(t);
160
  }
161
}
162
163
template <class T, class Time>
164
12
const T &SignalPtr<T, Time>::accessCopy() const {
165


12
  if (modeNoThrow && (!isPlugged()) && Signal<T, Time>::copyInit)
166
    return Signal<T, Time>::accessCopy();
167
12
  else if (autoref())
168
4
    return Signal<T, Time>::accessCopy();
169
8
  else if (transmitAbstract)
170
    return *transmitAbstractData;
171
  else
172
8
    return getPtr()->accessCopy();
173
}
174
template <class T, class Time>
175
1
std::ostream &SignalPtr<T, Time>::writeGraph(std::ostream &os) const {
176
2
  std::string LeaderLocalName;
177
1
  std::string LeaderNodeName;
178
1
  Signal<T, Time>::ExtractNodeAndLocalNames(LeaderLocalName, LeaderNodeName);
179


1
  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
2
  return os;
188
}
189
190
template <class T, class Time>
191
11
std::ostream &SignalPtr<T, Time>::display(std::ostream &os) const {
192
  dgTDEBUGIN(25) << SignalBase<Time>::name << this << "||" << isPlugged()
193
                 << "||" << signalPtr;
194
11
  { Signal<T, Time>::display(os); }
195
196

11
  if ((isAbstractPluged()) && (!autoref())) {
197
    os << " -->-- PLUGGED";
198
  } else {
199
11
    if (!isAbstractPluged())
200
5
      os << " UNPLUGGED";
201
6
    else if (autoref())
202
6
      os << " AUTOPLUGGED";
203
  }
204
205
  dgTDEBUGOUT(25);
206
11
  return os;
207
}
208
209
template <class T, class Time>
210
1
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

1
  if ((isAbstractPluged()) && (!autoref())) {
217
    getAbstractPtr()->displayDependencies(
218
        os, depth, space, next1 + "-- " + SignalBase<Time>::name + " -->",
219
        next2);
220
  } else {
221

1
    SignalBase<Time>::displayDependencies(os, depth, space, next1, next2);
222
  }
223
  dgTDEBUGOUT(25);
224
1
  return os;
225
}
226
227
}  // end of namespace dynamicgraph.
228
229
#endif  //! DYNAMIC_GRAPH_SIGNAL_PTR_T_CPP