sot-core  4.10.1
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 
26 
27  Switch(const std::string &name)
28  : Base(name, CLASS_NAME),
29  selectionSIN(NULL, "Switch(" + name + ")::input(int)::selection"),
30  boolSelectionSIN(NULL,
31  "Switch(" + name + ")::input(bool)::boolSelection") {
32  this->signalRegistration(selectionSIN << boolSelectionSIN);
33  this->SOUT.setFunction(boost::bind(&Switch::signal, this, _1, _2));
34  this->SOUT.addDependency(selectionSIN);
35  this->SOUT.addDependency(boolSelectionSIN);
36 
37  using command::makeCommandVoid1;
38  std::string docstring = "\n"
39  " Set number of input signals\n";
40  this->addCommand(
41  "setSignalNumber",
42  makeCommandVoid1(*(Base *)this, &Base::setSignalNumber, docstring));
43 
44  docstring = "\n"
45  " Get number of input signals\n";
46  this->addCommand("getSignalNumber",
47  new command::Getter<Base, int>(
48  *this, &Base::getSignalNumber, docstring));
49  }
50 
51  ~Switch() {}
52 
54  virtual std::string getDocString() const {
55  return "Dynamically select a given signal based on a input information.\n";
56  }
57 
58 private:
59  Value &signal(Value &ret, const Time &time) {
60  int sel;
61  if (selectionSIN.isPlugged()) {
62  sel = selectionSIN(time);
63  } else {
64  const bool &b = boolSelectionSIN(time);
65  sel = b ? 1 : 0;
66  }
67  if (sel < 0 || sel >= int(this->signalsIN.size()))
68  throw std::runtime_error("Signal selection is out of range.");
69 
70  ret = this->signalsIN[sel]->access(time);
71  return ret;
72  }
73 
74  SignalPtr<int, Time> selectionSIN;
75  SignalPtr<bool, Time> boolSelectionSIN;
76 };
77 } // namespace sot
78 } // namespace dynamicgraph
79 #endif // __SOT_SWITCH_H__
Switch.
Definition: switch.hh:22
Definition: variadic-op.hh:39
Definition: abstract-sot-external-interface.hh:17