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>
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<>
00154   void create_elements<unsigned char>(unsigned char* &ptr, int n,
00155                                       const Factory &);
00157   template<>
00158   void create_elements<bin>(bin* &ptr, int n, const Factory &);
00160   template<>
00161   void create_elements<short int>(short int* &ptr, int n, const Factory &);
00163   template<>
00164   void create_elements<int>(int* &ptr, int n, const Factory &);
00166   template<>
00167   void create_elements<double>(double* &ptr, int n, const Factory &);
00169   template<>
00170   void create_elements<std::complex<double> >(std::complex<double>* &ptr, int n, const Factory &);
00171 
00172 
00174   template<class T>
00175   void destroy_elements(T* &ptr, int n)
00176   {
00177     if (ptr) {
00178       for (int i = 0; i < n; ++i) {
00179         ptr[i].~T();
00180       }
00181       void *p = reinterpret_cast<void*>(ptr);
00182       operator delete(p);
00183       ptr = 0;
00184     }
00185   }
00186 
00188   template<>
00189   void destroy_elements<unsigned char>(unsigned char* &ptr, int n);
00191   template<>
00192   void destroy_elements<bin>(bin* &ptr, int n);
00194   template<>
00195   void destroy_elements<short int>(short int* &ptr, int n);
00197   template<>
00198   void destroy_elements<int>(int* &ptr, int n);
00200   template<>
00201   void destroy_elements<double>(double* &ptr, int n);
00203   template<>
00204   void destroy_elements<std::complex<double> >(std::complex<double>* &ptr,
00205                                                int n);
00206 
00207 
00209   template<class T>
00210   void create_elements(Array<T>* &ptr, int n, const Factory &f)
00211   {
00212     void *p = operator new(sizeof(Array<T>) * n);
00213     ptr = reinterpret_cast<Array<T>*>(p);
00214     for (int i = 0; i < n; ++i) {
00215       new (ptr + i) Array<T>(f);
00216     }
00217   }
00218 
00220   template<class T>
00221   void create_elements(Mat<T>* &ptr, int n, const Factory &f)
00222   {
00223     void *p = operator new(sizeof(Mat<T>) * n);
00224     ptr = reinterpret_cast<Mat<T>*>(p);
00225     for (int i = 0; i < n; ++i) {
00226       new (ptr + i) Mat<T>(f);
00227     }
00228   }
00229 
00231   template<class T>
00232   void create_elements(Vec<T>* &ptr, int n, const Factory &f)
00233   {
00234     void *p = operator new(sizeof(Vec<T>) * n);
00235     ptr = reinterpret_cast<Vec<T>*>(p);
00236     for (int i = 0; i < n; ++i) {
00237       new (ptr + i) Vec<T>(f);
00238     }
00239   }
00240 
00241 } // namespace itpp
00242 
00243 #endif // #ifndef FACTORY_H
SourceForge Logo

Generated on Thu Apr 24 13:38:58 2008 for IT++ by Doxygen 1.5.5