GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/dynamic-graph/signal-base.h Lines: 60 60 100.0 %
Date: 2023-03-15 12:04:10 Branches: 26 50 52.0 %

Line Branch Exec Source
1
// -*- mode: c++ -*-
2
// Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
3
// JRL, CNRS/AIST.
4
// LAAS, CNRS
5
//
6
7
#ifndef DYNAMIC_GRAPH_SIGNAL_BASE_H
8
#define DYNAMIC_GRAPH_SIGNAL_BASE_H
9
#include <dynamic-graph/exception-signal.h>
10
11
#include <boost/noncopyable.hpp>
12
#include <dynamic-graph/fwd.hh>
13
#include <sstream>
14
#include <string>
15
#include <typeinfo>
16
17
namespace dynamicgraph {
18
19
/** \brief The base class for signals: not to be used as such.
20
21
    Signal values can be accessed programmatically using the access
22
    () or accessCopy () methods; the former directly accesses the
23
    value of the signal, which can involve an extra computation,
24
    while the latter accesses a cached value, or 'copy'.
25
*/
26
template <class Time>
27
class SignalBase : public boost::noncopyable {
28
 public:
29
46
  explicit SignalBase(std::string name = "")
30
46
      : name(name), signalTime(0), ready(false) {}
31
32
64
  virtual ~SignalBase() {}
33
34
  /// \name Time
35
  /// \{
36
16072
  virtual const Time &getTime() const { return signalTime; }
37
38
4005
  virtual void setTime(const Time &t) { signalTime = t; }
39
40
7064
  const bool &getReady() const { return ready; }
41
42
33
  const std::string &getName() const { return name; }
43
44
  void getClassName(std::string &aClassName) const {
45
    aClassName = typeid(this).name();
46
  }
47
48
1
  virtual void setPeriodTime(const Time &) {}
49
50
1
  virtual Time getPeriodTime() const { return 1; }
51
52
  /// \}
53
54
  /// \name Dependencies
55
  /// \{
56
57
1
  virtual void addDependency(const SignalBase<Time> &) {}
58
59
1
  virtual void removeDependency(const SignalBase<Time> &) {}
60
61
1
  virtual void clearDependencies() {}
62
63
3
  virtual bool needUpdate(const Time &) const { return ready; }
64
65
3067
  inline void setReady(const bool sready = true) { ready = sready; }
66
67
1
  virtual std::ostream &writeGraph(std::ostream &os) const { return os; }
68
69
81
  virtual std::ostream &displayDependencies(std::ostream &os, const int = -1,
70
                                            std::string space = "",
71
                                            std::string next1 = "",
72
                                            std::string = "") const {
73
81
    os << space << next1 << "-- ";
74
81
    display(os);
75
81
    return os;
76
  }
77
78
  /// \}
79
80
  /// \name Plug
81
  /// \{
82
83
  /* Plug the arg-signal on the <this> object. Plug-in is always
84
   * a descending operation (the actual <this> object will call the arg-signal
85
   * and not the opposite).
86
   */
87
1
  virtual void plug(SignalBase<Time> *sigarg) {
88
    DG_THROW ExceptionSignal(
89
        ExceptionSignal::PLUG_IMPOSSIBLE,
90
        "Plug-in operation not possible with this signal. ",
91
1
        "(while trying to plug %s on %s).", sigarg->getName().c_str(),
92

2
        this->getName().c_str());
93
  }
94
95
1
  virtual void unplug() {
96
    DG_THROW ExceptionSignal(
97
        ExceptionSignal::PLUG_IMPOSSIBLE,
98
        "Plug-in operation not possible with this signal. ",
99

1
        "(while trying to unplug %s).", this->getName().c_str());
100
  }
101
102
1
  virtual bool isPlugged() const { return false; }
103
104
1
  virtual SignalBase<Time> *getPluged() const { return NULL; }
105
106
1
  virtual void setConstantDefault() {
107
    DG_THROW ExceptionSignal(
108
        ExceptionSignal::PLUG_IMPOSSIBLE,
109
        "Plug-in operation not possible with this signal. ",
110

1
        "(while trying to save %s).", this->getName().c_str());
111
  }
112
113
  /// \}
114
115
  /// \name Set
116
  /// \{
117
118
  /* Generic set function. Should be reimplemented by the specific
119
   * Signal.  Sets a signal value
120
   */
121
1
  virtual void set(std::istringstream &) {
122
    DG_THROW ExceptionSignal(ExceptionSignal::SET_IMPOSSIBLE,
123
                             "Set operation not possible with this signal. ",
124
                             "(while trying to set %s).",
125

1
                             this->getName().c_str());
126
  }
127
128
1
  virtual void get(std::ostream &) const {
129
    DG_THROW ExceptionSignal(ExceptionSignal::SET_IMPOSSIBLE,
130
                             "Get operation not possible with this signal. ",
131
                             "(while trying to get %s).",
132

1
                             this->getName().c_str());
133
  }
134
135
1
  virtual inline void recompute(const Time &) {
136
    DG_THROW ExceptionSignal(
137
        ExceptionSignal::SET_IMPOSSIBLE,
138
        "Recompute operation not possible with this signal. ",
139

1
        "(while trying to recompute %s).", this->getName().c_str());
140
  }
141
142
1
  virtual void trace(std::ostream &) const {
143
    DG_THROW ExceptionSignal(ExceptionSignal::SET_IMPOSSIBLE,
144
                             "Trace operation not possible with this signal. ",
145
                             "(while trying to trace %s).",
146

1
                             this->getName().c_str());
147
  }
148
149
  /// \}
150
151
  /// \name Display
152
  /// \{
153
154
1
  virtual std::ostream &display(std::ostream &os) const {
155
1
    os << "Sig:" << name;
156
1
    return os;
157
  }
158
159
2
  std::string shortName() const {
160
4
    std::istringstream iss(name);
161
2
    const int SIZE = 128;
162
    char buffer[SIZE];
163

12
    while (iss.good()) {
164
10
      iss.getline(buffer, SIZE, ':');
165
    }
166
2
    const std::string res(buffer);
167
4
    return res;
168
  }
169
  /// \}
170
171
  /// \name Information providers
172
  /// \{
173
174
1
  virtual void ExtractNodeAndLocalNames(std::string &LocalName,
175
                                        std::string &NodeName) const {
176
1
    std::string fullname = this->getName();
177
178
1
    size_t IdxPosLocalName = fullname.rfind(":");
179
1
    LocalName = fullname.substr(IdxPosLocalName + 1,
180
1
                                fullname.length() - IdxPosLocalName + 1);
181
1
    size_t IdxPosNodeNameStart = fullname.find("(");
182
1
    size_t IdxPosNodeNameEnd = fullname.find(")");
183
1
    NodeName = fullname.substr(IdxPosNodeNameStart + 1,
184
1
                               IdxPosNodeNameEnd - IdxPosNodeNameStart - 1);
185
1
  }
186
187
  /// \}
188
189
  /// \name Test
190
  /// \{
191
1
  virtual void checkCompatibility() {
192
    DG_THROW ExceptionSignal(ExceptionSignal::PLUG_IMPOSSIBLE,
193
                             "Abstract signal not compatible with anything.",
194
                             "(while trying to plug <%s>).",
195

1
                             this->getName().c_str());
196
  }
197
  /// \}
198
199
 protected:
200
  std::string name;
201
  Time signalTime;
202
  bool ready;
203
};
204
205
/// Forward to a virtual fonction.
206
template <class Time>
207
4
std::ostream &operator<<(std::ostream &os, const SignalBase<Time> &sig) {
208
4
  return sig.display(os);
209
}
210
}  // end of namespace dynamicgraph.
211
212
#endif  //! DYNAMIC_GRAPH_SIGNAL_BASE_H