MeVisLabToolboxReference
FMEwork/VTK/Sources/VTK/MLVTK/VTKSupport/mlVTKMLBaseWrapper.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00004 
00009 //----------------------------------------------------------------------------------
00010 #ifndef __mlVTKMLBaseWrapper_H
00011 #define __mlVTKMLBaseWrapper_H
00012 
00014 #include "mlInitSystemVTKSupport.h"
00015 
00017 #ifndef __mlModuleIncludes_H
00018 #include "mlModuleIncludes.h"
00019 #endif
00020 
00021 
00022 ML_START_NAMESPACE
00023 
00024 //----------------------------------------------------------------------------------
00028 //----------------------------------------------------------------------------------
00029 class MLVTK_SUPPORT_EXPORT VTKWrapperBase : public Base
00030 {
00031 public:
00033   VTKWrapperBase(){};
00034 
00035 protected:
00037   ML_ABSTRACT_CLASS_HEADER(VTKWrapperBase);
00038 };
00039 
00040 
00041 //----------------------------------------------------------------------------------
00043 //----------------------------------------------------------------------------------
00044 class WrapperSuperStructPtrs {
00045 public:
00047   WrapperSuperStructPtrs(const std::string * const nameStringsArg, const WrapperSuperStructPtrs * const pSSArgs) :
00048      nameStrings(nameStringsArg), parentSuperStruct(pSSArgs) {}
00049 
00051   const std::string * const nameStrings;
00052 
00054   const WrapperSuperStructPtrs * const parentSuperStruct;
00055 };
00056 
00057 
00058 //----------------------------------------------------------------------------------
00060 //----------------------------------------------------------------------------------
00061 template<class OBJ_TYPE, typename OBJ_TYPE_POINTER, typename WRAPPER_CLASS_NAME> class VTKMLBaseWrapper
00062 {
00063 public:
00064 
00065   //----------------------------------------------------------------------------------
00067   //----------------------------------------------------------------------------------
00068   VTKMLBaseWrapper(const WrapperSuperStructPtrs &parentSuperStruct, VTKWrapperBase &realWrapper) :
00069     _realWrapper(realWrapper), _wrapperSuperStructPtrs(&parentSuperStruct)
00070   {
00071     _outputBaseField = NULL;
00072     _inputBaseField  = NULL;
00073     _module          = NULL;
00074     _outputObj       = NULL;
00075     _inputObj        = NULL;
00076     _numAddOutputBaseFieldCalls=0;
00077     _numAddInputBaseFieldCalls=0;
00078   }
00079 
00080   //----------------------------------------------------------------------------------
00082   //----------------------------------------------------------------------------------
00083   virtual ~VTKMLBaseWrapper()
00084   {
00085     // Do nothing. The added base field will be destroyed by the Module
00086     // automatically and objects are destroyed automatically or by the application.
00087     // However this should not live longer than its referenced Module.
00088   }
00089 
00090   //----------------------------------------------------------------------------------
00095   //----------------------------------------------------------------------------------
00096   void addOutputBaseField(Module &module, const char *outputFieldName)
00097   {
00098     if (!outputFieldName){
00099       ML_PRINT_ERROR("VTKMLBaseWrapper::addOutputBaseField", ML_BAD_POINTER_OR_0, "Passed field name pointer is NULL. Ignoring call.");
00100       return;
00101     }
00102     if (_numAddOutputBaseFieldCalls!=0){
00103       ML_PRINT_ERROR("VTKMLBaseWrapper::addOutputBaseField", ML_BAD_STATE, "Ignoring multiple field add.");
00104       return;
00105     }
00106 
00107     _module = &module;
00108     _outputBaseField = module.addBase(outputFieldName);
00109     _outputBaseField->setBaseValue(&_realWrapper);
00110     ++_numAddOutputBaseFieldCalls;
00111   }
00112 
00113   //----------------------------------------------------------------------------------
00117   //----------------------------------------------------------------------------------
00118   void addInputBaseField(Module &module, const char *inputFieldName)
00119   {
00120     if (!inputFieldName){
00121       ML_PRINT_ERROR("VTKMLBaseWrapper::addInputBaseField", ML_BAD_POINTER_OR_0, "Passed field name pointer is NULL. Ignoring call.");
00122       return;
00123     }
00124     if (_numAddInputBaseFieldCalls!=0){
00125       ML_PRINT_ERROR("VTKMLBaseWrapper::addOutputBaseField", ML_BAD_STATE, "Ignoring multiple field add.");
00126       return;
00127     }
00128 
00129     _module = &module;
00130     _inputBaseField = module.addBase(inputFieldName);
00131     _inputBaseField->setBaseValue(NULL);
00132     ++_numAddInputBaseFieldCalls;
00133   }
00134 
00135   //----------------------------------------------------------------------------------
00137   //----------------------------------------------------------------------------------
00138   void setNewOutputBaseFieldObject(OBJ_TYPE_POINTER newObject)
00139   {
00140     _outputObj = newObject;
00141     if (_outputBaseField){ _outputBaseField->touch(); }
00142   }
00143 
00144   //----------------------------------------------------------------------------------
00146   //----------------------------------------------------------------------------------
00147   BaseField *getOutputBaseField()
00148   {
00149     return _outputBaseField;
00150   }
00151 
00152   //----------------------------------------------------------------------------------
00154   //----------------------------------------------------------------------------------
00155   BaseField *getInputBaseField()
00156   {
00157     return _inputBaseField;
00158   }
00159 
00160   //----------------------------------------------------------------------------------
00162   //----------------------------------------------------------------------------------
00163   OBJ_TYPE_POINTER getWrappedOutputObject()
00164   {
00165     return _outputObj;
00166   }
00167 
00168   //----------------------------------------------------------------------------------
00170   //----------------------------------------------------------------------------------
00171   bool hasSuperClass(const std::string &className) const
00172   {
00173     // Follow NameStrings and InheritrancePointer of class and super classes.
00174     // Terminated by return when class name matches or if className==parentClassName
00175     // in NameStrings. Termination criterion must be set correctly in Base classes
00176     // by specifying class name as super class name! Otherwise loop will not terminate.
00177     const WrapperSuperStructPtrs * follow = _wrapperSuperStructPtrs;
00178     do {
00179       if (follow->nameStrings[0] == className){ return true; }
00180       if (follow->nameStrings[0] == follow->nameStrings[1]){ return false; }
00181       follow = follow->parentSuperStruct;
00182     } while (1);
00183     return false;
00184   }
00185 
00186   /*---------------------------------------------------------------------------------- */
00190   /*---------------------------------------------------------------------------------- */
00191   OBJ_TYPE_POINTER getWrappedInputObject(const RuntimeType * /*rt*/ = NULL)
00192   {
00193 
00194     if (_inputBaseField){
00195       /* Get the pointer to the connected object and check its type.*/
00196       /* If the type is valid then return a the pointer to the wrapped input object.*/
00197       // Get base object from connected input.
00198       Base *inPtr = _inputBaseField->getBaseValue();
00199 
00200       // Is it derived from VTKWrapperBase? If not then we cannot use it.
00201       VTKWrapperBase *wrapperBaseInPtr =
00202          ML_BASE_IS_A(inPtr, VTKWrapperBase) ?
00203          static_cast<VTKWrapperBase*>(inPtr) :
00204          NULL;
00205       if (!wrapperBaseInPtr){ return NULL; }
00206 
00207       // Create wrapper of correct type by typecasting.
00208       WRAPPER_CLASS_NAME *wrapper = wrapperBaseInPtr ? static_cast<WRAPPER_CLASS_NAME *>(wrapperBaseInPtr) : NULL;
00209 
00210       // If wrapped object is derived from superclass then return casted pointer, otherwise return NULL.
00211       return (wrapper && wrapper->hasSuperClass(wrapper->getName())) ? wrapper->getWrappedOutputObject() : NULL;
00212     }
00213 
00214     /* No valid object connected to base field. */
00215     return NULL;
00216   }
00217 
00218 protected:
00219 
00221   Base &_realWrapper;
00222 
00224   const WrapperSuperStructPtrs *_wrapperSuperStructPtrs;
00225 
00228   BaseField                  *_outputBaseField;
00229 
00232   BaseField                  *_inputBaseField;
00233 
00235   Module                     *_module;
00236 
00239   OBJ_TYPE_POINTER            _outputObj;
00240 
00243   OBJ_TYPE_POINTER            _inputObj;
00244 
00247   MLint                       _numAddOutputBaseFieldCalls;
00248 
00251   MLint                       _numAddInputBaseFieldCalls;
00252 };
00253 
00254 
00255 //----------------------------------------------------------------------------------
00260 //----------------------------------------------------------------------------------
00261 #define ML_CREATE_BASE_WRAPPER_FOR_OBJECT_H(CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME, PARENT_CLASS_NAME, ML_EXPORT_MACRO) \
00262  \
00263  \
00264 /* Unified class which can be cast between all wrapped types. It accesses the internally specialized version */ \
00265 /* of the wrapped class objects and checks for correct inheritance information.                              */ \
00266 class ML_EXPORT_MACRO WRAPPER_CLASS_NAME : public VTKWrapperBase \
00267 { \
00268 public: \
00269   /* Implement a constructor and call of superclass constructor. */ \
00270   WRAPPER_CLASS_NAME(); \
00271   \
00272   /* Destructor. */ \
00273   ~WRAPPER_CLASS_NAME(); \
00274   \
00275   /* Implemented in .cpp. This interface must not contain class specific code since */ \
00276   /* between all class wrapper this class wrapper it must be legal to cast it.      */ \
00277   /* For documentation of the following methods see VTKMLBaseWrapper.               */ \
00278   void               addOutputBaseField(Module &module, const char *outputFieldName); \
00279   void               addInputBaseField(Module &module, const char *inputFieldName); \
00280   void               setNewOutputBaseFieldObject(CLASS_NAME_POINTER newObject); \
00281   BaseField         *getOutputBaseField(); \
00282   BaseField         *getInputBaseField(); \
00283   CLASS_NAME_POINTER getWrappedOutputObject(); \
00284   CLASS_NAME_POINTER getWrappedInputObject(const RuntimeType * /*rt*/ = NULL); \
00285   bool               hasSuperClass(const std::string &className) const; \
00286   const std::string &getName(); \
00287   \
00288   /* Pointer to the string names wrapped class and parent class. */\
00289   static const std::string         NameStrings[2]; \
00290   \
00291   \
00292   /* Reference the NameStrings for this class and for the parent class. */\
00293   static const WrapperSuperStructPtrs _wrapperSuperStructPtrs; \
00294   \
00295 private: \
00296   \
00297   /* Incomplete pointer to internal object which contains type specific information. */ \
00298   void *_obj;\
00299   \
00300   /* The header part of the runtime type system interface. */  \
00301   ML_CLASS_HEADER(WRAPPER_CLASS_NAME); \
00302 }; \
00303 
00304 
00305 
00306 
00307 //----------------------------------------------------------------------------------
00312 //----------------------------------------------------------------------------------
00313 #define ML_CREATE_BASE_WRAPPER_FOR_OBJECT_CPP(CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME, PARENT_CLASS_NAME, ML_EXPORT_MACRO) \
00314   \
00315   /* Pointer to the string names wrapped class and parent class. */\
00316   const std::string         WRAPPER_CLASS_NAME::NameStrings[2]         = { #CLASS_NAME, #PARENT_CLASS_NAME }; \
00317   \
00318   /* Reference the NameStrings for this class and for the parent class. */\
00319   const WrapperSuperStructPtrs WRAPPER_CLASS_NAME::_wrapperSuperStructPtrs(WRAPPER_CLASS_NAME::NameStrings, &PARENT_CLASS_NAME##Wrapper::_wrapperSuperStructPtrs); \
00320   \
00321   /* Implement a constructor and call of superclass constructor. */ \
00322   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)); } \
00323   \
00324   /* Destructor. */ \
00325   WRAPPER_CLASS_NAME::~WRAPPER_CLASS_NAME(){ if (_obj){ delete (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj)); _obj = NULL; }; } \
00326   \
00327   /* For documentation of the following methods see VTKMLBaseWrapper.               */ \
00328   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); } \
00329   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)  ; } \
00330   void                WRAPPER_CLASS_NAME::setNewOutputBaseFieldObject(CLASS_NAME_POINTER newObject)      { (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->setNewOutputBaseFieldObject(newObject)     ; } \
00331   BaseField          *WRAPPER_CLASS_NAME::getOutputBaseField()                                           { return (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getOutputBaseField()                ; } \
00332   BaseField          *WRAPPER_CLASS_NAME::getInputBaseField()                                            { return (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getInputBaseField()                 ; } \
00333   CLASS_NAME_POINTER  WRAPPER_CLASS_NAME::getWrappedOutputObject()                                       { return (static_cast<VTKMLBaseWrapper<CLASS_NAME, CLASS_NAME_POINTER, WRAPPER_CLASS_NAME>*>(_obj))->getWrappedOutputObject()            ; } \
00334   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)           ; } \
00335   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)            ; } \
00336   \
00337   const std::string & WRAPPER_CLASS_NAME::getName()                                                      { return NameStrings[0]; }\
00338   \
00339   /* The .cpp part of the runtime type system interface. */ \
00340   ML_CLASS_SOURCE(WRAPPER_CLASS_NAME, VTKWrapperBase);   \
00341   \
00342 
00343 
00344 
00345 ML_END_NAMESPACE
00346 
00347 #endif