charon-core  0.3.1
VirtualSlotSwitch.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 Gerald Mwangi
2 
3  This file is part of Charon.
4 
5  Charon is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Charon is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with Charon. If not, see <http://www.gnu.org/licenses/>.
17 */
27 
28 VirtualSlotSwitch::VirtualSlotSwitch(const std::string& name) :
30  "VirtualSlotSwitch", name,
31  "<h2>Connects its output to one of its inputs according to a "
32  "statement or inputnumber</h2><br>"
33  "Connects its output to one of its inputs according to a "
34  "statement or inputnumber."
35  ),
36  statement(true,false),
37  inputnumber(true,false),output(0)
38 {
39 
41  statement, "statement",
42  "statement to switch between input 0 and input 1",
43  "bool");
45  inputnumber, "inputnumber",
46  "connects output to according input",
47  "int");
48 
51  "output connected to one of the inputs",
52  output.getType());
53 
54  ParameteredObject::_addParameter< int >(
55  number_of_inputs, "number_of_inputs",
56  "number_of_inputs",
57  2, "int");
58  ParameteredObject::_addParameter< bool >(
59  boolean_inputnumber, "boolean_inputnumber",
60  "cast inputnumber to bool",
61  0, "bool");
62  _setDynamic(true);
63  oldselection=0;
64 
65 
66 
67 }
68 
69 VirtualSlotSwitch::~VirtualSlotSwitch()
70 {
71  setNumberOfVirtualSlots(0);
72 }
73 
75 
77  raise("Either statement or inputnumber must be connected!");
78  int selected_in=-1;
79  if(statement.connected())
80  selected_in=statement();
82  {
83 
84  selected_in=inputnumber();
85  if(boolean_inputnumber())
86  if(selected_in)
87  selected_in=1;
88  }
89  if(selected_in>=_virtualInputSlots.size()||selected_in<0)
90  {
91  std::stringstream msg;
92  msg<<"Out of range: Selected input: "<<selected_in<<". Possible range [0,"<<_virtualInputSlots.size()-1<<"]"<<std::endl;
93  raise(msg.str());
94 
95  }
96  if(oldselection!=selected_in)
97  {
98  sout<<"Switching to slot "<<selected_in<<std::endl;
99  output.setVirtualPartnerSlot(_virtualInputSlots[selected_in]);
100 
101  _virtualInputSlots[oldselection]->setVirtualPartnerSlot(_virtualOutputSlots[oldselection]);
102  oldselection=selected_in;
103  }
104 
105 
106 }
107 
109 {
110  number_of_inputs.load(file);
111 
112  if(number_of_inputs<2)
113  raise("Number of inputs must be at least 2");
114  setNumberOfVirtualSlots(number_of_inputs);
115  output.setVirtualPartnerSlot(_virtualInputSlots[0]);
116 
117 }
118 
119 void VirtualSlotSwitch::setNumberOfVirtualSlots(int num)
120 {
121 
122  //Temporary list of virtual slots
123  std::vector<VirtualOutputSlot*> tempOutputSlots;
124  std::vector<VirtualInputSlot*> tempInputSlots;
125 
126  //Keep smallest common number of slots
127  int nslots=std::min(num,(int)_virtualOutputSlots.size());
128  for(int i=0;i<nslots;i++)
129  {
130  tempInputSlots.push_back(_virtualInputSlots[i]);
131  tempOutputSlots.push_back(_virtualOutputSlots[i]);
132  }
133 
134  //Add more slots if needed
135  for(int i=nslots;i<num;i++)
136  {
139  out->setVirtualPartnerSlot(in);
140  tempInputSlots.push_back(in);
141  tempOutputSlots.push_back(out);
142  }
143 
144  //Remove slots if _virtualOutputSlots.size()> num
145  for(size_t i=num;i<_virtualOutputSlots.size();i++) {
146  _removeInputSlot(_virtualInputSlots[i]->getName());
147 
148  ((VirtualOutputSlot*)_virtualOutputSlots[i])->disconnect();
149  ((VirtualInputSlot*)_virtualInputSlots[i])->disconnect();
150  delete _virtualOutputSlots[i];
151  delete _virtualInputSlots[i];
152  }
153 
154  //Remember new slotlist
155  _virtualInputSlots=tempInputSlots;
156  _virtualOutputSlots=tempOutputSlots;
157 
158  //Eventually add new slots to internal interface
159  for(int i=nslots;i<num;i++)
160  {
161  _addInputSlot(*((Slot*)_virtualInputSlots[i]),
162  _virtualInputSlots[i]->getName(),
163  "Virtual Slot",_virtualInputSlots[i]->getType());
164  }
165 
166 
167 
168 
169 }
170 
171 // the following functions are needed
172 // for class VirtualSlotSwitch to work as a charon plugin.
174  create(const std::string& name, ParameteredObject::template_type) {
175  return new VirtualSlotSwitch(name);
176 }
177 
179  delete b;
180 }
181 
184 #ifdef _DEBUG
186 #else
188 #endif
189 }
This class serves to store parameters used within the Charon Project.
Definition: ParameterFile.h:68
forstatement_DECLDIR void destroy(ParameteredObject *b)
Deletes an instance of the plugin.
build_type
defined build type
Parameter< int > number_of_inputs
number_of_inputs
void setVirtualPartnerSlot(VirtualSlot *insl)
Set corresponding partner VirtualSlot.
Definition: Slots.cpp:277
SplitStream sout
Dummy instance for usage in other files (for interface too).
template_type
Integer which represents a template type.
Base class for serializable objects.
Convenience file to iclude all ParameteredObject dependencies and Template functions.
bool connected() const
Check if slot is connected.
Definition: Slots.cpp:85
void _setDynamic(bool v)
Specifies if the ParameteredObject is dynamic.
the object was compiled in release mode
std::string getName() const
get the Name
Definition: Slots.cpp:425
forstatement_DECLDIR ParameteredObject * create(const std::string &name, ParameteredObject::template_type)
Creates an instance of the plugin.
InputSlot< bool > statement
statement to switch between input 0 and input 1
std::string getType() const
overloaded getType
Definition: Slots.cpp:421
void _removeInputSlot(std::string name)
Remove an input slot.
void _addInputSlot(Slot &slot, const std::string &name, const std::string &doc, const std::string &type="")
These functions needs to be called by the derived class in order to register all objects which can be...
Connects its output to one of its inputs according to a statement or inputnumber. ...
virtualslotswitch_DECLDIR ParameteredObject::build_type getBuildType()
Report build configuration to prevent linking of incompatibel runtime libs.
VirtualOutputSlot output
output connected to one of the inputs
virtual void load(const ParameterFile &pf)
Load from ParameterFile.
Definition: Parameter.hxx:109
the object was compiled in debug mode
virtual void prepareDynamicInterface(const ParameterFile &file)
prepare interface of parameters and slots
Commom properties of slot objects.
Definition: Slots.h:47
void _addOutputSlot(Slot &slot, const std::string &name, const std::string &doc, const std::string &type="")
Register output slot.
#define virtualslotswitch_DECLDIR
Not needed without MSVC.
VirtualSlotSwitch(const std::string &name="")
default constructor
const std::string & getName() const
instance name
virtual void execute()
Update object.
InputSlot< int > inputnumber
connects output to according input