MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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 { \
90 /* Call correct template version of object creation for all types and dims. */ \
91 ML_IMPLEMENT_ALL_ITK_6D_CASES("CLASS_NAME##creator())", NEW_FUNC, NAMESPACE::CLASS_NAME, dType, dim); \
92 return nullptr; /* Error */ \
93 } \
94 \
95 /*--------------------------------------------------------------------*/ \
96 \
97 \
98 /*--------------------------------------------------------------------*/ \
99 static void CLASS_NAME##Destructor(void* itkFactoryObj, MLDataType dType, unsigned int dim) \
100 { \
101 /* Call correct template version of object destruction for all types and dims. */ \
102 ML_IMPLEMENT_ALL_ITK_6D_CASES("CLASS_NAME##destructor()", DEL_FUNC, NAMESPACE::CLASS_NAME, dType, dim); \
103 }
104
105
106/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
113#define MLITKObjectFactoryInit(FACTORY, CLASS_NAME) \
114 FACTORY._setCreatorAndDestructor(CLASS_NAME##Creator, CLASS_NAME##Destructor);
115
116
118
119/*---------------------------------------------------------------------------*/
124/*---------------------------------------------------------------------------*/
126{
127public:
129 typedef void* (*MLITKObjectFactoryCreator)(MLDataType dataType, unsigned int dim);
130
132 typedef void (*MLITKObjectFactoryDestructor)(void*, MLDataType dataType, unsigned int dim);
133
136 : _itkObjectVoid(nullptr)
137 , _dataType(ML_INVALID_DATA_TYPE)
138 , _dimension(0)
139 {}
140
143 {
144 _destroyInternalObject();
145 }
146
149 {
150 _createInternalObject(dType, dim);
151 }
152
155 {
156 _createInternalObject(dType, v.getExtDimension());
157 }
158
161 {
162 _destroyInternalObject();
163 }
164
166 void* getObject() const
167 {
168 return _itkObjectVoid;
169 }
170
173 {
174 return _dataType;
175 }
176
178 unsigned int getDimension() const
179 {
180 return _dimension;
181 }
182
184 void _setCreatorAndDestructor(MLITKObjectFactoryCreator cFunc, MLITKObjectFactoryDestructor dFunc)
185 {
186 _creatorFunc = cFunc;
187 _destructorFunc = dFunc;
188 }
189
190private:
191 //-------------------------------------------
193 //-------------------------------------------
194
196 void _createInternalObject(MLDataType dataType, MLint dim)
197 {
198 // Remove existing object if there is one.
199 _destroyInternalObject();
200
201 // Check for valid data type and dimension.
202 if (!MLIsValidType(dataType) || (dim < 1) || (dim > 6)) {
203 ML_PRINT_FATAL_ERROR("MLITKObjectFactory::_createInternalObject(MLDataType, int)", ML_BAD_PARAMETER,
204 "Not creating object.");
205 return;
206 }
207
208 ML_TRY
209 {
210 // Create object and store data type and dimension.
211 _itkObjectVoid = _creatorFunc(dataType, static_cast<unsigned int>(dim));
212 _dataType = dataType;
213 _dimension = static_cast<unsigned int>(dim);
214 }
215 ML_CATCH_BLOCK(...)
216 {
217 ML_PRINT_FATAL_ERROR("MLITKObjectFactory::_createInternalObject(MLDataType, int)", ML_UNKNOWN_EXCEPTION, "");
218 _itkObjectVoid = nullptr;
219 _dataType = ML_INVALID_DATA_TYPE;
220 _dimension = 0;
221 }
222 }
223
225 void _destroyInternalObject()
226 {
227 if (_itkObjectVoid) {
228 ML_TRY
229 {
230 _destructorFunc(_itkObjectVoid, _dataType, _dimension);
231 }
232 ML_CATCH_BLOCK(...)
233 {
234 ML_PRINT_ERROR("MLITKObjectFactory::_destroyInternalObject()", ML_UNKNOWN_EXCEPTION, "");
235 }
236
237 // Reset pointer and members.
238 _itkObjectVoid = nullptr;
239 _dataType = ML_INVALID_DATA_TYPE;
240 _dimension = 0;
241 }
242 }
243
245 MLITKObjectFactory(const MLITKObjectFactory&)
246 {}
247
249 MLITKObjectFactory& operator=(const MLITKObjectFactory&)
250 {
251 return *this;
252 }
254
255
256 //-------------------------------------------
258 //-------------------------------------------
260 void* _itkObjectVoid;
261
263 MLDataType _dataType;
264
266 unsigned int _dimension;
267
269 MLITKObjectFactoryCreator _creatorFunc;
270
272 MLITKObjectFactoryDestructor _destructorFunc;
274};
275
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 that is not 1.
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:609
MLint32 MLDataType
MLDataType.
Definition mlTypeDefs.h:592
#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:879
#define ML_UNKNOWN_EXCEPTION
An unknown exception was detected and caught; this usually means that something went seriously wrong ...
Definition mlTypeDefs.h:786
#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:489