Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Copyright 2018, |
3 |
|
|
* Mirabel Joseph |
4 |
|
|
* |
5 |
|
|
* CNRS/AIST |
6 |
|
|
* |
7 |
|
|
*/ |
8 |
|
|
|
9 |
|
|
#ifndef SOT_CORE_VARIADICOP_HH |
10 |
|
|
#define SOT_CORE_VARIADICOP_HH |
11 |
|
|
|
12 |
|
|
/* --------------------------------------------------------------------- */ |
13 |
|
|
/* --- INCLUDE --------------------------------------------------------- */ |
14 |
|
|
/* --------------------------------------------------------------------- */ |
15 |
|
|
|
16 |
|
|
/* Matrix */ |
17 |
|
|
#include <dynamic-graph/linear-algebra.h> |
18 |
|
|
|
19 |
|
|
/* SOT */ |
20 |
|
|
#include <dynamic-graph/all-signals.h> |
21 |
|
|
#include <dynamic-graph/entity.h> |
22 |
|
|
|
23 |
|
|
#include <sot/core/flags.hh> |
24 |
|
|
#include <sot/core/matrix-geometry.hh> |
25 |
|
|
#include <sot/core/pool.hh> |
26 |
|
|
|
27 |
|
|
/* STD */ |
28 |
|
|
#include <boost/function.hpp> |
29 |
|
|
#include <string> |
30 |
|
|
|
31 |
|
|
namespace dynamicgraph { |
32 |
|
|
namespace sot { |
33 |
|
|
|
34 |
|
|
/* --------------------------------------------------------------------- */ |
35 |
|
|
/* --- CLASS ----------------------------------------------------------- */ |
36 |
|
|
/* --------------------------------------------------------------------- */ |
37 |
|
|
|
38 |
|
|
template <typename Tin, typename Tout, typename Time> |
39 |
|
|
class VariadicAbstract : public Entity { |
40 |
|
|
public: /* --- CONSTRUCTION --- */ |
41 |
|
|
static std::string getTypeInName(void); |
42 |
|
|
static std::string getTypeOutName(void); |
43 |
|
|
|
44 |
|
✗ |
VariadicAbstract(const std::string &name, const std::string &className) |
45 |
|
|
: Entity(name), |
46 |
|
✗ |
SOUT(className + "(" + name + ")::output(" + getTypeOutName() + |
47 |
|
|
")::sout"), |
48 |
|
✗ |
baseSigname(className + "(" + name + ")::input(" + getTypeInName() + |
49 |
|
✗ |
")::") { |
50 |
|
✗ |
signalRegistration(SOUT); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
✗ |
virtual ~VariadicAbstract(void) { |
54 |
|
✗ |
for (std::size_t i = 0; i < signalsIN.size(); ++i) { |
55 |
|
✗ |
_removeSignal(i); |
56 |
|
|
} |
57 |
|
✗ |
}; |
58 |
|
|
|
59 |
|
|
public: /* --- SIGNAL --- */ |
60 |
|
|
typedef SignalPtr<Tin, int> signal_t; |
61 |
|
|
SignalTimeDependent<Tout, int> SOUT; |
62 |
|
|
|
63 |
|
✗ |
std::size_t addSignal() { |
64 |
|
✗ |
std::ostringstream oss; |
65 |
|
✗ |
oss << "sin" << signalsIN.size(); |
66 |
|
✗ |
return addSignal(oss.str()); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
✗ |
std::size_t addSignal(const std::string &name) { |
70 |
|
✗ |
signal_t *sig = new signal_t(NULL, baseSigname + name); |
71 |
|
|
try { |
72 |
|
✗ |
_declareSignal(sig); |
73 |
|
✗ |
signalsIN.push_back(sig); |
74 |
|
|
// names.push_back (name); |
75 |
|
✗ |
return signalsIN.size() - 1; |
76 |
|
✗ |
} catch (const ExceptionAbstract &) { |
77 |
|
✗ |
delete sig; |
78 |
|
✗ |
throw; |
79 |
|
|
} |
80 |
|
|
} |
81 |
|
|
|
82 |
|
✗ |
void removeSignal() { |
83 |
|
✗ |
assert(signalsIN.size() > 0); |
84 |
|
✗ |
_removeSignal(signalsIN.size() - 1); |
85 |
|
|
// names.pop_back(); |
86 |
|
✗ |
signalsIN.pop_back(); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
✗ |
void setSignalNumber(const int &n) { |
90 |
|
✗ |
assert(n >= 0); |
91 |
|
✗ |
const std::size_t oldSize = signalsIN.size(); |
92 |
|
✗ |
for (std::size_t i = n; i < oldSize; ++i) _removeSignal(i); |
93 |
|
✗ |
signalsIN.resize(n, NULL); |
94 |
|
|
// names.resize(n); |
95 |
|
|
|
96 |
|
✗ |
for (std::size_t i = oldSize; i < (std::size_t)n; ++i) { |
97 |
|
✗ |
assert(signalsIN[i] == NULL); |
98 |
|
✗ |
std::ostringstream oss; |
99 |
|
✗ |
oss << baseSigname << "sin" << i; |
100 |
|
|
// names[i] = oss.str(); |
101 |
|
|
// signal_t* s = new signal_t (NULL,names[i]); |
102 |
|
✗ |
signal_t *s = new signal_t(NULL, oss.str()); |
103 |
|
✗ |
signalsIN[i] = s; |
104 |
|
✗ |
_declareSignal(s); |
105 |
|
|
} |
106 |
|
✗ |
updateSignalNumber(n); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
✗ |
int getSignalNumber() const { return (int)signalsIN.size(); } |
110 |
|
|
|
111 |
|
✗ |
signal_t *getSignalIn(int i) { |
112 |
|
✗ |
if (i < 0 || i >= (int)signalsIN.size()) |
113 |
|
✗ |
throw std::out_of_range("Wrong signal index"); |
114 |
|
✗ |
return signalsIN[i]; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
protected: |
118 |
|
|
std::vector<signal_t *> signalsIN; |
119 |
|
|
// Use signal->shortName instead |
120 |
|
|
// std::vector< std::string > names; |
121 |
|
|
|
122 |
|
✗ |
virtual void updateSignalNumber(int n) { (void)n; }; |
123 |
|
|
|
124 |
|
|
private: |
125 |
|
✗ |
void _removeSignal(const std::size_t i) { |
126 |
|
|
// signalDeregistration(names[i]); |
127 |
|
✗ |
signalDeregistration(signalsIN[i]->shortName()); |
128 |
|
✗ |
SOUT.removeDependency(*signalsIN[i]); |
129 |
|
✗ |
delete signalsIN[i]; |
130 |
|
|
} |
131 |
|
✗ |
void _declareSignal(signal_t *s) { |
132 |
|
✗ |
signalRegistration(*s); |
133 |
|
✗ |
SOUT.addDependency(*s); |
134 |
|
|
} |
135 |
|
|
const std::string baseSigname; |
136 |
|
|
}; |
137 |
|
|
|
138 |
|
|
template <typename Operator> |
139 |
|
|
class VariadicOp : public VariadicAbstract<typename Operator::Tin, |
140 |
|
|
typename Operator::Tout, int> { |
141 |
|
|
typedef typename Operator::Tin Tin; |
142 |
|
|
typedef typename Operator::Tout Tout; |
143 |
|
|
typedef VariadicOp<Operator> Self; |
144 |
|
|
|
145 |
|
|
public: /* --- CONSTRUCTION --- */ |
146 |
|
|
Operator op; |
147 |
|
|
|
148 |
|
|
typedef VariadicAbstract<Tin, Tout, int> Base; |
149 |
|
|
|
150 |
|
|
// static std::string getTypeInName ( void ) { return Operator::nameTypeIn (); |
151 |
|
|
// } static std::string getTypeOutName( void ) { return |
152 |
|
|
// Operator::nameTypeOut(); } |
153 |
|
|
static const std::string CLASS_NAME; |
154 |
|
✗ |
virtual const std::string &getClassName() const { return CLASS_NAME; } |
155 |
|
✗ |
std::string getDocString() const { return op.getDocString(); } |
156 |
|
|
|
157 |
|
✗ |
VariadicOp(const std::string &name) : Base(name, CLASS_NAME) { |
158 |
|
✗ |
this->SOUT.setFunction(boost::bind(&Self::computeOperation, this, _1, _2)); |
159 |
|
✗ |
op.initialize(this, this->commandMap); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
✗ |
virtual ~VariadicOp(void){}; |
163 |
|
|
|
164 |
|
|
protected: |
165 |
|
✗ |
Tout &computeOperation(Tout &res, int time) { |
166 |
|
✗ |
std::vector<const Tin *> in(this->signalsIN.size()); |
167 |
|
✗ |
for (std::size_t i = 0; i < this->signalsIN.size(); ++i) { |
168 |
|
✗ |
const Tin &x = this->signalsIN[i]->access(time); |
169 |
|
✗ |
in[i] = &x; |
170 |
|
|
} |
171 |
|
✗ |
op(in, res); |
172 |
|
✗ |
return res; |
173 |
|
|
} |
174 |
|
|
|
175 |
|
✗ |
inline void updateSignalNumber(int n) { op.updateSignalNumber(n); } |
176 |
|
|
}; |
177 |
|
|
} // namespace sot |
178 |
|
|
} // namespace dynamicgraph |
179 |
|
|
|
180 |
|
|
#endif // #ifndef SOT_CORE_VARIADICOP_HH |
181 |
|
|
|