CLAM-Development  1.4.0
ControlArray.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 #ifndef _ControlArray_hxx_
23 #define _ControlArray_hxx_
24 
25 #include "Assert.hxx"
26 #include <vector>
27 #include <list>
28 #include <string>
29 #include <sstream>
30 
31 namespace CLAM
32 {
33 
34 class Processing;
35 
40 template <class ControlT>
42 {
43  typedef std::vector<ControlT *> Controls;
44  Controls mControls;
45 
46 public:
48  ControlArray(int size, const std::string &name, Processing* parent) {
49  Resize(size,name,parent);
50  }
51  ControlArray(int size, const std::list<std::string> &names,
52  Processing* parent)
53  {
54  Resize(size, names, parent);
55  }
56  template <typename ProcessingType, typename ValueType>
57  ControlArray(int size, const std::string &name, ProcessingType * parent,
58  void (ProcessingType::*method)(unsigned, ValueType) )
59  {
60  Resize(size,name,parent,method);
61  }
63 
64  ControlT &operator[](int i) { return *mControls[i]; }
65  const ControlT &operator[](int i) const { return *mControls[i]; }
66 
67  void Resize(int size, const std::string &name, Processing* parent);
68  void Resize(int size, const std::list<std::string>& names, Processing* parent);
69  template <typename ProcessingType, typename ValueType>
70  void Resize(int size, const std::string &name, ProcessingType * parent,
71  void (ProcessingType::*method)(unsigned, ValueType) )
72  {
73  int previousSize = mControls.size();
74  if(size < previousSize)
75  {
76  Shrink(size);
77  return;
78  }
79  mControls.resize(size);
80  for (int i = previousSize; i<size; i++) {
81  std::stringstream str;
82  str << name << "_" << i;
83  mControls[i] = new ControlT(i, str.str(), parent, method);
84  }
85  }
86 
87  void Append(int size, const std::string &names, Processing* parent);
88  void Append(int size, const std::list<std::string>& names, Processing* parent);
89 
90  int Size() const { return mControls.size(); }
91 
92  void Clear() { Shrink(0); }
93 
94 protected:
95  void Shrink(int size);
96 };
97 
98 template <class ControlT>
99 void ControlArray<ControlT>::Resize(int size, const std::string &name,
100  Processing* parent)
101 {
102  int previousSize = mControls.size();
103  if(size < previousSize)
104  {
105  Shrink(size);
106  return;
107  }
108  mControls.resize(size);
109  for (int i = previousSize; i<size; i++) {
110  std::stringstream str;
111  str << name << "_" << i;
112  mControls[i] = new ControlT(str.str(), parent);
113  }
114 
115 }
116 
117 template <class ControlT>
119  const std::list<std::string>& names, Processing* parent)
120 {
121  int previousSize = mControls.size();
122  if (size < previousSize)
123  {
124  Shrink(size);
125  return;
126  }
127  CLAM_ASSERT(size - previousSize <= int(names.size()),
128  "ControlArray::Resize: error, not enough labels provided");
129  mControls.resize(size);
130  std::list<std::string>::const_iterator name = names.begin();
131  for (int i = previousSize; i<size; i++, name++)
132  mControls[i] = new ControlT(*name, parent);
133 }
134 
135 template <class ControlT>
137  const std::string &name, Processing* parent)
138 {
139  Resize(Size() + count, name, parent);
140 }
141 
142 template <class ControlT>
144  const std::list<std::string>& names, Processing* parent)
145 {
146  Resize(Size() + count, names, parent);
147 }
148 
149 
150 template <class ControlT>
152 {
153  int previousSize = mControls.size();
154  if (size == previousSize) return;
155  CLAM_ASSERT(size < previousSize,
156  "ControlArray::Cannot Shrink a ControlArray to a larger size");
157  for (int i = previousSize-1; i >= size; i--)
158  delete mControls[i];
159  mControls.resize(size);
160 }
161 
162 }; //namespace CLAM
163 
164 #endif
165