CLAM-Development  1.4.0
Controller.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 // TokenDelay_example.cxx
23 // example for the TokenDelay processing object
24 
25 #ifndef _Controller_
26 #define _Controller_
27 
28 #include "OutControl.hxx"
29 #include "OutControlArray.hxx"
30 #include "Processing.hxx"
31 #include "ProcessingData.hxx"
32 
33 #include <typeinfo> // for bad_cast definition
34 #include <string>
35 #include <vector>
36 #include <queue>
37 
38 #include "Mutex.hxx"
39 
40 namespace CLAM {
41 
43 
49 {
50 public:
52  DYN_ATTRIBUTE (0, public, unsigned, NumControls);
53  DYN_CONTAINER_ATTRIBUTE (1, public, std::vector<TControlData>, MinValues, foo);
54  DYN_CONTAINER_ATTRIBUTE (2, public, std::vector<TControlData>, MaxValues, foo);
55 protected:
59  void DefaultInit(void)
60  {
61  AddAll();
62  UpdateData();
63  SetNumControls(0);
64  }
65 };
67 
77 class Controller : public Processing
78 {
79 public:
80  Controller ();
82  {
83  Configure(c);
84  }
85 
86  const char *GetClassName() const {return "Controller";}
87 
88  bool Do()
89  {
90  IdxList::iterator ListIt;
91 
92  Mutex::ScopedLock lock( mControllerDoMutex );
93 
94  IdxList qs = getQueues();
95 
96  if (!qs.empty())
97  {
98  for (ListIt=qs.begin();ListIt!=qs.end() ;ListIt++ )
99  {
100  TControlData val = PopControl( (*ListIt) );
101  OutControls[(*ListIt)].SendControl(val);
102  OutValues[(*ListIt)] = val;
103  }
104  }
105 
106 
107  return true;
108  }
109 
110  const ProcessingConfig& GetConfig() const;
111  virtual bool ConcreteConfigure(const ProcessingConfig&);
112 
113 
114 
116  void EnqueueControl(unsigned id, TControlData data);
117  TControlData LastDequeuedValue(unsigned id);
118 
120  std::vector<TControlData> OutValues;
121 private:
122  ControllerConfig mConfig;
123  // private methods
124  TControlData PopControl(unsigned id);
125  bool Empty(unsigned id);
126  // implementation details
127  typedef std::queue<TControlData> TQueue;
128  typedef std::list<int> IdxList;
129 
130  std::vector<TQueue> mDataQueues;
131 
132  void BufferQueueInit( int ncontrols );
133 
134  IdxList getQueues()
135  {
136  IdxList modifiedQs;
137  std::vector<TQueue>::iterator it;
138 
139  int k = 0;
140  for (it=mDataQueues.begin(); it != mDataQueues.end() ; it++ )
141  {
142  if (!(*it).empty())
143  {
144  modifiedQs.push_back(k);
145  }
146  k++;
147  }
148 
149  return modifiedQs;
150  }
151  Mutex mDataMutex;
152  Mutex mControllerDoMutex;
153 };
155 }; // namespace CLAM
156 
157 #endif // _Controller_
158