ControlPrinterTyped.cxx
Go to the documentation of this file.00001 #include "ControlPrinterTyped.hxx"
00002 #include <iostream>
00003 #include <sstream>
00004 #include "ProcessingFactory.hxx"
00005
00006 #include <CLAM/MIDIMessage.hxx>
00007
00008 namespace CLAM
00009 {
00010 namespace Hidden
00011 {
00012 static const char * metadata[] = {
00013 "key", "ControlPrinterTyped",
00014 "category", "Controls",
00015 "description", "ControlPrinterTyped",
00016 0
00017 };
00018 static FactoryRegistrator<ProcessingFactory, ControlPrinterTyped> reg = metadata;
00019 }
00020
00021 void ControlPrinterTypedConfig::DefaultInit()
00022 {
00023 AddAll();
00024 UpdateData();
00025 SetIdentifier( "ControlPrinterTyped" );
00026 SetTypesMask("f");
00027 SetGuiOnly(true);
00028 }
00029
00030 ControlPrinterTyped::ControlPrinterTyped()
00031 {
00032 Configure( _config );
00033 }
00034
00035 ControlPrinterTyped::ControlPrinterTyped( const ControlPrinterTypedConfig& cfg )
00036 {
00037 Configure( cfg );
00038 }
00039 ControlPrinterTyped::~ControlPrinterTyped()
00040 {
00041 RemoveOldControls();
00042 }
00043
00044 const unsigned int ControlPrinterTyped::GetInputsNumber() const
00045 {
00046 unsigned nInputs;
00047 std::string typespec;
00048 typespec=_config.GetTypesMask();
00049 for (nInputs=0; nInputs<typespec.size();nInputs++)
00050 {
00051 const char type = typespec[nInputs];
00052 if (type != 's' and type != 'i'
00053 and type != 'f' and type != 'd'
00054 and type != 'h' and type != 'M')
00055 return 0;
00056 }
00057 return nInputs;
00058 }
00059
00060 void ControlPrinterTyped::ResizeControls(unsigned nInputs, const std::string & baseName)
00061 {
00062 std::list<std::string> names;
00063 if (nInputs == 1)
00064 {
00065 names.push_back("In Control");
00066 ResizeControls(nInputs, names);
00067 return;
00068 }
00069 for (unsigned i=0; i<nInputs; i++)
00070 {
00071 std::ostringstream os;
00072 os << baseName << "_" << i;
00073 names.push_back(os.str());
00074 }
00075 ResizeControls(nInputs, names);
00076 }
00077 InControlBase * ControlPrinterTyped::createControl(const std::string & type, const std::string & name)
00078 {
00079 if (type=="s")
00080 return new InControl<std::string> (name,this);
00081 if (type=="f")
00082 return new FloatInControl (name,this);
00083 if (type=="d")
00084 return new InControl<double> (name,this);
00085 if (type=="i")
00086 return new InControl<int> (name,this);
00087 if (type=="M")
00088 return new InControl<MIDI::Message> (name,this);
00089
00090 return 0;
00091 }
00092 void ControlPrinterTyped::ResizeControls(unsigned nInputs, const std::list<std::string> & names)
00093 {
00094 ClearControls();
00095 unsigned i=0;
00096 for (std::list<std::string>::const_iterator it = names.begin(); it != names.end(); it++)
00097 {
00098 std::string type;
00099 type=_config.GetTypesMask()[i];
00100 mInControls.push_back(createControl(type,*it));
00101 i++;
00102 }
00103 }
00104 void ControlPrinterTyped::ClearControls()
00105 {
00106 for (InControls::iterator it = mInControls.begin(); it != mInControls.end(); it++)
00107 delete *it;
00108 mInControls.clear();
00109 }
00110
00111 bool ControlPrinterTyped::ConcreteConfigure( const ProcessingConfig& cfg )
00112 {
00113 RemoveOldControls();
00114
00115 CopyAsConcreteConfig( _config, cfg );
00116
00117 _config.AddAll();
00118 _config.UpdateData();
00119
00120 unsigned nInputs = GetInputsNumber();
00121 if (nInputs == 0)
00122 {
00123 AddConfigErrorMessage("No proper OSCTypeSpec setup. Use: 'f' for float, 'd' for double, 'i' for integer, 'h' for integer 64, 'M' for MIDI Message.");
00124 return false;
00125 }
00126 std::string baseName = nInputs==1 ? "In Control" : _config.GetIdentifier();
00127 ResizeControls(nInputs, baseName);
00128
00129 return true;
00130 }
00131
00132 bool ControlPrinterTyped::Do()
00133 {
00134 if (_config.GetGuiOnly())
00135 return true;
00136
00137 std::string separator = "";
00138 std::stringstream values;
00139 for (unsigned i = 0; i < mInControls.size(); i++)
00140 {
00141 values<<separator<<mInControls[i]->GetLastValueAsString();
00142 separator = ", ";
00143 }
00144 std::cout << _config.GetIdentifier() << ": "
00145 << values.str() << std::endl;
00146 return true;
00147 }
00148
00149 void ControlPrinterTyped::RemoveOldControls()
00150 {
00151 ClearControls();
00152 GetInControls().Clear();
00153 }
00154 }
00155