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 00039 // Forward declarations 00040 template<class T> class Array; 00041 template<class Num_T> class Mat; 00042 template<class Num_T> class Vec; 00043 00129 class Factory 00130 { 00131 public: 00133 Factory() {} 00135 virtual ~Factory() {} 00136 }; 00137 00139 const Factory DEFAULT_FACTORY; 00140 00141 00143 template<class T> inline 00144 void create_elements(T* &ptr, int n, const Factory &) 00145 { 00146 void *p = operator new(sizeof(T) * n); 00147 ptr = reinterpret_cast<T*>(p); 00148 for (int i = 0; i < n; i++) { 00149 new(ptr + i) T(); 00150 } 00151 } 00152 00153 00155 template<> inline 00156 void create_elements<unsigned char>(unsigned char* &ptr, int n, 00157 const Factory &) 00158 { 00159 void *p = operator new(sizeof(unsigned char) * n); 00160 ptr = reinterpret_cast<unsigned char*>(p); 00161 } 00162 00164 template<> inline 00165 void create_elements<bin>(bin* &ptr, int n, const Factory &) 00166 { 00167 void *p = operator new(sizeof(bin) * n); 00168 ptr = reinterpret_cast<bin*>(p); 00169 } 00170 00172 template<> inline 00173 void create_elements<short int>(short int* &ptr, int n, const Factory &) 00174 { 00175 void *p = operator new(sizeof(short int) * n); 00176 ptr = reinterpret_cast<short int*>(p); 00177 } 00178 00180 template<> inline 00181 void create_elements<int>(int* &ptr, int n, const Factory &) 00182 { 00183 void *p = operator new(sizeof(int) * n); 00184 ptr = reinterpret_cast<int*>(p); 00185 } 00186 00188 template<> inline 00189 void create_elements<double>(double* &ptr, int n, const Factory &) 00190 { 00191 void *p0 = operator new(sizeof(double) * n + 16); 00192 void *p1 = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(p0) + 16) 00193 & (~(std::size_t(15)))); 00194 *(reinterpret_cast<void**>(p1) - 1) = p0; 00195 ptr = reinterpret_cast<double*>(p1); 00196 } 00197 00199 template<> inline 00200 void create_elements<std::complex<double> >(std::complex<double>* &ptr, 00201 int n, const Factory &) 00202 { 00203 void *p0 = operator new(sizeof(std::complex<double>) * n + 16); 00204 void *p1 = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(p0) + 16) 00205 & (~(std::size_t(15)))); 00206 *(reinterpret_cast<void**>(p1) - 1) = p0; 00207 ptr = reinterpret_cast<std::complex<double>*>(p1); 00208 } 00209 00210 00211 00213 template<class T> inline 00214 void destroy_elements(T* &ptr, int n) 00215 { 00216 if (ptr) { 00217 for (int i = 0; i < n; ++i) { 00218 ptr[i].~T(); 00219 } 00220 void *p = reinterpret_cast<void*>(ptr); 00221 operator delete(p); 00222 ptr = 0; 00223 } 00224 } 00225 00227 template<> inline 00228 void destroy_elements<unsigned char>(unsigned char* &ptr, int) 00229 { 00230 if (ptr) { 00231 void *p = reinterpret_cast<void*>(ptr); 00232 operator delete(p); 00233 ptr = 0; 00234 } 00235 } 00236 00238 template<> inline 00239 void destroy_elements<bin>(bin* &ptr, int) 00240 { 00241 if (ptr) { 00242 void *p = reinterpret_cast<void*>(ptr); 00243 operator delete(p); 00244 ptr = 0; 00245 } 00246 } 00248 template<> inline 00249 void destroy_elements<short int>(short int* &ptr, int) 00250 { 00251 if (ptr) { 00252 void *p = reinterpret_cast<void*>(ptr); 00253 operator delete(p); 00254 ptr = 0; 00255 } 00256 } 00257 00259 template<> inline 00260 void destroy_elements<int>(int* &ptr, int) 00261 { 00262 if (ptr) { 00263 void *p = reinterpret_cast<void*>(ptr); 00264 operator delete(p); 00265 ptr = 0; 00266 } 00267 } 00268 00270 template<> inline 00271 void destroy_elements<double>(double* &ptr, int) 00272 { 00273 if (ptr) { 00274 void *p = *(reinterpret_cast<void**>(ptr) - 1); 00275 operator delete(p); 00276 ptr = 0; 00277 } 00278 } 00279 00281 template<> inline 00282 void destroy_elements<std::complex<double> >(std::complex<double>* &ptr, int) 00283 { 00284 if (ptr) { 00285 void *p = *(reinterpret_cast<void**>(ptr) - 1); 00286 operator delete(p); 00287 ptr = 0; 00288 } 00289 } 00290 00291 00293 template<class T> 00294 void create_elements(Array<T>* &ptr, int n, const Factory &f) 00295 { 00296 void *p = operator new(sizeof(Array<T>) * n); 00297 ptr = reinterpret_cast<Array<T>*>(p); 00298 for (int i = 0; i < n; ++i) { 00299 new(ptr + i) Array<T>(f); 00300 } 00301 } 00302 00304 template<class T> 00305 void create_elements(Mat<T>* &ptr, int n, const Factory &f) 00306 { 00307 void *p = operator new(sizeof(Mat<T>) * n); 00308 ptr = reinterpret_cast<Mat<T>*>(p); 00309 for (int i = 0; i < n; ++i) { 00310 new(ptr + i) Mat<T>(f); 00311 } 00312 } 00313 00315 template<class T> 00316 void create_elements(Vec<T>* &ptr, int n, const Factory &f) 00317 { 00318 void *p = operator new(sizeof(Vec<T>) * n); 00319 ptr = reinterpret_cast<Vec<T>*>(p); 00320 for (int i = 0; i < n; ++i) { 00321 new(ptr + i) Vec<T>(f); 00322 } 00323 } 00324 00325 } // namespace itpp 00326 00327 #endif // #ifndef FACTORY_H
Generated on Fri Aug 14 15:28:05 2009 for IT++ by Doxygen 1.5.9