MeVisLab Toolbox Reference
mlVTKMLBaseWrapper.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 "mlInitSystemVTKSupport.h"
17 
19 #include "mlModuleIncludes.h"
20 
21 
22 ML_START_NAMESPACE
23 
24 //----------------------------------------------------------------------------------
28 //----------------------------------------------------------------------------------
30 {
31 public:
34 
35 protected:
38 };
39 
40 
41 //----------------------------------------------------------------------------------
43 //----------------------------------------------------------------------------------
45 public:
47  WrapperSuperStructPtrs(const std::string * const nameStringsArg, const WrapperSuperStructPtrs * const pSSArgs) :
48  nameStrings(nameStringsArg), parentSuperStruct(pSSArgs) {}
49 
51  const std::string * const nameStrings;
52 
54  const WrapperSuperStructPtrs * const parentSuperStruct;
55 };
56 
57 
58 //----------------------------------------------------------------------------------
60 //----------------------------------------------------------------------------------
61 template<class OBJ_TYPE, typename OBJ_TYPE_POINTER, typename WRAPPER_CLASS_NAME> class VTKMLBaseWrapper
62 {
63 public:
64 
65  //----------------------------------------------------------------------------------
67  //----------------------------------------------------------------------------------
68  VTKMLBaseWrapper(const WrapperSuperStructPtrs &parentSuperStruct, VTKWrapperBase &realWrapper) :
69  _realWrapper(realWrapper), _wrapperSuperStructPtrs(&parentSuperStruct)
70  {
71  _outputBaseField = NULL;
72  _inputBaseField = NULL;
73  _module = NULL;
74  _outputObj = NULL;
75  _inputObj = NULL;
76  _numAddOutputBaseFieldCalls=0;
77  _numAddInputBaseFieldCalls=0;
78  }
79 
80  //----------------------------------------------------------------------------------
82  //----------------------------------------------------------------------------------
84  {
85  // Do nothing. The added base field will be destroyed by the Module
86  // automatically and objects are destroyed automatically or by the application.
87  // However this should not live longer than its referenced Module.
88  }
89 
90  //----------------------------------------------------------------------------------
95  //----------------------------------------------------------------------------------
96  void addOutputBaseField(Module &module, const char *outputFieldName)
97  {
98  if (!outputFieldName){
99  ML_PRINT_ERROR("VTKMLBaseWrapper::addOutputBaseField", ML_BAD_POINTER_OR_0, "Passed field name pointer is NULL. Ignoring call.");
100  return;
101  }
102  if (_numAddOutputBaseFieldCalls!=0){
103  ML_PRINT_ERROR("VTKMLBaseWrapper::addOutputBaseField", ML_BAD_STATE, "Ignoring multiple field add.");
104  return;
105  }
106 
107  _module = &module;
108  _outputBaseField = module.addBase(outputFieldName);
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("VTKMLBaseWrapper::addInputBaseField", ML_BAD_POINTER_OR_0, "Passed field name pointer is NULL. Ignoring call.");
122  return;
123  }
124  if (_numAddInputBaseFieldCalls!=0){
125  ML_PRINT_ERROR("VTKMLBaseWrapper::addOutputBaseField", ML_BAD_STATE, "Ignoring multiple field add.");
126  return;
127  }
128 
129  _module = &module;
130  _inputBaseField = module.addBase(inputFieldName);
131  _inputBaseField->setBaseValue(NULL);
132  ++_numAddInputBaseFieldCalls;
133  }
134 
135  //----------------------------------------------------------------------------------
137  //----------------------------------------------------------------------------------
138  void setNewOutputBaseFieldObject(OBJ_TYPE_POINTER newObject)
139  {
140  _outputObj = newObject;
141  if (_outputBaseField){ _outputBaseField->touch(); }
142  }
143 
144  //----------------------------------------------------------------------------------
146  //----------------------------------------------------------------------------------
148  {
149  return _outputBaseField;
150  }
151 
152  //----------------------------------------------------------------------------------
154  //----------------------------------------------------------------------------------
156  {
157  return _inputBaseField;
158  }
159 
160  //----------------------------------------------------------------------------------
162  //----------------------------------------------------------------------------------
163  OBJ_TYPE_POINTER getWrappedOutputObject()
164  {
165  return _outputObj;
166  }
167 
168  //----------------------------------------------------------------------------------
170  //----------------------------------------------------------------------------------
171  bool hasSuperClass(const std::string &className) const
172  {
173  // Follow NameStrings and InheritrancePointer of class and super classes.
174  // Terminated by return when class name matches or if className==parentClassName
175  // in NameStrings. Termination criterion must be set correctly in Base classes
176  // by specifying class name as super class name! Otherwise loop will not terminate.
177  const WrapperSuperStructPtrs * follow = _wrapperSuperStructPtrs;
178  do {
179  if (follow->nameStrings[0] == className){ return true; }
180  if (follow->nameStrings[0] == follow->nameStrings[1]){ return false; }
181  follow = follow->parentSuperStruct;
182  } while (1);
183  return false;
184  }
185 
186  /*---------------------------------------------------------------------------------- */
190  /*---------------------------------------------------------------------------------- */
191  OBJ_TYPE_POINTER getWrappedInputObject(const RuntimeType * /*rt*/ = NULL)
192  {
193 
194  if (_inputBaseField){
195  /* Get the pointer to the connected object and check its type.*/
196  /* If the type is valid then return a the pointer to the wrapped input object.*/
197  // Get base object from connected input.
198  Base *inPtr = _inputBaseField->getBaseValue();
199 
200  // Is it derived from VTKWrapperBase? If not then we cannot use it.
201  VTKWrapperBase *wrapperBaseInPtr =
202  ML_BASE_IS_A(inPtr, VTKWrapperBase) ?
203  static_cast<VTKWrapperBase*>(inPtr) :
204  NULL;
205  if (!wrapperBaseInPtr){ return NULL; }
206 
207  // Create wrapper of correct type by typecasting.
208  WRAPPER_CLASS_NAME *wrapper = wrapperBaseInPtr ? static_cast<WRAPPER_CLASS_NAME *>(wrapperBaseInPtr) : NULL;
209 
210  // If wrapped object is derived from superclass then return casted pointer, otherwise return NULL.
211  return (wrapper && wrapper->hasSuperClass(wrapper->getName())) ? wrapper->getWrappedOutputObject() : NULL;
212  }
213 
214  /* No valid object connected to base field. */
215  return NULL;
216  }
217 
218 protected:
219 
222 
225 
229 
233 
236 
239  OBJ_TYPE_POINTER _outputObj;
240 
243  OBJ_TYPE_POINTER _inputObj;
244 
248 
252 };
253 
254 
255 //----------------------------------------------------------------------------------
260 //----------------------------------------------------------------------------------
261 #define ML_CREATE_BASE_WRAPPER_FOR_OBJECT_H(CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME, PARENT_CLASS_NAME, ML_EXPORT_MACRO) \
262  \
263  \
264 /* Unified class which can be cast between all wrapped types. It accesses the internally specialized version */ \
265 /* of the wrapped class objects and checks for correct inheritance information. */ \
266 class ML_EXPORT_MACRO WRAPPER_CLASS_NAME : public VTKWrapperBase \
267 { \
268 public: \
269  /* Implement a constructor and call of superclass constructor. */ \
270  WRAPPER_CLASS_NAME(); \
271  \
272  /* Destructor. */ \
273  ~WRAPPER_CLASS_NAME(); \
274  \
275  /* Implemented in .cpp. This interface must not contain class specific code since */ \
276  /* between all class wrapper this class wrapper it must be legal to cast it. */ \
277  /* For documentation of the following methods see VTKMLBaseWrapper. */ \
278  void addOutputBaseField(Module &module, const char *outputFieldName); \
279  void addInputBaseField(Module &module, const char *inputFieldName); \
280  void setNewOutputBaseFieldObject(CLASS_NAME_POINTER newObject); \
281  BaseField *getOutputBaseField(); \
282  BaseField *getInputBaseField(); \
283  CLASS_NAME_POINTER getWrappedOutputObject(); \
284  CLASS_NAME_POINTER getWrappedInputObject(const RuntimeType * /*rt*/ = NULL); \
285  bool hasSuperClass(const std::string &className) const; \
286  \
287  virtual std::string detailString() const override { \
288  std::string result; \
289  vtkObjectType* object = const_cast<WRAPPER_CLASS_NAME*>(this)->getWrappedOutputObject(); \
290  if (object) {\
291  result = object->GetClassName();\
292  }\
293  return result;\
294  }\
295  \
296  const std::string &getName(); \
297  \
298  /* Pointer to the string names wrapped class and parent class. */\
299  static const std::string NameStrings[2]; \
300  \
301  \
302  /* Reference the NameStrings for this class and for the parent class. */\
303  static const WrapperSuperStructPtrs _wrapperSuperStructPtrs; \
304  \
305 private: \
306  \
307  /* Incomplete pointer to internal object which contains type specific information. */ \
308  void *_obj;\
309  \
310  /* The header part of the runtime type system interface. */ \
311  ML_CLASS_HEADER(WRAPPER_CLASS_NAME); \
312 }; \
313 
314 
315 
316 
317 //----------------------------------------------------------------------------------
322 //----------------------------------------------------------------------------------
323 #define ML_CREATE_BASE_WRAPPER_FOR_OBJECT_CPP(CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME, PARENT_CLASS_NAME, ML_EXPORT_MACRO) \
324  \
325  /* Pointer to the string names wrapped class and parent class. */\
326  const std::string WRAPPER_CLASS_NAME::NameStrings[2] = { #CLASS_NAME, #PARENT_CLASS_NAME }; \
327  \
328  /* Reference the NameStrings for this class and for the parent class. */\
329  const WrapperSuperStructPtrs WRAPPER_CLASS_NAME::_wrapperSuperStructPtrs(WRAPPER_CLASS_NAME::NameStrings, &PARENT_CLASS_NAME##Wrapper::_wrapperSuperStructPtrs); \
330  \
331  /* Implement a constructor and call of superclass constructor. */ \
332  WRAPPER_CLASS_NAME::WRAPPER_CLASS_NAME(){ _obj = static_cast<void*>(new VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>(WRAPPER_CLASS_NAME::_wrapperSuperStructPtrs, *this)); } \
333  \
334  /* Destructor. */ \
335  WRAPPER_CLASS_NAME::~WRAPPER_CLASS_NAME(){ if (_obj){ delete (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj)); _obj = NULL; }; } \
336  \
337  /* For documentation of the following methods see VTKMLBaseWrapper. */ \
338  void WRAPPER_CLASS_NAME::addOutputBaseField(Module &module, const char *outputFieldName){ (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->addOutputBaseField(module, outputFieldName); } \
339  void WRAPPER_CLASS_NAME::addInputBaseField(Module &module, const char *inputFieldName) { (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->addInputBaseField(module, inputFieldName) ; } \
340  void WRAPPER_CLASS_NAME::setNewOutputBaseFieldObject(CLASS_NAME_POINTER newObject) { (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->setNewOutputBaseFieldObject(newObject) ; } \
341  BaseField *WRAPPER_CLASS_NAME::getOutputBaseField() { return (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getOutputBaseField() ; } \
342  BaseField *WRAPPER_CLASS_NAME::getInputBaseField() { return (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getInputBaseField() ; } \
343  CLASS_NAME_POINTER WRAPPER_CLASS_NAME::getWrappedOutputObject() { return (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getWrappedOutputObject() ; } \
344  CLASS_NAME_POINTER WRAPPER_CLASS_NAME::getWrappedInputObject(const RuntimeType *rt) { return (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getWrappedInputObject(rt) ; } \
345  bool WRAPPER_CLASS_NAME::hasSuperClass(const std::string &className) const { return (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->hasSuperClass(className) ; } \
346  \
347  const std::string & WRAPPER_CLASS_NAME::getName() { return NameStrings[0]; }\
348  \
349  /* The .cpp part of the runtime type system interface. */ \
350  ML_CLASS_SOURCE(WRAPPER_CLASS_NAME, VTKWrapperBase); \
351  \
352 
353 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.
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
Class to create a Base wrapper around an object of a certain template type.
virtual ~VTKMLBaseWrapper()
Destructor.
MLint _numAddOutputBaseFieldCalls
Counter for the number of addOutputBaseField method calls.
BaseField * getOutputBaseField()
Return the pointer to the output base field or NULL if not created.
void addInputBaseField(Module &module, const char *inputFieldName)
Method to add a base field to the passed Module module.
BaseField * getInputBaseField()
Return the pointer to the input base field or NULL if not created.
OBJ_TYPE_POINTER getWrappedInputObject(const RuntimeType *=NULL)
void addOutputBaseField(Module &module, const char *outputFieldName)
Method to add a base field to the passed Module module.
bool hasSuperClass(const std::string &className) const
Return true if the passed parent class name is in the list of classes.
VTKMLBaseWrapper(const WrapperSuperStructPtrs &parentSuperStruct, VTKWrapperBase &realWrapper)
Constructor.
void setNewOutputBaseFieldObject(OBJ_TYPE_POINTER newObject)
Set base field so that it points to the new object.
OBJ_TYPE_POINTER getWrappedOutputObject()
Return the pointer to the wrapped object.
const WrapperSuperStructPtrs * _wrapperSuperStructPtrs
Reference to the inheritance pointer correlated to this class.
Base & _realWrapper
Reference to the real wrapper object creating this.
BaseField * _outputBaseField
Pointer to the field in _module which is used as output interface to another module; default is NULL.
OBJ_TYPE_POINTER _inputObj
Smart pointer to the connected OBJ_TYPE wrapped by this class if an input connector is created or NUL...
BaseField * _inputBaseField
Pointer to the field in _module which is used as input interface to another module; default is NULL.
Module * _module
Pointer to the Module which shall handle this; default is NULL.
OBJ_TYPE_POINTER _outputObj
Smart pointer to the OBJ_TYPE created and wrapped by this class if an output connector is created.
MLint _numAddInputBaseFieldCalls
Counter for the number of addInputBaseField method calls.
Helper class to have a common base class for all derived wrapper.
VTKWrapperBase()
Private on purpose to avoid instantiations.
ML_ABSTRACT_CLASS_HEADER(VTKWrapperBase)
Register this class abstractly.
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 MLVTK_SUPPORT_EXPORT
When included by other libraries MLVTK_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