ControlSelector.cxx

Go to the documentation of this file.
00001 #include "ControlSelector.hxx"
00002 #include <iostream>
00003 #include <sstream>
00004 #include "ProcessingFactory.hxx"
00005 
00006 
00007 namespace CLAM
00008 {
00009 namespace Hidden
00010 {
00011         static const char * metadata[] = {
00012                 "key", "ControlSelector",
00013                 "category", "Controls",
00014                 "description", "ControlSelector",
00015                 0
00016         };
00017         static FactoryRegistrator<ProcessingFactory, ControlSelector> reg = metadata;
00018 }
00019 
00020         void ControlSelectorConfig::DefaultInit()
00021         {
00022                 AddAll();
00023                 UpdateData();
00024                 SetNumberOfInputs(2);
00025                 SetDefaultIndex(1);
00026         }
00027 
00028         ControlSelector::ControlSelector()
00029                 : _indexControl(0,"In Control Index to send",this,&ControlSelector::InControlCallback)
00030                 , _outputControl("Output value",this)
00031         {
00032                 Configure( _config );   
00033         }
00034         
00035         ControlSelector::ControlSelector( const ControlSelectorConfig& cfg )
00036                 : _indexControl(0,"In Control Index to send",this,&ControlSelector::InControlCallback)
00037                 , _outputControl("Output value",this)
00038         { 
00039                 Configure( cfg );
00040         }
00041         ControlSelector::~ControlSelector()
00042         {
00043                 RemoveOldControls();
00044         }
00045                 
00046         bool ControlSelector::ConcreteConfigure( const ProcessingConfig& cfg )
00047         { 
00048                 RemoveOldControls();
00049 
00050                 CopyAsConcreteConfig( _config, cfg );
00051 
00052                 _config.AddAll();
00053                 _config.UpdateData();
00054 
00055 
00056                 _indexControl.DoControl(_config.GetDefaultIndex());
00057 
00058                 int nInputs = int(_config.GetNumberOfInputs());
00059                 if (nInputs == 0)
00060                         return AddConfigErrorMessage("Cannot work without any control input");
00061                 CreateInControls(nInputs);
00062                 return true; 
00063         }
00064 
00065 
00066         void ControlSelector::CreateInControls(unsigned numberOfControls)
00067         {
00068                 for (unsigned i=1; i<=numberOfControls; i++)
00069                 {
00070                         std::ostringstream controlName;
00071                         controlName<<"In Control Value"<<i;
00072                         _inControls.push_back(new FloatInControl(i,controlName.str(),this,&ControlSelector::InControlCallback));
00073                         std::cout<<"created control "<<i<<std::endl;
00074                 }
00075 
00076         }
00077         void ControlSelector::RemoveOldControls()
00078         {
00079                 for (unsigned i=0; i<_inControls.size();i++)
00080                 {
00081                         delete _inControls[i];
00082                 }
00083                 _inControls.clear();
00084         }
00085 
00086         void ControlSelector::InControlCallback(unsigned controlId, const TControlData & value)
00087         {
00088                 if (not IsConfigured()) return;
00089                 const unsigned index=_indexControl.GetLastValue();
00090                 if (controlId==0 or controlId==index+1) // index change or proper input
00091                 {
00092                         if (index>=_inControls.size())
00093                         {
00094                                 std::cout<<"WARNING: control selector receiving an index number greater than the number of input controls, ignoring it"<<std::endl;
00095                                 return;
00096                         }
00097                         _outputControl.SendControl(_inControls[index]->GetLastValue());
00098                 }
00099         }
00100         
00101         bool ControlSelector::Do()
00102         {
00103                 return true;
00104         }
00105 
00106 }
00107 
Generated by  doxygen 1.6.3