ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkMacro.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
38 #ifndef __itkMacro_h
39 #define __itkMacro_h
40 
41 #include "itkWin32Header.h"
42 #include "itkConfigure.h"
43 
44 #include <typeinfo>
45 
46 #include <string>
47 #include <cstdlib>
48 #ifndef NDEBUG
49 #include <cassert>
50 #endif
51 
52 #include <sstream>
53 
58 namespace itk
59 {
60 // end namespace itk - this is here for documentation purposes
61 }
62 
65 #define itkNotUsed(x)
66 
67 /*
68  * ITK only supports MSVC++ 7.1 and greater
69  * MSVC++ 11.0 _MSC_VER = 1700
70  * MSVC++ 10.0 _MSC_VER = 1600
71  * MSVC++ 9.0 _MSC_VER = 1500
72  * MSVC++ 8.0 _MSC_VER = 1400
73  * MSVC++ 7.1 _MSC_VER = 1310
74  * MSVC++ 7.0 _MSC_VER = 1300
75  * MSVC++ 6.0 _MSC_VER = 1200
76  * MSVC++ 5.0 _MSC_VER = 1100
77 */
78 #if defined( _MSC_VER ) && ( _MSC_VER < 1310 )
79 //#error "_MSC_VER < 1310 (MSVC++ 7.1) not supported under ITKv4"
80 #endif
81 #if defined( __SUNPRO_CC ) && ( __SUNPRO_CC < 0x590 )
82 #error "__SUNPRO_CC < 0x590 not supported under ITKv4"
83 #endif
84 #if defined( __CYGWIN__ )
85 #error "The Cygwin compiler is not supported in ITKv4 and above"
86 #endif
87 #if defined( __BORLANDC__ )
88 #error "The Borland C compiler is not supported in ITKv4 and above"
89 #endif
90 #if defined( __MWERKS__ )
91 #error "The MetroWerks compiler is not supported in ITKv4 and above"
92 #endif
93 #if defined( __GNUC__ ) && ( __GNUC__ < 3 )
94 #error "The __GNUC__ version 2.95 compiler is not supprted under ITKv4 and above"
95 #if defined( __sgi )
96 //This is true for IRIX 6.5.18m with MIPSPro 7.3.1.3m.
97 //TODO: At some future point, it may be necessary to
98 //define a minimum __sgi version that will work.
99 #error "The __sgi compiler is not supprted under ITKv4 and above"
100 #endif
101 #endif
102 
103 // Setup symbol exports
104 //
105 // When a class definition has ITK_EXPORT, the class will be
106 // checked automatically, by Utilities/Dart/PrintSelfCheck.tcl
107 #define ITK_EXPORT
108 
109 #if defined( _WIN32 ) || defined ( WIN32 )
110  #define ITK_ABI_IMPORT __declspec(dllimport)
111  #define ITK_ABI_EXPORT __declspec(dllexport)
112  #define ITK_ABI_HIDDEN
113 #else
114  #if __GNUC__ >= 4
115  #define ITK_ABI_IMPORT __attribute__ ((visibility ("default")))
116  #define ITK_ABI_EXPORT __attribute__ ((visibility ("default")))
117  #define ITK_ABI_HIDDEN __attribute__ ((visibility ("hidden")))
118  #else
119  #define ITK_ABI_IMPORT
120  #define ITK_ABI_EXPORT
121  #define ITK_ABI_HIDDEN
122  #endif
123 #endif
124 
125 #define ITKCommon_HIDDEN ITK_ABI_HIDDEN
126 
127 #if !defined( ITKSTATIC )
128  #ifdef ITKCommon_EXPORTS
129  #define ITKCommon_EXPORT ITK_ABI_EXPORT
130  #else
131  #define ITKCommon_EXPORT ITK_ABI_IMPORT
132  #endif /* ITKCommon_EXPORTS */
133 #else
134  /* ITKCommon is build as a static lib */
135  #if __GNUC__ >= 4
136  // Don't hide symbols in the static ITKCommon library in case
137  // -fvisibility=hidden is used
138  #define ITKCommon_EXPORT ITK_ABI_EXPORT
139  #else
140  #define ITKCommon_EXPORT
141  #endif
142 #endif
143 
144 //This is probably better, but requires a lot of extra work
145 //for gettting ExplicitInstantiation to work properly. \#define
146 // itkStaticConstMacro(name, type, value) static const type name = value
147 #define itkStaticConstMacro(name, type, value) enum { name = value }
148 
149 #define itkGetStaticConstMacro(name) (Self::name)
150 
152 #define itkSetInputMacro(name, type) \
153  virtual void Set##name(const type *_arg) \
154  { \
155  itkDebugMacro("setting input " #name " to " << _arg); \
156  if ( _arg != static_cast< type * >( this->ProcessObject::GetInput(#name) ) ) \
157  { \
158  this->ProcessObject::SetInput( #name, const_cast< type * >( _arg ) ); \
159  this->Modified(); \
160  } \
161  }
162 
163 
165 #define itkGetInputMacro(name, type) \
166  virtual const type * Get##name() const \
167  { \
168  itkDebugMacro( "returning input " << #name " of " \
169  << static_cast< const type * >( this->ProcessObject::GetInput(#name) ) ); \
170  return static_cast< const type * >( this->ProcessObject::GetInput(#name) ); \
171  }
172 
173 
175 #define itkSetDecoratedInputMacro(name, type) \
176  virtual void Set##name##Input(const SimpleDataObjectDecorator< type > *_arg) \
177  { \
178  itkDebugMacro("setting input " #name " to " << _arg); \
179  if ( _arg != static_cast< SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ) \
180  { \
181  this->ProcessObject::SetInput( #name, const_cast< SimpleDataObjectDecorator< type > * >( _arg ) ); \
182  this->Modified(); \
183  } \
184  } \
185  virtual void Set##name(const type &_arg) \
186  { \
187  typedef SimpleDataObjectDecorator< type > DecoratorType; \
188  itkDebugMacro("setting input " #name " to " << _arg); \
189  const DecoratorType *oldInput = \
190  static_cast< const DecoratorType * >( \
191  this->ProcessObject::GetInput(#name) ); \
192  if ( oldInput && oldInput->Get() == _arg ) \
193  { \
194  return; \
195  } \
196  typename DecoratorType::Pointer newInput = DecoratorType::New(); \
197  newInput->Set(_arg); \
198  this->Set##name##Input(newInput); \
199  }
200 
201 
203 #define itkGetDecoratedInputMacro(name, type) \
204  virtual const SimpleDataObjectDecorator< type > * Get##name##Input() const \
205  { \
206  itkDebugMacro( "returning input " << #name " of " \
207  << static_cast< const SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ); \
208  return static_cast< const SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ); \
209  } \
210  virtual const type & Get##name() const \
211  { \
212  itkDebugMacro("Getting input " #name); \
213  typedef SimpleDataObjectDecorator< type > DecoratorType; \
214  const DecoratorType *input = \
215  static_cast< const DecoratorType * >( \
216  this->ProcessObject::GetInput(#name) ); \
217  if( input == NULL ) \
218  { \
219  itkExceptionMacro(<<"input" #name " is not set"); \
220  } \
221  return input->Get(); \
222  }
223 
224 
227 #define itkSetGetDecoratedInputMacro(name, type) \
228  itkSetDecoratedInputMacro(name, type) \
229  itkGetDecoratedInputMacro(name, type)
230 
235 #define itkSetDecoratedObjectInputMacro(name, type) \
236  virtual void Set##name##Input(const DataObjectDecorator< type > *_arg) \
237  { \
238  itkDebugMacro("setting input " #name " to " << _arg); \
239  if ( _arg != static_cast< DataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ) \
240  { \
241  this->ProcessObject::SetInput( #name, const_cast< DataObjectDecorator< type > * >( _arg ) ); \
242  this->Modified(); \
243  } \
244  } \
245  virtual void Set##name(const type * _arg) \
246  { \
247  typedef DataObjectDecorator< type > DecoratorType; \
248  itkDebugMacro("setting input " #name " to " << _arg); \
249  const DecoratorType *oldInput = \
250  static_cast< const DecoratorType * >( \
251  this->ProcessObject::GetInput(#name) ); \
252  if ( oldInput && oldInput->Get() == _arg ) \
253  { \
254  return; \
255  } \
256  typename DecoratorType::Pointer newInput = DecoratorType::New(); \
257  newInput->Set(_arg); \
258  this->Set##name##Input(newInput); \
259  }
260 
261 
266 #define itkGetDecoratedObjectInputMacro(name, type) \
267  virtual const DataObjectDecorator< type > * Get##name##Input() const \
268  { \
269  itkDebugMacro( "returning input " << #name " of " \
270  << static_cast< const DataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ); \
271  return static_cast< const DataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ); \
272  } \
273  virtual const type * Get##name() const \
274  { \
275  itkDebugMacro("Getting input " #name); \
276  typedef DataObjectDecorator< type > DecoratorType; \
277  const DecoratorType *input = \
278  static_cast< const DecoratorType * >( \
279  this->ProcessObject::GetInput(#name) ); \
280  if( input == NULL ) \
281  { \
282  itkExceptionMacro(<<"input" #name " is not set"); \
283  } \
284  return input->Get(); \
285  }
286 
287 
290 #define itkSetGetDecoratedObjectInputMacro(name, type) \
291  itkSetDecoratedObjectInputMacro(name, type) \
292  itkGetDecoratedObjectInputMacro(name, type)
293 
295 #define itkSetMacro(name, type) \
296  virtual void Set##name (const type _arg) \
297  { \
298  itkDebugMacro("setting " #name " to " << _arg); \
299  if ( this->m_##name != _arg ) \
300  { \
301  this->m_##name = _arg; \
302  this->Modified(); \
303  } \
304  }
305 
306 
308 #define itkGetMacro(name, type) \
309  virtual type Get##name () \
310  { \
311  return this->m_##name; \
312  }
313 
314 
318 #define itkGetConstMacro(name, type) \
319  virtual type Get##name () const \
320  { \
321  return this->m_##name; \
322  }
323 
324 
329 #define itkGetConstReferenceMacro(name, type) \
330  virtual const type &Get##name () const \
331  { \
332  return this->m_##name; \
333  }
334 
335 
340 #define itkSetEnumMacro(name, type) \
341  virtual void Set##name (const type _arg) \
342  { \
343  itkDebugMacro( "setting " #name " to " << static_cast< long >( _arg ) ); \
344  if ( this->m_##name != _arg ) \
345  { \
346  this->m_##name = _arg; \
347  this->Modified(); \
348  } \
349  }
350 
351 
356 #define itkGetEnumMacro(name, type) \
357  virtual type Get##name () const \
358  { \
359  return this->m_##name; \
360  }
361 
362 
366 #define itkSetStringMacro(name) \
367  virtual void Set##name (const char *_arg) \
368  { \
369  if ( _arg && ( _arg == this->m_##name ) ) { return; } \
370  if ( _arg ) \
371  { \
372  this->m_##name = _arg; \
373  } \
374  else \
375  { \
376  this->m_##name = ""; \
377  } \
378  this->Modified(); \
379  } \
380  virtual void Set##name (const std::string & _arg) \
381  { \
382  this->Set##name( _arg.c_str() ); \
383  } \
384 
385 
386 
390 #define itkGetStringMacro(name) \
391  virtual const char *Get##name () const \
392  { \
393  return this->m_##name.c_str(); \
394  }
395 
399 #define itkSetClampMacro(name, type, min, max) \
400  virtual void Set##name (type _arg) \
401  { \
402  itkDebugMacro("setting " << #name " to " << _arg); \
403  if ( this->m_##name != ( _arg < min ? min : ( _arg > max ? max : _arg ) ) ) \
404  { \
405  this->m_##name = ( _arg < min ? min : ( _arg > max ? max : _arg ) ); \
406  this->Modified(); \
407  } \
408  }
409 
410 
415 #define itkSetObjectMacro(name, type) \
416  virtual void Set##name (type * _arg) \
417  { \
418  itkDebugMacro("setting " << #name " to " << _arg); \
419  if ( this->m_##name != _arg ) \
420  { \
421  this->m_##name = _arg; \
422  this->Modified(); \
423  } \
424  }
425 
426 
429 #define itkGetObjectMacro(name, type) \
430  virtual type * Get##name () \
431  { \
432  return this->m_##name.GetPointer(); \
433  }
434 
439 #define itkSetConstObjectMacro(name, type) \
440  virtual void Set##name (const type * _arg) \
441  { \
442  itkDebugMacro("setting " << #name " to " << _arg); \
443  if ( this->m_##name != _arg ) \
444  { \
445  this->m_##name = _arg; \
446  this->Modified(); \
447  } \
448  }
449 
450 
453 #define itkGetConstObjectMacro(name, type) \
454  virtual const type * Get##name () const \
455  { \
456  return this->m_##name.GetPointer(); \
457  }
458 
461 #define itkGetConstReferenceObjectMacro(name, type) \
462  virtual const typename type::Pointer & Get##name () const \
463  { \
464  return this->m_##name; \
465  }
466 
469 #define itkBooleanMacro(name) \
470  virtual void name##On () \
471  { \
472  this->Set##name(true); \
473  } \
474  virtual void name##Off () \
475  { \
476  this->Set##name(false); \
477  }
478 
479 
483 #define itkSetVectorMacro(name, type, count) \
484  virtual void Set##name(type data[]) \
485  { \
486  unsigned int i; \
487  for ( i = 0; i < count; i++ ) \
488  { \
489  if ( data[i] != this->m_##name[i] ) \
490  { \
491  break; \
492  } \
493  } \
494  if ( i < count ) \
495  { \
496  this->Modified(); \
497  for ( i = 0; i < count; i++ ) \
498  { \
499  this->m_##name[i] = data[i]; \
500  } \
501  } \
502  }
503 
504 
507 #define itkGetVectorMacro(name, type, count) \
508  virtual type * Get##name () const \
509  { \
510  return this->m_##name; \
511  }
512 
516 #define itkGPUKernelClassMacro(kernel) \
517 class ITK_EXPORT kernel \
518  { \
519  public: \
520  static const char* GetOpenCLSource(); \
521  private: \
522  kernel(); \
523  virtual ~kernel(); \
524  kernel(const kernel &); \
525  void operator=(const kernel &); \
526  };
527 
528 
529 #define itkGetOpenCLSourceFromKernelMacro(kernel) \
530  static const char* GetOpenCLSource() \
531  { \
532  return kernel::GetOpenCLSource(); \
533  }
534 
555 #define itkNewMacro(x) \
556  itkSimpleNewMacro(x) \
557  itkCreateAnotherMacro(x) \
558  itkCloneMacro(x)
559 
560 #define itkSimpleNewMacro(x) \
561  static Pointer New(void) \
562  { \
563  Pointer smartPtr = ::itk::ObjectFactory< x >::Create(); \
564  if ( smartPtr.GetPointer() == NULL ) \
565  { \
566  smartPtr = new x; \
567  } \
568  smartPtr->UnRegister(); \
569  return smartPtr; \
570  }
571 
572 #define itkCreateAnotherMacro(x) \
573  virtual::itk::LightObject::Pointer CreateAnother(void) const \
574  { \
575  ::itk::LightObject::Pointer smartPtr; \
576  smartPtr = x::New().GetPointer(); \
577  return smartPtr; \
578  }
579 
580 #define itkCloneMacro(x) \
581  Pointer Clone() const \
582  { \
583  Pointer rval = \
584  dynamic_cast<x *>(this->InternalClone().GetPointer()); \
585  return rval; \
586  }
587 
604 #define itkFactorylessNewMacro(x) \
605  static Pointer New(void) \
606  { \
607  Pointer smartPtr; \
608  x * rawPtr = new x; \
609  smartPtr = rawPtr; \
610  rawPtr->UnRegister(); \
611  return smartPtr; \
612  } \
613  virtual::itk::LightObject::Pointer CreateAnother(void) const \
614  { \
615  ::itk::LightObject::Pointer smartPtr; \
616  smartPtr = x::New().GetPointer(); \
617  return smartPtr; \
618  }
619 
620 
623 #define itkTypeMacro(thisClass, superclass) \
624  virtual const char *GetNameOfClass() const \
625  { \
626  return #thisClass; \
627  }
628 
629 namespace itk
630 {
637 extern ITKCommon_EXPORT void OutputWindowDisplayText(const char *);
639 
640 extern ITKCommon_EXPORT void OutputWindowDisplayErrorText(const char *);
641 
642 extern ITKCommon_EXPORT void OutputWindowDisplayWarningText(const char *);
643 
644 extern ITKCommon_EXPORT void OutputWindowDisplayGenericOutputText(const char *);
645 
646 extern ITKCommon_EXPORT void OutputWindowDisplayDebugText(const char *);
647 } // end namespace itk
648 
652 #if defined( NDEBUG )
653 #define itkDebugMacro(x)
654 #define itkDebugStatement(x)
655 #else
656 #define itkDebugMacro(x) \
657  { \
658  if ( this->GetDebug() && ::itk::Object::GetGlobalWarningDisplay() ) \
659  { \
660  std::ostringstream itkmsg; \
661  itkmsg << "Debug: In " __FILE__ ", line " << __LINE__ << "\n" \
662  << this->GetNameOfClass() << " (" << this << "): " x \
663  << "\n\n"; \
664  ::itk::OutputWindowDisplayDebugText( itkmsg.str().c_str() ); \
665  } \
666  }
667 
668 
669 //The itkDebugStatement is to be used to protect code that is only
670 //used in the itkDebugMacro
671 #define itkDebugStatement(x) x
672 #endif
673 
677 #define itkWarningMacro(x) \
678  { \
679  if ( ::itk::Object::GetGlobalWarningDisplay() ) \
680  { \
681  std::ostringstream itkmsg; \
682  itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
683  << this->GetNameOfClass() << " (" << this << "): " x \
684  << "\n\n"; \
685  ::itk::OutputWindowDisplayWarningText( itkmsg.str().c_str() ); \
686  } \
687  }
688 
689 
690 //The itkDebugStatement is to be used ot protect code that is only
691 //used in the itkDebugMacro
692 #define itkWarningStatement(x) x
693 
694 #if defined( ITK_CPP_FUNCTION )
695  #if defined( _WIN32 ) && !defined( __MINGW32__ ) && !defined( CABLE_CONFIGURATION ) \
696  && !defined( CSWIG )
697  #define ITK_LOCATION __FUNCSIG__
698  #elif defined( __GNUC__ )
699  #define ITK_LOCATION __PRETTY_FUNCTION__
700  #else
701  #define ITK_LOCATION __FUNCTION__
702  #endif
703 #else
704  #define ITK_LOCATION "unknown"
705 #endif
706 
707 #include "itkExceptionObject.h"
708 
712 #define itkExceptionMacro(x) \
713  { \
714  std::ostringstream message; \
715  message << "itk::ERROR: " << this->GetNameOfClass() \
716  << "(" << this << "): " x; \
717  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(), ITK_LOCATION); \
718  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
719  }
720 
721 
722 #define itkGenericExceptionMacro(x) \
723  { \
724  std::ostringstream message; \
725  message << "itk::ERROR: " x; \
726  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(), ITK_LOCATION); \
727  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
728  }
729 
730 #define itkDeclareExceptionMacro(newexcp,parentexcp,whatmessage) \
731 namespace itk { \
732 class ITK_EXPORT newexcp : public parentexcp \
733 { \
734 public: \
735 newexcp( const char *file, unsigned int lineNumber ) : \
736 parentexcp( file, lineNumber ) \
737 { \
738  this->SetDescription( whatmessage ); \
739 } \
740 newexcp( const std::string & file, unsigned int lineNumber ) : \
741 parentexcp( file, lineNumber ) \
742 { \
743  this->SetDescription( whatmessage ); \
744 } \
745 itkTypeMacro(newexcp, parentexcp); \
746 }; \
747 }
748 
749 #define itkSpecializedExceptionMacro(exceptiontype) \
750  { \
751  ::itk::exceptiontype e_(__FILE__, __LINE__); \
752  e_.SetLocation(ITK_LOCATION); \
753  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
754  }
755 
756 #define itkSpecializedMessageExceptionMacro(exceptiontype,x) \
757  { \
758  ::itk::exceptiontype e_(__FILE__, __LINE__); \
759  std::ostringstream message; \
760  message << "itk::ERROR: " x; \
761  e_.SetDescription(message.str().c_str()); \
762  e_.SetLocation(ITK_LOCATION); \
763  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
764  }
765 
766 
767 #define itkGenericOutputMacro(x) \
768  { \
769  if ( ::itk::Object::GetGlobalWarningDisplay() ) \
770  { \
771  std::ostringstream itkmsg; \
772  itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
773  x << "\n\n"; \
774  ::itk::OutputWindowDisplayGenericOutputText( itkmsg.str().c_str() ); \
775  } \
776  }
777 
778 //----------------------------------------------------------------------------
779 // Macros for simplifying the use of logging
780 //
781 #define itkLogMacro(x, y) \
782  { \
783  if ( this->GetLogger() ) \
784  { \
785  this->GetLogger()->Write(::itk::LoggerBase::x, y); \
786  } \
787  }
788 
789 #define itkLogMacroStatic(obj, x, y) \
790  { \
791  if ( obj->GetLogger() ) \
792  { \
793  obj->GetLogger()->Write(::itk::LoggerBase::x, y); \
794  } \
795  }
796 
797 //----------------------------------------------------------------------------
798 // Setup legacy code policy.
799 //
800 // CMake options ITK_LEGACY_REMOVE and ITK_LEGACY_SILENT are converted
801 // They may be used to completely remove legacy code or silence the
802 // warnings. The default is to warn about their use.
803 //
804 // Source files that test the legacy code may define ITK_LEGACY_TEST
805 // like this:
806 //
807 // #define ITK_LEGACY_TEST
808 // #include "itkClassWithDeprecatedMethod.h"
809 //
810 // in order to silence the warnings for calling deprecated methods.
811 // No other source files in ITK should call the methods since they are
812 // provided only for compatibility with older user code.
813 
814 // Define itkLegacyMacro to mark legacy methods where they are
815 // declared in their class. Example usage:
816 //
817 // // @deprecated Replaced by MyOtherMethod() as of ITK 2.0.
818 // itkLegacyMacro(void MyMethod());
819 #if defined( ITK_LEGACY_REMOVE )
820 // Remove legacy methods completely. Put a bogus declaration in
821 // place to avoid stray semicolons because this is an error for some
822 // compilers. Using a class forward declaration allows any number
823 // of repeats in any context without generating unique names.
824 #define itkLegacyMacro(method) class itkLegacyMethodRemoved /* no ';' */
825 #elif defined( ITK_LEGACY_SILENT ) || defined( ITK_LEGACY_TEST ) || defined( CSWIG )
826 // Provide legacy methods with no warnings.
827 #define itkLegacyMacro(method) method
828 #else
829 // Setup compile-time warnings for uses of deprecated methods if
830 // possible on this compiler.
831 #if defined( __GNUC__ ) && !defined( __INTEL_COMPILER ) && ( __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 1 ) )
832 #define itkLegacyMacro(method) method __attribute__( ( deprecated ) )
833 #elif defined( _MSC_VER )
834 #define itkLegacyMacro(method) __declspec(deprecated) method
835 #else
836 #define itkLegacyMacro(method) method
837 #endif
838 #endif
839 
840 // Macros to create runtime deprecation warning messages in function
841 // bodies. Example usage:
842 //
843 // void itkMyClass::MyOldMethod()
844 // {
845 // itkLegacyBodyMacro(itkMyClass::MyOldMethod, 2.0);
846 // }
847 //
848 // void itkMyClass::MyMethod()
849 // {
850 // itkLegacyReplaceBodyMacro(itkMyClass::MyMethod, 2.0,
851 // itkMyClass::MyOtherMethod);
852 // }
853 #if defined( ITK_LEGACY_REMOVE ) || defined( ITK_LEGACY_SILENT )
854 #define itkLegacyBodyMacro(method, version)
855 #define itkLegacyReplaceBodyMacro(method, version, replace)
856 #define itkGenericLegacyBodyMacro(method, version)
857 #define itkGenericLegacyReplaceBodyMacro(method, version, replace)
858 #else
859 #define itkLegacyBodyMacro(method, version) \
860  itkWarningMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
861 #define itkLegacyReplaceBodyMacro(method, version, replace) \
862  itkWarningMacro( \
863  #method " was deprecated for ITK " #version " and will be removed in a future version. Use " #replace \
864  " instead.")
865 #define itkGenericLegacyBodyMacro(method, version) \
866  itkGenericOutputMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
867 #define itkGenericLegacyReplaceBodyMacro(method, version, replace) \
868  itkGenericOutputMacro( \
869  #method " was deprecated for ITK " #version " and will be removed in a future version. Use " #replace \
870  " instead.")
871 #endif
872 
873 #if defined ( ITK_LEGACY_REMOVE )
874 #define ITK_TEMPLATE_TXX "error ITK_TEMPLATE_TXX is no longer a supported identifier, you should replace with ITK_MANUAL_INSTANTIATION as a replacement"
875 #else
876 #define ITK_TEMPLATE_TXX 1
877 #endif
878 
879 
880 // Most modern x86 CPUs have 64 byte aligned blocks which are used for
881 // the cache lines. By aligning multi-threaded structures with the
882 // cache lines, false shared can be reduced, and performance
883 // increased.
884 #define ITK_CACHE_LINE_ALIGNMENT 64
885 
886 //
887 // itkAlignedTypedef is a macro which creates a new typedef to make a
888 // data structure aligned.
889 //
890 #if defined ( ITK_HAS_CPP11_ALIGNAS )
891 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
892  typedef oldtype newtype alignas(alignment)
893 #elif defined( ITK_HAS_GNU_ATTRIBUTE_ALIGNED )
894 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
895  typedef oldtype newtype __attribute__((aligned(alignment)))
896 #elif defined ( _MSC_VER )
897 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
898  typedef __declspec(align( alignment )) struct oldtype newtype
899 #else
900 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
901  typedef oldtype newtype
902 #endif
903 
904 //=============================================================================
905 /* Define a common way of declaring a templated function as a friend inside a class.
906  - ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENTS(T)
907 
908  The following templated function
909 
910  template <T>
911  T add(const T & a, const T & b);
912 
913  is declared as friend in some compilers as:
914 
915  class A
916  {
917  public:
918  friend Self add<Self>( const Self & a, const Self & b );
919  }
920 
921  while other compilers will do
922 
923  class A
924  {
925  public:
926  friend Self add<>( const Self & a, const Self & b );
927  }
928 
929  This characteristic of the compiler is checked by a TRY_COMPILE
930  command defined in Insight/CMake/itkTestFriendTemplatedFunction.cxx
931 
932 */
933 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_NULL_STRING )
934 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T)
935 #else
936 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_EMPTY_BRACKETS )
937 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T) < >
938 #else
939 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_TEMPLATE_ARGUMENTS )
940 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T) < T >
941 #endif
942 #endif
943 #endif
944 // THIS IS A TEMPORARY PATCH FOR Visual Studio 10. The correct solution must
945 // be implemented in Insight/CMake/itkTestFriendTemplatedFunction.cxx
946 #if ( defined ( _MSC_VER ) && ( _MSC_VER >= 1600 ) )
947 #ifdef ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT
948 #undef ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT
949 #endif
950 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T)
951 #endif
952 
953 //=============================================================================
954 /* Choose a way to prevent template instantiation on this platform.
955  - ITK_TEMPLATE_DO_NOT_INSTANTIATE = use #pragma do_not_instantiate to
956  prevent instantiation
957  - ITK_TEMPLATE_EXTERN = use extern template to prevent instantiation
958 */
959 #if defined( __INTEL_COMPILER ) && __INTEL_COMPILER >= 700
960 #define ITK_TEMPLATE_EXTERN 1
961 #elif defined( __GNUC__ ) && __GNUC__ >= 3
962 #define ITK_TEMPLATE_EXTERN 1
963 #elif defined( _MSC_VER )
964 #define ITK_TEMPLATE_EXTERN 1
965 #endif
966 #if !defined( ITK_TEMPLATE_DO_NOT_INSTANTIATE )
967 #define ITK_TEMPLATE_DO_NOT_INSTANTIATE 0
968 #endif
969 #if !defined( ITK_TEMPLATE_EXTERN )
970 #define ITK_TEMPLATE_EXTERN 0
971 #endif
972 
973 /* Define a macro to explicitly instantiate a template.
974  - ITK_TEMPLATE_EXPORT(X) =
975  Explicitly instantiate X, where X is of the form N(a1[,a2...,aN]).
976  examples: ITK_TEMPLATE_EXPORT(1(class Foo<int>))
977  ITK_TEMPLATE_EXPORT(2(class Bar<int, char>))
978  Use one level of expansion delay to allow user code to have
979  a macro determining the number of arguments. */
980 #define ITK_TEMPLATE_EXPORT(x) ITK_TEMPLATE_EXPORT_DELAY(x)
981 #define ITK_TEMPLATE_EXPORT_DELAY(x) template ITK_TEMPLATE_##x;
982 
983 /* Define a macro to prevent template instantiations.
984  - ITK_TEMPLATE_IMPORT(X) =
985  Prevent instantiation of X, where X is of the form N(a1[,a2...,aN]).
986  examples: ITK_TEMPLATE_IMPORT(1(class Foo<int>))
987  ITK_TEMPLATE_IMPORT(2(class Bar<int, char>))
988  Use one level of expansion delay to allow user code to have
989  a macro determining the number of arguments.
990 */
991 #if ITK_TEMPLATE_EXTERN
992 #define ITK_TEMPLATE_IMPORT_DELAY(x) extern template ITK_TEMPLATE_##x;
993 #elif ITK_TEMPLATE_DO_NOT_INSTANTIATE
994 #define ITK_TEMPLATE_IMPORT_DELAY(x) \
995  ITK_TEMPLATE_IMPORT_IMPL(do_not_instantiate ITK_TEMPLATE_##x)
996 #define ITK_TEMPLATE_IMPORT_IMPL(x) _Pragma(#x)
997 #endif
998 #if defined( ITK_TEMPLATE_IMPORT_DELAY )
999 #define ITK_TEMPLATE_IMPORT(x) ITK_TEMPLATE_IMPORT_DELAY(x)
1000 #define ITK_TEMPLATE_IMPORT_WORKS 1
1001 #else
1002 #define ITK_TEMPLATE_IMPORT(x)
1003 #define ITK_TEMPLATE_IMPORT_WORKS 0
1004 #endif
1005 
1006 //=============================================================================
1007 
1008 /* Define macros to export and import template instantiations for each
1009  library in ITK. */
1010 #define ITK_EXPORT_ITKCommon(c, x, n) \
1011  ITK_EXPORT_TEMPLATE(ITKCommon_EXPORT, c, x, n)
1012 #define ITK_IMPORT_ITKCommon(c, x, n) \
1013  ITK_IMPORT_TEMPLATE(ITKCommon_EXPORT, c, x, n)
1014 
1015 //--------------------------------------------------------------------------------
1016 // Helper macros for Template Meta-Programming techniques of for-loops
1017 // unrolling
1018 //--------------------------------------------------------------------------------
1019 
1020 //--------------------------------------------------------------------------------
1021 // Macro that generates an unrolled for loop for assigning elements of one array
1022 // to elements of another array The array are assumed to be of same length
1023 // (dimension), and this is also assumed to be the value of NumberOfIterations.
1024 // No verification of size is performed. Casting is perfomed as part of the
1025 // assignment, by using the DestinationElementType as the casting type.
1026 // Source and destination array types must have defined opearator[] in their
1027 // API.
1028 #define itkForLoopAssignmentMacro(DestinationType, \
1029  SourceType, \
1030  DestinationElementType, \
1031  DestinationArray, \
1032  SourceArray, \
1033  NumberOfIterations) \
1034  for ( unsigned int i = 0; i < NumberOfIterations; ++i ) \
1035  { \
1036  DestinationArray[i] = static_cast< DestinationElementType >( SourceArray[i] ); \
1037  }
1038 
1039 //--------------------------------------------------------------------------------
1040 // Macro that generates an unrolled for loop for rounding and assigning
1041 // elements of one array to elements of another array The array are assumed to
1042 // be of same length (dimension), and this is also assumed to be the value of
1043 // NumberOfIterations. No verification of size is performed. Casting is
1044 // perfomed as part of the assignment, by using the DestinationElementType as
1045 // the casting type.
1046 // Source and destination array types must have defined opearator[] in their
1047 // API.
1048 #define itkForLoopRoundingAndAssignmentMacro(DestinationType, \
1049  Sourcrnd_halfintup, \
1050  DestinationElementType, \
1051  DestinationArray, \
1052  SourceArray, \
1053  NumberOfIterations) \
1054  for ( unsigned int i = 0; i < NumberOfIterations; ++i ) \
1055  { \
1056  DestinationArray[i] = itk::Math::Round< DestinationElementType >(SourceArray[i]); \
1057  }
1058 
1059 // end of Template Meta Programming helper macros
1060 
1061 #ifndef NDEBUG
1062 
1063 #ifdef _POSIX_SOURCE
1064 #define itkAssertInDebugOrThrowInReleaseMacro(msg) __assert_fail (msg, __FILE__, __LINE__, __ASSERT_FUNCTION);
1065 #else
1066 #define itkAssertInDebugOrThrowInReleaseMacro(msg) itkGenericExceptionMacro(<< msg);
1067 #endif
1068 
1069 #else
1070 #define itkAssertInDebugOrThrowInReleaseMacro(msg) itkGenericExceptionMacro(<< msg);
1071 #endif
1072 
1073 #define itkAssertOrThrowMacro(test, message) \
1074  if ( !( test ) ) \
1075  { \
1076  std::ostringstream msgstr; \
1077  msgstr << message; \
1078  itkAssertInDebugOrThrowInReleaseMacro( msgstr.str().c_str() ); \
1079  }
1080 
1081 #ifndef NDEBUG
1082 #define itkAssertInDebugAndIgnoreInReleaseMacro(X) assert(X)
1083 #else
1084 #define itkAssertInDebugAndIgnoreInReleaseMacro(X)
1085 #endif
1086 
1087 #ifdef ITKV3_COMPATIBILITY
1088 // As of MSVS++ 7.1 and greater, typename is supported in templates
1089 // All ITKv4 compilers support the typename keyword, but this is
1090 // needed to ease transition from ITKv3.
1091 #define ITK_TYPENAME typename
1092 #endif
1093 
1097 template <typename TTarget, typename TSource>
1098 TTarget itkDynamicCastInDebugMode(TSource x)
1099 {
1100 #ifndef NDEBUG
1101  if(x == 0)
1102  {
1103  return 0;
1104  }
1105  TTarget rval = dynamic_cast<TTarget>(x);
1106  if(rval == 0)
1107  {
1108  itkGenericExceptionMacro(<< "Failed dynamic cast to "
1109  << typeid(TTarget).name()
1110  << " object type = "
1111  << x->GetNameOfClass());
1112  }
1113  return rval;
1114 #else
1115  return static_cast<TTarget>(x);
1116 #endif
1117 }
1119 
1120 #endif //end of itkMacro.h
1121