ITK  4.8.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 #include "ITKCommonExport.h"
44 
45 #include <typeinfo>
46 
47 #include <string>
48 #include <cstdlib>
49 #ifndef NDEBUG
50 #include <cassert>
51 #endif
52 
53 #include <sstream>
54 
59 namespace itk
60 {
61 // end namespace itk - this is here for documentation purposes
62 }
63 
66 #define itkNotUsed(x)
67 
68 
69 // The clang compiler has many useful non-default compiler warnings
70 // that tend to have a high false positive rate.
71 // The following set of defines allows us to suppress false positives
72 // and still track down suspicious code
73 #if defined(__clang__) && defined(__has_warning)
74 #define CLANG_PRAGMA_PUSH _Pragma("clang diagnostic push")
75 #define CLANG_PRAGMA_POP _Pragma("clang diagnostic pop")
76 # if __has_warning("-Wfloat-equal")
77 #define CLANG_SUPPRESS_Wfloat_equal _Pragma("clang diagnostic ignored \"-Wfloat-equal\"" )
78 # endif
79 #else
80 #define CLANG_PRAGMA_PUSH
81 #define CLANG_PRAGMA_POP
82 #define CLANG_SUPPRESS_Wfloat_equal
83 #endif
84 
85 
86 /*
87  * ITK only supports MSVC++ 7.1 and greater
88  * MSVC++ 11.0 _MSC_VER = 1700
89  * MSVC++ 10.0 _MSC_VER = 1600
90  * MSVC++ 9.0 _MSC_VER = 1500
91  * MSVC++ 8.0 _MSC_VER = 1400
92  * MSVC++ 7.1 _MSC_VER = 1310
93  * MSVC++ 7.0 _MSC_VER = 1300
94  * MSVC++ 6.0 _MSC_VER = 1200
95  * MSVC++ 5.0 _MSC_VER = 1100
96 */
97 #if defined( _MSC_VER ) && ( _MSC_VER < 1310 )
98 //#error "_MSC_VER < 1310 (MSVC++ 7.1) not supported under ITKv4"
99 #endif
100 #if defined( __SUNPRO_CC ) && ( __SUNPRO_CC < 0x590 )
101 #error "__SUNPRO_CC < 0x590 not supported under ITKv4"
102 #endif
103 #if defined( __CYGWIN__ )
104 #error "The Cygwin compiler is not supported in ITKv4 and above"
105 #endif
106 #if defined( __BORLANDC__ )
107 #error "The Borland C compiler is not supported in ITKv4 and above"
108 #endif
109 #if defined( __MWERKS__ )
110 #error "The MetroWerks compiler is not supported in ITKv4 and above"
111 #endif
112 #if defined( __GNUC__ ) && ( __GNUC__ < 3 )
113 #error "The __GNUC__ version 2.95 compiler is not supprted under ITKv4 and above"
114 #if defined( __sgi )
115 //This is true for IRIX 6.5.18m with MIPSPro 7.3.1.3m.
116 //TODO: At some future point, it may be necessary to
117 //define a minimum __sgi version that will work.
118 #error "The __sgi compiler is not supprted under ITKv4 and above"
119 #endif
120 #endif
121 
122 // Setup symbol exports
123 #if defined( _WIN32 ) || defined ( WIN32 )
124  #define ITK_ABI_IMPORT __declspec(dllimport)
125  #define ITK_ABI_EXPORT __declspec(dllexport)
126  #define ITK_ABI_HIDDEN
127 #else
128  #if __GNUC__ >= 4
129  #define ITK_ABI_IMPORT __attribute__ ((visibility ("default")))
130  #define ITK_ABI_EXPORT __attribute__ ((visibility ("default")))
131  #define ITK_ABI_HIDDEN __attribute__ ((visibility ("hidden")))
132  #else
133  #define ITK_ABI_IMPORT
134  #define ITK_ABI_EXPORT
135  #define ITK_ABI_HIDDEN
136  #endif
137 #endif
138 
139 #if __cplusplus >= 201103L
140 // In c++11 the override keyword allows you to explicity define that a function
141 // is intended to override the base-class version. This makes the code more
142 // managable and fixes a set of common hard-to-find bugs.
143 #define ITK_OVERRIDE override
144 // In c++11 there is an explicit nullptr type that introduces a new keyword to
145 // serve as a distinguished null pointer constant: nullptr. It is of type
146 // nullptr_t, which is implicitly convertible and comparable to any pointer type
147 // or pointer-to-member type. It is not implicitly convertible or comparable to
148 // integral types, except for bool.
149 #define ITK_NULLPTR nullptr
150 // In C++11 the throw-list specification has been deprecated,
151 // replaces with the noexcept specifier. Using this function
152 // specification adds the run-time check that the method does not
153 // throw, if it does throw then std::terminate will be called.
154 // Use cautiously.
155 #define ITK_NOEXCEPT noexcept
156 #else
157 #define ITK_OVERRIDE
158 #define ITK_NULLPTR NULL
159 #define ITK_NOEXCEPT throw()
160 #endif
161 
162 
183 #define itkNewMacro(x) \
184  itkSimpleNewMacro(x) \
185  itkCreateAnotherMacro(x) \
186  itkCloneMacro(x)
187 
188 #define itkSimpleNewMacro(x) \
189  static Pointer New(void) \
190  { \
191  Pointer smartPtr = ::itk::ObjectFactory< x >::Create(); \
192  if ( smartPtr.GetPointer() == ITK_NULLPTR ) \
193  { \
194  smartPtr = new x; \
195  } \
196  smartPtr->UnRegister(); \
197  return smartPtr; \
198  }
199 
200 #define itkCreateAnotherMacro(x) \
201  virtual ::itk::LightObject::Pointer CreateAnother(void) const ITK_OVERRIDE \
202  { \
203  ::itk::LightObject::Pointer smartPtr; \
204  smartPtr = x::New().GetPointer(); \
205  return smartPtr; \
206  }
207 
208 #define itkCloneMacro(x) \
209  Pointer Clone() const \
210  { \
211  Pointer rval = \
212  dynamic_cast<x *>(this->InternalClone().GetPointer()); \
213  return rval; \
214  }
215 
232 #define itkFactorylessNewMacro(x) \
233  static Pointer New(void) \
234  { \
235  Pointer smartPtr; \
236  x * rawPtr = new x; \
237  smartPtr = rawPtr; \
238  rawPtr->UnRegister(); \
239  return smartPtr; \
240  } \
241  virtual ::itk::LightObject::Pointer CreateAnother(void) const ITK_OVERRIDE \
242  { \
243  ::itk::LightObject::Pointer smartPtr; \
244  smartPtr = x::New().GetPointer(); \
245  return smartPtr; \
246  }
247 
248 
251 #define itkTypeMacro(thisClass, superclass) \
252  virtual const char *GetNameOfClass() const ITK_OVERRIDE \
253  { \
254  return #thisClass; \
255  }
256 
257 #define itkTypeMacroNoParent(thisClass) \
258  virtual const char *GetNameOfClass() const \
259  { \
260  return #thisClass; \
261  }
262 
263 namespace itk
264 {
271 extern ITKCommon_EXPORT void OutputWindowDisplayText(const char *);
273 
274 extern ITKCommon_EXPORT void OutputWindowDisplayErrorText(const char *);
275 
276 extern ITKCommon_EXPORT void OutputWindowDisplayWarningText(const char *);
277 
278 extern ITKCommon_EXPORT void OutputWindowDisplayGenericOutputText(const char *);
279 
280 extern ITKCommon_EXPORT void OutputWindowDisplayDebugText(const char *);
281 } // end namespace itk
282 
286 #if defined( NDEBUG )
287 #define itkDebugMacro(x)
288 #define itkDebugStatement(x)
289 #else
290 #define itkDebugMacro(x) \
291  { \
292  if ( this->GetDebug() && ::itk::Object::GetGlobalWarningDisplay() ) \
293  { \
294  std::ostringstream itkmsg; \
295  itkmsg << "Debug: In " __FILE__ ", line " << __LINE__ << "\n" \
296  << this->GetNameOfClass() << " (" << this << "): " x \
297  << "\n\n"; \
298  ::itk::OutputWindowDisplayDebugText( itkmsg.str().c_str() ); \
299  } \
300  }
301 
302 
303 //The itkDebugStatement is to be used to protect code that is only
304 //used in the itkDebugMacro
305 #define itkDebugStatement(x) x
306 #endif
307 
311 #define itkWarningMacro(x) \
312  { \
313  if ( ::itk::Object::GetGlobalWarningDisplay() ) \
314  { \
315  std::ostringstream itkmsg; \
316  itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
317  << this->GetNameOfClass() << " (" << this << "): " x \
318  << "\n\n"; \
319  ::itk::OutputWindowDisplayWarningText( itkmsg.str().c_str() ); \
320  } \
321  }
322 
323 
324 //The itkDebugStatement is to be used ot protect code that is only
325 //used in the itkDebugMacro
326 #define itkWarningStatement(x) x
327 
328 #if defined( ITK_CPP_FUNCTION )
329  #if defined( _WIN32 ) && !defined( __MINGW32__ ) && !defined( CABLE_CONFIGURATION ) \
330  && !defined( __GCCXML__ )
331  #define ITK_LOCATION __FUNCSIG__
332  #elif defined( __GNUC__ )
333  #define ITK_LOCATION __PRETTY_FUNCTION__
334  #else
335  #define ITK_LOCATION __FUNCTION__
336  #endif
337 #else
338  #define ITK_LOCATION "unknown"
339 #endif
340 
341 #include "itkExceptionObject.h"
342 
346 #define itkExceptionMacro(x) \
347  { \
348  std::ostringstream message; \
349  message << "itk::ERROR: " << this->GetNameOfClass() \
350  << "(" << this << "): " x; \
351  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(), ITK_LOCATION); \
352  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
353  }
354 
355 
356 #define itkGenericExceptionMacro(x) \
357  { \
358  std::ostringstream message; \
359  message << "itk::ERROR: " x; \
360  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(), ITK_LOCATION); \
361  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
362  }
363 
364 #define itkDeclareExceptionMacro(newexcp,parentexcp,whatmessage) \
365 namespace itk { \
366 class newexcp : public parentexcp \
367 { \
368 public: \
369 newexcp( const char *file, unsigned int lineNumber ) : \
370 parentexcp( file, lineNumber ) \
371 { \
372  this->SetDescription( whatmessage ); \
373 } \
374 newexcp( const std::string & file, unsigned int lineNumber ) : \
375 parentexcp( file, lineNumber ) \
376 { \
377  this->SetDescription( whatmessage ); \
378 } \
379 itkTypeMacro(newexcp, parentexcp); \
380 }; \
381 }
382 
383 #define itkSpecializedExceptionMacro(exceptiontype) \
384  { \
385  ::itk::exceptiontype e_(__FILE__, __LINE__); \
386  e_.SetLocation(ITK_LOCATION); \
387  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
388  }
389 
390 #define itkSpecializedMessageExceptionMacro(exceptiontype,x) \
391  { \
392  ::itk::exceptiontype e_(__FILE__, __LINE__); \
393  std::ostringstream message; \
394  message << "itk::ERROR: " x; \
395  e_.SetDescription(message.str().c_str()); \
396  e_.SetLocation(ITK_LOCATION); \
397  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
398  }
399 
400 
401 #define itkGenericOutputMacro(x) \
402  { \
403  if ( ::itk::Object::GetGlobalWarningDisplay() ) \
404  { \
405  std::ostringstream itkmsg; \
406  itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
407  x << "\n\n"; \
408  ::itk::OutputWindowDisplayGenericOutputText( itkmsg.str().c_str() ); \
409  } \
410  }
411 
412 //----------------------------------------------------------------------------
413 // Macros for simplifying the use of logging
414 //
415 #define itkLogMacro(x, y) \
416  { \
417  if ( this->GetLogger() ) \
418  { \
419  this->GetLogger()->Write(::itk::LoggerBase::x, y); \
420  } \
421  }
422 
423 #define itkLogMacroStatic(obj, x, y) \
424  { \
425  if ( obj->GetLogger() ) \
426  { \
427  obj->GetLogger()->Write(::itk::LoggerBase::x, y); \
428  } \
429  }
430 
431 //----------------------------------------------------------------------------
432 // Setup legacy code policy.
433 //
434 // CMake options ITK_LEGACY_REMOVE and ITK_LEGACY_SILENT are converted
435 // They may be used to completely remove legacy code or silence the
436 // warnings. The default is to warn about their use.
437 //
438 // Source files that test the legacy code may define ITK_LEGACY_TEST
439 // like this:
440 //
441 // #define ITK_LEGACY_TEST
442 // #include "itkClassWithDeprecatedMethod.h"
443 //
444 // in order to silence the warnings for calling deprecated methods.
445 // No other source files in ITK should call the methods since they are
446 // provided only for compatibility with older user code.
447 
448 // Define itkLegacyMacro to mark legacy methods where they are
449 // declared in their class. Example usage:
450 //
451 // // @deprecated Replaced by MyOtherMethod() as of ITK 2.0.
452 // itkLegacyMacro(void MyMethod());
453 #if defined( ITK_LEGACY_REMOVE )
454 #define itkLegacyMacro(method) /* no ';' */
455 #elif defined( ITK_LEGACY_SILENT ) || defined( ITK_LEGACY_TEST ) || defined( __GCCXML__ )
456 // Provide legacy methods with no warnings.
457 #define itkLegacyMacro(method) method
458 #else
459 // Setup compile-time warnings for uses of deprecated methods if
460 // possible on this compiler.
461 #if defined( __GNUC__ ) && !defined( __INTEL_COMPILER ) && ( __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 1 ) )
462 #define itkLegacyMacro(method) method __attribute__( ( deprecated ) )
463 #elif defined( _MSC_VER )
464 #define itkLegacyMacro(method) __declspec(deprecated) method
465 #else
466 #define itkLegacyMacro(method) method
467 #endif
468 #endif
469 
470 // Macros to create runtime deprecation warning messages in function
471 // bodies. Example usage:
472 //
473 // void itkMyClass::MyOldMethod()
474 // {
475 // itkLegacyBodyMacro(itkMyClass::MyOldMethod, 2.0);
476 // }
477 //
478 // void itkMyClass::MyMethod()
479 // {
480 // itkLegacyReplaceBodyMacro(itkMyClass::MyMethod, 2.0,
481 // itkMyClass::MyOtherMethod);
482 // }
483 #if defined( ITK_LEGACY_REMOVE ) || defined( ITK_LEGACY_SILENT )
484 #define itkLegacyBodyMacro(method, version)
485 #define itkLegacyReplaceBodyMacro(method, version, replace)
486 #define itkGenericLegacyBodyMacro(method, version)
487 #define itkGenericLegacyReplaceBodyMacro(method, version, replace)
488 #else
489 #define itkLegacyBodyMacro(method, version) \
490  itkWarningMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
491 #define itkLegacyReplaceBodyMacro(method, version, replace) \
492  itkWarningMacro( \
493  #method " was deprecated for ITK " #version " and will be removed in a future version. Use " #replace \
494  " instead.")
495 #define itkGenericLegacyBodyMacro(method, version) \
496  itkGenericOutputMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
497 #define itkGenericLegacyReplaceBodyMacro(method, version, replace) \
498  itkGenericOutputMacro( \
499  #method " was deprecated for ITK " #version " and will be removed in a future version. Use " #replace \
500  " instead.")
501 #endif
502 
503 #if defined ( ITK_LEGACY_REMOVE )
504 #define ITK_TEMPLATE_TXX "error ITK_TEMPLATE_TXX is no longer a supported identifier, you should replace with ITK_MANUAL_INSTANTIATION as a replacement"
505 #else
506 #define ITK_TEMPLATE_TXX 1
507 #endif
508 
509 
510 // Most modern x86 CPUs have 64 byte aligned blocks which are used for
511 // the cache lines. By aligning multi-threaded structures with the
512 // cache lines, false shared can be reduced, and performance
513 // increased.
514 #define ITK_CACHE_LINE_ALIGNMENT 64
515 
516 //
517 // itkPadStruct will add padding to a structure to ensure a minimum size
518 // for ensuring that adjacent structures do not share CACHE lines.
519 // Each struct will take up some multiple of cacheline sizes.
520 // This is particularly useful for arrays of thread private variables.
521 //
522 #define itkPadStruct( mincachesize, oldtype, newtype ) \
523  struct newtype: public oldtype \
524  { \
525  char _StructPadding[mincachesize - (sizeof(oldtype)%mincachesize) ]; \
526  };
527 
528 //
529 // itkAlignedTypedef is a macro which creates a new typedef to make a
530 // data structure aligned.
531 //
532 #if defined ( ITK_HAS_CPP11_ALIGNAS )
533 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
534  typedef oldtype newtype alignas(alignment)
535 #elif defined( ITK_HAS_GNU_ATTRIBUTE_ALIGNED )
536 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
537  typedef oldtype newtype __attribute__((aligned(alignment)))
538 #elif defined ( _MSC_VER )
539 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
540  typedef __declspec(align( alignment )) oldtype newtype
541 #else
542 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
543  typedef oldtype newtype
544 #endif
545 
546 //=============================================================================
547 /* Define a common way of declaring a templated function as a friend inside a class.
548  - ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENTS(T)
549 
550  The following templated function
551 
552  template <T>
553  T add(const T & a, const T & b);
554 
555  is declared as friend in some compilers as:
556 
557  class A
558  {
559  public:
560  friend Self add<Self>( const Self & a, const Self & b );
561  }
562 
563  while other compilers will do
564 
565  class A
566  {
567  public:
568  friend Self add<>( const Self & a, const Self & b );
569  }
570 
571  This characteristic of the compiler is checked by a TRY_COMPILE
572  command defined in Insight/CMake/itkTestFriendTemplatedFunction.cxx
573 
574 */
575 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_NULL_STRING )
576 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T)
577 #else
578 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_EMPTY_BRACKETS )
579 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T) < >
580 #else
581 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_TEMPLATE_ARGUMENTS )
582 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T) < T >
583 #endif
584 #endif
585 #endif
586 // THIS IS A TEMPORARY PATCH FOR Visual Studio 10. The correct solution must
587 // be implemented in Insight/CMake/itkTestFriendTemplatedFunction.cxx
588 #if ( defined ( _MSC_VER ) && ( _MSC_VER >= 1600 ) )
589 #ifdef ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT
590 #undef ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT
591 #endif
592 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T)
593 #endif
594 
595 //--------------------------------------------------------------------------------
596 // Helper macros for Template Meta-Programming techniques of for-loops
597 // unrolling
598 //--------------------------------------------------------------------------------
599 
600 //--------------------------------------------------------------------------------
601 // Macro that generates an unrolled for loop for assigning elements of one array
602 // to elements of another array The array are assumed to be of same length
603 // (dimension), and this is also assumed to be the value of NumberOfIterations.
604 // No verification of size is performed. Casting is perfomed as part of the
605 // assignment, by using the DestinationElementType as the casting type.
606 // Source and destination array types must have defined opearator[] in their
607 // API.
608 #define itkForLoopAssignmentMacro(DestinationType, \
609  SourceType, \
610  DestinationElementType, \
611  DestinationArray, \
612  SourceArray, \
613  NumberOfIterations) \
614  for ( unsigned int i = 0; i < NumberOfIterations; ++i ) \
615  { \
616  DestinationArray[i] = static_cast< DestinationElementType >( SourceArray[i] ); \
617  }
618 
619 //--------------------------------------------------------------------------------
620 // Macro that generates an unrolled for loop for rounding and assigning
621 // elements of one array to elements of another array The array are assumed to
622 // be of same length (dimension), and this is also assumed to be the value of
623 // NumberOfIterations. No verification of size is performed. Casting is
624 // perfomed as part of the assignment, by using the DestinationElementType as
625 // the casting type.
626 // Source and destination array types must have defined opearator[] in their
627 // API.
628 #define itkForLoopRoundingAndAssignmentMacro(DestinationType, \
629  Sourcrnd_halfintup, \
630  DestinationElementType, \
631  DestinationArray, \
632  SourceArray, \
633  NumberOfIterations) \
634  for ( unsigned int i = 0; i < NumberOfIterations; ++i ) \
635  { \
636  DestinationArray[i] = itk::Math::Round< DestinationElementType >(SourceArray[i]); \
637  }
638 
639 // end of Template Meta Programming helper macros
640 
641 #ifndef NDEBUG
642 
643 #ifdef _POSIX_SOURCE
644 #define itkAssertInDebugOrThrowInReleaseMacro(msg) __assert_fail (msg, __FILE__, __LINE__, __ASSERT_FUNCTION);
645 #else
646 #define itkAssertInDebugOrThrowInReleaseMacro(msg) itkGenericExceptionMacro(<< msg);
647 #endif
648 
649 #else
650 #define itkAssertInDebugOrThrowInReleaseMacro(msg) itkGenericExceptionMacro(<< msg);
651 #endif
652 
653 #define itkAssertOrThrowMacro(test, message) \
654  if ( !( test ) ) \
655  { \
656  std::ostringstream msgstr; \
657  msgstr << message; \
658  itkAssertInDebugOrThrowInReleaseMacro( msgstr.str().c_str() ); \
659  }
660 
661 #ifndef NDEBUG
662 #define itkAssertInDebugAndIgnoreInReleaseMacro(X) assert(X)
663 #else
664 #define itkAssertInDebugAndIgnoreInReleaseMacro(X)
665 #endif
666 
667 #ifdef ITKV3_COMPATIBILITY
668 // As of MSVS++ 7.1 and greater, typename is supported in templates
669 // All ITKv4 compilers support the typename keyword, but this is
670 // needed to ease transition from ITKv3.
671 #define ITK_TYPENAME typename
672 #endif
673 
677 template <typename TTarget, typename TSource>
678 TTarget itkDynamicCastInDebugMode(TSource x)
679 {
680 #ifndef NDEBUG
681  if(x == 0)
682  {
683  return 0;
684  }
685  TTarget rval = dynamic_cast<TTarget>(x);
686  if(rval == 0)
687  {
688  itkGenericExceptionMacro(<< "Failed dynamic cast to "
689  << typeid(TTarget).name()
690  << " object type = "
691  << x->GetNameOfClass());
692  }
693  return rval;
694 #else
695  return static_cast<TTarget>(x);
696 #endif
697 }
699 
700 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
701 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
702 // !! The ITK Get/Set Macros for various types !!
703 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
704 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
705 //This is probably better, but requires a lot of extra work
706 //for gettting ExplicitInstantiation to work properly. \#define
707 // itkStaticConstMacro(name, type, value) static const type name = value
708 #define itkStaticConstMacro(name, type, value) enum { name = value }
709 
710 #define itkGetStaticConstMacro(name) (Self::name)
711 
713 #define itkSetInputMacro(name, type) \
714  virtual void Set##name(const type *_arg) \
715  { \
716  itkDebugMacro("setting input " #name " to " << _arg); \
717  if ( _arg != itkDynamicCastInDebugMode< type * >( this->ProcessObject::GetInput(#name) ) ) \
718  { \
719  this->ProcessObject::SetInput( #name, const_cast< type * >( _arg ) ); \
720  this->Modified(); \
721  } \
722  }
723 
724 
726 #define itkGetInputMacro(name, type) \
727  virtual const type * Get##name() const \
728  { \
729  itkDebugMacro( "returning input " << #name " of " << this->ProcessObject::GetInput(#name) ); \
730  return itkDynamicCastInDebugMode< const type * >( this->ProcessObject::GetInput(#name) ); \
731  }
732 
733 
735 #define itkSetDecoratedInputMacro(name, type) \
736  virtual void Set##name##Input(const SimpleDataObjectDecorator< type > *_arg) \
737  { \
738  itkDebugMacro("setting input " #name " to " << _arg); \
739  if ( _arg != itkDynamicCastInDebugMode< SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ) \
740  { \
741  this->ProcessObject::SetInput( #name, const_cast< SimpleDataObjectDecorator< type > * >( _arg ) ); \
742  this->Modified(); \
743  } \
744  } \
745  virtual void Set##name(const SimpleDataObjectDecorator< type > *_arg) \
746  { \
747  this->Set##name##Input(_arg); \
748  } \
749  virtual void Set##name(const type &_arg) \
750  { \
751  typedef SimpleDataObjectDecorator< type > DecoratorType; \
752  itkDebugMacro("setting input " #name " to " << _arg); \
753  const DecoratorType *oldInput = \
754  itkDynamicCastInDebugMode< const DecoratorType * >( \
755  this->ProcessObject::GetInput(#name) ); \
756  if ( oldInput && oldInput->Get() == _arg ) \
757  { \
758  return; \
759  } \
760  typename DecoratorType::Pointer newInput = DecoratorType::New(); \
761  newInput->Set(_arg); \
762  this->Set##name##Input(newInput); \
763  }
764 
765 
767 #define itkGetDecoratedInputMacro(name, type) \
768  virtual const SimpleDataObjectDecorator< type > * Get##name##Input() const \
769  { \
770  itkDebugMacro( "returning input " << #name " of " << this->ProcessObject::GetInput(#name) ); \
771  return itkDynamicCastInDebugMode< const SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ); \
772  } \
773  virtual const type & Get##name() const \
774  { \
775  itkDebugMacro("Getting input " #name); \
776  typedef SimpleDataObjectDecorator< type > DecoratorType; \
777  const DecoratorType *input = \
778  itkDynamicCastInDebugMode< const DecoratorType * >( \
779  this->ProcessObject::GetInput(#name) ); \
780  if( input == ITK_NULLPTR ) \
781  { \
782  itkExceptionMacro(<<"input" #name " is not set"); \
783  } \
784  return input->Get(); \
785  }
786 
787 
790 #define itkSetGetDecoratedInputMacro(name, type) \
791  itkSetDecoratedInputMacro(name, type) \
792  itkGetDecoratedInputMacro(name, type)
793 
798 #define itkSetDecoratedObjectInputMacro(name, type) \
799  virtual void Set##name##Input(const DataObjectDecorator< type > *_arg) \
800  { \
801  itkDebugMacro("setting input " #name " to " << _arg); \
802  if ( _arg != itkDynamicCastInDebugMode< DataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ) \
803  { \
804  this->ProcessObject::SetInput( #name, const_cast< DataObjectDecorator< type > * >( _arg ) ); \
805  this->Modified(); \
806  } \
807  } \
808  virtual void Set##name(const type * _arg) \
809  { \
810  typedef DataObjectDecorator< type > DecoratorType; \
811  itkDebugMacro("setting input " #name " to " << _arg); \
812  const DecoratorType *oldInput = \
813  itkDynamicCastInDebugMode< const DecoratorType * >( \
814  this->ProcessObject::GetInput(#name) ); \
815  if ( oldInput && oldInput->Get() == _arg ) \
816  { \
817  return; \
818  } \
819  typename DecoratorType::Pointer newInput = DecoratorType::New(); \
820  newInput->Set(_arg); \
821  this->Set##name##Input(newInput); \
822  }
823 
824 
829 #define itkGetDecoratedObjectInputMacro(name, type) \
830  virtual const DataObjectDecorator< type > * Get##name##Input() const \
831  { \
832  itkDebugMacro( "returning input " << #name " of "<< this->ProcessObject::GetInput(#name) ); \
833  return itkDynamicCastInDebugMode< const DataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ); \
834  } \
835  virtual const type * Get##name() const \
836  { \
837  itkDebugMacro("Getting input " #name); \
838  typedef DataObjectDecorator< type > DecoratorType; \
839  const DecoratorType *input = \
840  itkDynamicCastInDebugMode< const DecoratorType * >( \
841  this->ProcessObject::GetInput(#name) ); \
842  if( input == ITK_NULLPTR ) \
843  { \
844  return ITK_NULLPTR; \
845  } \
846  return input->Get(); \
847  }
848 
849 
852 #define itkSetGetDecoratedObjectInputMacro(name, type) \
853  itkSetDecoratedObjectInputMacro(name, type) \
854  itkGetDecoratedObjectInputMacro(name, type)
855 
857 #define itkSetMacro(name, type) \
858  virtual void Set##name (const type _arg) \
859  { \
860  itkDebugMacro("setting " #name " to " << _arg); \
861 CLANG_PRAGMA_PUSH \
862 CLANG_SUPPRESS_Wfloat_equal \
863  if ( this->m_##name != _arg ) \
864  { \
865  this->m_##name = _arg; \
866  this->Modified(); \
867  } \
868 CLANG_PRAGMA_POP \
869  }
870 
871 
873 #define itkGetMacro(name, type) \
874  virtual type Get##name () \
875  { \
876  return this->m_##name; \
877  }
878 
879 
883 #define itkGetConstMacro(name, type) \
884  virtual type Get##name () const \
885  { \
886  return this->m_##name; \
887  }
888 
889 
894 #define itkGetConstReferenceMacro(name, type) \
895  virtual const type &Get##name () const \
896  { \
897  return this->m_##name; \
898  }
899 
900 
905 #define itkSetEnumMacro(name, type) \
906  virtual void Set##name (const type _arg) \
907  { \
908  itkDebugMacro( "setting " #name " to " << static_cast< long >( _arg ) ); \
909  if ( this->m_##name != _arg ) \
910  { \
911  this->m_##name = _arg; \
912  this->Modified(); \
913  } \
914  }
915 
916 
921 #define itkGetEnumMacro(name, type) \
922  virtual type Get##name () const \
923  { \
924  return this->m_##name; \
925  }
926 
927 
931 #define itkSetStringMacro(name) \
932  virtual void Set##name (const char *_arg) \
933  { \
934  if ( _arg && ( _arg == this->m_##name ) ) { return; } \
935  if ( _arg ) \
936  { \
937  this->m_##name = _arg; \
938  } \
939  else \
940  { \
941  this->m_##name = ""; \
942  } \
943  this->Modified(); \
944  } \
945  virtual void Set##name (const std::string & _arg) \
946  { \
947  this->Set##name( _arg.c_str() ); \
948  } \
949 
950 
951 
955 #define itkGetStringMacro(name) \
956  virtual const char *Get##name () const \
957  { \
958  return this->m_##name.c_str(); \
959  }
960 
964 #define itkSetClampMacro(name, type, min, max) \
965  virtual void Set##name (type _arg) \
966  { \
967  const type temp_extrema=( _arg < min ? min : ( _arg > max ? max : _arg ) );\
968  itkDebugMacro("setting " << #name " to " << _arg); \
969 CLANG_PRAGMA_PUSH \
970 CLANG_SUPPRESS_Wfloat_equal \
971  if ( this->m_##name != temp_extrema ) \
972  { \
973  this->m_##name = temp_extrema; \
974  this->Modified(); \
975  } \
976 CLANG_PRAGMA_POP \
977  }
978 
979 
980 //NOTE: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
985 #define itkSetObjectMacro(name, type) \
986  virtual void Set##name (type * _arg) \
987  { \
988  itkDebugMacro("setting " << #name " to " << _arg); \
989 CLANG_PRAGMA_PUSH \
990 CLANG_SUPPRESS_Wfloat_equal \
991  if ( this->m_##name != _arg ) \
992  { \
993  this->m_##name = _arg; \
994  this->Modified(); \
995  } \
996 CLANG_PRAGMA_POP \
997  }
998 
999 
1008 // NOTE: A class can use either itkGetModifiableObjectMacro
1009 // or itkGetObjectMacro, but not both.
1010 // A class can use either itkGetModifiableObjectMacro
1011 // or itkGetConstObjectMacro, but not both.
1012 // If the desired behavior is to only provide const
1013 // access to the itkObject ivar, then use itkGetConstObjectMacro,
1014 // else use itkGetModifiableObjectMacro for read/write access to
1015 // the ivar.
1016 // It is permissable to use both itkGetObjectMacro and itkGetConstObjectMacro
1017 // for backwards compatibility.
1018 // If the ITK_LEGACY_REMOVE=FALSE, then it is
1019 // permissable to use itkGetObjectMacro which
1020 // defines both signatures itk::GetXXX() and
1021 // itk::GetModifiableXXX()
1022 
1025 #define itkGetConstObjectMacro(name, type) \
1026  virtual const type * Get##name () const \
1027  { \
1028  return this->m_##name.GetPointer(); \
1029  }
1030 
1031 
1032 #if defined ( ITK_FUTURE_LEGACY_REMOVE )
1033 // In the future, the itkGetObjectMacro will be deprecated with the ITK_LEGACY_REMOVE
1034 // flag. For now, this very advanced feature is only available
1035 // through manual setting of a compiler define -DITK_FUTURE_LEGACY_REMOVE
1036 // ("/DITK_FUTURE_LEGACY_REMOVE /EHsc" with Visual Studio)
1037 // to ease the transition from the historical GetObjectMacro to the GetModifiableObjectMacro
1038 # define itkGetObjectMacro(name, type) \
1039  virtual type * Get##name () \
1040  { \
1041  purposeful_error("itkGetObjectMacro should be replaced with itkGetModifiableObjectMacro."); \
1042  }
1043 
1044 # define itkGetModifiableObjectMacro(name, type) \
1045  virtual type * GetModifiable##name () \
1046  { \
1047  return this->m_##name.GetPointer(); \
1048  } \
1049  itkGetConstObjectMacro(name, type)
1050 
1051 #else // defined ( ITK_FUTURE_LEGACY_REMOVE )
1052 
1054 # define itkGetObjectMacro(name, type) \
1055  virtual type * Get##name () \
1056  { \
1057  return this->m_##name.GetPointer(); \
1058  }
1059 # define itkGetModifiableObjectMacro(name, type) \
1060  virtual type * GetModifiable##name () \
1061  { \
1062  return this->m_##name.GetPointer(); \
1063  } \
1064  itkGetConstObjectMacro(name, type) \
1065  itkGetObjectMacro(name, type)
1066 #endif // defined ( ITK_FUTURE_LEGACY_REMOVE )
1067 
1068 
1069 // For backwards compatibility define ITK_EXPORT to nothing
1070 #define ITK_EXPORT
1071 
1072 
1075 #define itkGetConstReferenceObjectMacro(name, type) \
1076  virtual const typename type::Pointer & Get##name () const \
1077  { \
1078  return this->m_##name; \
1079  }
1080 
1085 #define itkSetConstObjectMacro(name, type) \
1086  virtual void Set##name (const type * _arg) \
1087  { \
1088  itkDebugMacro("setting " << #name " to " << _arg); \
1089  if ( this->m_##name != _arg ) \
1090  { \
1091  this->m_##name = _arg; \
1092  this->Modified(); \
1093  } \
1094  }
1095 
1096 
1099 #define itkBooleanMacro(name) \
1100  virtual void name##On () \
1101  { \
1102  this->Set##name(true); \
1103  } \
1104  virtual void name##Off () \
1105  { \
1106  this->Set##name(false); \
1107  }
1108 
1109 
1113 #define itkSetVectorMacro(name, type, count) \
1114  virtual void Set##name(type data[]) \
1115  { \
1116  unsigned int i; \
1117  for ( i = 0; i < count; i++ ) \
1118  { \
1119  if ( data[i] != this->m_##name[i] ) \
1120  { \
1121  break; \
1122  } \
1123  } \
1124  if ( i < count ) \
1125  { \
1126  this->Modified(); \
1127  for ( i = 0; i < count; i++ ) \
1128  { \
1129  this->m_##name[i] = data[i]; \
1130  } \
1131  } \
1132  }
1133 
1134 
1137 #define itkGetVectorMacro(name, type, count) \
1138  virtual type * Get##name () const \
1139  { \
1140  return this->m_##name; \
1141  }
1142 
1146 #define itkGPUKernelClassMacro(kernel) \
1147 class kernel \
1148  { \
1149  public: \
1150  static const char* GetOpenCLSource(); \
1151  private: \
1152  kernel(); \
1153  virtual ~kernel(); \
1154  kernel(const kernel &); \
1155  void operator=(const kernel &); \
1156  };
1157 
1158 
1159 #define itkGetOpenCLSourceFromKernelMacro(kernel) \
1160  static const char* GetOpenCLSource() \
1161  { \
1162  return kernel::GetOpenCLSource(); \
1163  }
1164 
1165 // A useful macro in the PrintSelf method for printing member variables
1166 // which are pointers to object based on the LightObject class.
1167 #define itkPrintSelfObjectMacro(name) \
1168  if (static_cast<const LightObject*>(this->m_##name) == ITK_NULLPTR) \
1169  { \
1170  os << indent << #name << ": (null)" << std::endl; \
1171  } \
1172  else \
1173  { \
1174  os << indent << #name << ": " << std::endl; \
1175  this->m_##name->Print(os,indent.GetNextIndent()); \
1176  }
1177 
1178 
1180 #define itkSetDecoratedOutputMacro(name, type) \
1181  virtual void Set##name##Output(const SimpleDataObjectDecorator< type > *_arg) \
1182  { \
1183  itkDebugMacro("setting output " #name " to " << _arg); \
1184  if ( _arg != itkDynamicCastInDebugMode< SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetOutput(#name) ) ) \
1185  { \
1186  this->ProcessObject::SetOutput( #name, const_cast< SimpleDataObjectDecorator< type > * >( _arg ) ); \
1187  this->Modified(); \
1188  } \
1189  } \
1190  virtual void Set##name(const type &_arg) \
1191  { \
1192  typedef SimpleDataObjectDecorator< type > DecoratorType; \
1193  itkDebugMacro("setting output " #name " to " << _arg); \
1194  DecoratorType *output = itkDynamicCastInDebugMode< DecoratorType * >( \
1195  this->ProcessObject::GetOutput(#name) ); \
1196  if ( output ) \
1197  { \
1198  if ( output->Get() == _arg ) \
1199  { \
1200  return; \
1201  } \
1202  else \
1203  { \
1204  output->Set(_arg); \
1205  } \
1206  } \
1207  else \
1208  { \
1209  typename DecoratorType::Pointer newOutput = DecoratorType::New(); \
1210  newOutput->Set(_arg); \
1211  this->Set##name##Output(newOutput); \
1212  } \
1213  }
1214 
1215 
1217 #define itkGetDecoratedOutputMacro(name, type) \
1218  virtual const SimpleDataObjectDecorator< type > * Get##name##Output() const \
1219  { \
1220  itkDebugMacro( "returning output " << #name " of " << this->ProcessObject::GetOutput(#name) ); \
1221  return itkDynamicCastInDebugMode< const SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetOutput(#name) ); \
1222  } \
1223  virtual const type & Get##name() const \
1224  { \
1225  itkDebugMacro("Getting output " #name); \
1226  typedef SimpleDataObjectDecorator< type > DecoratorType; \
1227  const DecoratorType *output = \
1228  itkDynamicCastInDebugMode< const DecoratorType * >( \
1229  this->ProcessObject::GetOutput(#name) ); \
1230  if( output == ITK_NULLPTR ) \
1231  { \
1232  itkExceptionMacro(<<"output" #name " is not set"); \
1233  } \
1234  return output->Get(); \
1235  }
1236 
1237 
1238 
1239 #endif //end of itkMacro.h