IGSTK
igstkMacros.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Image Guided Surgery Software Toolkit
4  Module: $RCSfile: igstkMacros.h,v $
5  Language: C++
6  Date: $Date: 2010-11-17 18:00:21 $
7  Version: $Revision: 1.50 $
8 
9  Copyright (c) ISC Insight Software Consortium. All rights reserved.
10  See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __igstkMacros_h
18 #define __igstkMacros_h
19 
20 #include "igstkLogger.h"
21 #include "itkCommand.h"
22 #include <cstdlib>
23 
29 #if defined(_MSC_VER)
30 // Warning about: identifier was truncated to '255' characters
31 // in the debug information (MVC6.0 Debug)
32 #pragma warning( disable : 4786 )
33 // warning C4503: 'insert' : decorated name length exceeded,
34 // name was truncated
35 #pragma warning ( disable : 4503 )
36 #endif
37 
38 namespace igstk
39 {
40 
44 #define igstkLogMacro( x, y) \
45 { \
46  if (this->GetLogger() ) \
47  { \
48  if (this->GetLogger()->ShouldBuildMessage( ::igstk::Logger::x ) ) \
49  { \
50  std::ostringstream message; \
51  message << y; \
52  this->GetLogger()->Write(::itk::Logger::x, message.str()); \
53  } \
54  } \
55 }
56 
57 
61 #define igstkLogMacroStatic( obj, x, y) \
62 { \
63  if (obj->GetLogger() ) \
64  { \
65  if (obj->GetLogger()->ShouldBuildMessage( ::igstk::Logger::x ) ) \
66  { \
67  std::ostringstream message; \
68  message << y; \
69  obj->GetLogger()->Write(::itk::Logger::x, message.str()); \
70  } \
71  } \
72 }
73 
74 
78 #define igstkLogMacro2( logger, x, y) \
79 { \
80  if ( logger ) \
81  { \
82  if (logger->ShouldBuildMessage( ::igstk::Logger::x )) \
83  { \
84  std::ostringstream message; \
85  message << y; \
86  logger->Write(::itk::Logger::x, message.str()); \
87  } \
88  } \
89 }
90 
91 
93 #define igstkSetMacro(name,type) \
94 virtual void Set##name (const type & _arg) \
95 { \
96  if (this->m_##name != _arg) \
97  { \
98  this->m_##name = _arg; \
99  } \
100 }
101 
102 
105 #define igstkGetMacro(name,type) \
106 virtual const type & Get##name () const \
107 { \
108  return this->m_##name; \
109 }
110 
111 
115 #define igstkNewMacro(x) \
116 static Pointer New(void) \
117 { \
118  Pointer smartPtr; \
119  x *rawPtr = new x; \
120  smartPtr = rawPtr; \
121  rawPtr->UnRegister(); \
122  return smartPtr; \
123 }
124 
125 
128 #define igstkTypeMacro(thisClass,superclass) \
129  virtual const char *GetNameOfClass() const {return #thisClass;}
130 
131 
134 #if defined(__GNUC__)
135 #define igstkFriendClassMacro(type) friend class type
136 #else
137 #define igstkFriendClassMacro(type) friend type
138 #endif
139 
140 
143 #define igstkLoggerMacro() \
144 public: \
145  typedef ::igstk::Logger LoggerType; \
146 protected: \
147  LoggerType* GetLogger() const { return m_Logger; } \
148 private: \
149  mutable LoggerType::Pointer m_Logger; \
150 public: \
151  void SetLogger(LoggerType* logger) { m_Logger = logger; }
152 
153 
157 #define igstkSetStringMacro(name) \
158  virtual void Set##name (const char* _arg) \
159  { \
160  if ( _arg && (_arg == this->m_##name) ) { return;} \
161  if (_arg) \
162  { \
163  this->m_##name = _arg;\
164  } \
165  else \
166  { \
167  this->m_##name = ""; \
168  } \
169  this->Modified(); \
170  } \
171  virtual void Set##name (const std::string & _arg) \
172  { \
173  this->Set##name( _arg.c_str() ); \
174  } \
175 
176 
180 #define igstkGetStringMacro(name) \
181  virtual const char* Get##name () const \
182  { \
183  return this->m_##name.c_str(); \
184  }
185 
186 
190 #define igstkStateMachineMacroBase( igstktypename ) \
191 private: \
192  typedef ::igstk::StateMachine< Self > StateMachineType; \
193  typedef igstktypename StateMachineType::TMemberFunctionPointer ActionType; \
194  typedef igstktypename StateMachineType::StateType StateType; \
195  typedef igstktypename StateMachineType::InputType InputType; \
196  typedef igstktypename StateMachineType::OutputStreamType OutputStreamType; \
197  igstkFriendClassMacro( ::igstk::StateMachine< Self > ); \
198  StateMachineType m_StateMachine; \
199  typedef ::itk::ReceptorMemberCommand< Self > ReceptorObserverType; \
200  typedef igstktypename ReceptorObserverType::Pointer \
201  ReceptorObserverPointer; \
202 public: \
203  void ExportStateMachineDescription( OutputStreamType & ostr, \
204  bool skipLoops=false ) const \
205  { m_StateMachine.ExportDescription( ostr, skipLoops ); } \
206  void ExportStateMachineDescriptionToLTS( OutputStreamType & ostr,\
207  bool skipLoops=false ) const \
208  { m_StateMachine.ExportDescriptionToLTS( ostr, skipLoops ); } \
209  void ExportStateMachineDescriptionToSCXML( OutputStreamType & ostr,\
210  bool skipLoops=false ) const \
211  { m_StateMachine.ExportDescriptionToSCXML( ostr, skipLoops ); }
212 
213 #define EMPTYPARAMETER
214 
216 #define igstkStateMachineMacro() igstkStateMachineMacroBase( EMPTYPARAMETER )
217 
219 #define igstkStateMachineTemplatedMacro() igstkStateMachineMacroBase( typename )
220 
221 
223 #define igstkDeclareInputMacro( inputname ) \
224  InputType m_##inputname##Input
225 
226 
228 #define igstkDeclareStateMacro( inputname ) \
229  StateType m_##inputname##State
230 
231 
233 #define igstkAddInputMacro( inputname ) \
234  this->m_StateMachine.AddInput( this->m_##inputname##Input, \
235  #inputname"Input" );
236 
237 
239 #define igstkAddStateMacro( statename ) \
240  this->m_StateMachine.AddState( this->m_##statename##State,\
241  #statename"State" );
242 
244 #define igstkAddTransitionMacro( state1, input, state2, action ) \
245  this->m_StateMachine.AddTransition( this->m_##state1##State, \
246  this->m_##input##Input, \
247  this->m_##state2##State, \
248  & Self::action##Processing );
249 
250 
252 #define igstkSetInitialStateMacro( inputname ) \
253  this->m_StateMachine.SelectInitialState( this->m_##inputname##State );
254 
255 
257 #define igstkPushInputMacro( inputname ) \
258  this->m_StateMachine.PushInput( this->m_##inputname##Input );
259 
260 
262 #define igstkStandardClassBasicTraitsMacro( classname, superclassname ) \
263  typedef classname Self; \
264  typedef superclassname Superclass; \
265  typedef ::itk::SmartPointer< Self > Pointer; \
266  typedef ::itk::SmartPointer< const Self > ConstPointer; \
267  igstkTypeMacro( classname, superclassname);
268 
270 #define igstkStandardAbstractClassTraitsMacro( classname, superclassname ) \
271  igstkStandardClassBasicTraitsMacro( classname, superclassname ) \
272  igstkStateMachineMacro();
273 
275 #define igstkStandardClassTraitsMacro( classname, superclassname ) \
276  igstkStandardAbstractClassTraitsMacro( classname, superclassname ) \
277  igstkNewMacro( Self );
278 
280 #define igstkStandardTemplatedAbstractClassTraitsMacro( classname, \
281  superclassname ) \
282  igstkStandardClassBasicTraitsMacro( classname, superclassname ) \
283  igstkStateMachineTemplatedMacro();
284 
286 #define igstkStandardTemplatedClassTraitsMacro( classname, superclassname ) \
287  igstkStandardTemplatedAbstractClassTraitsMacro( classname, superclassname ) \
288  igstkNewMacro( Self );
289 
290 
293 #define igstkEventTransductionMacro( event, input ) \
294 private: \
295  ReceptorObserverPointer m_Observer##event##input; \
296  void Callback##event##input##Input( const ::itk::EventObject & ) \
297  { \
298  igstkPushInputMacro( input ); \
299  m_StateMachine.ProcessInputs(); \
300  } \
301 public: \
302  void Observe##event##Event(const ::igstk::Object * object ) \
303  { \
304  m_Observer##event##input = ReceptorObserverType::New(); \
305  m_Observer##event##input->SetCallbackFunction( this, \
306  & Self::Callback##event##input##Input ); \
307  unsigned long tag = object->AddObserver( \
308  ::igstk::event##Event(),m_Observer##event##input ); \
309  this->RegisterObservedObject( object, tag ); \
310  }
311 
312 
316 #define igstkLoadedEventTransductionMacro( event, input ) \
317 private: \
318  ReceptorObserverPointer m_Observer##event##input; \
319  ::igstk::event##Event::PayloadType m_##input##InputToBeSet; \
320  void Callback##event##input##Input( const ::itk::EventObject & eventvar ) \
321  { \
322  const ::igstk::event##Event * realevent = \
323  dynamic_cast < const ::igstk::event##Event * > ( &eventvar ); \
324  if( realevent ) \
325  { \
326  m_##input##InputToBeSet = realevent->Get(); \
327  igstkPushInputMacro( input ); \
328  m_StateMachine.ProcessInputs(); \
329  } \
330  } \
331 public: \
332  void Observe##input##Input(const ::igstk::Object * object ) \
333  { \
334  m_Observer##event##input = ReceptorObserverType::New(); \
335  m_Observer##event##input->SetCallbackFunction( this,\
336  & Self::Callback##event##input##Input ); \
337  unsigned long tag = object->AddObserver( \
338  ::igstk::event##Event(),m_Observer##event##input ); \
339  this->RegisterObservedObject( object, tag ); \
340  }
341 #define igstkLoadedObjectEventTransductionMacro( event, input ) \
342 private: \
343  ReceptorObserverPointer m_Observer##event##input; \
344  ::igstk::event##Event::PayloadType * m_##input##InputToBeSet; \
345  void Callback##event##input##Input( const ::itk::EventObject & eventvar ) \
346  { \
347  const ::igstk::event##Event * realevent = \
348  dynamic_cast < const ::igstk::event##Event * > ( &eventvar ); \
349  if( realevent ) \
350  { \
351  m_##input##InputToBeSet = realevent->Get(); \
352  igstkPushInputMacro( input ); \
353  m_StateMachine.ProcessInputs(); \
354  } \
355  } \
356 public: \
357  void Observe##input##Input(const ::igstk::Object * object ) \
358  { \
359  m_Observer##event##input = ReceptorObserverType::New(); \
360  m_Observer##event##input->SetCallbackFunction( this,\
361  & Self::Callback##event##input##Input ); \
362  unsigned long tag = object->AddObserver( \
363  ::igstk::event##Event(),m_Observer##event##input ); \
364  this->RegisterObservedObject( object, tag ); \
365  }
366 
367 
368 #define igstkObserverMacro( name, eventType, payloadType ) \
369 class name##Observer : public ::itk::Command \
370 { \
371 public: \
372  typedef name##Observer Self; \
373  typedef ::itk::Command Superclass;\
374  typedef ::itk::SmartPointer<Self> Pointer;\
375  itkNewMacro( Self );\
376 protected:\
377  name##Observer() \
378  {\
379  m_GotObject = false;\
380  }\
381  ~name##Observer() {}\
382 public:\
383  typedef eventType EventType;\
384  void Execute(itk::Object *caller, const itk::EventObject & event)\
385  {\
386  const itk::Object * constCaller = caller;\
387  this->Execute( constCaller, event );\
388  }\
389  void Execute(const itk::Object *caller, const itk::EventObject & event)\
390  {\
391  m_GotObject = false;\
392  if( EventType().CheckEvent( &event ) )\
393  {\
394  const EventType * objectEvent = \
395  dynamic_cast< const EventType *>( &event );\
396  if( objectEvent )\
397  {\
398  m_Object = objectEvent->Get();\
399  m_GotObject = true;\
400  }\
401  }\
402  (void) caller;\
403  }\
404  bool Got##name() const\
405  {\
406  return m_GotObject;\
407  }\
408  void Reset() \
409  {\
410  m_GotObject = false; \
411  }\
412  payloadType Get##name() const\
413  {\
414  return m_Object;\
415  }\
416 private:\
417  payloadType m_Object;\
418  bool m_GotObject;\
419 };
420 
421 
422 #define igstkObserverObjectMacro( name, eventType, payloadType ) \
423 class name##Observer : public ::itk::Command \
424 { \
425 public: \
426  typedef name##Observer Self; \
427  typedef ::itk::Command Superclass;\
428  typedef ::itk::SmartPointer<Self> Pointer;\
429  itkNewMacro( Self );\
430 protected:\
431  name##Observer() \
432  {\
433  m_GotObject = false;\
434  }\
435  ~name##Observer() {}\
436 public:\
437  typedef eventType EventType;\
438  void Execute(itk::Object *caller, const itk::EventObject & event)\
439  {\
440  const itk::Object * constCaller = caller;\
441  this->Execute( constCaller, event );\
442  }\
443  void Execute(const itk::Object *caller, const itk::EventObject & event)\
444  {\
445  m_GotObject = false;\
446  if( EventType().CheckEvent( &event ) )\
447  {\
448  const EventType * objectEvent = \
449  dynamic_cast< const EventType *>( &event );\
450  if( objectEvent )\
451  {\
452  m_Object = objectEvent->Get();\
453  m_GotObject = true;\
454  }\
455  }\
456  (void) caller;\
457  }\
458  bool Got##name() const\
459  {\
460  return m_GotObject;\
461  }\
462  void Reset() \
463  {\
464  m_GotObject = false; \
465  }\
466  payloadType::Pointer Get##name() const\
467  {\
468  return m_Object;\
469  }\
470 private:\
471  payloadType::Pointer m_Object;\
472  bool m_GotObject;\
473 };
474 
475 
476 #define igstkObserverConstObjectMacro( name, eventType, payloadType ) \
477 class name##Observer : public ::itk::Command \
478 { \
479 public: \
480  typedef name##Observer Self; \
481  typedef ::itk::Command Superclass;\
482  typedef ::itk::SmartPointer<Self> Pointer;\
483  itkNewMacro( Self );\
484 protected:\
485  name##Observer() \
486  {\
487  m_GotObject = false;\
488  }\
489  ~name##Observer() {}\
490 public:\
491  typedef eventType EventType;\
492  void Execute(itk::Object *caller, const itk::EventObject & event)\
493  {\
494  const itk::Object * constCaller = caller;\
495  this->Execute( constCaller, event );\
496  }\
497  void Execute(const itk::Object *caller, const itk::EventObject & event)\
498  {\
499  m_GotObject = false;\
500  if( EventType().CheckEvent( &event ) )\
501  {\
502  const EventType * objectEvent = \
503  dynamic_cast< const EventType *>( &event );\
504  if( objectEvent )\
505  {\
506  m_Object = objectEvent->Get();\
507  m_GotObject = true;\
508  }\
509  }\
510  (void) caller;\
511  }\
512  bool Got##name() const\
513  {\
514  return m_GotObject;\
515  }\
516  void Reset() \
517  {\
518  m_GotObject = false; \
519  }\
520  payloadType::ConstPointer Get##name() const\
521  {\
522  return m_Object;\
523  }\
524 private:\
525  payloadType::ConstPointer m_Object;\
526  bool m_GotObject;\
527 };
528 
529 }
530 #endif // __igstk_Macros_h_