CLAM-Development
1.1
|
00001 /* 00002 * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 */ 00021 00022 // TokenDelay_example.cxx 00023 // example for the TokenDelay processing object 00024 00025 #ifndef _Controller_ 00026 #define _Controller_ 00027 00028 #include "OutControl.hxx" 00029 #include "OutControlArray.hxx" 00030 #include "Processing.hxx" 00031 #include "ProcessingData.hxx" 00032 00033 #include <typeinfo> // for bad_cast definition 00034 #include <string> 00035 #include <vector> 00036 #include <queue> 00037 00038 #include "Mutex.hxx" 00039 00040 namespace CLAM { 00041 00043 00048 class ControllerConfig : public ProcessingConfig 00049 { 00050 public: 00051 DYNAMIC_TYPE_USING_INTERFACE (ControllerConfig, 3, ProcessingConfig); 00052 DYN_ATTRIBUTE (0, public, unsigned, NumControls); 00053 DYN_CONTAINER_ATTRIBUTE (1, public, std::vector<TControlData>, MinValues, foo); 00054 DYN_CONTAINER_ATTRIBUTE (2, public, std::vector<TControlData>, MaxValues, foo); 00055 protected: 00059 void DefaultInit(void) 00060 { 00061 AddAll(); 00062 UpdateData(); 00063 SetNumControls(0); 00064 } 00065 }; 00067 00077 class Controller : public Processing 00078 { 00079 public: 00080 Controller (); 00081 Controller(const ControllerConfig& c) 00082 { 00083 Configure(c); 00084 } 00085 00086 const char *GetClassName() const {return "Controller";} 00087 00088 bool Do() 00089 { 00090 IdxList::iterator ListIt; 00091 00092 Mutex::ScopedLock lock( mControllerDoMutex ); 00093 00094 IdxList qs = getQueues(); 00095 00096 if (!qs.empty()) 00097 { 00098 for (ListIt=qs.begin();ListIt!=qs.end() ;ListIt++ ) 00099 { 00100 TControlData val = PopControl( (*ListIt) ); 00101 OutControls[(*ListIt)].SendControl(val); 00102 OutValues[(*ListIt)] = val; 00103 } 00104 } 00105 00106 00107 return true; 00108 } 00109 00110 const ProcessingConfig& GetConfig() const; 00111 virtual bool ConcreteConfigure(const ProcessingConfig&); 00112 00113 00114 00116 void EnqueueControl(unsigned id, TControlData data); 00117 TControlData LastDequeuedValue(unsigned id); 00118 00119 OutControlArray OutControls; 00120 std::vector<TControlData> OutValues; 00121 private: 00122 ControllerConfig mConfig; 00123 // private methods 00124 TControlData PopControl(unsigned id); 00125 bool Empty(unsigned id); 00126 // implementation details 00127 typedef std::queue<TControlData> TQueue; 00128 typedef std::list<int> IdxList; 00129 00130 std::vector<TQueue> mDataQueues; 00131 00132 void BufferQueueInit( int ncontrols ); 00133 00134 IdxList getQueues() 00135 { 00136 IdxList modifiedQs; 00137 std::vector<TQueue>::iterator it; 00138 00139 int k = 0; 00140 for (it=mDataQueues.begin(); it != mDataQueues.end() ; it++ ) 00141 { 00142 if (!(*it).empty()) 00143 { 00144 modifiedQs.push_back(k); 00145 } 00146 k++; 00147 } 00148 00149 return modifiedQs; 00150 } 00151 Mutex mDataMutex; 00152 Mutex mControllerDoMutex; 00153 }; 00155 }; // namespace CLAM 00156 00157 #endif // _Controller_ 00158