IT++ Logo
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 #include <itpp/itexports.h>
35 
36 namespace itpp
37 {
38 
39 // Forward declarations
40 template<class T> class Array;
41 template<class Num_T> class Mat;
42 template<class Num_T> class Vec;
43 
129 class ITPP_EXPORT Factory
130 {
131 public:
133  Factory() {}
135  virtual ~Factory() {}
136 };
137 
140 
141 
143 template<class T> inline
144 void create_elements(T* &ptr, int n, const Factory &)
145 {
146  void *p = operator new(sizeof(T) * n);
147  ptr = reinterpret_cast<T*>(p);
148  for (int i = 0; i < n; i++) {
149  new(ptr + i) T();
150  }
151 }
152 
153 
155 template<> inline
156 void create_elements<unsigned char>(unsigned char* &ptr, int n,
157  const Factory &)
158 {
159  void *p = operator new(sizeof(unsigned char) * n);
160  ptr = reinterpret_cast<unsigned char*>(p);
161 }
162 
164 template<> inline
165 void create_elements<bin>(bin* &ptr, int n, const Factory &)
166 {
167  void *p = operator new(sizeof(bin) * n);
168  ptr = reinterpret_cast<bin*>(p);
169 }
170 
172 template<> inline
173 void create_elements<short int>(short int* &ptr, int n, const Factory &)
174 {
175  void *p = operator new(sizeof(short int) * n);
176  ptr = reinterpret_cast<short int*>(p);
177 }
178 
180 template<> inline
181 void create_elements<int>(int* &ptr, int n, const Factory &)
182 {
183  void *p = operator new(sizeof(int) * n);
184  ptr = reinterpret_cast<int*>(p);
185 }
186 
188 template<> inline
189 void create_elements<double>(double* &ptr, int n, const Factory &)
190 {
191  void *p0 = operator new(sizeof(double) * n + 16);
192  void *p1 = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(p0) + 16)
193  & (~(std::size_t(15))));
194  *(reinterpret_cast<void**>(p1) - 1) = p0;
195  ptr = reinterpret_cast<double*>(p1);
196 }
197 
199 template<> inline
200 void create_elements<std::complex<double> >(std::complex<double>* &ptr,
201  int n, const Factory &)
202 {
203  void *p0 = operator new(sizeof(std::complex<double>) * n + 16);
204  void *p1 = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(p0) + 16)
205  & (~(std::size_t(15))));
206  *(reinterpret_cast<void**>(p1) - 1) = p0;
207  ptr = reinterpret_cast<std::complex<double>*>(p1);
208 }
209 
210 
211 
213 template<class T> inline
214 void destroy_elements(T* &ptr, int n)
215 {
216  if (ptr) {
217  for (int i = 0; i < n; ++i) {
218  ptr[i].~T();
219  }
220  void *p = reinterpret_cast<void*>(ptr);
221  operator delete(p);
222  ptr = 0;
223  }
224 }
225 
227 template<> inline
228 void destroy_elements<unsigned char>(unsigned char* &ptr, int)
229 {
230  if (ptr) {
231  void *p = reinterpret_cast<void*>(ptr);
232  operator delete(p);
233  ptr = 0;
234  }
235 }
236 
238 template<> inline
239 void destroy_elements<bin>(bin* &ptr, int)
240 {
241  if (ptr) {
242  void *p = reinterpret_cast<void*>(ptr);
243  operator delete(p);
244  ptr = 0;
245  }
246 }
248 template<> inline
249 void destroy_elements<short int>(short int* &ptr, int)
250 {
251  if (ptr) {
252  void *p = reinterpret_cast<void*>(ptr);
253  operator delete(p);
254  ptr = 0;
255  }
256 }
257 
259 template<> inline
260 void destroy_elements<int>(int* &ptr, int)
261 {
262  if (ptr) {
263  void *p = reinterpret_cast<void*>(ptr);
264  operator delete(p);
265  ptr = 0;
266  }
267 }
268 
270 template<> inline
271 void destroy_elements<double>(double* &ptr, int)
272 {
273  if (ptr) {
274  void *p = *(reinterpret_cast<void**>(ptr) - 1);
275  operator delete(p);
276  ptr = 0;
277  }
278 }
279 
281 template<> inline
282 void destroy_elements<std::complex<double> >(std::complex<double>* &ptr, int)
283 {
284  if (ptr) {
285  void *p = *(reinterpret_cast<void**>(ptr) - 1);
286  operator delete(p);
287  ptr = 0;
288  }
289 }
290 
291 
293 template<class T>
294 void create_elements(Array<T>* &ptr, int n, const Factory &f)
295 {
296  void *p = operator new(sizeof(Array<T>) * n);
297  ptr = reinterpret_cast<Array<T>*>(p);
298  for (int i = 0; i < n; ++i) {
299  new(ptr + i) Array<T>(f);
300  }
301 }
302 
304 template<class T>
305 void create_elements(Mat<T>* &ptr, int n, const Factory &f)
306 {
307  void *p = operator new(sizeof(Mat<T>) * n);
308  ptr = reinterpret_cast<Mat<T>*>(p);
309  for (int i = 0; i < n; ++i) {
310  new(ptr + i) Mat<T>(f);
311  }
312 }
313 
315 template<class T>
316 void create_elements(Vec<T>* &ptr, int n, const Factory &f)
317 {
318  void *p = operator new(sizeof(Vec<T>) * n);
319  ptr = reinterpret_cast<Vec<T>*>(p);
320  for (int i = 0; i < n; ++i) {
321  new(ptr + i) Vec<T>(f);
322  }
323 }
324 
325 } // namespace itpp
326 
327 #endif // #ifndef FACTORY_H
SourceForge Logo

Generated on Sat May 25 2013 16:32:18 for IT++ by Doxygen 1.8.2