|
CLAM-Development
1.3
|
00001 /* 00002 * Copyright (c) 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 #ifndef _ControlArray_hxx_ 00023 #define _ControlArray_hxx_ 00024 00025 #include "Assert.hxx" 00026 #include <vector> 00027 #include <list> 00028 #include <string> 00029 #include <sstream> 00030 00031 namespace CLAM 00032 { 00033 00034 class Processing; 00035 00040 template <class ControlT> 00041 class ControlArray 00042 { 00043 typedef std::vector<ControlT *> Controls; 00044 Controls mControls; 00045 00046 public: 00047 ControlArray() { } 00048 ControlArray(int size, const std::string &name, Processing* parent) { 00049 Resize(size,name,parent); 00050 } 00051 ControlArray(int size, const std::list<std::string> &names, 00052 Processing* parent) 00053 { 00054 Resize(size, names, parent); 00055 } 00056 template <typename ProcessingType, typename ValueType> 00057 ControlArray(int size, const std::string &name, ProcessingType * parent, 00058 void (ProcessingType::*method)(unsigned, ValueType) ) 00059 { 00060 Resize(size,name,parent,method); 00061 } 00062 ~ControlArray() { Clear(); } 00063 00064 ControlT &operator[](int i) { return *mControls[i]; } 00065 const ControlT &operator[](int i) const { return *mControls[i]; } 00066 00067 void Resize(int size, const std::string &name, Processing* parent); 00068 void Resize(int size, const std::list<std::string>& names, Processing* parent); 00069 template <typename ProcessingType, typename ValueType> 00070 void Resize(int size, const std::string &name, ProcessingType * parent, 00071 void (ProcessingType::*method)(unsigned, ValueType) ) 00072 { 00073 int previousSize = mControls.size(); 00074 if(size < previousSize) 00075 { 00076 Shrink(size); 00077 return; 00078 } 00079 mControls.resize(size); 00080 for (int i = previousSize; i<size; i++) { 00081 std::stringstream str; 00082 str << name << "_" << i; 00083 mControls[i] = new ControlT(i, str.str(), parent, method); 00084 } 00085 } 00086 00087 void Append(int size, const std::string &names, Processing* parent); 00088 void Append(int size, const std::list<std::string>& names, Processing* parent); 00089 00090 int Size() const { return mControls.size(); } 00091 00092 void Clear() { Shrink(0); } 00093 00094 protected: 00095 void Shrink(int size); 00096 }; 00097 00098 template <class ControlT> 00099 void ControlArray<ControlT>::Resize(int size, const std::string &name, 00100 Processing* parent) 00101 { 00102 int previousSize = mControls.size(); 00103 if(size < previousSize) 00104 { 00105 Shrink(size); 00106 return; 00107 } 00108 mControls.resize(size); 00109 for (int i = previousSize; i<size; i++) { 00110 std::stringstream str; 00111 str << name << "_" << i; 00112 mControls[i] = new ControlT(str.str(), parent); 00113 } 00114 00115 } 00116 00117 template <class ControlT> 00118 void ControlArray<ControlT>::Resize(int size, 00119 const std::list<std::string>& names, Processing* parent) 00120 { 00121 int previousSize = mControls.size(); 00122 if (size < previousSize) 00123 { 00124 Shrink(size); 00125 return; 00126 } 00127 CLAM_ASSERT(size - previousSize <= int(names.size()), 00128 "ControlArray::Resize: error, not enough labels provided"); 00129 mControls.resize(size); 00130 std::list<std::string>::const_iterator name = names.begin(); 00131 for (int i = previousSize; i<size; i++, name++) 00132 mControls[i] = new ControlT(*name, parent); 00133 } 00134 00135 template <class ControlT> 00136 void ControlArray<ControlT>::Append(int count, 00137 const std::string &name, Processing* parent) 00138 { 00139 Resize(Size() + count, name, parent); 00140 } 00141 00142 template <class ControlT> 00143 void ControlArray<ControlT>::Append(int count, 00144 const std::list<std::string>& names, Processing* parent) 00145 { 00146 Resize(Size() + count, names, parent); 00147 } 00148 00149 00150 template <class ControlT> 00151 void ControlArray<ControlT>::Shrink(int size) 00152 { 00153 int previousSize = mControls.size(); 00154 if (size == previousSize) return; 00155 CLAM_ASSERT(size < previousSize, 00156 "ControlArray::Cannot Shrink a ControlArray to a larger size"); 00157 for (int i = previousSize-1; i >= size; i--) 00158 delete mControls[i]; 00159 mControls.resize(size); 00160 } 00161 00162 }; //namespace CLAM 00163 00164 #endif 00165
1.7.6.1