factory.h
Go to the documentation of this file.
#ifndef FACTORY_H
#define FACTORY_H
#include <complex>
#include <itpp/base/binary.h>
#include <itpp/itexports.h>
namespace itpp
{
// Forward declarations
template<class T> class Array;
template<class Num_T> class Mat;
template<class Num_T> class Vec;
class ITPP_EXPORT Factory
{
public:
Factory() {}
virtual ~Factory() {}
};
const Factory DEFAULT_FACTORY;
template<class T> inline
void create_elements(T* &ptr, int n, const Factory &)
{
void *p = operator new(sizeof(T) * n);
ptr = reinterpret_cast<T*>(p);
for (int i = 0; i < n; i++) {
new(ptr + i) T();
}
}
template<> inline
void create_elements<unsigned char>(unsigned char* &ptr, int n,
const Factory &)
{
void *p = operator new(sizeof(unsigned char) * n);
ptr = reinterpret_cast<unsigned char*>(p);
}
template<> inline
void create_elements<bin>(bin* &ptr, int n, const Factory &)
{
void *p = operator new(sizeof(bin) * n);
ptr = reinterpret_cast<bin*>(p);
}
template<> inline
void create_elements<short int>(short int* &ptr, int n, const Factory &)
{
void *p = operator new(sizeof(short int) * n);
ptr = reinterpret_cast<short int*>(p);
}
template<> inline
void create_elements<int>(int* &ptr, int n, const Factory &)
{
void *p = operator new(sizeof(int) * n);
ptr = reinterpret_cast<int*>(p);
}
template<> inline
void create_elements<double>(double* &ptr, int n, const Factory &)
{
void *p0 = operator new(sizeof(double) * n + 16);
void *p1 = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(p0) + 16)
& (~(std::size_t(15))));
*(reinterpret_cast<void**>(p1) - 1) = p0;
ptr = reinterpret_cast<double*>(p1);
}
template<> inline
void create_elements<std::complex<double> >(std::complex<double>* &ptr,
int n, const Factory &)
{
void *p0 = operator new(sizeof(std::complex<double>) * n + 16);
void *p1 = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(p0) + 16)
& (~(std::size_t(15))));
*(reinterpret_cast<void**>(p1) - 1) = p0;
ptr = reinterpret_cast<std::complex<double>*>(p1);
}
template<class T> inline
void destroy_elements(T* &ptr, int n)
{
if (ptr) {
for (int i = 0; i < n; ++i) {
ptr[i].~T();
}
void *p = reinterpret_cast<void*>(ptr);
operator delete(p);
ptr = 0;
}
}
template<> inline
void destroy_elements<unsigned char>(unsigned char* &ptr, int)
{
if (ptr) {
void *p = reinterpret_cast<void*>(ptr);
operator delete(p);
ptr = 0;
}
}
template<> inline
void destroy_elements<bin>(bin* &ptr, int)
{
if (ptr) {
void *p = reinterpret_cast<void*>(ptr);
operator delete(p);
ptr = 0;
}
}
template<> inline
void destroy_elements<short int>(short int* &ptr, int)
{
if (ptr) {
void *p = reinterpret_cast<void*>(ptr);
operator delete(p);
ptr = 0;
}
}
template<> inline
void destroy_elements<int>(int* &ptr, int)
{
if (ptr) {
void *p = reinterpret_cast<void*>(ptr);
operator delete(p);
ptr = 0;
}
}
template<> inline
void destroy_elements<double>(double* &ptr, int)
{
if (ptr) {
void *p = *(reinterpret_cast<void**>(ptr) - 1);
operator delete(p);
ptr = 0;
}
}
template<> inline
void destroy_elements<std::complex<double> >(std::complex<double>* &ptr, int)
{
if (ptr) {
void *p = *(reinterpret_cast<void**>(ptr) - 1);
operator delete(p);
ptr = 0;
}
}
template<class T>
void create_elements(Array<T>* &ptr, int n, const Factory &f)
{
void *p = operator new(sizeof(Array<T>) * n);
ptr = reinterpret_cast<Array<T>*>(p);
for (int i = 0; i < n; ++i) {
new(ptr + i) Array<T>(f);
}
}
template<class T>
void create_elements(Mat<T>* &ptr, int n, const Factory &f)
{
void *p = operator new(sizeof(Mat<T>) * n);
ptr = reinterpret_cast<Mat<T>*>(p);
for (int i = 0; i < n; ++i) {
new(ptr + i) Mat<T>(f);
}
}
template<class T>
void create_elements(Vec<T>* &ptr, int n, const Factory &f)
{
void *p = operator new(sizeof(Vec<T>) * n);
ptr = reinterpret_cast<Vec<T>*>(p);
for (int i = 0; i < n; ++i) {
new(ptr + i) Vec<T>(f);
}
}
} // namespace itpp
#endif // #ifndef FACTORY_H
binary.h
Binary class definition.
itpp::Array
General array class.
Definition array.h:105
itpp::Factory
Base class for class factories.
Definition factory.h:130
itpp::Factory::Factory
Factory()
Default constructor.
Definition factory.h:133
itpp::Factory::~Factory
virtual ~Factory()
Destructor.
Definition factory.h:135
itpp::Mat
Matrix Class (Templated).
Definition mat.h:202
itpp::Vec
Vector Class (Templated).
Definition vec.h:245
itpp::bin
Binary arithmetic (boolean) class.
Definition binary.h:57
itpp
itpp namespace
Definition itmex.h:37
itpp::destroy_elements< int >
void destroy_elements< int >(int *&ptr, int)
Specialization for integer data arrays.
Definition factory.h:260
itpp::create_elements< int >
void create_elements< int >(int *&ptr, int n, const Factory &)
Specialization for integer data arrays.
Definition factory.h:181
itpp::destroy_elements< bin >
void destroy_elements< bin >(bin *&ptr, int)
Specialization for binary data arrays.
Definition factory.h:239
itpp::create_elements< short int >
void create_elements< short int >(short int *&ptr, int n, const Factory &)
Specialization for short integer data arrays.
Definition factory.h:173
itpp::create_elements< bin >
void create_elements< bin >(bin *&ptr, int n, const Factory &)
Specialization for binary data arrays.
Definition factory.h:165
itpp::create_elements< unsigned char >
void create_elements< unsigned char >(unsigned char *&ptr, int n, const Factory &)
Specialization for unsigned char data arrays (used in GF2Mat).
Definition factory.h:156
itpp::destroy_elements< unsigned char >
void destroy_elements< unsigned char >(unsigned char *&ptr, int)
Specialization for unsigned char data arrays (used in GF2Mat).
Definition factory.h:228
itpp::destroy_elements< short int >
void destroy_elements< short int >(short int *&ptr, int)
Specialization for short integer data arrays.
Definition factory.h:249
itpp::destroy_elements
void destroy_elements(T *&ptr, int n)
Destroy an array of Array, Vec or Mat elements.
Definition factory.h:214
itpp::destroy_elements< double >
void destroy_elements< double >(double *&ptr, int)
Specialisation for 16-byte aligned double data arrays.
Definition factory.h:271
itpp::create_elements< double >
void create_elements< double >(double *&ptr, int n, const Factory &)
Specialization for 16-byte aligned double data arrays.
Definition factory.h:189
itpp::DEFAULT_FACTORY
const Factory DEFAULT_FACTORY
Default (dummy) factory.
Definition factory.h:139
itpp::create_elements
void create_elements(T *&ptr, int n, const Factory &)
Create an n-length array of T to be used as Array, Vec or Mat elements.
Definition factory.h:144

Generated on for IT++ by Doxygen 1.15.0