variadic-op.hh
Go to the documentation of this file.
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 */
18 
19 /* SOT */
21 #include <dynamic-graph/entity.h>
22 
23 #include <sot/core/flags.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  ")::") {
51  }
52 
53  virtual ~VariadicAbstract(void) {
54  for (std::size_t i = 0; i < signalsIN.size(); ++i) {
56  }
57  };
58 
59  public: /* --- SIGNAL --- */
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 {
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 size_type &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  }
107  }
108 
109  size_type getSignalNumber() const { return (size_type)signalsIN.size(); }
110 
112  if (i < 0 || i >= (size_type)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(size_type n) { (void)n; };
123 
124  private:
125  void _removeSignal(const std::size_t i) {
126  // signalDeregistration(names[i]);
127  signalDeregistration(signalsIN[i]->shortName());
129  delete signalsIN[i];
130  }
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, sigtime_t> {
141  typedef typename Operator::Tin Tin;
142  typedef typename Operator::Tout Tout;
144 
145  public: /* --- CONSTRUCTION --- */
146  Operator op;
147 
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, sigtime_t 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(size_type n) { op.updateSignalNumber(n); }
176 };
177 } // namespace sot
178 } // namespace dynamicgraph
179 
180 #endif // #ifndef SOT_CORE_VARIADICOP_HH
dynamicgraph::sot::VariadicOp::op
Operator op
Definition: variadic-op.hh:146
dynamicgraph::sot::VariadicAbstract::signal_t
SignalPtr< Tin, sigtime_t > signal_t
Definition: variadic-op.hh:57
dynamicgraph::sot::VariadicAbstract::getTypeInName
static std::string getTypeInName(void)
Definition: operator.hh:825
dynamicgraph::sot::VariadicAbstract::~VariadicAbstract
virtual ~VariadicAbstract(void)
Definition: variadic-op.hh:53
dynamicgraph::SignalPtr< Tin, sigtime_t >
dynamicgraph
dynamicgraph::Entity::commandMap
CommandMap_t commandMap
i
int i
dynamicgraph::Entity
dynamicgraph::sot::VariadicAbstract::removeSignal
void removeSignal()
Definition: variadic-op.hh:82
dynamicgraph::sot::VariadicAbstract::addSignal
std::size_t addSignal(const std::string &name)
Definition: variadic-op.hh:69
dynamicgraph::Entity::name
std::string name
dynamicgraph::sot::VariadicAbstract::setSignalNumber
void setSignalNumber(const size_type &n)
Definition: variadic-op.hh:89
dynamicgraph::sot::VariadicAbstract::VariadicAbstract
VariadicAbstract(const std::string &name, const std::string &className)
Definition: variadic-op.hh:44
dynamicgraph::sot::VariadicOp::VariadicOp
VariadicOp(const std::string &name)
Definition: variadic-op.hh:157
dynamicgraph::sot::VariadicAbstract
Definition: variadic-op.hh:39
res
res
dynamicgraph::sot::VariadicAbstract::updateSignalNumber
virtual void updateSignalNumber(size_type n)
Definition: variadic-op.hh:122
s
s
dynamicgraph::sot::VariadicAbstract::getTypeOutName
static std::string getTypeOutName(void)
Definition: operator.hh:829
dynamicgraph::sigtime_t
int64_t sigtime_t
dynamicgraph::ExceptionAbstract
dynamicgraph::SignalTimeDependent::addDependency
virtual void addDependency(const SignalBase< Time > &signal)
dynamicgraph::sot::VariadicOp::Base
VariadicAbstract< Tin, Tout, sigtime_t > Base
Definition: variadic-op.hh:148
dynamicgraph::sot::VariadicAbstract::baseSigname
const std::string baseSigname
Definition: variadic-op.hh:135
dynamicgraph::sot::VariadicOp::Tin
Operator::Tin Tin
Definition: variadic-op.hh:141
dynamicgraph::sot::VariadicOp::CLASS_NAME
static const std::string CLASS_NAME
Definition: variadic-op.hh:153
x
x
dynamicgraph::SignalTimeDependent::removeDependency
virtual void removeDependency(const SignalBase< Time > &signal)
all-signals.h
dynamicgraph::size_type
Matrix::Index size_type
dynamicgraph::sot::VariadicAbstract::SOUT
SignalTimeDependent< Tout, sigtime_t > SOUT
Definition: variadic-op.hh:61
dynamicgraph::sot::VariadicAbstract::_declareSignal
void _declareSignal(signal_t *s)
Definition: variadic-op.hh:131
flags.hh
linear-algebra.h
pool.hh
dynamicgraph::sot::VariadicOp::~VariadicOp
virtual ~VariadicOp(void)
Definition: variadic-op.hh:162
dynamicgraph::sot::VariadicOp::getClassName
virtual const std::string & getClassName() const
Definition: variadic-op.hh:154
dynamicgraph::sot::VariadicAbstract::signalsIN
std::vector< signal_t * > signalsIN
Definition: variadic-op.hh:118
matrix-geometry.hh
dynamicgraph::SignalTimeDependent< Tout, sigtime_t >
dynamicgraph::sot::VariadicOp::getDocString
std::string getDocString() const
Definition: variadic-op.hh:155
dynamicgraph::sot::VariadicAbstract::getSignalIn
signal_t * getSignalIn(size_type i)
Definition: variadic-op.hh:111
dynamicgraph::sot::VariadicOp::Tout
Operator::Tout Tout
Definition: variadic-op.hh:142
dynamicgraph::sot::VariadicOp::computeOperation
Tout & computeOperation(Tout &res, sigtime_t time)
Definition: variadic-op.hh:165
dynamicgraph::sot::VariadicOp
Definition: variadic-op.hh:139
dynamicgraph::sot::VariadicAbstract::addSignal
std::size_t addSignal()
Definition: variadic-op.hh:63
sig
Signal< dynamicgraph::Matrix, sigtime_t > sig("matrix")
dynamicgraph::sot::VariadicOp::updateSignalNumber
void updateSignalNumber(size_type n)
Definition: variadic-op.hh:175
dynamicgraph::Entity::signalDeregistration
void signalDeregistration(const std::string &name)
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
dynamicgraph::sot::VariadicOp::Self
VariadicOp< Operator > Self
Definition: variadic-op.hh:143
dynamicgraph::sot::VariadicAbstract::_removeSignal
void _removeSignal(const std::size_t i)
Definition: variadic-op.hh:125
n
Vec3f n
dynamicgraph::sot::VariadicAbstract::getSignalNumber
size_type getSignalNumber() const
Definition: variadic-op.hh:109


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Tue Oct 24 2023 02:26:32