MeVisLab Toolbox Reference
mlITKObjectFactory.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#pragma once
14
15#include <mlITKSupport.h>
16
17//-------------------------------------------------------------------------------------
26//-------------------------------------------------------------------------------------
27#define _ML_CREATE_ITK_OBJECT_VOID(ML_OUTDATATYPE, ITKOUTDATATYPE, MLINDATATYPE, ITKINDATATYPE, DIM, CLASS_NAME) \
28{ \
29 return new CLASS_NAME<ITKINDATATYPE, DIM>(); \
30} \
31
32//-------------------------------------------------------------------------------------
40//-------------------------------------------------------------------------------------
41#define _ML_DESTROY_ITK_OBJECT_VOID(MLOUTDATATYPE, ITKOUTDATATYPE, MLINDATATYPE, ITKINDATATYPE, DIM, CLASS_NAME) \
42{ \
43 delete static_cast<CLASS_NAME<ITKINDATATYPE, DIM>*>(itkFactoryObj); \
44} \
45
46
47//-------------------------------------------------------------------------------------
56//-------------------------------------------------------------------------------------
57#define _ML_CREATE_ITK_OBJECT_VOID_WO_DT(ML_OUTDATATYPE, ITKOUTDATATYPE, MLINDATATYPE, ITKINDATATYPE, DIM, CLASS_NAME) \
58{ \
59 return new CLASS_NAME<DIM>(); \
60} \
61
62//-------------------------------------------------------------------------------------
70//-------------------------------------------------------------------------------------
71#define _ML_DESTROY_ITK_OBJECT_VOID_WO_DT(MLOUTDATATYPE, ITKOUTDATATYPE, MLINDATATYPE, ITKINDATATYPE, DIM, CLASS_NAME) \
72{ \
73 delete static_cast<CLASS_NAME<DIM>*>(itkFactoryObj); \
74} \
75
76//-------------------------------------------------------------------------------------
83//-------------------------------------------------------------------------------------
84#define MLITKImplementFactoryFuncs(CLASS_NAME, NAMESPACE, NEW_FUNC, DEL_FUNC) \
85 /*--------------------------------------------------------------------*/ \
86 \
87 /*--------------------------------------------------------------------*/ \
88 static void *CLASS_NAME##Creator(MLDataType dType, unsigned int dim){ \
89 /* Call correct template version of object creation for all types and dims. */ \
90 ML_IMPLEMENT_ALL_ITK_6D_CASES("CLASS_NAME##creator())", \
91 NEW_FUNC, \
92 NAMESPACE::CLASS_NAME, dType, dim); \
93 return nullptr; /* Error */ \
94 } \
95 \
96 /*--------------------------------------------------------------------*/ \
97 \
98 \
99 /*--------------------------------------------------------------------*/ \
100 static void CLASS_NAME##Destructor(void *itkFactoryObj, MLDataType dType, unsigned int dim){ \
101 /* Call correct template version of object destruction for all types and dims. */ \
102 ML_IMPLEMENT_ALL_ITK_6D_CASES("CLASS_NAME##destructor()", \
103 DEL_FUNC, \
104 NAMESPACE::CLASS_NAME, dType, dim); \
105 } \
106
107
108/*---------------------------------------------------------------------------*/
114/*---------------------------------------------------------------------------*/
115#define MLITKObjectFactoryInit(FACTORY, CLASS_NAME) \
116 FACTORY._setCreatorAndDestructor(CLASS_NAME##Creator, CLASS_NAME##Destructor);
117
118
120
121/*---------------------------------------------------------------------------*/
126/*---------------------------------------------------------------------------*/
128{
129 public:
130
132 typedef void *(*MLITKObjectFactoryCreator)(MLDataType dataType, unsigned int dim);
133
135 typedef void (*MLITKObjectFactoryDestructor)(void *, MLDataType dataType, unsigned int dim);
136
138 MLITKObjectFactory(): _itkObjectVoid(nullptr), _dataType(ML_INVALID_DATA_TYPE), _dimension(0){ }
139
141 ~MLITKObjectFactory() { _destroyInternalObject(); }
142
144 void createObject(MLDataType dType, int dim) { _createInternalObject(dType, dim); }
145
147 void createObject(MLDataType dType, ImageVector v){ _createInternalObject(dType, v.getExtDimension()); }
148
150 void destroyObject() { _destroyInternalObject(); }
151
153 void *getObject() const { return _itkObjectVoid; }
154
156 MLDataType getDataType() const { return _dataType; }
157
159 unsigned int getDimension() const { return _dimension; }
160
162 void _setCreatorAndDestructor(MLITKObjectFactoryCreator cFunc,
163 MLITKObjectFactoryDestructor dFunc)
164 {
165 _creatorFunc = cFunc;
166 _destructorFunc = dFunc;
167 }
168
169 private:
170
171 //-------------------------------------------
173 //-------------------------------------------
174
176 void _createInternalObject(MLDataType dataType, MLint dim)
177 {
178 // Remove existing object if there is one.
179 _destroyInternalObject();
180
181 // Check for valid data type and dimension.
182 if (!MLIsValidType(dataType) || (dim < 1) || (dim > 6)){
183 ML_PRINT_FATAL_ERROR("MLITKObjectFactory::_createInternalObject(MLDataType, int)",
184 ML_BAD_PARAMETER, "Not creating object.");
185 return;
186 }
187
188 ML_TRY
189 {
190 // Create object and store data type and dimension.
191 _itkObjectVoid = _creatorFunc(dataType, static_cast<unsigned int>(dim));
192 _dataType = dataType;
193 _dimension = static_cast<unsigned int>(dim);
194 }
195 ML_CATCH_BLOCK(...)
196 {
197 ML_PRINT_FATAL_ERROR("MLITKObjectFactory::_createInternalObject(MLDataType, int)",
199 _itkObjectVoid = nullptr;
200 _dataType = ML_INVALID_DATA_TYPE;
201 _dimension = 0;
202 }
203 }
204
206 void _destroyInternalObject()
207 {
208 if (_itkObjectVoid){
209 ML_TRY
210 {
211 _destructorFunc(_itkObjectVoid, _dataType, _dimension);
212 }
213 ML_CATCH_BLOCK(...)
214 {
215 ML_PRINT_ERROR("MLITKObjectFactory::_destroyInternalObject()", ML_UNKNOWN_EXCEPTION, "");
216 }
217
218 // Reset pointer and members.
219 _itkObjectVoid = nullptr;
220 _dataType = ML_INVALID_DATA_TYPE;
221 _dimension = 0;
222 }
223 }
224
226 MLITKObjectFactory(const MLITKObjectFactory &){}
227
229 MLITKObjectFactory &operator=(const MLITKObjectFactory &){ return *this; }
231
232
233 //-------------------------------------------
235 //-------------------------------------------
237 void *_itkObjectVoid;
238
240 MLDataType _dataType;
241
243 unsigned int _dimension;
244
246 MLITKObjectFactoryCreator _creatorFunc;
247
249 MLITKObjectFactoryDestructor _destructorFunc;
251};
252
A class to manage a void pointer to an object of a certain class type a type dataType and dimension d...
~MLITKObjectFactory()
Destroy internally stored object.
MLDataType getDataType() const
Returns data type of the managed internal object.
void destroyObject()
Destroy object.
void _setCreatorAndDestructor(MLITKObjectFactoryCreator cFunc, MLITKObjectFactoryDestructor dFunc)
Sets creator and destructor functions. Only to be called by the macro.
unsigned int getDimension() const
Returns dimension of the managed internal object.
void * getObject() const
Returns void pointer to the managed internal object.
void createObject(MLDataType dType, ImageVector v)
Create object.
void createObject(MLDataType dType, int dim)
Create object.
MLITKObjectFactory()
Create an empty object.
MLint getExtDimension() const
Returns the index to the highest vector component which is not 1 which is useful to get the real dime...
MLEXPORT MLint32 MLIsValidType(MLDataType dataType)
Returns true(=1) if data type dataType seems to be valid, otherwise 0(=false).
#define ML_INVALID_DATA_TYPE
Defines an invalid MLDataType.
Definition mlTypeDefs.h:613
MLint32 MLDataType
MLDataType.
Definition mlTypeDefs.h:596
#define ML_BAD_PARAMETER
A bad/invalid parameter (or even an inappropriate image) has been passed to a module or an algorithm;...
Definition mlTypeDefs.h:823
#define ML_UNKNOWN_EXCEPTION
An unknown exception was detected and caught; this usually means that something for an unknown reason...
Definition mlTypeDefs.h:730
#define ML_PRINT_FATAL_ERROR(FUNC_NAME, REASON, HANDLING)
Like ML_PRINT_FATAL_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) without a runtime object to be du...
#define ML_PRINT_ERROR(FUNC_NAME, REASON, HANDLING)
Like ML_PRINT_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) without a runtime object to be dumped.
#define ML_CATCH_BLOCK(__paramType)
#define ML_TRY
#define MLITK_SUPPORT_EXPORT
When included by other libraries MLITK_SUPPORT_EXPORT is compiled as import symbol.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
Definition mlTypeDefs.h:490