MeVisLab Toolbox Reference
mlITKMLBaseWrapper.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
17
19#include "mlModuleIncludes.h"
20
22
23//----------------------------------------------------------------------------------
27//----------------------------------------------------------------------------------
38
39
40//----------------------------------------------------------------------------------
42//----------------------------------------------------------------------------------
44public:
46 WrapperSuperStructPtrs(const std::string * const nameStringsArg, const WrapperSuperStructPtrs * const pSSArgs) :
47 nameStrings(nameStringsArg), parentSuperStruct(pSSArgs) {}
48
50 const std::string * const nameStrings;
51
54};
55
56
57//----------------------------------------------------------------------------------
59//----------------------------------------------------------------------------------
60template<class OBJ_TYPE, typename OBJ_TYPE_POINTER, typename WRAPPER_CLASS_NAME> class ITKMLBaseWrapper
61{
62public:
63
64 //----------------------------------------------------------------------------------
66 //----------------------------------------------------------------------------------
68 _realWrapper(realWrapper), _wrapperSuperStructPtrs(&parentSuperStruct)
69 {
70 _outputBaseField = nullptr;
71 _inputBaseField = nullptr;
72 _module = nullptr;
73 _outputObj = nullptr;
74 _inputObj = nullptr;
75 _numAddOutputBaseFieldCalls=0;
76 _numAddInputBaseFieldCalls=0;
77 }
78
79 //----------------------------------------------------------------------------------
81 //----------------------------------------------------------------------------------
83 {
84 // Do nothing. The added base field will be destroyed by the Module
85 // automatically and objects are destroyed automatically or by the application.
86 // However this should not live longer than its referenced Module.
87 }
88
89 //----------------------------------------------------------------------------------
94 //----------------------------------------------------------------------------------
95 void addOutputBaseField(Module &module, const char *outputFieldName)
96 {
97 if (!outputFieldName){
98 ML_PRINT_ERROR("ITKMLBaseWrapper::addOutputBaseField", ML_BAD_POINTER_OR_0, "Passed field name pointer is nullptr. Ignoring call.");
99 return;
100 }
101 if (_numAddOutputBaseFieldCalls!=0){
102 ML_PRINT_ERROR("ITKMLBaseWrapper::addOutputBaseField", ML_BAD_STATE, "Ignoring multiple field add.");
103 return;
104 }
105
106 _module = &module;
107 _outputBaseField = module.addBase(outputFieldName);
108 _outputBaseField->addAllowedType<ITKWrapperBase>();
109 _outputBaseField->setBaseValue(&_realWrapper);
110 ++_numAddOutputBaseFieldCalls;
111 }
112
113 //----------------------------------------------------------------------------------
117 //----------------------------------------------------------------------------------
118 void addInputBaseField(Module &module, const char *inputFieldName)
119 {
120 if (!inputFieldName){
121 ML_PRINT_ERROR("ITKMLBaseWrapper::addInputBaseField", ML_BAD_POINTER_OR_0, "Passed field name pointer is nullptr. Ignoring call.");
122 return;
123 }
124 if (_numAddInputBaseFieldCalls!=0){
125 ML_PRINT_ERROR("ITKMLBaseWrapper::addOutputBaseField", ML_BAD_STATE, "Ignoring multiple field add.");
126 return;
127 }
128
129 _module = &module;
130 _inputBaseField = module.addBase(inputFieldName);
131 _inputBaseField->addAllowedType<ITKWrapperBase>();
132 _inputBaseField->setBaseValue(nullptr);
133 ++_numAddInputBaseFieldCalls;
134 }
135
136 //----------------------------------------------------------------------------------
138 //----------------------------------------------------------------------------------
140 {
141 _outputObj = newObject;
142 if (_outputBaseField){ _outputBaseField->touch(); }
143 }
144
145 //----------------------------------------------------------------------------------
147 //----------------------------------------------------------------------------------
149 {
150 return _outputBaseField;
151 }
152
153 //----------------------------------------------------------------------------------
155 //----------------------------------------------------------------------------------
157 {
158 return _inputBaseField;
159 }
160
161 //----------------------------------------------------------------------------------
163 //----------------------------------------------------------------------------------
165 {
166 return _outputObj;
167 }
168
169 //----------------------------------------------------------------------------------
171 //----------------------------------------------------------------------------------
172 bool hasSuperClass(const std::string &className) const
173 {
174 // Follow NameStrings and InheritrancePointer of class and super classes.
175 // Terminated by return when class name matches or if className==parentClassName
176 // in NameStrings. Termination criterion must be set correctly in Base classes
177 // by specifying class name as super class name! Otherwise loop will not terminate.
178 const WrapperSuperStructPtrs * follow = _wrapperSuperStructPtrs;
179 do {
180 if (follow->nameStrings[0] == className){ return true; }
181 if (follow->nameStrings[0] == follow->nameStrings[1]){ return false; }
183 } while (1);
184 return false;
185 }
186
187 /*---------------------------------------------------------------------------------- */
191 /*---------------------------------------------------------------------------------- */
193 {
194
195 if (_inputBaseField){
196 /* Get the pointer to the connected object and check its type.*/
197 /* If the type is valid then return a the pointer to the wrapped input object.*/
198 // Get base object from connected input.
199 Base *inPtr = _inputBaseField->getBaseValue();
200
201 // Is it derived from ITKWrapperBase? If not then we cannot use it.
204 static_cast<ITKWrapperBase*>(inPtr) :
205 nullptr;
206 if (!wrapperBaseInPtr)
207 {
208 return nullptr;
209 }
210
211 // Create wrapper of correct type by typecasting.
213
214 // If wrapped object is derived from superclass then return casted pointer, otherwise return nullptr.
215 return (wrapper && wrapper->hasSuperClass(wrapper->getName())) ? wrapper->getWrappedOutputObject() : nullptr;
216 }
217
218 /* No valid object connected to base field. */
219 return nullptr;
220 }
221
222protected:
223
226
229
233
237
240
244
248
252
256};
257
258
259//----------------------------------------------------------------------------------
264//----------------------------------------------------------------------------------
265#define ML_CREATE_BASE_WRAPPER_FOR_OBJECT_H(CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME, PARENT_CLASS_NAME, ML_EXPORT_MACRO) \
266 \
267 \
268/* Unified class which can be cast between all wrapped types. It accesses the internally specialized version */ \
269/* of the wrapped class objects and checks for correct inheritance information. */ \
270class ML_EXPORT_MACRO WRAPPER_CLASS_NAME : public ITKWrapperBase \
271{ \
272public: \
273 /* Implement a constructor and call of superclass constructor. */ \
274 WRAPPER_CLASS_NAME(); \
275 \
276 /* Destructor. */ \
277 ~WRAPPER_CLASS_NAME(); \
278 \
279 /* Implemented in .cpp. This interface must not contain class specific code since */ \
280 /* between all class wrapper this class wrapper it must be legal to cast it. */ \
281 /* For documentation of the following methods see ITKMLBaseWrapper. */ \
282 void addOutputBaseField(Module &module, const char *outputFieldName); \
283 void addInputBaseField(Module &module, const char *inputFieldName); \
284 void setNewOutputBaseFieldObject(CLASS_NAME_POINTER newObject); \
285 BaseField *getOutputBaseField(); \
286 BaseField *getInputBaseField(); \
287 CLASS_NAME_POINTER getWrappedOutputObject(); \
288 CLASS_NAME_POINTER getWrappedInputObject(const RuntimeType * /*rt*/ = nullptr); \
289 bool hasSuperClass(const std::string &className) const; \
290 const std::string &getName(); \
291 \
292 /* Pointer to the string names wrapped class and parent class. */\
293 static const std::string NameStrings[2]; \
294 \
295 \
296 /* Reference the NameStrings for this class and for the parent class. */\
297 static const WrapperSuperStructPtrs _wrapperSuperStructPtrs; \
298 \
299private: \
300 \
301 /* Incomplete pointer to internal object which contains type specific information. */ \
302 void *_obj;\
303 \
304 /* The header part of the runtime type system interface. */ \
305 ML_CLASS_HEADER(WRAPPER_CLASS_NAME); \
306}; \
307
308
309
310
311//----------------------------------------------------------------------------------
316//----------------------------------------------------------------------------------
317#define ML_CREATE_BASE_WRAPPER_FOR_OBJECT_CPP(CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME, PARENT_CLASS_NAME, ML_EXPORT_MACRO) \
318 \
319 /* Pointer to the string names wrapped class and parent class. */\
320 const std::string WRAPPER_CLASS_NAME::NameStrings[2] = { #CLASS_NAME, #PARENT_CLASS_NAME }; \
321 \
322 /* Reference the NameStrings for this class and for the parent class. */\
323 const WrapperSuperStructPtrs WRAPPER_CLASS_NAME::_wrapperSuperStructPtrs(WRAPPER_CLASS_NAME::NameStrings, &PARENT_CLASS_NAME##Wrapper::_wrapperSuperStructPtrs); \
324 \
325 /* Implement a constructor and call of superclass constructor. */ \
326 WRAPPER_CLASS_NAME::WRAPPER_CLASS_NAME(){ _obj = static_cast<void*>(new ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>(WRAPPER_CLASS_NAME::_wrapperSuperStructPtrs, *this)); } \
327 \
328 /* Destructor. */ \
329 WRAPPER_CLASS_NAME::~WRAPPER_CLASS_NAME(){ if (_obj){ delete (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj)); _obj = nullptr; }; } \
330 \
331 /* For documentation of the following methods see ITKMLBaseWrapper. */ \
332 void WRAPPER_CLASS_NAME::addOutputBaseField(Module &module, const char *outputFieldName){ (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->addOutputBaseField(module, outputFieldName); } \
333 void WRAPPER_CLASS_NAME::addInputBaseField(Module &module, const char *inputFieldName) { (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->addInputBaseField(module, inputFieldName) ; } \
334 void WRAPPER_CLASS_NAME::setNewOutputBaseFieldObject(CLASS_NAME_POINTER newObject) { (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->setNewOutputBaseFieldObject(newObject) ; } \
335 BaseField *WRAPPER_CLASS_NAME::getOutputBaseField() { return (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getOutputBaseField() ; } \
336 BaseField *WRAPPER_CLASS_NAME::getInputBaseField() { return (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getInputBaseField() ; } \
337 CLASS_NAME_POINTER WRAPPER_CLASS_NAME::getWrappedOutputObject() { return (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getWrappedOutputObject() ; } \
338 CLASS_NAME_POINTER WRAPPER_CLASS_NAME::getWrappedInputObject(const RuntimeType *rt) { return (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getWrappedInputObject(rt) ; } \
339 bool WRAPPER_CLASS_NAME::hasSuperClass(const std::string &className) const { return (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->hasSuperClass(className) ; } \
340 \
341 const std::string & WRAPPER_CLASS_NAME::getName() { return NameStrings[0]; }\
342 \
343 /* The .cpp part of the runtime type system interface. */ \
344 ML_CLASS_SOURCE(WRAPPER_CLASS_NAME, ITKWrapperBase); \
345 \
346
Field to encapsulate a pointer to an ML base object.
Definition mlFields.h:797
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition mlBase.h:59
Class to create a Base wrapper around an object of a certain template type.
const WrapperSuperStructPtrs * _wrapperSuperStructPtrs
Reference to the inheritance pointer correlated to this class.
OBJ_TYPE_POINTER _outputObj
Smart pointer to the OBJ_TYPE created and wrapped by this class if an output connector is created.
OBJ_TYPE_POINTER getWrappedOutputObject()
Return the pointer to the wrapped object.
void addOutputBaseField(Module &module, const char *outputFieldName)
Method to add a base field to the passed Module module.
void setNewOutputBaseFieldObject(OBJ_TYPE_POINTER newObject)
Set base field so that it points to the new object.
BaseField * getInputBaseField()
Return the pointer to the input base field or nullptr if not created.
bool hasSuperClass(const std::string &className) const
Return true if the passed parent class name is in the list of classes.
BaseField * getOutputBaseField()
Return the pointer to the output base field or nullptr if not created.
Base & _realWrapper
Reference to the real wrapper object creating this.
OBJ_TYPE_POINTER _inputObj
Smart pointer to the connected OBJ_TYPE wrapped by this class if an input connector is created or nul...
OBJ_TYPE_POINTER getWrappedInputObject(const RuntimeType *=nullptr)
MLint _numAddInputBaseFieldCalls
Counter for the number of addInputBaseField method calls.
void addInputBaseField(Module &module, const char *inputFieldName)
Method to add a base field to the passed Module module.
virtual ~ITKMLBaseWrapper()
Destructor.
ITKMLBaseWrapper(const WrapperSuperStructPtrs &parentSuperStruct, ITKWrapperBase &realWrapper)
Constructor.
MLint _numAddOutputBaseFieldCalls
Counter for the number of addOutputBaseField method calls.
Module * _module
Pointer to the Module which shall handle this; default is nullptr.
BaseField * _outputBaseField
Pointer to the field in _module which is used as output interface to another module; default is nullp...
BaseField * _inputBaseField
Pointer to the field in _module which is used as input interface to another module; default is nullpt...
Helper class to have a common base class for all derived wrapper.
ML_ABSTRACT_CLASS_HEADER(ITKWrapperBase)
Register this class abstractly.
ITKWrapperBase()
Private on purpose to avoid instantiations.
Base class for an image processing module of the ML.
Definition mlModule.h:151
RuntimeType contains type and inheritance information of a class and a static dictionary with informa...
Link structure between names and superclass names.
const WrapperSuperStructPtrs *const parentSuperStruct
Pointer to the WrapperSuperStructPtrs of the parent class.
WrapperSuperStructPtrs(const std::string *const nameStringsArg, const WrapperSuperStructPtrs *const pSSArgs)
Default constructor.
const std::string *const nameStrings
Pointer to the type name and parent type name strings.
#define ML_BASE_IS_A(base, type)
This file defines macros, which are inserted in classes to declare and implement additional class mem...
#define ML_BAD_POINTER_OR_0
A pointer is NULL or a value is NULL or 0 where it should not be; this sometimes indicates a memory a...
Definition mlTypeDefs.h:933
#define ML_BAD_STATE
The current state of an object is not appropriate for an operation; perhaps it is not initialized or ...
Definition mlTypeDefs.h:938
#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 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