IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
factory.h
Go to the documentation of this file.
1 
29 #ifndef FACTORY_H
30 #define FACTORY_H
31 
32 #include <complex>
33 #include <itpp/base/binary.h>
34 
35 namespace itpp
36 {
37 
38 // Forward declarations
39 template<class T> class Array;
40 template<class Num_T> class Mat;
41 template<class Num_T> class Vec;
42 
128 class Factory
129 {
130 public:
132  Factory() {}
134  virtual ~Factory() {}
135 };
136 
139 
140 
142 template<class T> inline
143 void create_elements(T* &ptr, int n, const Factory &)
144 {
145  void *p = operator new(sizeof(T) * n);
146  ptr = reinterpret_cast<T*>(p);
147  for (int i = 0; i < n; i++) {
148  new(ptr + i) T();
149  }
150 }
151 
152 
154 template<> inline
155 void create_elements<unsigned char>(unsigned char* &ptr, int n,
156  const Factory &)
157 {
158  void *p = operator new(sizeof(unsigned char) * n);
159  ptr = reinterpret_cast<unsigned char*>(p);
160 }
161 
163 template<> inline
164 void create_elements<bin>(bin* &ptr, int n, const Factory &)
165 {
166  void *p = operator new(sizeof(bin) * n);
167  ptr = reinterpret_cast<bin*>(p);
168 }
169 
171 template<> inline
172 void create_elements<short int>(short int* &ptr, int n, const Factory &)
173 {
174  void *p = operator new(sizeof(short int) * n);
175  ptr = reinterpret_cast<short int*>(p);
176 }
177 
179 template<> inline
180 void create_elements<int>(int* &ptr, int n, const Factory &)
181 {
182  void *p = operator new(sizeof(int) * n);
183  ptr = reinterpret_cast<int*>(p);
184 }
185 
187 template<> inline
188 void create_elements<double>(double* &ptr, int n, const Factory &)
189 {
190  void *p0 = operator new(sizeof(double) * n + 16);
191  void *p1 = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(p0) + 16)
192  & (~(std::size_t(15))));
193  *(reinterpret_cast<void**>(p1) - 1) = p0;
194  ptr = reinterpret_cast<double*>(p1);
195 }
196 
198 template<> inline
199 void create_elements<std::complex<double> >(std::complex<double>* &ptr,
200  int n, const Factory &)
201 {
202  void *p0 = operator new(sizeof(std::complex<double>) * n + 16);
203  void *p1 = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(p0) + 16)
204  & (~(std::size_t(15))));
205  *(reinterpret_cast<void**>(p1) - 1) = p0;
206  ptr = reinterpret_cast<std::complex<double>*>(p1);
207 }
208 
209 
210 
212 template<class T> inline
213 void destroy_elements(T* &ptr, int n)
214 {
215  if (ptr) {
216  for (int i = 0; i < n; ++i) {
217  ptr[i].~T();
218  }
219  void *p = reinterpret_cast<void*>(ptr);
220  operator delete(p);
221  ptr = 0;
222  }
223 }
224 
226 template<> inline
227 void destroy_elements<unsigned char>(unsigned char* &ptr, int)
228 {
229  if (ptr) {
230  void *p = reinterpret_cast<void*>(ptr);
231  operator delete(p);
232  ptr = 0;
233  }
234 }
235 
237 template<> inline
238 void destroy_elements<bin>(bin* &ptr, int)
239 {
240  if (ptr) {
241  void *p = reinterpret_cast<void*>(ptr);
242  operator delete(p);
243  ptr = 0;
244  }
245 }
247 template<> inline
248 void destroy_elements<short int>(short int* &ptr, int)
249 {
250  if (ptr) {
251  void *p = reinterpret_cast<void*>(ptr);
252  operator delete(p);
253  ptr = 0;
254  }
255 }
256 
258 template<> inline
259 void destroy_elements<int>(int* &ptr, int)
260 {
261  if (ptr) {
262  void *p = reinterpret_cast<void*>(ptr);
263  operator delete(p);
264  ptr = 0;
265  }
266 }
267 
269 template<> inline
270 void destroy_elements<double>(double* &ptr, int)
271 {
272  if (ptr) {
273  void *p = *(reinterpret_cast<void**>(ptr) - 1);
274  operator delete(p);
275  ptr = 0;
276  }
277 }
278 
280 template<> inline
281 void destroy_elements<std::complex<double> >(std::complex<double>* &ptr, int)
282 {
283  if (ptr) {
284  void *p = *(reinterpret_cast<void**>(ptr) - 1);
285  operator delete(p);
286  ptr = 0;
287  }
288 }
289 
290 
292 template<class T>
293 void create_elements(Array<T>* &ptr, int n, const Factory &f)
294 {
295  void *p = operator new(sizeof(Array<T>) * n);
296  ptr = reinterpret_cast<Array<T>*>(p);
297  for (int i = 0; i < n; ++i) {
298  new(ptr + i) Array<T>(f);
299  }
300 }
301 
303 template<class T>
304 void create_elements(Mat<T>* &ptr, int n, const Factory &f)
305 {
306  void *p = operator new(sizeof(Mat<T>) * n);
307  ptr = reinterpret_cast<Mat<T>*>(p);
308  for (int i = 0; i < n; ++i) {
309  new(ptr + i) Mat<T>(f);
310  }
311 }
312 
314 template<class T>
315 void create_elements(Vec<T>* &ptr, int n, const Factory &f)
316 {
317  void *p = operator new(sizeof(Vec<T>) * n);
318  ptr = reinterpret_cast<Vec<T>*>(p);
319  for (int i = 0; i < n; ++i) {
320  new(ptr + i) Vec<T>(f);
321  }
322 }
323 
324 } // namespace itpp
325 
326 #endif // #ifndef FACTORY_H
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:12 for IT++ by Doxygen 1.8.1.2