00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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() { mControls.resize(0); }
00048 ControlArray(int size, const std::string &name, Processing* parent) {
00049 mControls.resize(0);
00050 Resize(size,name,parent);
00051 }
00052 ControlArray(int size, const std::list<std::string> &names,
00053 Processing* parent)
00054 {
00055 mControls.resize(0);
00056 Resize(size, names, parent);
00057 }
00058 ~ControlArray() { Clear(); }
00059
00060 ControlT &operator[](int i) { return *mControls[i]; }
00061 const ControlT &operator[](int i) const { return *mControls[i]; }
00062
00063 void Resize(int size, const std::string &name, Processing* parent);
00064 void Resize(int size, const std::list<std::string>& names, Processing* parent);
00065
00066 void Append(int size, const std::string &names, Processing* parent);
00067 void Append(int size, const std::list<std::string>& names, Processing* parent);
00068
00069 int Size() const { return mControls.size(); }
00070
00071 void Clear() { Shrink(0); }
00072
00073 protected:
00074 void Shrink(int size);
00075 };
00076
00077 template <class ControlT>
00078 void ControlArray<ControlT>::Resize(int size, const std::string &name,
00079 Processing* parent)
00080 {
00081 int previousSize = mControls.size();
00082 if(size < previousSize)
00083 {
00084 Shrink(size);
00085 return;
00086 }
00087 mControls.resize(size);
00088 for (int i = previousSize; i<size; i++) {
00089 std::stringstream str;
00090 str << name << "_" << i;
00091 mControls[i] = new ControlT(str.str(), parent);
00092 }
00093
00094 }
00095
00096 template <class ControlT>
00097 void ControlArray<ControlT>::Resize(int size,
00098 const std::list<std::string>& names, Processing* parent)
00099 {
00100 int previousSize = mControls.size();
00101 if (size < previousSize)
00102 {
00103 Shrink(size);
00104 return;
00105 }
00106 CLAM_ASSERT(size - previousSize <= int(names.size()),
00107 "ControlArray::Resize: error, not enough labels provided");
00108 mControls.resize(size);
00109 std::list<std::string>::const_iterator name = names.begin();
00110 for (int i = previousSize; i<size; i++, name++)
00111 mControls[i] = new ControlT(*name, parent);
00112 }
00113
00114 template <class ControlT>
00115 void ControlArray<ControlT>::Append(int count,
00116 const std::string &name, Processing* parent)
00117 {
00118 Resize(Size() + count, name, parent);
00119 }
00120
00121 template <class ControlT>
00122 void ControlArray<ControlT>::Append(int count,
00123 const std::list<std::string>& names, Processing* parent)
00124 {
00125 Resize(Size() + count, names, parent);
00126 }
00127
00128
00129 template <class ControlT>
00130 void ControlArray<ControlT>::Shrink(int size)
00131 {
00132 int previousSize = mControls.size();
00133 if (size == previousSize) return;
00134 CLAM_ASSERT(size < previousSize,
00135 "ControlArray::Cannot Shrink a ControlArray to a larger size");
00136 for (int i = previousSize-1; i >= size; i--)
00137 delete mControls[i];
00138 mControls.resize(size);
00139 }
00140
00141 };
00142
00143 #endif
00144