IT++ Logo

factory.h

Go to the documentation of this file.
00001 
00030 #ifndef FACTORY_H
00031 #define FACTORY_H
00032 
00033 #include <complex>
00034 #include <itpp/base/binary.h>
00035 
00036 namespace itpp {
00037 
00038   // Forward declarations
00039   template<class T> class Array;
00040   template<class Num_T> class Mat;
00041   template<class Num_T> class Vec;
00042 
00128   class Factory {
00129   public:
00131     Factory() {}
00133     virtual ~Factory() {}
00134   };
00135 
00137   const Factory DEFAULT_FACTORY;
00138 
00139 
00141   template<class T> inline
00142   void create_elements(T* &ptr, int n, const Factory &)
00143   {
00144     void *p = operator new(sizeof(T) * n);
00145     ptr = reinterpret_cast<T*>(p);
00146     for (int i = 0; i < n; i++) {
00147       new (ptr + i) T();
00148     }
00149   }
00150 
00151 
00153   template<> inline
00154   void create_elements<unsigned char>(unsigned char* &ptr, int n,
00155                                       const Factory &)
00156   {
00157     void *p = operator new(sizeof(unsigned char) * n);
00158     ptr = reinterpret_cast<unsigned char*>(p);
00159   }
00160 
00162   template<> inline
00163   void create_elements<bin>(bin* &ptr, int n, const Factory &)
00164   {
00165     void *p = operator new(sizeof(bin) * n);
00166     ptr = reinterpret_cast<bin*>(p);
00167   }
00168 
00170   template<> inline
00171   void create_elements<short int>(short int* &ptr, int n, const Factory &)
00172   {
00173     void *p = operator new(sizeof(short int) * n);
00174     ptr = reinterpret_cast<short int*>(p);
00175   }
00176 
00178   template<> inline
00179   void create_elements<int>(int* &ptr, int n, const Factory &)
00180   {
00181     void *p = operator new(sizeof(int) * n);
00182     ptr = reinterpret_cast<int*>(p);
00183   }
00184 
00186   template<> inline
00187   void create_elements<double>(double* &ptr, int n, const Factory &)
00188   {
00189     void *p0 = operator new(sizeof(double) * n + 16);
00190     void *p1 = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(p0) + 16)
00191                                        & (~(std::size_t(15))));
00192     *(reinterpret_cast<void**>(p1) - 1) = p0;
00193     ptr = reinterpret_cast<double*>(p1);
00194   }
00195 
00197   template<> inline
00198   void create_elements<std::complex<double> >(std::complex<double>* &ptr,
00199                                               int n, const Factory &)
00200   {
00201     void *p0 = operator new(sizeof(std::complex<double>) * n + 16);
00202     void *p1 = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(p0) + 16)
00203                                        & (~(std::size_t(15))));
00204     *(reinterpret_cast<void**>(p1) - 1) = p0;
00205     ptr = reinterpret_cast<std::complex<double>*>(p1);
00206   }
00207 
00208 
00209 
00211   template<class T> inline
00212   void destroy_elements(T* &ptr, int n)
00213   {
00214     if (ptr) {
00215       for (int i = 0; i < n; ++i) {
00216         ptr[i].~T();
00217       }
00218       void *p = reinterpret_cast<void*>(ptr);
00219       operator delete(p);
00220       ptr = 0;
00221     }
00222   }
00223 
00225   template<> inline
00226   void destroy_elements<unsigned char>(unsigned char* &ptr, int n)
00227   {
00228     if (ptr) {
00229       void *p = reinterpret_cast<void*>(ptr);
00230       operator delete(p);
00231     }
00232   }
00233 
00235   template<> inline
00236   void destroy_elements<bin>(bin* &ptr, int n)
00237   {
00238     if (ptr) {
00239       void *p = reinterpret_cast<void*>(ptr);
00240       operator delete(p);
00241       ptr = 0;
00242     }
00243   }
00245   template<> inline
00246   void destroy_elements<short int>(short int* &ptr, int n)
00247   {
00248     if (ptr) {
00249       void *p = reinterpret_cast<void*>(ptr);
00250       operator delete(p);
00251       ptr = 0;
00252     }
00253   }
00254 
00256   template<> inline
00257   void destroy_elements<int>(int* &ptr, int n)
00258   {
00259     if (ptr) {
00260       void *p = reinterpret_cast<void*>(ptr);
00261       operator delete(p);
00262       ptr = 0;
00263     }
00264   }
00265 
00267   template<> inline
00268   void destroy_elements<double>(double* &ptr, int n)
00269   {
00270     if (ptr) {
00271       void *p = *(reinterpret_cast<void**>(ptr) - 1);
00272       operator delete(p);
00273       ptr = 0;
00274     }
00275   }
00276 
00278   template<> inline
00279   void destroy_elements<std::complex<double> >(std::complex<double>* &ptr,
00280                                                int n)
00281   {
00282     if (ptr) {
00283       void *p = *(reinterpret_cast<void**>(ptr) - 1);
00284       operator delete(p);
00285       ptr = 0;
00286     }
00287   }
00288 
00289 
00291   template<class T>
00292   void create_elements(Array<T>* &ptr, int n, const Factory &f)
00293   {
00294     void *p = operator new(sizeof(Array<T>) * n);
00295     ptr = reinterpret_cast<Array<T>*>(p);
00296     for (int i = 0; i < n; ++i) {
00297       new (ptr + i) Array<T>(f);
00298     }
00299   }
00300 
00302   template<class T>
00303   void create_elements(Mat<T>* &ptr, int n, const Factory &f)
00304   {
00305     void *p = operator new(sizeof(Mat<T>) * n);
00306     ptr = reinterpret_cast<Mat<T>*>(p);
00307     for (int i = 0; i < n; ++i) {
00308       new (ptr + i) Mat<T>(f);
00309     }
00310   }
00311 
00313   template<class T>
00314   void create_elements(Vec<T>* &ptr, int n, const Factory &f)
00315   {
00316     void *p = operator new(sizeof(Vec<T>) * n);
00317     ptr = reinterpret_cast<Vec<T>*>(p);
00318     for (int i = 0; i < n; ++i) {
00319       new (ptr + i) Vec<T>(f);
00320     }
00321   }
00322 
00323 } // namespace itpp
00324 
00325 #endif // #ifndef FACTORY_H
SourceForge Logo

Generated on Sat May 3 16:10:40 2008 for IT++ by Doxygen 1.5.5