CLAM-Development  1.4.0
ControlPrinterTyped.cxx
Go to the documentation of this file.
2 #include <iostream>
3 #include <sstream>
4 #include "ProcessingFactory.hxx"
5 
6 #include <CLAM/MIDIMessage.hxx>
7 
8 namespace CLAM
9 {
10 namespace Hidden
11 {
12  static const char * metadata[] = {
13  "key", "ControlPrinterTyped",
14  "category", "Controls",
15  "description", "ControlPrinterTyped",
16  0
17  };
18  static FactoryRegistrator<ProcessingFactory, ControlPrinterTyped> reg = metadata;
19 }
20 
21  void ControlPrinterTypedConfig::DefaultInit()
22  {
23  AddAll();
24  UpdateData();
25  SetIdentifier( "ControlPrinterTyped" );
26  SetTypesMask("f");
27  SetGuiOnly(true);
28  }
29 
31  {
32  Configure( _config );
33  }
34 
36  {
37  Configure( cfg );
38  }
40  {
42  }
43 
44  const unsigned int ControlPrinterTyped::GetInputsNumber() const
45  {
46  unsigned nInputs;
47  std::string typespec;
48  typespec=_config.GetTypesMask();
49  for (nInputs=0; nInputs<typespec.size();nInputs++)
50  {
51  const char type = typespec[nInputs];
52  if (type != 's' and type != 'i'
53  and type != 'f' and type != 'd'
54  and type != 'h' and type != 'M')
55  return 0; // return 0 if there is any non-compatible type
56  }
57  return nInputs;
58  }
59 
60  void ControlPrinterTyped::ResizeControls(unsigned nInputs, const std::string & baseName)
61  {
62  std::list<std::string> names;
63  if (nInputs == 1)
64  {
65  names.push_back("In Control");
66  ResizeControls(nInputs, names);
67  return;
68  }
69  for (unsigned i=0; i<nInputs; i++)
70  {
71  std::ostringstream os;
72  os << baseName << "_" << i;
73  names.push_back(os.str());
74  }
75  ResizeControls(nInputs, names);
76  }
77  InControlBase * ControlPrinterTyped::createControl(const std::string & type, const std::string & name)
78  {
79  if (type=="s")
80  return new InControl<std::string> (name,this);
81  if (type=="f")
82  return new FloatInControl (name,this);
83  if (type=="d")
84  return new InControl<double> (name,this);
85  if (type=="i")
86  return new InControl<int> (name,this);
87  if (type=="M")
88  return new InControl<MIDI::Message> (name,this);
89  // TODO: Decide whether ASSERTing (contract) or throw (control)
90  return 0;
91  }
92  void ControlPrinterTyped::ResizeControls(unsigned nInputs, const std::list<std::string> & names)
93  {
94  ClearControls();
95  unsigned i=0;
96  for (std::list<std::string>::const_iterator it = names.begin(); it != names.end(); it++)
97  {
98  std::string type;
99  type=_config.GetTypesMask()[i];
100  mInControls.push_back(createControl(type,*it));
101  i++;
102  }
103  }
104  void ControlPrinterTyped::ClearControls()
105  {
106  for (InControls::iterator it = mInControls.begin(); it != mInControls.end(); it++)
107  delete *it;
108  mInControls.clear();
109  }
110 
112  {
114 
115  CopyAsConcreteConfig( _config, cfg );
116 
117  _config.AddAll();
118  _config.UpdateData();
119 
120  unsigned nInputs = GetInputsNumber();
121  if (nInputs == 0)
122  {
123  AddConfigErrorMessage("No proper OSCTypeSpec setup. Use: 'f' for float, 'd' for double, 'i' for integer, 'h' for integer 64, 'M' for MIDI Message.");
124  return false;
125  }
126  std::string baseName = nInputs==1 ? "In Control" : _config.GetIdentifier();
127  ResizeControls(nInputs, baseName);
128 
129  return true;
130  }
131 
133  {
134  if (_config.GetGuiOnly())
135  return true;
136 
137  std::string separator = "";
138  std::stringstream values;
139  for (unsigned i = 0; i < mInControls.size(); i++)
140  {
141  values<<separator<<mInControls[i]->GetLastValueAsString();
142  separator = ", ";
143  }
144  std::cout << _config.GetIdentifier() << ": "
145  << values.str() << std::endl;
146  return true;
147  }
148 
150  {
151  ClearControls();
152  GetInControls().Clear();
153  }
154 }
155