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