IGSTK
|
00001 /*========================================================================= 00002 00003 Program: Image Guided Surgery Software Toolkit 00004 Module: $RCSfile: igstkMacros.h,v $ 00005 Language: C++ 00006 Date: $Date: 2010-11-17 18:00:21 $ 00007 Version: $Revision: 1.50 $ 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 std::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 std::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 std::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 (void) caller;\ 00403 }\ 00404 bool Got##name() const\ 00405 {\ 00406 return m_GotObject;\ 00407 }\ 00408 void Reset() \ 00409 {\ 00410 m_GotObject = false; \ 00411 }\ 00412 payloadType Get##name() const\ 00413 {\ 00414 return m_Object;\ 00415 }\ 00416 private:\ 00417 payloadType m_Object;\ 00418 bool m_GotObject;\ 00419 }; 00420 00421 00422 #define igstkObserverObjectMacro( name, eventType, payloadType ) \ 00423 class name##Observer : public ::itk::Command \ 00424 { \ 00425 public: \ 00426 typedef name##Observer Self; \ 00427 typedef ::itk::Command Superclass;\ 00428 typedef ::itk::SmartPointer<Self> Pointer;\ 00429 itkNewMacro( Self );\ 00430 protected:\ 00431 name##Observer() \ 00432 {\ 00433 m_GotObject = false;\ 00434 }\ 00435 ~name##Observer() {}\ 00436 public:\ 00437 typedef eventType EventType;\ 00438 void Execute(itk::Object *caller, const itk::EventObject & event)\ 00439 {\ 00440 const itk::Object * constCaller = caller;\ 00441 this->Execute( constCaller, event );\ 00442 }\ 00443 void Execute(const itk::Object *caller, const itk::EventObject & event)\ 00444 {\ 00445 m_GotObject = false;\ 00446 if( EventType().CheckEvent( &event ) )\ 00447 {\ 00448 const EventType * objectEvent = \ 00449 dynamic_cast< const EventType *>( &event );\ 00450 if( objectEvent )\ 00451 {\ 00452 m_Object = objectEvent->Get();\ 00453 m_GotObject = true;\ 00454 }\ 00455 }\ 00456 (void) caller;\ 00457 }\ 00458 bool Got##name() const\ 00459 {\ 00460 return m_GotObject;\ 00461 }\ 00462 void Reset() \ 00463 {\ 00464 m_GotObject = false; \ 00465 }\ 00466 payloadType::Pointer Get##name() const\ 00467 {\ 00468 return m_Object;\ 00469 }\ 00470 private:\ 00471 payloadType::Pointer m_Object;\ 00472 bool m_GotObject;\ 00473 }; 00474 00475 00476 #define igstkObserverConstObjectMacro( name, eventType, payloadType ) \ 00477 class name##Observer : public ::itk::Command \ 00478 { \ 00479 public: \ 00480 typedef name##Observer Self; \ 00481 typedef ::itk::Command Superclass;\ 00482 typedef ::itk::SmartPointer<Self> Pointer;\ 00483 itkNewMacro( Self );\ 00484 protected:\ 00485 name##Observer() \ 00486 {\ 00487 m_GotObject = false;\ 00488 }\ 00489 ~name##Observer() {}\ 00490 public:\ 00491 typedef eventType EventType;\ 00492 void Execute(itk::Object *caller, const itk::EventObject & event)\ 00493 {\ 00494 const itk::Object * constCaller = caller;\ 00495 this->Execute( constCaller, event );\ 00496 }\ 00497 void Execute(const itk::Object *caller, const itk::EventObject & event)\ 00498 {\ 00499 m_GotObject = false;\ 00500 if( EventType().CheckEvent( &event ) )\ 00501 {\ 00502 const EventType * objectEvent = \ 00503 dynamic_cast< const EventType *>( &event );\ 00504 if( objectEvent )\ 00505 {\ 00506 m_Object = objectEvent->Get();\ 00507 m_GotObject = true;\ 00508 }\ 00509 }\ 00510 (void) caller;\ 00511 }\ 00512 bool Got##name() const\ 00513 {\ 00514 return m_GotObject;\ 00515 }\ 00516 void Reset() \ 00517 {\ 00518 m_GotObject = false; \ 00519 }\ 00520 payloadType::ConstPointer Get##name() const\ 00521 {\ 00522 return m_Object;\ 00523 }\ 00524 private:\ 00525 payloadType::ConstPointer m_Object;\ 00526 bool m_GotObject;\ 00527 }; 00528 00529 } 00530 #endif // __igstk_Macros_h_