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
36namespace itpp
37{
38
39// Forward declarations
40template<class T> class Array;
41template<class Num_T> class Mat;
42template<class Num_T> class Vec;
43
129class ITPP_EXPORT Factory
130{
131public:
135 virtual ~Factory() {}
136};
137
140
141
143template<class T> inline
144void 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
155template<> inline
156void 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
164template<> inline
165void 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
172template<> inline
173void 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
180template<> inline
181void 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
188template<> inline
189void 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
199template<> inline
200void 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
213template<class T> inline
214void 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
227template<> inline
228void 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
238template<> inline
240{
241 if (ptr) {
242 void *p = reinterpret_cast<void*>(ptr);
243 operator delete(p);
244 ptr = 0;
245 }
246}
248template<> inline
249void 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
259template<> inline
260void 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
270template<> inline
271void 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
281template<> inline
282void 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
293template<class T>
294void 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
304template<class T>
305void 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
315template<class T>
316void 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
Binary class definition.
General array class.
Definition: array.h:105
Base class for class factories.
Definition: factory.h:130
Factory()
Default constructor.
Definition: factory.h:133
virtual ~Factory()
Destructor.
Definition: factory.h:135
Binary arithmetic (boolean) class.
Definition: binary.h:57
itpp namespace
Definition: itmex.h:37
void destroy_elements< int >(int *&ptr, int)
Specialization for integer data arrays.
Definition: factory.h:260
void create_elements< int >(int *&ptr, int n, const Factory &)
Specialization for integer data arrays.
Definition: factory.h:181
void destroy_elements< bin >(bin *&ptr, int)
Specialization for binary data arrays.
Definition: factory.h:239
void create_elements< short int >(short int *&ptr, int n, const Factory &)
Specialization for short integer data arrays.
Definition: factory.h:173
void create_elements< bin >(bin *&ptr, int n, const Factory &)
Specialization for binary data arrays.
Definition: factory.h:165
void create_elements< unsigned char >(unsigned char *&ptr, int n, const Factory &)
Specialization for unsigned char data arrays (used in GF2Mat)
Definition: factory.h:156
void destroy_elements< unsigned char >(unsigned char *&ptr, int)
Specialization for unsigned char data arrays (used in GF2Mat)
Definition: factory.h:228
void destroy_elements< short int >(short int *&ptr, int)
Specialization for short integer data arrays.
Definition: factory.h:249
void destroy_elements(T *&ptr, int n)
Destroy an array of Array, Vec or Mat elements.
Definition: factory.h:214
void destroy_elements< double >(double *&ptr, int)
Specialisation for 16-byte aligned double data arrays.
Definition: factory.h:271
void create_elements< double >(double *&ptr, int n, const Factory &)
Specialization for 16-byte aligned double data arrays.
Definition: factory.h:189
const Factory DEFAULT_FACTORY
Default (dummy) factory.
Definition: factory.h:139
void create_elements(T *&ptr, int n, const Factory &)
Create an n-length array of T to be used as Array, Vec or Mat elements.
Definition: factory.h:144

Generated on Tue Aug 17 2021 10:59:15 for IT++ by Doxygen 1.9.4