InControlTmplArray.hxx

Go to the documentation of this file.
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 _InControlTmplArray_hxx_
00023 #define _InControlTmplArray_hxx_
00024 
00025 #include "InControl.hxx"
00026 #include <vector>
00027 
00028 namespace CLAM
00029 {
00033 template <class TProcessing>
00034 class InControlTmplArray
00035 {
00036         typedef InControlTmpl<TProcessing> TInControl;
00037         typedef typename TInControl::TPtrMemberFuncId TPtrMemberFuncId;
00038         typedef std::vector<TInControl*> Controls;
00039         Controls mControls;
00040 
00041 public:
00042         InControlTmplArray(int size, const std::string &name, TProcessing* parent,      TPtrMemberFuncId f);
00045         InControlTmplArray(int size, const std::list<std::string>& names, TProcessing* parent, TPtrMemberFuncId f);
00046         /* This overload could be substituted by giving the size parameter a default value but this would       
00047          * not be backward compatible*/
00048         InControlTmplArray();
00049 
00050         ~InControlTmplArray();
00051 
00052         TInControl& operator[](int i) { return *mControls[i]; }
00053         const TInControl& operator[](int i) const { return *mControls[i]; }
00054 
00055         void Resize(int size, const std::string& name, TProcessing* parent, TPtrMemberFuncId f);
00056         void Resize(int size, const std::list<std::string>& names, TProcessing* parent, TPtrMemberFuncId f);
00057 
00058         int Size() const {return mControls.size();}
00059 
00060 protected:
00061         void Shrink(int size);
00062 
00063 };
00064 
00065 //------------------------------------------------------------------------------
00066 // Implementation
00067 template <class TProcessing>
00068 InControlTmplArray<TProcessing>::InControlTmplArray(
00069                 int size, 
00070                 const std::string &name,
00071                 TProcessing *parent, 
00072                 TPtrMemberFuncId f)
00073 {
00074         CLAM_ASSERT(parent, "InControlTmplArray must be published. Check ctr processing* parameter");
00075         Resize(size,name,parent,f);
00076 }
00077 
00078 template <class TProcessing>
00079 InControlTmplArray<TProcessing>::InControlTmplArray(
00080                 int size, 
00081                 const std::list<std::string>& names,
00082                 TProcessing *parent, 
00083                 TPtrMemberFuncId f)
00084 {
00085         CLAM_ASSERT(parent, "InControlTmplArray must be published. Check ctr processing* parameter");
00086         Resize(size, names, parent,f);
00087 }
00088 
00089 template <class TProcessing>
00090 InControlTmplArray<TProcessing>::InControlTmplArray()
00091 {
00092         mControls.resize(0);
00093 }
00094 
00095 template <class TProcessing>
00096 void InControlTmplArray<TProcessing>::Resize(int size, const std::string& name, TProcessing* parent, TPtrMemberFuncId f)
00097 {
00098         int previousSize = mControls.size();
00099         if(size < previousSize) 
00100         {
00101                 Shrink(size);
00102                 return;
00103         }
00104         
00105         mControls.resize(size);
00106         for (int i = previousSize; i<size; i++) {
00107                 std::stringstream str;
00108                 str << name << "_" << i;
00109                 mControls[i] = new TInControl(i, str.str(), parent, f); 
00110         }
00111 }
00112 
00113 template <class TProcessing>
00114 void InControlTmplArray<TProcessing>::Resize(int size, const std::list<std::string>& names , TProcessing* parent, TPtrMemberFuncId f)
00115 {
00116         int previousSize = mControls.size();
00117         if(size < previousSize) 
00118         {
00119                 Shrink(size);
00120                 return;
00121         }
00122         
00123         CLAM_ASSERT(size-previousSize <= names.size(), "InControlTmplArray::Resize not enoough labels are given");
00124         mControls.resize(size);
00125         std::list<std::string>::iterator name = names.begin();
00126         for (int i = previousSize; i<size; i++) {
00127                 mControls[i] = new TInControl(i, *name, parent, f);     
00128         }
00129 }
00130 
00131 template <class TProcessing>
00132 void InControlTmplArray<TProcessing>::Shrink(int size)
00133 {
00134         int previousSize = mControls.size();
00135         CLAM_ASSERT(size < previousSize, "InControlArray::Cannot Shrink a Control Array to a larger size");
00136         for (int i = previousSize-1; i >= size; i--) {
00137                 delete mControls[i];    
00138         }
00139         mControls.resize(size);
00140 }
00141 
00142 
00143 template <class TProcessing>
00144 InControlTmplArray<TProcessing>::~InControlTmplArray()
00145 {
00146         Shrink(0);
00147 }
00148 
00149 } //namespace CLAM
00150 
00151 #endif
00152 

Generated on Tue Aug 12 22:33:43 2008 for CLAM by  doxygen 1.5.5