IGSTK
|
00001 /*========================================================================= 00002 00003 Program: Image Guided Surgery Software Toolkit 00004 Module: $RCSfile: igstkMacros.h,v $ 00005 Language: C++ 00006 Date: $Date: 2008-06-29 13:37:53 $ 00007 Version: $Revision: 1.47 $ 00008 00009 Copyright (c) ISC Insight Software Consortium. All rights reserved. 00010 See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 #ifndef __igstkMacros_h 00018 #define __igstkMacros_h 00019 00020 #include "igstkLogger.h" 00021 #include "itkCommand.h" 00022 #include <cstdlib> 00023 00029 #if defined(_MSC_VER) 00030 // Warning about: identifier was truncated to '255' characters 00031 // in the debug information (MVC6.0 Debug) 00032 #pragma warning( disable : 4786 ) 00033 // warning C4503: 'insert' : decorated name length exceeded, 00034 // name was truncated 00035 #pragma warning ( disable : 4503 ) 00036 #endif 00037 00038 namespace igstk 00039 { 00040 00044 #define igstkLogMacro( x, y) \ 00045 { \ 00046 if (this->GetLogger() ) \ 00047 { \ 00048 if (this->GetLogger()->ShouldBuildMessage( ::igstk::Logger::x ) ) \ 00049 { \ 00050 ::itk::OStringStream message; \ 00051 message << y; \ 00052 this->GetLogger()->Write(::itk::Logger::x, message.str()); \ 00053 } \ 00054 } \ 00055 } 00056 00057 00061 #define igstkLogMacroStatic( obj, x, y) \ 00062 { \ 00063 if (obj->GetLogger() ) \ 00064 { \ 00065 if (obj->GetLogger()->ShouldBuildMessage( ::igstk::Logger::x ) ) \ 00066 { \ 00067 ::itk::OStringStream message; \ 00068 message << y; \ 00069 obj->GetLogger()->Write(::itk::Logger::x, message.str()); \ 00070 } \ 00071 } \ 00072 } 00073 00074 00078 #define igstkLogMacro2( logger, x, y) \ 00079 { \ 00080 if ( logger ) \ 00081 { \ 00082 if (logger->ShouldBuildMessage( ::igstk::Logger::x )) \ 00083 { \ 00084 ::itk::OStringStream message; \ 00085 message << y; \ 00086 logger->Write(::itk::Logger::x, message.str()); \ 00087 } \ 00088 } \ 00089 } 00090 00091 00093 #define igstkSetMacro(name,type) \ 00094 virtual void Set##name (const type & _arg) \ 00095 { \ 00096 if (this->m_##name != _arg) \ 00097 { \ 00098 this->m_##name = _arg; \ 00099 } \ 00100 } 00101 00102 00105 #define igstkGetMacro(name,type) \ 00106 virtual const type & Get##name () const \ 00107 { \ 00108 return this->m_##name; \ 00109 } 00110 00111 00115 #define igstkNewMacro(x) \ 00116 static Pointer New(void) \ 00117 { \ 00118 Pointer smartPtr; \ 00119 x *rawPtr = new x; \ 00120 smartPtr = rawPtr; \ 00121 rawPtr->UnRegister(); \ 00122 return smartPtr; \ 00123 } 00124 00125 00128 #define igstkTypeMacro(thisClass,superclass) \ 00129 virtual const char *GetNameOfClass() const {return #thisClass;} 00130 00131 00134 #if defined(__GNUC__) 00135 #define igstkFriendClassMacro(type) friend class type 00136 #else 00137 #define igstkFriendClassMacro(type) friend type 00138 #endif 00139 00140 00143 #define igstkLoggerMacro() \ 00144 public: \ 00145 typedef ::igstk::Logger LoggerType; \ 00146 protected: \ 00147 LoggerType* GetLogger() const { return m_Logger; } \ 00148 private: \ 00149 mutable LoggerType::Pointer m_Logger; \ 00150 public: \ 00151 void SetLogger(LoggerType* logger) { m_Logger = logger; } 00152 00153 00157 #define igstkSetStringMacro(name) \ 00158 virtual void Set##name (const char* _arg) \ 00159 { \ 00160 if ( _arg && (_arg == this->m_##name) ) { return;} \ 00161 if (_arg) \ 00162 { \ 00163 this->m_##name = _arg;\ 00164 } \ 00165 else \ 00166 { \ 00167 this->m_##name = ""; \ 00168 } \ 00169 this->Modified(); \ 00170 } \ 00171 virtual void Set##name (const std::string & _arg) \ 00172 { \ 00173 this->Set##name( _arg.c_str() ); \ 00174 } \ 00175 00176 00180 #define igstkGetStringMacro(name) \ 00181 virtual const char* Get##name () const \ 00182 { \ 00183 return this->m_##name.c_str(); \ 00184 } 00185 00186 00190 #define igstkStateMachineMacroBase( igstktypename ) \ 00191 private: \ 00192 typedef ::igstk::StateMachine< Self > StateMachineType; \ 00193 typedef igstktypename StateMachineType::TMemberFunctionPointer ActionType; \ 00194 typedef igstktypename StateMachineType::StateType StateType; \ 00195 typedef igstktypename StateMachineType::InputType InputType; \ 00196 typedef igstktypename StateMachineType::OutputStreamType OutputStreamType; \ 00197 igstkFriendClassMacro( ::igstk::StateMachine< Self > ); \ 00198 StateMachineType m_StateMachine; \ 00199 typedef ::itk::ReceptorMemberCommand< Self > ReceptorObserverType; \ 00200 typedef igstktypename ReceptorObserverType::Pointer \ 00201 ReceptorObserverPointer; \ 00202 public: \ 00203 void ExportStateMachineDescription( OutputStreamType & ostr, \ 00204 bool skipLoops=false ) const \ 00205 { m_StateMachine.ExportDescription( ostr, skipLoops ); } \ 00206 void ExportStateMachineDescriptionToLTS( OutputStreamType & ostr,\ 00207 bool skipLoops=false ) const \ 00208 { m_StateMachine.ExportDescriptionToLTS( ostr, skipLoops ); } \ 00209 void ExportStateMachineDescriptionToSCXML( OutputStreamType & ostr,\ 00210 bool skipLoops=false ) const \ 00211 { m_StateMachine.ExportDescriptionToSCXML( ostr, skipLoops ); } 00212 00213 #define EMPTYPARAMETER 00214 00216 #define igstkStateMachineMacro() igstkStateMachineMacroBase( EMPTYPARAMETER ) 00217 00219 #define igstkStateMachineTemplatedMacro() igstkStateMachineMacroBase( typename ) 00220 00221 00223 #define igstkDeclareInputMacro( inputname ) \ 00224 InputType m_##inputname##Input 00225 00226 00228 #define igstkDeclareStateMacro( inputname ) \ 00229 StateType m_##inputname##State 00230 00231 00233 #define igstkAddInputMacro( inputname ) \ 00234 this->m_StateMachine.AddInput( this->m_##inputname##Input, \ 00235 #inputname"Input" ); 00236 00237 00239 #define igstkAddStateMacro( statename ) \ 00240 this->m_StateMachine.AddState( this->m_##statename##State,\ 00241 #statename"State" ); 00242 00244 #define igstkAddTransitionMacro( state1, input, state2, action ) \ 00245 this->m_StateMachine.AddTransition( this->m_##state1##State, \ 00246 this->m_##input##Input, \ 00247 this->m_##state2##State, \ 00248 & Self::action##Processing ); 00249 00250 00252 #define igstkSetInitialStateMacro( inputname ) \ 00253 this->m_StateMachine.SelectInitialState( this->m_##inputname##State ); 00254 00255 00257 #define igstkPushInputMacro( inputname ) \ 00258 this->m_StateMachine.PushInput( this->m_##inputname##Input ); 00259 00260 00262 #define igstkStandardClassBasicTraitsMacro( classname, superclassname ) \ 00263 typedef classname Self; \ 00264 typedef superclassname Superclass; \ 00265 typedef ::itk::SmartPointer< Self > Pointer; \ 00266 typedef ::itk::SmartPointer< const Self > ConstPointer; \ 00267 igstkTypeMacro( classname, superclassname); 00268 00270 #define igstkStandardAbstractClassTraitsMacro( classname, superclassname ) \ 00271 igstkStandardClassBasicTraitsMacro( classname, superclassname ) \ 00272 igstkStateMachineMacro(); 00273 00275 #define igstkStandardClassTraitsMacro( classname, superclassname ) \ 00276 igstkStandardAbstractClassTraitsMacro( classname, superclassname ) \ 00277 igstkNewMacro( Self ); 00278 00280 #define igstkStandardTemplatedAbstractClassTraitsMacro( classname, \ 00281 superclassname ) \ 00282 igstkStandardClassBasicTraitsMacro( classname, superclassname ) \ 00283 igstkStateMachineTemplatedMacro(); 00284 00286 #define igstkStandardTemplatedClassTraitsMacro( classname, superclassname ) \ 00287 igstkStandardTemplatedAbstractClassTraitsMacro( classname, superclassname ) \ 00288 igstkNewMacro( Self ); 00289 00290 00293 #define igstkEventTransductionMacro( event, input ) \ 00294 private: \ 00295 ReceptorObserverPointer m_Observer##event##input; \ 00296 void Callback##event##input##Input( const ::itk::EventObject & ) \ 00297 { \ 00298 igstkPushInputMacro( input ); \ 00299 m_StateMachine.ProcessInputs(); \ 00300 } \ 00301 public: \ 00302 void Observe##event##Event(const ::igstk::Object * object ) \ 00303 { \ 00304 m_Observer##event##input = ReceptorObserverType::New(); \ 00305 m_Observer##event##input->SetCallbackFunction( this, \ 00306 & Self::Callback##event##input##Input ); \ 00307 unsigned long tag = object->AddObserver( \ 00308 ::igstk::event##Event(),m_Observer##event##input ); \ 00309 this->RegisterObservedObject( object, tag ); \ 00310 } 00311 00312 00316 #define igstkLoadedEventTransductionMacro( event, input ) \ 00317 private: \ 00318 ReceptorObserverPointer m_Observer##event##input; \ 00319 ::igstk::event##Event::PayloadType m_##input##InputToBeSet; \ 00320 void Callback##event##input##Input( const ::itk::EventObject & eventvar ) \ 00321 { \ 00322 const ::igstk::event##Event * realevent = \ 00323 dynamic_cast < const ::igstk::event##Event * > ( &eventvar ); \ 00324 if( realevent ) \ 00325 { \ 00326 m_##input##InputToBeSet = realevent->Get(); \ 00327 igstkPushInputMacro( input ); \ 00328 m_StateMachine.ProcessInputs(); \ 00329 } \ 00330 } \ 00331 public: \ 00332 void Observe##input##Input(const ::igstk::Object * object ) \ 00333 { \ 00334 m_Observer##event##input = ReceptorObserverType::New(); \ 00335 m_Observer##event##input->SetCallbackFunction( this,\ 00336 & Self::Callback##event##input##Input ); \ 00337 unsigned long tag = object->AddObserver( \ 00338 ::igstk::event##Event(),m_Observer##event##input ); \ 00339 this->RegisterObservedObject( object, tag ); \ 00340 } 00341 #define igstkLoadedObjectEventTransductionMacro( event, input ) \ 00342 private: \ 00343 ReceptorObserverPointer m_Observer##event##input; \ 00344 ::igstk::event##Event::PayloadType * m_##input##InputToBeSet; \ 00345 void Callback##event##input##Input( const ::itk::EventObject & eventvar ) \ 00346 { \ 00347 const ::igstk::event##Event * realevent = \ 00348 dynamic_cast < const ::igstk::event##Event * > ( &eventvar ); \ 00349 if( realevent ) \ 00350 { \ 00351 m_##input##InputToBeSet = realevent->Get(); \ 00352 igstkPushInputMacro( input ); \ 00353 m_StateMachine.ProcessInputs(); \ 00354 } \ 00355 } \ 00356 public: \ 00357 void Observe##input##Input(const ::igstk::Object * object ) \ 00358 { \ 00359 m_Observer##event##input = ReceptorObserverType::New(); \ 00360 m_Observer##event##input->SetCallbackFunction( this,\ 00361 & Self::Callback##event##input##Input ); \ 00362 unsigned long tag = object->AddObserver( \ 00363 ::igstk::event##Event(),m_Observer##event##input ); \ 00364 this->RegisterObservedObject( object, tag ); \ 00365 } 00366 00367 00368 #define igstkObserverMacro( name, eventType, payloadType ) \ 00369 class name##Observer : public ::itk::Command \ 00370 { \ 00371 public: \ 00372 typedef name##Observer Self; \ 00373 typedef ::itk::Command Superclass;\ 00374 typedef ::itk::SmartPointer<Self> Pointer;\ 00375 itkNewMacro( Self );\ 00376 protected:\ 00377 name##Observer() \ 00378 {\ 00379 m_GotObject = false;\ 00380 }\ 00381 ~name##Observer() {}\ 00382 public:\ 00383 typedef eventType EventType;\ 00384 void Execute(itk::Object *caller, const itk::EventObject & event)\ 00385 {\ 00386 const itk::Object * constCaller = caller;\ 00387 this->Execute( constCaller, event );\ 00388 }\ 00389 void Execute(const itk::Object *caller, const itk::EventObject & event)\ 00390 {\ 00391 m_GotObject = false;\ 00392 if( EventType().CheckEvent( &event ) )\ 00393 {\ 00394 const EventType * objectEvent = \ 00395 dynamic_cast< const EventType *>( &event );\ 00396 if( objectEvent )\ 00397 {\ 00398 m_Object = objectEvent->Get();\ 00399 m_GotObject = true;\ 00400 }\ 00401 }\ 00402 }\ 00403 bool Got##name() const\ 00404 {\ 00405 return m_GotObject;\ 00406 }\ 00407 void Reset() \ 00408 {\ 00409 m_GotObject = false; \ 00410 }\ 00411 payloadType Get##name() const\ 00412 {\ 00413 return m_Object;\ 00414 }\ 00415 private:\ 00416 payloadType m_Object;\ 00417 bool m_GotObject;\ 00418 }; 00419 00420 00421 #define igstkObserverObjectMacro( name, eventType, payloadType ) \ 00422 class name##Observer : public ::itk::Command \ 00423 { \ 00424 public: \ 00425 typedef name##Observer Self; \ 00426 typedef ::itk::Command Superclass;\ 00427 typedef ::itk::SmartPointer<Self> Pointer;\ 00428 itkNewMacro( Self );\ 00429 protected:\ 00430 name##Observer() \ 00431 {\ 00432 m_GotObject = false;\ 00433 }\ 00434 ~name##Observer() {}\ 00435 public:\ 00436 typedef eventType EventType;\ 00437 void Execute(itk::Object *caller, const itk::EventObject & event)\ 00438 {\ 00439 const itk::Object * constCaller = caller;\ 00440 this->Execute( constCaller, event );\ 00441 }\ 00442 void Execute(const itk::Object *caller, const itk::EventObject & event)\ 00443 {\ 00444 m_GotObject = false;\ 00445 if( EventType().CheckEvent( &event ) )\ 00446 {\ 00447 const EventType * objectEvent = \ 00448 dynamic_cast< const EventType *>( &event );\ 00449 if( objectEvent )\ 00450 {\ 00451 m_Object = objectEvent->Get();\ 00452 m_GotObject = true;\ 00453 }\ 00454 }\ 00455 }\ 00456 bool Got##name() const\ 00457 {\ 00458 return m_GotObject;\ 00459 }\ 00460 void Reset() \ 00461 {\ 00462 m_GotObject = false; \ 00463 }\ 00464 payloadType::Pointer Get##name() const\ 00465 {\ 00466 return m_Object;\ 00467 }\ 00468 private:\ 00469 payloadType::Pointer m_Object;\ 00470 bool m_GotObject;\ 00471 }; 00472 00473 00474 #define igstkObserverConstObjectMacro( name, eventType, payloadType ) \ 00475 class name##Observer : public ::itk::Command \ 00476 { \ 00477 public: \ 00478 typedef name##Observer Self; \ 00479 typedef ::itk::Command Superclass;\ 00480 typedef ::itk::SmartPointer<Self> Pointer;\ 00481 itkNewMacro( Self );\ 00482 protected:\ 00483 name##Observer() \ 00484 {\ 00485 m_GotObject = false;\ 00486 }\ 00487 ~name##Observer() {}\ 00488 public:\ 00489 typedef eventType EventType;\ 00490 void Execute(itk::Object *caller, const itk::EventObject & event)\ 00491 {\ 00492 const itk::Object * constCaller = caller;\ 00493 this->Execute( constCaller, event );\ 00494 }\ 00495 void Execute(const itk::Object *caller, const itk::EventObject & event)\ 00496 {\ 00497 m_GotObject = false;\ 00498 if( EventType().CheckEvent( &event ) )\ 00499 {\ 00500 const EventType * objectEvent = \ 00501 dynamic_cast< const EventType *>( &event );\ 00502 if( objectEvent )\ 00503 {\ 00504 m_Object = objectEvent->Get();\ 00505 m_GotObject = true;\ 00506 }\ 00507 }\ 00508 }\ 00509 bool Got##name() const\ 00510 {\ 00511 return m_GotObject;\ 00512 }\ 00513 void Reset() \ 00514 {\ 00515 m_GotObject = false; \ 00516 }\ 00517 payloadType::ConstPointer Get##name() const\ 00518 {\ 00519 return m_Object;\ 00520 }\ 00521 private:\ 00522 payloadType::ConstPointer m_Object;\ 00523 bool m_GotObject;\ 00524 }; 00525 00526 } 00527 #endif // __igstk_Macros_h_