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 
16 #include "mlInitSystemITKSupport.h"
17 
19 #include "mlModuleIncludes.h"
20 
21 ML_START_NAMESPACE
22 
23 //----------------------------------------------------------------------------------
27 //----------------------------------------------------------------------------------
29 {
30 public:
33 
34 protected:
37 };
38 
39 
40 //----------------------------------------------------------------------------------
42 //----------------------------------------------------------------------------------
44 public:
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 //----------------------------------------------------------------------------------
60 template<class OBJ_TYPE, typename OBJ_TYPE_POINTER, typename WRAPPER_CLASS_NAME> class ITKMLBaseWrapper
61 {
62 public:
63 
64  //----------------------------------------------------------------------------------
66  //----------------------------------------------------------------------------------
67  ITKMLBaseWrapper(const WrapperSuperStructPtrs &parentSuperStruct, ITKWrapperBase &realWrapper) :
68  _realWrapper(realWrapper), _wrapperSuperStructPtrs(&parentSuperStruct)
69  {
70  _outputBaseField = NULL;
71  _inputBaseField = NULL;
72  _module = NULL;
73  _outputObj = NULL;
74  _inputObj = NULL;
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 NULL. 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->setBaseValue(&_realWrapper);
109  ++_numAddOutputBaseFieldCalls;
110  }
111 
112  //----------------------------------------------------------------------------------
116  //----------------------------------------------------------------------------------
117  void addInputBaseField(Module &module, const char *inputFieldName)
118  {
119  if (!inputFieldName){
120  ML_PRINT_ERROR("ITKMLBaseWrapper::addInputBaseField", ML_BAD_POINTER_OR_0, "Passed field name pointer is NULL. Ignoring call.");
121  return;
122  }
123  if (_numAddInputBaseFieldCalls!=0){
124  ML_PRINT_ERROR("ITKMLBaseWrapper::addOutputBaseField", ML_BAD_STATE, "Ignoring multiple field add.");
125  return;
126  }
127 
128  _module = &module;
129  _inputBaseField = module.addBase(inputFieldName);
130  _inputBaseField->setBaseValue(NULL);
131  ++_numAddInputBaseFieldCalls;
132  }
133 
134  //----------------------------------------------------------------------------------
136  //----------------------------------------------------------------------------------
137  void setNewOutputBaseFieldObject(OBJ_TYPE_POINTER newObject)
138  {
139  _outputObj = newObject;
140  if (_outputBaseField){ _outputBaseField->touch(); }
141  }
142 
143  //----------------------------------------------------------------------------------
145  //----------------------------------------------------------------------------------
147  {
148  return _outputBaseField;
149  }
150 
151  //----------------------------------------------------------------------------------
153  //----------------------------------------------------------------------------------
155  {
156  return _inputBaseField;
157  }
158 
159  //----------------------------------------------------------------------------------
161  //----------------------------------------------------------------------------------
162  OBJ_TYPE_POINTER getWrappedOutputObject()
163  {
164  return _outputObj;
165  }
166 
167  //----------------------------------------------------------------------------------
169  //----------------------------------------------------------------------------------
170  bool hasSuperClass(const std::string &className) const
171  {
172  // Follow NameStrings and InheritrancePointer of class and super classes.
173  // Terminated by return when class name matches or if className==parentClassName
174  // in NameStrings. Termination criterion must be set correctly in Base classes
175  // by specifying class name as super class name! Otherwise loop will not terminate.
176  const WrapperSuperStructPtrs * follow = _wrapperSuperStructPtrs;
177  do {
178  if (follow->nameStrings[0] == className){ return true; }
179  if (follow->nameStrings[0] == follow->nameStrings[1]){ return false; }
180  follow = follow->parentSuperStruct;
181  } while (1);
182  return false;
183  }
184 
185  /*---------------------------------------------------------------------------------- */
189  /*---------------------------------------------------------------------------------- */
190  OBJ_TYPE_POINTER getWrappedInputObject(const RuntimeType * /*rt*/ = NULL)
191  {
192 
193  if (_inputBaseField){
194  /* Get the pointer to the connected object and check its type.*/
195  /* If the type is valid then return a the pointer to the wrapped input object.*/
196  // Get base object from connected input.
197  Base *inPtr = _inputBaseField->getBaseValue();
198 
199  // Is it derived from ITKWrapperBase? If not then we cannot use it.
200  ITKWrapperBase *wrapperBaseInPtr =
201  ML_BASE_IS_A(inPtr, ITKWrapperBase) ?
202  static_cast<ITKWrapperBase*>(inPtr) :
203  NULL;
204  if (!wrapperBaseInPtr){ return NULL; }
205 
206  // Create wrapper of correct type by typecasting.
207  WRAPPER_CLASS_NAME *wrapper = wrapperBaseInPtr ? static_cast<WRAPPER_CLASS_NAME *>(wrapperBaseInPtr) : NULL;
208 
209  // If wrapped object is derived from superclass then return casted pointer, otherwise return NULL.
210  return (wrapper && wrapper->hasSuperClass(wrapper->getName())) ? wrapper->getWrappedOutputObject() : NULL;
211  }
212 
213  /* No valid object connected to base field. */
214  return NULL;
215  }
216 
217 protected:
218 
221 
224 
228 
232 
235 
238  OBJ_TYPE_POINTER _outputObj;
239 
242  OBJ_TYPE_POINTER _inputObj;
243 
247 
251 };
252 
253 
254 //----------------------------------------------------------------------------------
259 //----------------------------------------------------------------------------------
260 #define ML_CREATE_BASE_WRAPPER_FOR_OBJECT_H(CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME, PARENT_CLASS_NAME, ML_EXPORT_MACRO) \
261  \
262  \
263 /* Unified class which can be cast between all wrapped types. It accesses the internally specialized version */ \
264 /* of the wrapped class objects and checks for correct inheritance information. */ \
265 class ML_EXPORT_MACRO WRAPPER_CLASS_NAME : public ITKWrapperBase \
266 { \
267 public: \
268  /* Implement a constructor and call of superclass constructor. */ \
269  WRAPPER_CLASS_NAME(); \
270  \
271  /* Destructor. */ \
272  ~WRAPPER_CLASS_NAME(); \
273  \
274  /* Implemented in .cpp. This interface must not contain class specific code since */ \
275  /* between all class wrapper this class wrapper it must be legal to cast it. */ \
276  /* For documentation of the following methods see ITKMLBaseWrapper. */ \
277  void addOutputBaseField(Module &module, const char *outputFieldName); \
278  void addInputBaseField(Module &module, const char *inputFieldName); \
279  void setNewOutputBaseFieldObject(CLASS_NAME_POINTER newObject); \
280  BaseField *getOutputBaseField(); \
281  BaseField *getInputBaseField(); \
282  CLASS_NAME_POINTER getWrappedOutputObject(); \
283  CLASS_NAME_POINTER getWrappedInputObject(const RuntimeType * /*rt*/ = NULL); \
284  bool hasSuperClass(const std::string &className) const; \
285  const std::string &getName(); \
286  \
287  /* Pointer to the string names wrapped class and parent class. */\
288  static const std::string NameStrings[2]; \
289  \
290  \
291  /* Reference the NameStrings for this class and for the parent class. */\
292  static const WrapperSuperStructPtrs _wrapperSuperStructPtrs; \
293  \
294 private: \
295  \
296  /* Incomplete pointer to internal object which contains type specific information. */ \
297  void *_obj;\
298  \
299  /* The header part of the runtime type system interface. */ \
300  ML_CLASS_HEADER(WRAPPER_CLASS_NAME); \
301 }; \
302 
303 
304 
305 
306 //----------------------------------------------------------------------------------
311 //----------------------------------------------------------------------------------
312 #define ML_CREATE_BASE_WRAPPER_FOR_OBJECT_CPP(CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME, PARENT_CLASS_NAME, ML_EXPORT_MACRO) \
313  \
314  /* Pointer to the string names wrapped class and parent class. */\
315  const std::string WRAPPER_CLASS_NAME::NameStrings[2] = { #CLASS_NAME, #PARENT_CLASS_NAME }; \
316  \
317  /* Reference the NameStrings for this class and for the parent class. */\
318  const WrapperSuperStructPtrs WRAPPER_CLASS_NAME::_wrapperSuperStructPtrs(WRAPPER_CLASS_NAME::NameStrings, &PARENT_CLASS_NAME##Wrapper::_wrapperSuperStructPtrs); \
319  \
320  /* Implement a constructor and call of superclass constructor. */ \
321  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)); } \
322  \
323  /* Destructor. */ \
324  WRAPPER_CLASS_NAME::~WRAPPER_CLASS_NAME(){ if (_obj){ delete (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj)); _obj = NULL; }; } \
325  \
326  /* For documentation of the following methods see ITKMLBaseWrapper. */ \
327  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); } \
328  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) ; } \
329  void WRAPPER_CLASS_NAME::setNewOutputBaseFieldObject(CLASS_NAME_POINTER newObject) { (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->setNewOutputBaseFieldObject(newObject) ; } \
330  BaseField *WRAPPER_CLASS_NAME::getOutputBaseField() { return (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getOutputBaseField() ; } \
331  BaseField *WRAPPER_CLASS_NAME::getInputBaseField() { return (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getInputBaseField() ; } \
332  CLASS_NAME_POINTER WRAPPER_CLASS_NAME::getWrappedOutputObject() { return (static_cast<ITKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getWrappedOutputObject() ; } \
333  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) ; } \
334  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) ; } \
335  \
336  const std::string & WRAPPER_CLASS_NAME::getName() { return NameStrings[0]; }\
337  \
338  /* The .cpp part of the runtime type system interface. */ \
339  ML_CLASS_SOURCE(WRAPPER_CLASS_NAME, ITKWrapperBase); \
340  \
341 
342 ML_END_NAMESPACE
Field to encapsulate a pointer to an ML base object.
Definition: mlFields.h:1187
void setBaseValue(Base *basePointerValue)
Sets the value of the field to basePointerValue.
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition: mlBase.h:62
BaseField * addBase(const char *name)
Creates a Base field with name and adds it to the container. Default value is NULL.
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.
OBJ_TYPE_POINTER getWrappedInputObject(const RuntimeType *=NULL)
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 NULL 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...
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 NULL.
BaseField * getInputBaseField()
Return the pointer to the input base field or NULL if not created.
BaseField * _outputBaseField
Pointer to the field in _module which is used as output interface to another module; default is NULL.
BaseField * _inputBaseField
Pointer to the field in _module which is used as input interface to another module; default is NULL.
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:156
RuntimeType contains type and inheritance information of a class and a static dictionary with informa...
Definition: mlRuntimeType.h:53
Link structure between names and superclass names.
const WrapperSuperStructPtrs *const parentSuperStruct
Pointer to the WrapperSuperStructPtrs of the parent class.
const std::string *const nameStrings
Pointer to the type name and parent type name strings.
WrapperSuperStructPtrs(const std::string *const nameStringsArg, const WrapperSuperStructPtrs *const pSSArgs)
Default constructor.
#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:1035
#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:1040
#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.
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
Definition: mlTypeDefs.h:578