sot-core  4.11.6
Hierarchical task solver plug-in for dynamic-graph.
switch.hh
Go to the documentation of this file.
1 // Copyright (c) 2018, Joseph Mirabel
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 
4 #ifndef __SOT_SWITCH_H__
5 #define __SOT_SWITCH_H__
6 
7 #include <dynamic-graph/command-bind.h>
8 #include <dynamic-graph/command-getter.h>
9 #include <dynamic-graph/entity.h>
10 #include <dynamic-graph/pool.h>
11 #include <dynamic-graph/signal-ptr.h>
12 #include <dynamic-graph/signal-time-dependent.h>
13 #include <dynamic-graph/signal.h>
14 
15 #include <sot/core/config.hh>
16 #include <sot/core/variadic-op.hh>
17 
18 namespace dynamicgraph {
19 namespace sot {
21 template <typename Value, typename Time = int>
22 class SOT_CORE_DLLAPI Switch : public VariadicAbstract<Value, Value, Time> {
23  DYNAMIC_GRAPH_ENTITY_DECL();
24 
25 public:
27 
28  Switch(const std::string &name)
29  : Base(name, CLASS_NAME),
30  selectionSIN(NULL, "Switch(" + name + ")::input(int)::selection"),
31  boolSelectionSIN(NULL,
32  "Switch(" + name + ")::input(bool)::boolSelection") {
33  this->signalRegistration(selectionSIN << boolSelectionSIN);
34  this->SOUT.setFunction(boost::bind(&Switch::signal, this, _1, _2));
35  this->SOUT.addDependency(selectionSIN);
36  this->SOUT.addDependency(boolSelectionSIN);
37 
38  using command::makeCommandVoid1;
39  std::string docstring = "\n"
40  " Set number of input signals\n";
41  this->addCommand(
42  "setSignalNumber",
43  makeCommandVoid1(*(Base *)this, &Base::setSignalNumber, docstring));
44 
45  docstring = "\n"
46  " Get number of input signals\n";
47  this->addCommand("getSignalNumber",
48  new command::Getter<Base, int>(
49  *this, &Base::getSignalNumber, docstring));
50  }
51 
52  ~Switch() {}
53 
55  virtual std::string getDocString() const {
56  return "Dynamically select a given signal based on a input information.\n";
57  }
58 
59  SignalPtr<int, Time> selectionSIN;
60  SignalPtr<bool, Time> boolSelectionSIN;
61 
62 private:
63  Value &signal(Value &ret, const Time &time) {
64  int sel;
65  if (selectionSIN.isPlugged()) {
66  sel = selectionSIN(time);
67  } else {
68  const bool &b = boolSelectionSIN(time);
69  sel = b ? 1 : 0;
70  }
71  if (sel < 0 || sel >= int(this->signalsIN.size()))
72  throw std::runtime_error("Signal selection is out of range.");
73 
74  ret = this->signalsIN[sel]->access(time);
75  return ret;
76  }
77 };
78 } // namespace sot
79 } // namespace dynamicgraph
80 #endif // __SOT_SWITCH_H__
Switch.
Definition: switch.hh:22
~Switch()
Definition: switch.hh:52
Switch(const std::string &name)
Definition: switch.hh:28
SignalPtr< int, Time > selectionSIN
Definition: switch.hh:59
VariadicAbstract< Value, Value, Time > Base
Definition: switch.hh:26
Definition: variadic-op.hh:39
virtual std::string getDocString() const
Header documentation of the python class.
Definition: switch.hh:55
Definition: abstract-sot-external-interface.hh:17
SignalPtr< bool, Time > boolSelectionSIN
Definition: switch.hh:60