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     virtual ~Factory() {}
00132   };
00133 
00135   const Factory DEFAULT_FACTORY;
00136 
00137 
00139   template<class T>
00140   void create_elements(T* &ptr, int n, const Factory &)
00141   {
00142     void *p = operator new(sizeof(T) * n);
00143     ptr = reinterpret_cast<T*>(p);
00144     for (int i = 0; i < n; i++) {
00145       new (ptr + i) T();
00146     }
00147   }
00148 
00149 
00151   template<>
00152   void create_elements<unsigned char>(unsigned char* &ptr, int n,
00153               const Factory &);
00155   template<>
00156   void create_elements<bin>(bin* &ptr, int n, const Factory &);
00158   template<>
00159   void create_elements<short int>(short int* &ptr, int n, const Factory &);
00161   template<>
00162   void create_elements<int>(int* &ptr, int n, const Factory &);
00164   template<>
00165   void create_elements<double>(double* &ptr, int n, const Factory &);
00167   template<>
00168   void create_elements<std::complex<double> >(std::complex<double>* &ptr, int n, const Factory &);
00169 
00170 
00172   template<class T>
00173   void destroy_elements(T* &ptr, int n)
00174   {
00175     if (ptr) {
00176       for (int i = 0; i < n; ++i) {
00177   ptr[i].~T();
00178       }
00179       void *p = reinterpret_cast<void*>(ptr);
00180       operator delete(p);
00181       ptr = 0;
00182     }
00183   }
00184 
00186   template<>
00187   void destroy_elements<unsigned char>(unsigned char* &ptr, int n);
00189   template<>
00190   void destroy_elements<bin>(bin* &ptr, int n);
00192   template<>
00193   void destroy_elements<short int>(short int* &ptr, int n);
00195   template<>
00196   void destroy_elements<int>(int* &ptr, int n);
00198   template<>
00199   void destroy_elements<double>(double* &ptr, int n);
00201   template<>
00202   void destroy_elements<std::complex<double> >(std::complex<double>* &ptr,
00203                  int n);
00204 
00205 
00207   template<class T>
00208   void create_elements(Array<T>* &ptr, int n, const Factory &f)
00209   {
00210     void *p = operator new(sizeof(Array<T>) * n);
00211     ptr = reinterpret_cast<Array<T>*>(p);
00212     for (int i = 0; i < n; ++i) {
00213       new (ptr + i) Array<T>(f);
00214     }
00215   }
00216 
00218   template<class T>
00219   void create_elements(Mat<T>* &ptr, int n, const Factory &f)
00220   {
00221     void *p = operator new(sizeof(Mat<T>) * n);
00222     ptr = reinterpret_cast<Mat<T>*>(p);
00223     for (int i = 0; i < n; ++i) {
00224       new (ptr + i) Mat<T>(f);
00225     }
00226   }
00227 
00229   template<class T>
00230   void create_elements(Vec<T>* &ptr, int n, const Factory &f)
00231   {
00232     void *p = operator new(sizeof(Vec<T>) * n);
00233     ptr = reinterpret_cast<Vec<T>*>(p);
00234     for (int i = 0; i < n; ++i) {
00235       new (ptr + i) Vec<T>(f);
00236     }
00237   }
00238 
00239 } // namespace itpp
00240 
00241 #endif // #ifndef FACTORY_H
SourceForge Logo

Generated on Sun Dec 9 17:30:59 2007 for IT++ by Doxygen 1.5.4