MeVisLabToolboxReference
FMEwork/ITK/Sources/ITK/MLITK/ITKSupport/mlITKObjectFactory.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00004 
00008 //----------------------------------------------------------------------------------
00009 #ifndef __mlITKObjectFactory_H
00010 #define __mlITKObjectFactory_H
00011 
00012 #ifndef __mlITKSupport_H
00013 #include <mlITKSupport.h>
00014 #endif
00015 
00016 //-------------------------------------------------------------------------------------
00025 //-------------------------------------------------------------------------------------
00026 #define _ML_CREATE_ITK_OBJECT_VOID(ML_OUTDATATYPE, ITKOUTDATATYPE, MLINDATATYPE, ITKINDATATYPE, DIM, CLASS_NAME) \
00027 {                                                                                                                \
00028   return new CLASS_NAME<ITKINDATATYPE, DIM>();                                                                   \
00029 }                                                                                                                \
00030 
00031 //-------------------------------------------------------------------------------------
00039 //-------------------------------------------------------------------------------------
00040 #define _ML_DESTROY_ITK_OBJECT_VOID(MLOUTDATATYPE, ITKOUTDATATYPE, MLINDATATYPE, ITKINDATATYPE, DIM, CLASS_NAME) \
00041 {                                                                                                                \
00042   delete (CLASS_NAME<ITKINDATATYPE, DIM>*)itkFactoryObj;                                                         \
00043 }                                                                                                                \
00044 
00045 
00046 //-------------------------------------------------------------------------------------
00055 //-------------------------------------------------------------------------------------
00056 #define _ML_CREATE_ITK_OBJECT_VOID_WO_DT(ML_OUTDATATYPE, ITKOUTDATATYPE, MLINDATATYPE, ITKINDATATYPE, DIM, CLASS_NAME) \
00057 {                                                                                                                      \
00058   return new CLASS_NAME<DIM>();                                                                                        \
00059 }                                                                                                                      \
00060 
00061 //-------------------------------------------------------------------------------------
00069 //-------------------------------------------------------------------------------------
00070 #define _ML_DESTROY_ITK_OBJECT_VOID_WO_DT(MLOUTDATATYPE, ITKOUTDATATYPE, MLINDATATYPE, ITKINDATATYPE, DIM, CLASS_NAME) \
00071 {                                                                                                                      \
00072   delete static_cast<CLASS_NAME<DIM>*>(itkFactoryObj);                                                                 \
00073 }                                                                                                                      \
00074 
00075 //-------------------------------------------------------------------------------------
00082 //-------------------------------------------------------------------------------------
00083 #define MLITKImplementFactoryFuncs(CLASS_NAME, NAMESPACE, NEW_FUNC, DEL_FUNC)                   \
00084   /*--------------------------------------------------------------------*/                      \
00085                       \
00086   /*--------------------------------------------------------------------*/                      \
00087   static void *CLASS_NAME##Creator(MLDataType dType, unsigned int dim){                         \
00088     /* Call correct template version of object creation for all types and dims. */              \
00089     ML_IMPLEMENT_ALL_ITK_6D_CASES("CLASS_NAME##creator())",                                     \
00090                                   NEW_FUNC,                                                     \
00091                                   NAMESPACE::CLASS_NAME, dType, dim);                           \
00092     return NULL; /* Error */                                                                    \
00093   }                                                                                             \
00094                                                                                                 \
00095   /*--------------------------------------------------------------------*/                      \
00096                       \
00097                       \
00098   /*--------------------------------------------------------------------*/                      \
00099   static void CLASS_NAME##Destructor(void *itkFactoryObj, MLDataType dType, unsigned int dim){  \
00100     /* Call correct template version of object destruction for all types and dims. */           \
00101     ML_IMPLEMENT_ALL_ITK_6D_CASES("CLASS_NAME##destructor()",                                   \
00102                                   DEL_FUNC,                                                     \
00103                                   NAMESPACE::CLASS_NAME, dType, dim);                           \
00104   }                                                                                             \
00105 
00106 
00107 /*---------------------------------------------------------------------------*/
00113 /*---------------------------------------------------------------------------*/
00114 #define MLITKObjectFactoryInit(FACTORY, CLASS_NAME) \
00115   FACTORY._setCreatorAndDestructor(CLASS_NAME##Creator, CLASS_NAME##Destructor);
00116 
00117 
00118 ML_START_NAMESPACE
00119 
00120 /*---------------------------------------------------------------------------*/
00125 /*---------------------------------------------------------------------------*/
00126 class MLITK_SUPPORT_EXPORT MLITKObjectFactory
00127 {
00128   public:
00129 
00131     typedef void *(*MLITKObjectFactoryCreator)(MLDataType dataType, unsigned int dim);
00132 
00134     typedef void (*MLITKObjectFactoryDestructor)(void *, MLDataType dataType, unsigned int dim);
00135 
00137     MLITKObjectFactory(): _itkObjectVoid(NULL), _dataType(ML_INVALID_DATA_TYPE), _dimension(0){ }
00138 
00140     ~MLITKObjectFactory()                        { _destroyInternalObject(); }
00141 
00143     void createObject(MLDataType dType, int dim) { _createInternalObject(dType, dim); }
00144 
00146     void createObject(MLDataType dType, ImageVector v){ _createInternalObject(dType, v.getExtDimension()); }
00147 
00149     void destroyObject()                         { _destroyInternalObject(); }
00150 
00152     void *getObject() const                      { return _itkObjectVoid; }
00153 
00155     MLDataType getDataType() const               { return _dataType; }
00156 
00158     int getDimension() const                     { return _dimension; }
00159 
00161     void _setCreatorAndDestructor(MLITKObjectFactoryCreator    cFunc,
00162                                   MLITKObjectFactoryDestructor dFunc)
00163     {
00164       _creatorFunc    = cFunc;
00165       _destructorFunc = dFunc;
00166     }
00167 
00168   private:
00169 
00170     //-------------------------------------------
00172     //-------------------------------------------
00173 
00175     void _createInternalObject(MLDataType dataType, MLint dim)
00176     {
00177       // Remove existing object if there is one.
00178       _destroyInternalObject();
00179 
00180       // Check for valid data type and dimension.
00181       if (!MLIsValidType(dataType) || (dim < 1) || (dim > 6)){
00182         ML_PRINT_FATAL_ERROR("MLITKObjectFactory::_createInternalObject(MLDataType, int)",
00183                              ML_BAD_PARAMETER, "Not creating object.");
00184         return;
00185       }
00186 
00187       ML_TRY
00188       {
00189         // Create object and store data type and dimension.
00190         _itkObjectVoid = _creatorFunc(dataType, dim);
00191         _dataType      = dataType;
00192         _dimension     = dim;
00193       }
00194       ML_CATCH_BLOCK(...)
00195       {
00196         ML_PRINT_FATAL_ERROR("MLITKObjectFactory::_createInternalObject(MLDataType, int)",
00197                              ML_UNKNOWN_EXCEPTION, "");
00198         _itkObjectVoid = NULL;
00199         _dataType      = ML_INVALID_DATA_TYPE;
00200         _dimension     = 0;
00201       }
00202     }
00203 
00205     void _destroyInternalObject()
00206     {
00207       if (_itkObjectVoid){
00208         ML_TRY
00209         {
00210           _destructorFunc(_itkObjectVoid, _dataType, _dimension);
00211         }
00212         ML_CATCH_BLOCK(...)
00213         {
00214           ML_PRINT_ERROR("MLITKObjectFactory::_destroyInternalObject()", ML_UNKNOWN_EXCEPTION, "");
00215         }
00216 
00217         // Reset pointer and members.
00218         _itkObjectVoid = NULL;
00219         _dataType      = ML_INVALID_DATA_TYPE;
00220         _dimension     = 0;
00221       }
00222     }
00223 
00225     MLITKObjectFactory(const MLITKObjectFactory &){}
00226 
00228     MLITKObjectFactory &operator=(const MLITKObjectFactory &){ return *this; }
00230 
00231 
00232     //-------------------------------------------
00234     //-------------------------------------------
00236     void *_itkObjectVoid;
00237 
00239     MLDataType _dataType;
00240 
00242     unsigned int _dimension;
00243 
00245     MLITKObjectFactoryCreator _creatorFunc;
00246 
00248     MLITKObjectFactoryDestructor _destructorFunc;
00250 };
00251 
00252 ML_END_NAMESPACE
00253 
00254 
00255 #endif //of __mlITKObjectFactory_H
00256