IT++ Logo
factory.h
Go to the documentation of this file.
00001 
00029 #ifndef FACTORY_H
00030 #define FACTORY_H
00031 
00032 #include <complex>
00033 #include <itpp/base/binary.h>
00034 
00035 namespace itpp
00036 {
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 {
00130 public:
00132   Factory() {}
00134   virtual ~Factory() {}
00135 };
00136 
00138 const Factory DEFAULT_FACTORY;
00139 
00140 
00142 template<class T> inline
00143 void create_elements(T* &ptr, int n, const Factory &)
00144 {
00145   void *p = operator new(sizeof(T) * n);
00146   ptr = reinterpret_cast<T*>(p);
00147   for (int i = 0; i < n; i++) {
00148     new(ptr + i) T();
00149   }
00150 }
00151 
00152 
00154 template<> inline
00155 void create_elements<unsigned char>(unsigned char* &ptr, int n,
00156                                     const Factory &)
00157 {
00158   void *p = operator new(sizeof(unsigned char) * n);
00159   ptr = reinterpret_cast<unsigned char*>(p);
00160 }
00161 
00163 template<> inline
00164 void create_elements<bin>(bin* &ptr, int n, const Factory &)
00165 {
00166   void *p = operator new(sizeof(bin) * n);
00167   ptr = reinterpret_cast<bin*>(p);
00168 }
00169 
00171 template<> inline
00172 void create_elements<short int>(short int* &ptr, int n, const Factory &)
00173 {
00174   void *p = operator new(sizeof(short int) * n);
00175   ptr = reinterpret_cast<short int*>(p);
00176 }
00177 
00179 template<> inline
00180 void create_elements<int>(int* &ptr, int n, const Factory &)
00181 {
00182   void *p = operator new(sizeof(int) * n);
00183   ptr = reinterpret_cast<int*>(p);
00184 }
00185 
00187 template<> inline
00188 void create_elements<double>(double* &ptr, int n, const Factory &)
00189 {
00190   void *p0 = operator new(sizeof(double) * n + 16);
00191   void *p1 = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(p0) + 16)
00192                                      & (~(std::size_t(15))));
00193   *(reinterpret_cast<void**>(p1) - 1) = p0;
00194   ptr = reinterpret_cast<double*>(p1);
00195 }
00196 
00198 template<> inline
00199 void create_elements<std::complex<double> >(std::complex<double>* &ptr,
00200     int n, const Factory &)
00201 {
00202   void *p0 = operator new(sizeof(std::complex<double>) * n + 16);
00203   void *p1 = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(p0) + 16)
00204                                      & (~(std::size_t(15))));
00205   *(reinterpret_cast<void**>(p1) - 1) = p0;
00206   ptr = reinterpret_cast<std::complex<double>*>(p1);
00207 }
00208 
00209 
00210 
00212 template<class T> inline
00213 void destroy_elements(T* &ptr, int n)
00214 {
00215   if (ptr) {
00216     for (int i = 0; i < n; ++i) {
00217       ptr[i].~T();
00218     }
00219     void *p = reinterpret_cast<void*>(ptr);
00220     operator delete(p);
00221     ptr = 0;
00222   }
00223 }
00224 
00226 template<> inline
00227 void destroy_elements<unsigned char>(unsigned char* &ptr, int)
00228 {
00229   if (ptr) {
00230     void *p = reinterpret_cast<void*>(ptr);
00231     operator delete(p);
00232         ptr = 0;
00233   }
00234 }
00235 
00237 template<> inline
00238 void destroy_elements<bin>(bin* &ptr, int)
00239 {
00240   if (ptr) {
00241     void *p = reinterpret_cast<void*>(ptr);
00242     operator delete(p);
00243     ptr = 0;
00244   }
00245 }
00247 template<> inline
00248 void destroy_elements<short int>(short int* &ptr, int)
00249 {
00250   if (ptr) {
00251     void *p = reinterpret_cast<void*>(ptr);
00252     operator delete(p);
00253     ptr = 0;
00254   }
00255 }
00256 
00258 template<> inline
00259 void destroy_elements<int>(int* &ptr, int)
00260 {
00261   if (ptr) {
00262     void *p = reinterpret_cast<void*>(ptr);
00263     operator delete(p);
00264     ptr = 0;
00265   }
00266 }
00267 
00269 template<> inline
00270 void destroy_elements<double>(double* &ptr, int)
00271 {
00272   if (ptr) {
00273     void *p = *(reinterpret_cast<void**>(ptr) - 1);
00274     operator delete(p);
00275     ptr = 0;
00276   }
00277 }
00278 
00280 template<> inline
00281 void destroy_elements<std::complex<double> >(std::complex<double>* &ptr, int)
00282 {
00283   if (ptr) {
00284     void *p = *(reinterpret_cast<void**>(ptr) - 1);
00285     operator delete(p);
00286     ptr = 0;
00287   }
00288 }
00289 
00290 
00292 template<class T>
00293 void create_elements(Array<T>* &ptr, int n, const Factory &f)
00294 {
00295   void *p = operator new(sizeof(Array<T>) * n);
00296   ptr = reinterpret_cast<Array<T>*>(p);
00297   for (int i = 0; i < n; ++i) {
00298     new(ptr + i) Array<T>(f);
00299   }
00300 }
00301 
00303 template<class T>
00304 void create_elements(Mat<T>* &ptr, int n, const Factory &f)
00305 {
00306   void *p = operator new(sizeof(Mat<T>) * n);
00307   ptr = reinterpret_cast<Mat<T>*>(p);
00308   for (int i = 0; i < n; ++i) {
00309     new(ptr + i) Mat<T>(f);
00310   }
00311 }
00312 
00314 template<class T>
00315 void create_elements(Vec<T>* &ptr, int n, const Factory &f)
00316 {
00317   void *p = operator new(sizeof(Vec<T>) * n);
00318   ptr = reinterpret_cast<Vec<T>*>(p);
00319   for (int i = 0; i < n; ++i) {
00320     new(ptr + i) Vec<T>(f);
00321   }
00322 }
00323 
00324 } // namespace itpp
00325 
00326 #endif // #ifndef FACTORY_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SourceForge Logo

Generated on Sat Jul 9 2011 15:21:29 for IT++ by Doxygen 1.7.4