ITK  4.12.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 // Setup symbol exports
183 #ifndef ITK_TEMPLATE_EXPORT
184  #ifdef ITK_TEMPLATE_VISIBILITY_DEFAULT
185  #define ITK_TEMPLATE_EXPORT __attribute__ ((visibility ("default")))
186  #else
187  #define ITK_TEMPLATE_EXPORT
188  #endif
189 #endif
190 
191 // Setup symbol exports
192 #ifdef ITK_TEMPLATE_VISIBILITY_DEFAULT
193  #define ITK_FORCE_EXPORT_MACRO(moduleName) __attribute__ ((visibility ("default")))
194 #else
195  #define ITK_FORCE_EXPORT_MACRO(moduleName) moduleName ## _EXPORT
196 #endif
197 
198 #ifndef ITK_FORWARD_EXPORT
199  // If build with shared libraries, on MacOS, if USE_COMPILER_HIDDEN_VISIBILITY is ON
200  #if defined(ITK_TEMPLATE_VISIBILITY_DEFAULT) && defined(ITK_BUILD_SHARED_LIBS) && defined(USE_COMPILER_HIDDEN_VISIBILITY)
201  #define ITK_FORWARD_EXPORT __attribute__ ((visibility ("default")))
202  #else
203  #define ITK_FORWARD_EXPORT
204  #endif
205 #endif
206 
207 #if ITK_COMPILED_CXX_STANDARD_VERSION >= 201103L
208  #define ITK_HAS_CXX11_RVREF
209 #endif
210 
211 #if ! defined( ITK_FUTURE_LEGACY_REMOVE )
212  #if ITK_COMPILER_CXX_STATIC_ASSERT
213  #define ITK_HAS_CXX11_STATIC_ASSERT //NOTE DEPRECATED! should be ITK_COMPILER_CXX_STATIC_ASSERT
214  #endif
215 
216  #if ITK_COMPILER_CXX_DELETED_FUNCTIONS
217  #define ITK_DELETE_FUNCTION =delete //NOTE DEPRECATED! should be ITK_DELETED_FUNCTION
218  #else
219  #define ITK_DELETE_FUNCTION
220  #endif
221  #if ITK_COMPILER_CXX_ALIGNAS //NOTE DEPRECATED! should use ITK_COMPILER_CXX_ALIGNAS
222  // defined if the compiler supports C++11 alignas type specifier
223  #define ITK_HAS_CPP11_ALIGNAS
224  #else
225  #ifdef ITK_HAS_CPP11_ALIGNAS
226  #undef ITK_HAS_CPP11_ALIGNAS
227  #endif
228  #endif
229 #endif
230 
231 #if ITK_COMPILER_CXX_NOEXCEPT
232  #define ITK_NOEXCEPT_OR_THROW noexcept
233 #else
234  #define ITK_NOEXCEPT_OR_THROW throw()
235 #endif
236 
237 #if ITK_COMPILER_CXX_CONSTEXPR
238  #define ITK_CONSTEXPR_FUNC constexpr
239  #define ITK_CONSTEXPR_VAR constexpr
240 #else
241  #define ITK_CONSTEXPR_FUNC inline
242  #define ITK_CONSTEXPR_VAR const
243 #endif
244 
245 // Use "ITK_FALLTHROUGH;" to annotate deliberate fall-through in switches,
246 // use it analogously to "break;". The trailing semi-colon is required.
247 #if defined( __GNUC__ ) && !defined( __INTEL_COMPILER )
248 # if ( __GNUC__ >= 7 )
249 # define ITK_FALLTHROUGH __attribute__((fallthrough))
250 # endif
251 #elif ITK_COMPILED_CXX_STANDARD_VERSION >= 201103L && defined(__has_warning)
252 # if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
253 # define ITK_FALLTHROUGH [[clang::fallthrough]]
254 # endif
255 #endif
256 
257 #ifndef ITK_FALLTHROUGH
258 # define ITK_FALLTHROUGH ((void)0)
259 #endif
260 
281 #define itkNewMacro(x) \
282  itkSimpleNewMacro(x) \
283  itkCreateAnotherMacro(x) \
284  itkCloneMacro(x)
285 
286 #define itkSimpleNewMacro(x) \
287  static Pointer New(void) \
288  { \
289  Pointer smartPtr = ::itk::ObjectFactory< x >::Create(); \
290  if ( smartPtr.GetPointer() == ITK_NULLPTR ) \
291  { \
292  smartPtr = new x; \
293  } \
294  smartPtr->UnRegister(); \
295  return smartPtr; \
296  }
297 
298 #define itkCreateAnotherMacro(x) \
299  virtual ::itk::LightObject::Pointer CreateAnother(void) const ITK_OVERRIDE \
300  { \
301  ::itk::LightObject::Pointer smartPtr; \
302  smartPtr = x::New().GetPointer(); \
303  return smartPtr; \
304  }
305 
306 #define itkCloneMacro(x) \
307  Pointer Clone() const \
308  { \
309  Pointer rval = \
310  dynamic_cast<x *>(this->InternalClone().GetPointer()); \
311  return rval; \
312  }
313 
326 #define itkFactorylessNewMacro(x) \
327  static Pointer New(void) \
328  { \
329  Pointer smartPtr; \
330  x * rawPtr = new x; \
331  smartPtr = rawPtr; \
332  rawPtr->UnRegister(); \
333  return smartPtr; \
334  } \
335  virtual ::itk::LightObject::Pointer CreateAnother(void) const ITK_OVERRIDE \
336  { \
337  ::itk::LightObject::Pointer smartPtr; \
338  smartPtr = x::New().GetPointer(); \
339  return smartPtr; \
340  }
341 
342 
343 //
344 // A macro to disallow the copy constructor and operator= functions
345 // This should be used in the private: declarations for a class
346 //
347 // ITK's paradigm for smart pointer and pipeline consistency
348 // prohibits the use of copy construction and operator= functions.
349 //
350 #define ITK_DISALLOW_COPY_AND_ASSIGN(TypeName) \
351  TypeName(const TypeName&) ITK_DELETED_FUNCTION; \
352  void operator=(const TypeName&) ITK_DELETED_FUNCTION
353 
356 #define itkTypeMacro(thisClass, superclass) \
357  virtual const char *GetNameOfClass() const ITK_OVERRIDE \
358  { \
359  return #thisClass; \
360  }
361 
362 #define itkTypeMacroNoParent(thisClass) \
363  virtual const char *GetNameOfClass() const \
364  { \
365  return #thisClass; \
366  }
367 
368 namespace itk
369 {
376 extern ITKCommon_EXPORT void OutputWindowDisplayText(const char *);
378 
379 extern ITKCommon_EXPORT void OutputWindowDisplayErrorText(const char *);
380 
381 extern ITKCommon_EXPORT void OutputWindowDisplayWarningText(const char *);
382 
383 extern ITKCommon_EXPORT void OutputWindowDisplayGenericOutputText(const char *);
384 
385 extern ITKCommon_EXPORT void OutputWindowDisplayDebugText(const char *);
386 } // end namespace itk
387 
391 #if defined( NDEBUG )
392 #define itkDebugMacro(x)
393 #define itkDebugStatement(x)
394 #else
395 #define itkDebugMacro(x) \
396  { \
397  if ( this->GetDebug() && ::itk::Object::GetGlobalWarningDisplay() ) \
398  { \
399  std::ostringstream itkmsg; \
400  itkmsg << "Debug: In " __FILE__ ", line " << __LINE__ << "\n" \
401  << this->GetNameOfClass() << " (" << this << "): " x \
402  << "\n\n"; \
403  ::itk::OutputWindowDisplayDebugText( itkmsg.str().c_str() ); \
404  } \
405  }
406 
407 
408 //The itkDebugStatement is to be used to protect code that is only
409 //used in the itkDebugMacro
410 #define itkDebugStatement(x) x
411 #endif
412 
416 #define itkWarningMacro(x) \
417  { \
418  if ( ::itk::Object::GetGlobalWarningDisplay() ) \
419  { \
420  std::ostringstream itkmsg; \
421  itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
422  << this->GetNameOfClass() << " (" << this << "): " x \
423  << "\n\n"; \
424  ::itk::OutputWindowDisplayWarningText( itkmsg.str().c_str() ); \
425  } \
426  }
427 
428 
429 //The itkDebugStatement is to be used ot protect code that is only
430 //used in the itkDebugMacro
431 #define itkWarningStatement(x) x
432 
433 #if defined( ITK_CPP_FUNCTION )
434  #if defined( _WIN32 ) && !defined( __MINGW32__ ) && !defined( ITK_WRAPPING_PARSER )
435  #define ITK_LOCATION __FUNCSIG__
436  #elif defined( __GNUC__ )
437  #define ITK_LOCATION __PRETTY_FUNCTION__
438  #else
439  #define ITK_LOCATION __FUNCTION__
440  #endif
441 #else
442  #define ITK_LOCATION "unknown"
443 #endif
444 
445 #include "itkExceptionObject.h"
446 
450 #define itkExceptionMacro(x) \
451  { \
452  std::ostringstream message; \
453  message << "itk::ERROR: " << this->GetNameOfClass() \
454  << "(" << this << "): " x; \
455  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(), ITK_LOCATION); \
456  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
457  }
458 
459 
460 #define itkGenericExceptionMacro(x) \
461  { \
462  std::ostringstream message; \
463  message << "itk::ERROR: " x; \
464  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(), ITK_LOCATION); \
465  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
466  }
467 
468 #define itkDeclareExceptionMacro(newexcp,parentexcp,whatmessage) \
469 namespace itk { \
470 class newexcp : public parentexcp \
471 { \
472 public: \
473 newexcp( const char *file, unsigned int lineNumber ) : \
474 parentexcp( file, lineNumber ) \
475 { \
476  this->SetDescription( whatmessage ); \
477 } \
478 newexcp( const std::string & file, unsigned int lineNumber ) : \
479 parentexcp( file, lineNumber ) \
480 { \
481  this->SetDescription( whatmessage ); \
482 } \
483 itkTypeMacro(newexcp, parentexcp); \
484 }; \
485 }
486 
487 #define itkSpecializedExceptionMacro(exceptiontype) \
488  { \
489  ::itk::exceptiontype e_(__FILE__, __LINE__); \
490  e_.SetLocation(ITK_LOCATION); \
491  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
492  }
493 
494 #define itkSpecializedMessageExceptionMacro(exceptiontype,x) \
495  { \
496  ::itk::exceptiontype e_(__FILE__, __LINE__); \
497  std::ostringstream message; \
498  message << "itk::ERROR: " x; \
499  e_.SetDescription(message.str().c_str()); \
500  e_.SetLocation(ITK_LOCATION); \
501  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
502  }
503 
504 
505 #define itkGenericOutputMacro(x) \
506  { \
507  if ( ::itk::Object::GetGlobalWarningDisplay() ) \
508  { \
509  std::ostringstream itkmsg; \
510  itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
511  x << "\n\n"; \
512  ::itk::OutputWindowDisplayGenericOutputText( itkmsg.str().c_str() ); \
513  } \
514  }
515 
516 //----------------------------------------------------------------------------
517 // Macros for simplifying the use of logging
518 //
519 #define itkLogMacro(x, y) \
520  { \
521  if ( this->GetLogger() ) \
522  { \
523  this->GetLogger()->Write(::itk::LoggerBase::x, y); \
524  } \
525  }
526 
527 #define itkLogMacroStatic(obj, x, y) \
528  { \
529  if ( obj->GetLogger() ) \
530  { \
531  obj->GetLogger()->Write(::itk::LoggerBase::x, y); \
532  } \
533  }
534 
535 //----------------------------------------------------------------------------
536 // Setup legacy code policy.
537 //
538 // CMake options ITK_LEGACY_REMOVE and ITK_LEGACY_SILENT are converted
539 // They may be used to completely remove legacy code or silence the
540 // warnings. The default is to warn about their use.
541 //
542 // Source files that test the legacy code may define ITK_LEGACY_TEST
543 // like this:
544 //
545 // #define ITK_LEGACY_TEST
546 // #include "itkClassWithDeprecatedMethod.h"
547 //
548 // in order to silence the warnings for calling deprecated methods.
549 // No other source files in ITK should call the methods since they are
550 // provided only for compatibility with older user code.
551 
552 // Define itkLegacyMacro to mark legacy methods where they are
553 // declared in their class. Example usage:
554 //
555 // // \deprecated Replaced by MyOtherMethod() as of ITK 2.0.
556 // itkLegacyMacro(void MyMethod());
557 //
558 // See below for what to do for the method definition.
559 #if defined( ITK_LEGACY_REMOVE )
560 #define itkLegacyMacro(method) /* no ';' */
561 #elif defined( ITK_LEGACY_SILENT ) || defined( ITK_LEGACY_TEST ) || defined( ITK_WRAPPING_PARSER )
562 // Provide legacy methods with no warnings.
563 #define itkLegacyMacro(method) method
564 #else
565 // Setup compile-time warnings for uses of deprecated methods if
566 // possible on this compiler.
567 #if defined( __GNUC__ ) && !defined( __INTEL_COMPILER ) && ( __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 1 ) )
568 #define itkLegacyMacro(method) method __attribute__( ( deprecated ) )
569 #elif defined( _MSC_VER )
570 #define itkLegacyMacro(method) __declspec(deprecated) method
571 #else
572 #define itkLegacyMacro(method) method
573 #endif
574 #endif
575 
576 // Macros to create runtime deprecation warning messages in function
577 // bodies. Example usage:
578 //
579 // #if !defined( ITK_LEGACY_REMOVE )
580 // void itkMyClass::MyOldMethod()
581 // {
582 // itkLegacyBodyMacro(itkMyClass::MyOldMethod, 2.0);
583 // }
584 //
585 // void itkMyClass::MyMethod()
586 // {
587 // itkLegacyReplaceBodyMacro(itkMyClass::MyMethod, 2.0,
588 // itkMyClass::MyOtherMethod);
589 // }
590 // #endif
591 #if defined( ITK_LEGACY_REMOVE ) || defined( ITK_LEGACY_SILENT )
592 #define itkLegacyBodyMacro(method, version)
593 #define itkLegacyReplaceBodyMacro(method, version, replace)
594 #define itkGenericLegacyBodyMacro(method, version)
595 #define itkGenericLegacyReplaceBodyMacro(method, version, replace)
596 #else
597 #define itkLegacyBodyMacro(method, version) \
598  itkWarningMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
599 #define itkLegacyReplaceBodyMacro(method, version, replace) \
600  itkWarningMacro( \
601  #method " was deprecated for ITK " #version " and will be removed in a future version. Use " #replace \
602  " instead.")
603 #define itkGenericLegacyBodyMacro(method, version) \
604  itkGenericOutputMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
605 #define itkGenericLegacyReplaceBodyMacro(method, version, replace) \
606  itkGenericOutputMacro( \
607  #method " was deprecated for ITK " #version " and will be removed in a future version. Use " #replace \
608  " instead.")
609 #endif
610 
611 #if defined ( ITK_LEGACY_REMOVE )
612 #define ITK_TEMPLATE_TXX "error ITK_TEMPLATE_TXX is no longer a supported identifier, you should replace with ITK_MANUAL_INSTANTIATION as a replacement"
613 #else
614 #define ITK_TEMPLATE_TXX 1
615 #endif
616 
617 
618 // Most modern x86 CPUs have 64 byte aligned blocks which are used for
619 // the cache lines. By aligning multi-threaded structures with the
620 // cache lines, false shared can be reduced, and performance
621 // increased.
622 #define ITK_CACHE_LINE_ALIGNMENT 64
623 
624 //
625 // itkPadStruct will add padding to a structure to ensure a minimum size
626 // for ensuring that adjacent structures do not share CACHE lines.
627 // Each struct will take up some multiple of cacheline sizes.
628 // This is particularly useful for arrays of thread private variables.
629 //
630 #define itkPadStruct( mincachesize, oldtype, newtype ) \
631  struct newtype: public oldtype \
632  { \
633  char _StructPadding[mincachesize - (sizeof(oldtype)%mincachesize) ]; \
634  };
635 
636 //
637 // itkAlignedTypedef is a macro which creates a new typedef to make a
638 // data structure aligned.
639 //
640 #if defined( ITK_HAS_GNU_ATTRIBUTE_ALIGNED )
641 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
642  typedef oldtype newtype __attribute__((aligned(alignment)))
643 #elif defined ( _MSC_VER )
644 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
645  typedef __declspec(align( alignment )) oldtype newtype
646 #else
647 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
648  typedef oldtype newtype
649 #endif
650 
651 //=============================================================================
652 /* Define a common way of declaring a templated function as a friend inside a class.
653  - ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENTS(T)
654 
655  The following templated function
656 
657  template <T>
658  T add(const T & a, const T & b);
659 
660  is declared as friend in some compilers as:
661 
662  class A
663  {
664  public:
665  friend Self add<Self>( const Self & a, const Self & b );
666  }
667 
668  while other compilers will do
669 
670  class A
671  {
672  public:
673  friend Self add<>( const Self & a, const Self & b );
674  }
675 
676  This characteristic of the compiler is checked by a TRY_COMPILE
677  command defined in Insight/CMake/itkTestFriendTemplatedFunction.cxx
678 
679 */
680 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_NULL_STRING )
681 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T)
682 #else
683 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_EMPTY_BRACKETS )
684 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T) < >
685 #else
686 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_TEMPLATE_ARGUMENTS )
687 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T) < T >
688 #endif
689 #endif
690 #endif
691 // THIS IS A TEMPORARY PATCH FOR Visual Studio 10. The correct solution must
692 // be implemented in Insight/CMake/itkTestFriendTemplatedFunction.cxx
693 #if ( defined ( _MSC_VER ) && ( _MSC_VER >= 1600 ) )
694 #ifdef ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT
695 #undef ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT
696 #endif
697 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T)
698 #endif
699 
700 //--------------------------------------------------------------------------------
701 // Helper macros for Template Meta-Programming techniques of for-loops
702 // unrolling
703 //--------------------------------------------------------------------------------
704 
705 //--------------------------------------------------------------------------------
706 // Macro that generates an unrolled for loop for assigning elements of one array
707 // to elements of another array The array are assumed to be of same length
708 // (dimension), and this is also assumed to be the value of NumberOfIterations.
709 // No verification of size is performed. Casting is perfomed as part of the
710 // assignment, by using the DestinationElementType as the casting type.
711 // Source and destination array types must have defined opearator[] in their
712 // API.
713 #define itkForLoopAssignmentMacro(DestinationType, \
714  SourceType, \
715  DestinationElementType, \
716  DestinationArray, \
717  SourceArray, \
718  NumberOfIterations) \
719  for ( unsigned int i = 0; i < NumberOfIterations; ++i ) \
720  { \
721  DestinationArray[i] = static_cast< DestinationElementType >( SourceArray[i] ); \
722  }
723 
724 //--------------------------------------------------------------------------------
725 // Macro that generates an unrolled for loop for rounding and assigning
726 // elements of one array to elements of another array The array are assumed to
727 // be of same length (dimension), and this is also assumed to be the value of
728 // NumberOfIterations. No verification of size is performed. Casting is
729 // perfomed as part of the assignment, by using the DestinationElementType as
730 // the casting type.
731 // Source and destination array types must have defined opearator[] in their
732 // API.
733 #define itkForLoopRoundingAndAssignmentMacro(DestinationType, \
734  Sourcrnd_halfintup, \
735  DestinationElementType, \
736  DestinationArray, \
737  SourceArray, \
738  NumberOfIterations) \
739  for ( unsigned int i = 0; i < NumberOfIterations; ++i ) \
740  { \
741  DestinationArray[i] = itk::Math::Round< DestinationElementType >(SourceArray[i]); \
742  }
743 
744 // end of Template Meta Programming helper macros
745 
746 #ifndef NDEBUG
747 
748 #ifdef _POSIX_SOURCE
749 #define itkAssertInDebugOrThrowInReleaseMacro(msg) __assert_fail (msg, __FILE__, __LINE__, __ASSERT_FUNCTION);
750 #else
751 #define itkAssertInDebugOrThrowInReleaseMacro(msg) itkGenericExceptionMacro(<< msg);
752 #endif
753 
754 #else
755 #define itkAssertInDebugOrThrowInReleaseMacro(msg) itkGenericExceptionMacro(<< msg);
756 #endif
757 
758 #define itkAssertOrThrowMacro(test, message) \
759  if ( !( test ) ) \
760  { \
761  std::ostringstream msgstr; \
762  msgstr << message; \
763  itkAssertInDebugOrThrowInReleaseMacro( msgstr.str().c_str() ); \
764  }
765 
766 #ifndef NDEBUG
767 #define itkAssertInDebugAndIgnoreInReleaseMacro(X) assert(X)
768 #else
769 #define itkAssertInDebugAndIgnoreInReleaseMacro(X)
770 #endif
771 
772 #ifdef ITKV3_COMPATIBILITY
773 // As of MSVS++ 7.1 and greater, typename is supported in templates
774 // All ITKv4 compilers support the typename keyword, but this is
775 // needed to ease transition from ITKv3.
776 #define ITK_TYPENAME typename
777 #endif
778 
782 template <typename TTarget, typename TSource>
783 TTarget itkDynamicCastInDebugMode(TSource x)
784 {
785 #ifndef NDEBUG
786  if(x == 0)
787  {
788  return 0;
789  }
790  TTarget rval = dynamic_cast<TTarget>(x);
791  if(rval == 0)
792  {
793  itkGenericExceptionMacro(<< "Failed dynamic cast to "
794  << typeid(TTarget).name()
795  << " object type = "
796  << x->GetNameOfClass());
797  }
798  return rval;
799 #else
800  return static_cast<TTarget>(x);
801 #endif
802 }
804 
805 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
806 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
807 // !! The ITK Get/Set Macros for various types !!
808 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
809 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
824 #if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__ ) < 405 && !defined( __clang__ ) && !defined( __INTEL_COMPILER )
825 # define itkStaticConstMacro(name,type,value) enum { name = value }
826 #else
827 # define itkStaticConstMacro(name,type,value) static ITK_CONSTEXPR_VAR type name = value
828 #endif
829 
830 #define itkGetStaticConstMacro(name) (Self::name)
831 
833 #define itkSetInputMacro(name, type) \
834  virtual void Set##name(const type *_arg) \
835  { \
836  itkDebugMacro("setting input " #name " to " << _arg); \
837  if ( _arg != itkDynamicCastInDebugMode< type * >( this->ProcessObject::GetInput(#name) ) ) \
838  { \
839  this->ProcessObject::SetInput( #name, const_cast< type * >( _arg ) ); \
840  this->Modified(); \
841  } \
842  }
843 
844 
846 #define itkGetInputMacro(name, type) \
847  virtual const type * Get##name() const \
848  { \
849  itkDebugMacro( "returning input " << #name " of " << this->ProcessObject::GetInput(#name) ); \
850  return itkDynamicCastInDebugMode< const type * >( this->ProcessObject::GetInput(#name) ); \
851  }
852 
853 
855 #define itkSetDecoratedInputMacro(name, type) \
856  virtual void Set##name##Input(const SimpleDataObjectDecorator< type > *_arg) \
857  { \
858  itkDebugMacro("setting input " #name " to " << _arg); \
859  if ( _arg != itkDynamicCastInDebugMode< SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ) \
860  { \
861  this->ProcessObject::SetInput( #name, const_cast< SimpleDataObjectDecorator< type > * >( _arg ) ); \
862  this->Modified(); \
863  } \
864  } \
865  virtual void Set##name(const SimpleDataObjectDecorator< type > *_arg) \
866  { \
867  this->Set##name##Input(_arg); \
868  } \
869  virtual void Set##name(const type &_arg) \
870  { \
871  typedef SimpleDataObjectDecorator< type > DecoratorType; \
872  itkDebugMacro("setting input " #name " to " << _arg); \
873  const DecoratorType *oldInput = \
874  itkDynamicCastInDebugMode< const DecoratorType * >( \
875  this->ProcessObject::GetInput(#name) ); \
876 CLANG_PRAGMA_PUSH \
877 CLANG_SUPPRESS_Wfloat_equal \
878  if ( oldInput && oldInput->Get() == _arg ) \
879 CLANG_PRAGMA_POP \
880  { \
881  return; \
882  } \
883  typename DecoratorType::Pointer newInput = DecoratorType::New(); \
884  newInput->Set(_arg); \
885  this->Set##name##Input(newInput); \
886  }
887 
888 
890 #define itkGetDecoratedInputMacro(name, type) \
891  virtual const SimpleDataObjectDecorator< type > * Get##name##Input() const \
892  { \
893  itkDebugMacro( "returning input " << #name " of " << this->ProcessObject::GetInput(#name) ); \
894  return itkDynamicCastInDebugMode< const SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ); \
895  } \
896  virtual const type & Get##name() const \
897  { \
898  itkDebugMacro("Getting input " #name); \
899  typedef SimpleDataObjectDecorator< type > DecoratorType; \
900  const DecoratorType *input = \
901  itkDynamicCastInDebugMode< const DecoratorType * >( \
902  this->ProcessObject::GetInput(#name) ); \
903  if( input == ITK_NULLPTR ) \
904  { \
905  itkExceptionMacro(<<"input" #name " is not set"); \
906  } \
907  return input->Get(); \
908  }
909 
910 
913 #define itkSetGetDecoratedInputMacro(name, type) \
914  itkSetDecoratedInputMacro(name, type) \
915  itkGetDecoratedInputMacro(name, type)
916 
921 #define itkSetDecoratedObjectInputMacro(name, type) \
922  virtual void Set##name##Input(const DataObjectDecorator< type > *_arg) \
923  { \
924  itkDebugMacro("setting input " #name " to " << _arg); \
925  if ( _arg != itkDynamicCastInDebugMode< DataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ) \
926  { \
927  this->ProcessObject::SetInput( #name, const_cast< DataObjectDecorator< type > * >( _arg ) ); \
928  this->Modified(); \
929  } \
930  } \
931  virtual void Set##name(const type * _arg) \
932  { \
933  typedef DataObjectDecorator< type > DecoratorType; \
934  itkDebugMacro("setting input " #name " to " << _arg); \
935  const DecoratorType *oldInput = \
936  itkDynamicCastInDebugMode< const DecoratorType * >( \
937  this->ProcessObject::GetInput(#name) ); \
938  if ( oldInput && oldInput->Get() == _arg ) \
939  { \
940  return; \
941  } \
942  typename DecoratorType::Pointer newInput = DecoratorType::New(); \
943  newInput->Set(_arg); \
944  this->Set##name##Input(newInput); \
945  }
946 
947 
952 #define itkGetDecoratedObjectInputMacro(name, type) \
953  virtual const DataObjectDecorator< type > * Get##name##Input() const \
954  { \
955  itkDebugMacro( "returning input " << #name " of "<< this->ProcessObject::GetInput(#name) ); \
956  return itkDynamicCastInDebugMode< const DataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ); \
957  } \
958  virtual const type * Get##name() const \
959  { \
960  itkDebugMacro("Getting input " #name); \
961  typedef DataObjectDecorator< type > DecoratorType; \
962  const DecoratorType *input = \
963  itkDynamicCastInDebugMode< const DecoratorType * >( \
964  this->ProcessObject::GetInput(#name) ); \
965  if( input == ITK_NULLPTR ) \
966  { \
967  return ITK_NULLPTR; \
968  } \
969  return input->Get(); \
970  }
971 
972 
975 #define itkSetGetDecoratedObjectInputMacro(name, type) \
976  itkSetDecoratedObjectInputMacro(name, type) \
977  itkGetDecoratedObjectInputMacro(name, type)
978 
980 #define itkSetMacro(name, type) \
981  virtual void Set##name (const type _arg) \
982  { \
983  itkDebugMacro("setting " #name " to " << _arg); \
984 CLANG_PRAGMA_PUSH \
985 CLANG_SUPPRESS_Wfloat_equal \
986  if ( this->m_##name != _arg ) \
987  { \
988  this->m_##name = _arg; \
989  this->Modified(); \
990  } \
991 CLANG_PRAGMA_POP \
992  }
993 
994 
996 #define itkGetMacro(name, type) \
997  virtual type Get##name () \
998  { \
999  return this->m_##name; \
1000  }
1001 
1002 
1006 #define itkGetConstMacro(name, type) \
1007  virtual type Get##name () const \
1008  { \
1009  return this->m_##name; \
1010  }
1011 
1012 
1017 #define itkGetConstReferenceMacro(name, type) \
1018  virtual const type &Get##name () const \
1019  { \
1020  return this->m_##name; \
1021  }
1022 
1023 
1028 #define itkSetEnumMacro(name, type) \
1029  virtual void Set##name (const type _arg) \
1030  { \
1031  itkDebugMacro( "setting " #name " to " << static_cast< long >( _arg ) ); \
1032  if ( this->m_##name != _arg ) \
1033  { \
1034  this->m_##name = _arg; \
1035  this->Modified(); \
1036  } \
1037  }
1038 
1039 
1044 #define itkGetEnumMacro(name, type) \
1045  virtual type Get##name () const \
1046  { \
1047  return this->m_##name; \
1048  }
1049 
1050 
1054 #define itkSetStringMacro(name) \
1055  virtual void Set##name (const char *_arg) \
1056  { \
1057  if ( _arg && ( _arg == this->m_##name ) ) { return; } \
1058  if ( _arg ) \
1059  { \
1060  this->m_##name = _arg; \
1061  } \
1062  else \
1063  { \
1064  this->m_##name = ""; \
1065  } \
1066  this->Modified(); \
1067  } \
1068  virtual void Set##name (const std::string & _arg) \
1069  { \
1070  this->Set##name( _arg.c_str() ); \
1071  } \
1072 
1073 
1074 
1078 #define itkGetStringMacro(name) \
1079  virtual const char *Get##name () const \
1080  { \
1081  return this->m_##name.c_str(); \
1082  }
1083 
1087 #define itkSetClampMacro(name, type, min, max) \
1088  virtual void Set##name (type _arg) \
1089  { \
1090  const type temp_extrema=( _arg < min ? min : ( _arg > max ? max : _arg ) );\
1091  itkDebugMacro("setting " << #name " to " << _arg); \
1092 CLANG_PRAGMA_PUSH \
1093 CLANG_SUPPRESS_Wfloat_equal \
1094  if ( this->m_##name != temp_extrema ) \
1095  { \
1096  this->m_##name = temp_extrema; \
1097  this->Modified(); \
1098  } \
1099 CLANG_PRAGMA_POP \
1100  }
1101 
1102 
1103 //NOTE: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
1108 #define itkSetObjectMacro(name, type) \
1109  virtual void Set##name (type * _arg) \
1110  { \
1111  itkDebugMacro("setting " << #name " to " << _arg); \
1112 CLANG_PRAGMA_PUSH \
1113 CLANG_SUPPRESS_Wfloat_equal \
1114  if ( this->m_##name != _arg ) \
1115  { \
1116  this->m_##name = _arg; \
1117  this->Modified(); \
1118  } \
1119 CLANG_PRAGMA_POP \
1120  }
1121 
1122 
1131 // NOTE: A class can use either itkGetModifiableObjectMacro
1132 // or itkGetObjectMacro, but not both.
1133 // A class can use either itkGetModifiableObjectMacro
1134 // or itkGetConstObjectMacro, but not both.
1135 // If the desired behavior is to only provide const
1136 // access to the itkObject ivar, then use itkGetConstObjectMacro,
1137 // else use itkGetModifiableObjectMacro for read/write access to
1138 // the ivar.
1139 // It is permissable to use both itkGetObjectMacro and itkGetConstObjectMacro
1140 // for backwards compatibility.
1141 // If the ITK_LEGACY_REMOVE=FALSE, then it is
1142 // permissable to use itkGetObjectMacro which
1143 // defines both signatures itk::GetXXX() and
1144 // itk::GetModifiableXXX()
1145 
1148 #define itkGetConstObjectMacro(name, type) \
1149  virtual const type * Get##name () const \
1150  { \
1151  return this->m_##name.GetPointer(); \
1152  }
1153 
1154 
1155 #if defined ( ITK_FUTURE_LEGACY_REMOVE )
1156 // In the future, the itkGetObjectMacro will be deprecated with the ITK_LEGACY_REMOVE
1157 // flag. For now, this very advanced feature is only available
1158 // through manual setting of a compiler define -DITK_FUTURE_LEGACY_REMOVE
1159 // ("/DITK_FUTURE_LEGACY_REMOVE /EHsc" with Visual Studio)
1160 // to ease the transition from the historical GetObjectMacro to the GetModifiableObjectMacro
1161 # define itkGetObjectMacro(name, type) \
1162  virtual type * Get##name () \
1163  { \
1164  purposeful_error("itkGetObjectMacro should be replaced with itkGetModifiableObjectMacro."); \
1165  }
1166 
1167 # define itkGetModifiableObjectMacro(name, type) \
1168  virtual type * GetModifiable##name () \
1169  { \
1170  return this->m_##name.GetPointer(); \
1171  } \
1172  itkGetConstObjectMacro(name, type)
1173 
1174 #else // defined ( ITK_FUTURE_LEGACY_REMOVE )
1175 
1177 # define itkGetObjectMacro(name, type) \
1178  virtual type * Get##name () \
1179  { \
1180  return this->m_##name.GetPointer(); \
1181  }
1182 # define itkGetModifiableObjectMacro(name, type) \
1183  virtual type * GetModifiable##name () \
1184  { \
1185  return this->m_##name.GetPointer(); \
1186  } \
1187  itkGetConstObjectMacro(name, type) \
1188  itkGetObjectMacro(name, type)
1189 #endif // defined ( ITK_FUTURE_LEGACY_REMOVE )
1190 
1191 
1192 // For backwards compatibility define ITK_EXPORT to nothing
1193 #define ITK_EXPORT
1194 
1195 
1198 #define itkGetConstReferenceObjectMacro(name, type) \
1199  virtual const typename type::Pointer & Get##name () const \
1200  { \
1201  return this->m_##name; \
1202  }
1203 
1208 #define itkSetConstObjectMacro(name, type) \
1209  virtual void Set##name (const type * _arg) \
1210  { \
1211  itkDebugMacro("setting " << #name " to " << _arg); \
1212  if ( this->m_##name != _arg ) \
1213  { \
1214  this->m_##name = _arg; \
1215  this->Modified(); \
1216  } \
1217  }
1218 
1219 
1222 #define itkBooleanMacro(name) \
1223  virtual void name##On () \
1224  { \
1225  this->Set##name(true); \
1226  } \
1227  virtual void name##Off () \
1228  { \
1229  this->Set##name(false); \
1230  }
1231 
1232 
1236 #define itkSetVectorMacro(name, type, count) \
1237  virtual void Set##name(type data[]) \
1238  { \
1239  unsigned int i; \
1240  for ( i = 0; i < count; i++ ) \
1241  { \
1242 CLANG_PRAGMA_PUSH \
1243 CLANG_SUPPRESS_Wfloat_equal \
1244  if ( data[i] != this->m_##name[i] ) \
1245 CLANG_PRAGMA_POP \
1246  { \
1247  break; \
1248  } \
1249  } \
1250  if ( i < count ) \
1251  { \
1252  this->Modified(); \
1253  for ( i = 0; i < count; i++ ) \
1254  { \
1255  this->m_##name[i] = data[i]; \
1256  } \
1257  } \
1258  }
1259 
1260 
1263 #define itkGetVectorMacro(name, type, count) \
1264  virtual type * Get##name () const \
1265  { \
1266  return this->m_##name; \
1267  }
1268 
1273 #define itkGPUKernelClassMacro(kernel) \
1274 
1275 
1280 
1295 
1317 
1352 
1354