Skip to content

Commit 5c1661b

Browse files
committed
Make Switch derive from VariadicAbstract
1 parent abcdfa4 commit 5c1661b

File tree

2 files changed

+20
-52
lines changed

2 files changed

+20
-52
lines changed

include/sot/core/switch.hh

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,40 @@
2828
#include <dynamic-graph/command-getter.h>
2929

3030
#include <sot/core/config.hh>
31+
#include <sot/core/variadic-op.hh>
3132

3233
namespace dynamicgraph {
3334
namespace sot {
3435
/// Switch
3536
template <typename Value, typename Time = int>
36-
class SOT_CORE_DLLAPI Switch : public dynamicgraph::Entity
37+
class SOT_CORE_DLLAPI Switch : public VariadicAbstract<Value,Value,Time>
3738
{
3839
DYNAMIC_GRAPH_ENTITY_DECL();
3940

41+
typedef VariadicAbstract<Value,Value,Time> Base;
42+
4043
Switch (const std::string& name) :
41-
Entity (name),
44+
Base (name,CLASS_NAME),
4245
selectionSIN(NULL,"Switch("+name+")::input(int)::selection"),
43-
boolSelectionSIN(NULL,"Switch("+name+")::input(bool)::boolSelection"),
44-
signalSOUT ("Switch("+name+")::output(" + typeName() + ")::sout")
46+
boolSelectionSIN(NULL,"Switch("+name+")::input(bool)::boolSelection")
4547
{
46-
signalSOUT.setFunction (boost::bind (&Switch::signal, this, _1, _2));
47-
signalRegistration (selectionSIN << boolSelectionSIN << signalSOUT);
48+
this->signalRegistration (selectionSIN << boolSelectionSIN);
49+
this->SOUT.setFunction (boost::bind(&Switch::signal,this,_1,_2));
50+
this->SOUT.addDependency (selectionSIN);
51+
this->SOUT.addDependency (boolSelectionSIN);
4852

4953
using command::makeCommandVoid1;
5054
std::string docstring =
5155
"\n"
5256
" Set number of input signals\n";
53-
addCommand ("setSignalNumber", makeCommandVoid1
54-
(*this, &Switch::setSignalNumber, docstring));
57+
this->addCommand ("setSignalNumber", makeCommandVoid1
58+
(*(Base*)this, &Base::setSignalNumber, docstring));
5559

5660
docstring =
5761
"\n"
5862
" Get number of input signals\n";
59-
addCommand ("getSignalNumber",
60-
new command::Getter<Switch, int> (*this, &Switch::getSignalNumber, docstring));
63+
this->addCommand ("getSignalNumber",
64+
new command::Getter<Base, int> (*this, &Base::getSignalNumber, docstring));
6165
}
6266

6367
~Switch () {}
@@ -69,39 +73,7 @@ namespace dynamicgraph {
6973
"Dynamically select a given signal based on a input information.\n";
7074
}
7175

72-
void setSignalNumber (const int& n)
73-
{
74-
assert (n>=0);
75-
const std::size_t oldSize = signals.size();
76-
for (std::size_t i = n; i < oldSize; ++i)
77-
{
78-
std::ostringstream oss; oss << "sin" << i;
79-
signalDeregistration(oss.str());
80-
delete signals[i];
81-
}
82-
signals.resize(n,NULL);
83-
84-
for (std::size_t i = oldSize; i < (std::size_t)n; ++i)
85-
{
86-
assert (signals[i]==NULL);
87-
std::ostringstream oss;
88-
oss << "Switch("<< getName()<< ")::input(" << typeName() << ")::sin" << i;
89-
signals[i] = new Signal_t (NULL,oss.str());
90-
signalRegistration(*signals[i]);
91-
}
92-
}
93-
94-
int getSignalNumber () const
95-
{
96-
return (int)signals.size();
97-
}
98-
9976
private:
100-
typedef SignalPtr<Value, Time> Signal_t;
101-
typedef std::vector<Signal_t*> Signals_t;
102-
103-
static const std::string& typeName ();
104-
10577
Value& signal (Value& ret, const Time& time)
10678
{
10779
int sel;
@@ -111,18 +83,15 @@ namespace dynamicgraph {
11183
const bool& b = boolSelectionSIN(time);
11284
sel = b ? 1 : 0;
11385
}
114-
if (sel < 0 || sel >= int(signals.size()))
86+
if (sel < 0 || sel >= int(this->signalsIN.size()))
11587
throw std::runtime_error ("Signal selection is out of range.");
11688

117-
ret = (*signals[sel]) (time);
89+
ret = this->signalsIN[sel]->access (time);
11890
return ret;
11991
}
12092

121-
Signals_t signals;
12293
SignalPtr <int, Time> selectionSIN;
12394
SignalPtr <bool, Time> boolSelectionSIN;
124-
125-
Signal <Value, Time> signalSOUT;
12695
};
12796
} // namespace sot
12897
} // namespace dynamicgraph

src/tools/switch.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222

2323
namespace dynamicgraph {
2424
namespace sot {
25-
template <typename Value, typename Time>
26-
const std::string& Switch<Value,Time>::typeName ()
27-
{
28-
return TypeNameHelper<Value>::typeName;
29-
}
25+
template< typename Tin, typename Tout, typename Time >
26+
std::string VariadicAbstract<Tin,Tout,Time>::getTypeInName (void) { return TypeNameHelper<Tin>::typeName; }
27+
template< typename Tin, typename Tout, typename Time >
28+
std::string VariadicAbstract<Tin,Tout,Time>::getTypeOutName(void) { return TypeNameHelper<Tout>::typeName; }
3029

3130
typedef Switch<Vector,int> SwitchVector;
3231
template<>

0 commit comments

Comments
 (0)