ITK  5.1.0
Insight Toolkit
itkMacro.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 // Define ITK_PRAGMA macro.
70 //
71 // It sets "#pragma" preprocessor directives without expecting the arguments
72 // to be quoted.
73 #define ITK_PRAGMA(x) _Pragma(#x)
74 
75 // The clang compiler has many useful non-default compiler warnings
76 // that tend to have a high false positive rate.
77 // The following set of defines allows us to suppress false positives
78 // and still track down suspicious code
79 #if defined(__clang__) && defined(__has_warning)
80 # define CLANG_PRAGMA_PUSH ITK_PRAGMA(clang diagnostic push)
81 # define CLANG_PRAGMA_POP ITK_PRAGMA(clang diagnostic pop)
82 # if __has_warning("-Wfloat-equal")
83 # define CLANG_SUPPRESS_Wfloat_equal ITK_PRAGMA(clang diagnostic ignored "-Wfloat-equal")
84 # else
85 # define CLANG_SUPPRESS_Wfloat_equal
86 # endif
87 # if __has_warning("-Wc++14-extensions")
88 # define CLANG_SUPPRESS_Wc__14_extensions ITK_PRAGMA(clang diagnostic ignored "-Wc++14-extensions")
89 # else
90 # define CLANG_SUPPRESS_Wc__14_extensions
91 # endif
92 #else
93 # define CLANG_PRAGMA_PUSH
94 # define CLANG_PRAGMA_POP
95 # define CLANG_SUPPRESS_Wfloat_equal
96 # define CLANG_SUPPRESS_Wc__14_extensions
97 #endif
98 
99 // Intel compiler convenience macros
100 #if defined(__INTEL_COMPILER)
101 # define INTEL_PRAGMA_WARN_PUSH ITK_PRAGMA(warning push)
102 # define INTEL_PRAGMA_WARN_POP ITK_PRAGMA(warning pop)
103 # define INTEL_SUPPRESS_warning_1292 ITK_PRAGMA(warning disable 1292)
104 #else
105 # define INTEL_PRAGMA_WARN_PUSH
106 # define INTEL_PRAGMA_WARN_POP
107 # define INTEL_SUPPRESS_warning_1292
108 #endif
109 
110 // Define ITK_GCC_PRAGMA_DIAG(param1 [param2 [...]]) macro.
111 //
112 // This macro sets a pragma diagnostic
113 //
114 // Define ITK_GCC_PRAGMA_DIAG_(PUSH|POP) macros.
115 //
116 // These macros respectively push and pop the diagnostic context
117 //
118 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
119 # define ITK_GCC_PRAGMA_DIAG(x) ITK_PRAGMA(GCC diagnostic x)
120 # define ITK_GCC_PRAGMA_DIAG_PUSH() ITK_GCC_PRAGMA_DIAG(push)
121 # define ITK_GCC_PRAGMA_DIAG_POP() ITK_GCC_PRAGMA_DIAG(pop)
122 #else
123 # define ITK_GCC_PRAGMA_DIAG(x)
124 # define ITK_GCC_PRAGMA_DIAG_PUSH()
125 # define ITK_GCC_PRAGMA_DIAG_POP()
126 #endif
127 
128 /*
129  * ITK only supports MSVC++ 14.0 and greater
130  * MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015 version 14.0)
131  * MSVC++ 14.1 _MSC_VER == 1910 (Visual Studio 2017 version 15.0)
132  * MSVC++ 14.11 _MSC_VER == 1911 (Visual Studio 2017 version 15.3)
133  * MSVC++ 14.12 _MSC_VER == 1912 (Visual Studio 2017 version 15.5)
134  * MSVC++ 14.13 _MSC_VER == 1913 (Visual Studio 2017 version 15.6)
135  * MSVC++ 14.14 _MSC_VER == 1914 (Visual Studio 2017 version 15.7)
136  * MSVC++ 14.15 _MSC_VER == 1915 (Visual Studio 2017 version 15.8)
137  * MSVC++ 14.16 _MSC_VER == 1916 (Visual Studio 2017 version 15.9)
138  * MSVC++ 14.2 _MSC_VER == 1920 (Visual Studio 2019 Version 16.0)
139  */
140 #if defined(_MSC_VER) && (_MSC_VER < 1900)
141 # error "Visual Studio < 2015 is not supported under ITKv5"
142 #endif
143 #if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140)
144 # error "SUNPro C++ < 5.14.0 is not supported under ITKv5 and above"
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__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && \
156  ((__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ < 8)))
157 # error "GCC < 4.8 is not supported under ITKv5"
158 #endif
159 #if defined(__sgi)
160 // This is true for IRIX 6.5.18m with MIPSPro 7.3.1.3m.
161 // TODO: At some future point, it may be necessary to
162 // define a minimum __sgi version that will work.
163 # error "The SGI compiler is not supported under ITKv4 and above"
164 #endif
165 #if defined(__APPLE__)
166 # if defined(__clang__) && (__cplusplus < 201103L)
167 # error "Apple LLVM < 5.0 (clang < 3.3) is not supported under ITKv5"
168 # endif
169 #elif defined(__clang__) && ((__clang_major__ < 3) || ((__clang_major__ == 3) && (__clang_minor__ < 3)))
170 # error "Clang < 3.3 is not supported under ITKv5"
171 #endif
172 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER < 1504)
173 # error "Intel C++ < 15.0.4 is not supported under ITKv5"
174 #endif
175 
176 // Setup symbol exports
177 #if defined(_WIN32) || defined(WIN32)
178 # define ITK_ABI_IMPORT __declspec(dllimport)
179 # define ITK_ABI_EXPORT __declspec(dllexport)
180 # define ITK_ABI_HIDDEN
181 #else
182 # if __GNUC__ >= 4
183 # define ITK_ABI_IMPORT __attribute__((visibility("default")))
184 # define ITK_ABI_EXPORT __attribute__((visibility("default")))
185 # define ITK_ABI_HIDDEN __attribute__((visibility("hidden")))
186 # else
187 # define ITK_ABI_IMPORT
188 # define ITK_ABI_EXPORT
189 # define ITK_ABI_HIDDEN
190 # endif
191 #endif
192 
193 // Setup symbol exports
194 #ifndef ITK_TEMPLATE_EXPORT
195 # ifdef ITK_TEMPLATE_VISIBILITY_DEFAULT
196 # define ITK_TEMPLATE_EXPORT __attribute__((visibility("default")))
197 # else
198 # define ITK_TEMPLATE_EXPORT
199 # endif
200 #endif
201 
202 // Setup symbol exports
203 #ifdef ITK_TEMPLATE_VISIBILITY_DEFAULT
204 # define ITK_FORCE_EXPORT_MACRO(moduleName) __attribute__((visibility("default")))
205 #else
206 # define ITK_FORCE_EXPORT_MACRO(moduleName) moduleName##_EXPORT
207 #endif
208 
209 #ifndef ITK_FORWARD_EXPORT
210 // If build with shared libraries, on MacOS, if USE_COMPILER_HIDDEN_VISIBILITY is ON
211 # if defined(__APPLE__) && defined(ITK_TEMPLATE_VISIBILITY_DEFAULT) && defined(ITK_BUILD_SHARED_LIBS) && \
212  defined(USE_COMPILER_HIDDEN_VISIBILITY)
213 # define ITK_FORWARD_EXPORT __attribute__((visibility("default")))
214 # else
215 # define ITK_FORWARD_EXPORT
216 # endif
217 #endif
218 
219 
220 //-*-*-*
221 // The following deprecations should be removed in ITKV6 and later
222 // NOTE DEPRECATED should be ITK_NOEXCEPT
223 #define ITK_NOEXCEPT_OR_THROW error "Replace ITK_NOEXCEPT_OR_THROW with ITK_NOEXCEPT"
224 // NOTE DEPRECATED! should be ITK_COMPILER_CXX_STATIC_ASSERT
225 #if !defined(ITK_LEGACY_REMOVE)
226 # define ITK_DELETE_FUNCTION = delete
227 #else
228 # define ITK_DELETE_FUNCTION error "Replace ITK_DELETE_FUNCTION with = delete"
229 #endif
230 //-*-*-*
231 
232 // DEPRECATED: These macros are left here for compatibility with remote modules.
233 // Once they have been removed from all known remote modules, this code should
234 // be removed.
235 #if defined(ITK_FUTURE_LEGACY_REMOVE)
236 # define ITK_CONSTEXPR_FUNC "Replace ITK_CONSTEXPR_FUNC with constexpr"
237 # define ITK_CONSTEXPR_VAR "Replace ITK_CONSTEXPR_VAR with constexpr"
238 
239 # define itkExposeEnumValue(name) static_cast<int>(name)
240 // Future remove# define itkExposeEnumValue(name) "Replace type of " # name " with proper enumeration instead of
241 // integer."
242 #else
243 # define ITK_CONSTEXPR_FUNC constexpr
244 # define ITK_CONSTEXPR_VAR constexpr
245 
247 # define itkExposeEnumValue(name) static_cast<int>(name)
248 #endif
249 
250 // Use "ITK_FALLTHROUGH;" to annotate deliberate fall-through in switches,
251 // use it analogously to "break;". The trailing semi-colon is required.
252 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
253 # if (__GNUC__ >= 7)
254 # define ITK_FALLTHROUGH __attribute__((fallthrough))
255 # endif
256 #elif defined(__has_warning)
257 # if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
258 # define ITK_FALLTHROUGH [[clang::fallthrough]]
259 # endif
260 #endif
261 
262 #ifndef ITK_FALLTHROUGH
263 # define ITK_FALLTHROUGH ((void)0)
264 #endif
265 
286 #define itkNewMacro(x) itkSimpleNewMacro(x) itkCreateAnotherMacro(x) itkCloneMacro(x)
287 
288 #define itkSimpleNewMacro(x) \
289  static Pointer New() \
290  { \
291  Pointer smartPtr = ::itk::ObjectFactory<x>::Create(); \
292  if (smartPtr == nullptr) \
293  { \
294  smartPtr = new x; \
295  } \
296  smartPtr->UnRegister(); \
297  return smartPtr; \
298  }
299 
300 #define itkCreateAnotherMacro(x) \
301  ::itk::LightObject::Pointer CreateAnother() const override \
302  { \
303  ::itk::LightObject::Pointer smartPtr; \
304  smartPtr = x::New().GetPointer(); \
305  return smartPtr; \
306  }
307 
308 #define itkCloneMacro(x) \
309  Pointer Clone() const \
310  { \
311  Pointer rval = dynamic_cast<x *>(this->InternalClone().GetPointer()); \
312  return rval; \
313  }
314 
327 #define itkFactorylessNewMacro(x) \
328  static Pointer New() \
329  { \
330  Pointer smartPtr; \
331  x * rawPtr = new x; \
332  smartPtr = rawPtr; \
333  rawPtr->UnRegister(); \
334  return smartPtr; \
335  } \
336  ::itk::LightObject::Pointer CreateAnother() const override \
337  { \
338  ::itk::LightObject::Pointer smartPtr; \
339  smartPtr = x::New().GetPointer(); \
340  return smartPtr; \
341  }
342 
343 
344 //
345 // A macro to disallow the copy constructor, copy assignment,
346 // move constructor, and move assignment functions.
347 // This should be used in the public: declarations for a class
348 //
349 // ITK's paradigm for smart pointer and pipeline consistency
350 // prohibits the use of copy/move construction and copy/move assignment
351 // functions.
352 //
353 #define ITK_DISALLOW_COPY_AND_ASSIGN(TypeName) \
354  TypeName(const TypeName &) = delete; \
355  TypeName & operator=(const TypeName &) = delete; \
356  TypeName(TypeName &&) = delete; \
357  TypeName & operator=(TypeName &&) = delete
358 
361 #define itkTypeMacro(thisClass, superclass) \
362  const char * GetNameOfClass() const override { return #thisClass; }
363 
364 #define itkTypeMacroNoParent(thisClass) \
365  virtual const char * GetNameOfClass() const { return #thisClass; }
366 
367 namespace itk
368 {
375 extern ITKCommon_EXPORT void
376 OutputWindowDisplayText(const char *);
378 
379 extern ITKCommon_EXPORT void
380 OutputWindowDisplayErrorText(const char *);
381 
382 extern ITKCommon_EXPORT void
383 OutputWindowDisplayWarningText(const char *);
384 
385 extern ITKCommon_EXPORT void
386 OutputWindowDisplayGenericOutputText(const char *);
387 
388 extern ITKCommon_EXPORT void
389 OutputWindowDisplayDebugText(const char *);
390 } // end namespace itk
391 
395 #if defined(NDEBUG)
396 # define itkDebugMacro(x)
397 # define itkDebugStatement(x)
398 #else
399 # define itkDebugMacro(x) \
400  do \
401  { \
402  if (this->GetDebug() && ::itk::Object::GetGlobalWarningDisplay()) \
403  { \
404  std::ostringstream itkmsg; \
405  itkmsg << "Debug: In " __FILE__ ", line " << __LINE__ << "\n" \
406  << this->GetNameOfClass() << " (" << this << "): " x << "\n\n"; \
407  ::itk::OutputWindowDisplayDebugText(itkmsg.str().c_str()); \
408  } \
409  } while (0)
410 
411 
412 // The itkDebugStatement is to be used to protect code that is only
413 // used in the itkDebugMacro
414 # define itkDebugStatement(x) x
415 #endif
416 
420 #define itkWarningMacro(x) \
421  do \
422  { \
423  if (::itk::Object::GetGlobalWarningDisplay()) \
424  { \
425  std::ostringstream itkmsg; \
426  itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
427  << this->GetNameOfClass() << " (" << this << "): " x << "\n\n"; \
428  ::itk::OutputWindowDisplayWarningText(itkmsg.str().c_str()); \
429  } \
430  } while (0)
431 
432 
433 // The itkDebugStatement is to be used ot protect code that is only
434 // used in the itkDebugMacro
435 #define itkWarningStatement(x) x
436 
437 #if defined(ITK_CPP_FUNCTION)
438 # if defined(_WIN32) && !defined(__MINGW32__) && !defined(ITK_WRAPPING_PARSER)
439 # define ITK_LOCATION __FUNCSIG__
440 # elif defined(__GNUC__)
441 # define ITK_LOCATION __PRETTY_FUNCTION__
442 # else
443 # define ITK_LOCATION __FUNCTION__
444 # endif
445 #else
446 # define ITK_LOCATION "unknown"
447 #endif
448 
452 #define itkExceptionMacro(x) \
453  { \
454  std::ostringstream message; \
455  message << "itk::ERROR: " << this->GetNameOfClass() << "(" << this << "): " x; \
456  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(), ITK_LOCATION); \
457  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
458  }
459 
460 
461 #define itkGenericExceptionMacro(x) \
462  { \
463  std::ostringstream message; \
464  message << "itk::ERROR: " x; \
465  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(), ITK_LOCATION); \
466  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
467  }
468 
469 #define itkDeclareExceptionMacro(newexcp, parentexcp, whatmessage) \
470  namespace itk \
471  { \
472  class newexcp : public parentexcp \
473  { \
474  public: \
475  newexcp(const char * file, unsigned int lineNumber) \
476  : parentexcp(file, lineNumber) \
477  { \
478  this->SetDescription(whatmessage); \
479  } \
480  newexcp(const std::string & file, unsigned int lineNumber) \
481  : parentexcp(file, lineNumber) \
482  { \
483  this->SetDescription(whatmessage); \
484  } \
485  itkTypeMacro(newexcp, parentexcp); \
486  }; \
487  }
488 
489 #define itkSpecializedExceptionMacro(exceptiontype) \
490  { \
491  ::itk::exceptiontype e_(__FILE__, __LINE__); \
492  e_.SetLocation(ITK_LOCATION); \
493  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
494  }
495 
496 #define itkSpecializedMessageExceptionMacro(exceptiontype, x) \
497  { \
498  ::itk::exceptiontype e_(__FILE__, __LINE__); \
499  std::ostringstream message; \
500  message << "itk::ERROR: " x; \
501  e_.SetDescription(message.str().c_str()); \
502  e_.SetLocation(ITK_LOCATION); \
503  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
504  }
505 
506 
507 #define itkGenericOutputMacro(x) \
508  { \
509  if (::itk::Object::GetGlobalWarningDisplay()) \
510  { \
511  std::ostringstream itkmsg; \
512  itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" x << "\n\n"; \
513  ::itk::OutputWindowDisplayGenericOutputText(itkmsg.str().c_str()); \
514  } \
515  }
516 
517 //----------------------------------------------------------------------------
518 // Macros for simplifying the use of logging
519 //
520 #define itkLogMacro(x, y) \
521  { \
522  if (this->GetLogger()) \
523  { \
524  this->GetLogger()->Write(::itk::LoggerBase::x, y); \
525  } \
526  }
527 
528 #define itkLogMacroStatic(obj, x, y) \
529  { \
530  if (obj->GetLogger()) \
531  { \
532  obj->GetLogger()->Write(::itk::LoggerBase::x, y); \
533  } \
534  }
535 
536 //----------------------------------------------------------------------------
537 // Setup legacy code policy.
538 //
539 // CMake options:
540 // - When ITK_LEGACY_REMOVE:BOOL=ON, legacy code is hidden, thus causing compiler errors for code that depends on it
541 // - When ITK_LEGACY_REMOVE:BOOL=OFF, and ITK_LEGACY_SILENT:BOOL=ON, use
542 // of legacy code will not produce compiler warnings.
543 // - When ITK_LEGACY_REMOVE:BOOL=OFF, and ITK_LEGACY_SILENT:BOOL=OFF, use
544 // of legacy code will produce compiler warnings
545 //
546 // ITK_LEGACY_SILENT silently use legacy code. The default is to warn about legacy code use.
547 //
548 // Source files that test the legacy code may define ITK_LEGACY_TEST
549 // like this:
550 //
551 // #define ITK_LEGACY_TEST
552 // #include "itkClassWithDeprecatedMethod.h"
553 //
554 // in order to silence the warnings for calling deprecated methods.
555 // No other source files in ITK should call the methods since they are
556 // provided only for compatibility with older user code.
557 
558 // Define itkLegacyMacro to mark legacy methods where they are
559 // declared in their class. Example usage:
560 //
561 // // \deprecated Replaced by MyOtherMethod() as of ITK 2.0.
562 // itkLegacyMacro(void MyMethod());
563 //
564 // See below for what to do for the method definition.
565 #if defined(ITK_LEGACY_REMOVE)
566 # define itkLegacyMacro(method) /* no ';' */
567 #else
568 # if defined(ITK_LEGACY_SILENT) || defined(ITK_LEGACY_TEST)
569 // Provide legacy methods with no warnings.
570 # define itkLegacyMacro(method) method
571 # else
572 // Setup compile-time warnings for uses of deprecated methods if
573 // possible on this compiler.
574 # if defined(__GNUC__) && !defined(__INTEL_COMPILER)
575 # define itkLegacyMacro(method) method __attribute__((deprecated))
576 # elif defined(_MSC_VER)
577 # define itkLegacyMacro(method) __declspec(deprecated) method
578 # else
579 # define itkLegacyMacro(method) method
580 # endif
581 # endif
582 #endif
583 
584 // Macros to create runtime deprecation warning messages in function
585 // bodies. Example usage:
586 //
587 // #if !defined( ITK_LEGACY_REMOVE )
588 // void itkMyClass::MyOldMethod()
589 // {
590 // itkLegacyBodyMacro(itkMyClass::MyOldMethod, 2.0);
591 // }
592 //
593 // void itkMyClass::MyMethod()
594 // {
595 // itkLegacyReplaceBodyMacro(itkMyClass::MyMethod, 2.0,
596 // itkMyClass::MyOtherMethod);
597 // }
598 // #endif
599 //
600 // NOTE: These 4 macros itkLegacyBodyMacro, itkLegacyReplaceBodyMacro,
601 // itkGenericLegacyBodyMacro, and itkGenericLegacyReplaceBodyMacro
602 // are purposefully not defined when ITK_LEGACY_REMOVE is on,
603 // because these macros are only relevant inside code segments
604 // that are conditionally compiled only when ITK_LEGACY_REMOVE
605 // is off.
606 #if defined(ITK_LEGACY_SILENT)
607 # define itkLegacyBodyMacro(method, version)
608 # define itkLegacyReplaceBodyMacro(method, version, replace)
609 # define itkGenericLegacyBodyMacro(method, version)
610 # define itkGenericLegacyReplaceBodyMacro(method, version, replace)
611 #else
612 # define itkLegacyBodyMacro(method, version) \
613  itkWarningMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
614 # define itkLegacyReplaceBodyMacro(method, version, replace) \
615  itkWarningMacro(#method " was deprecated for ITK " #version \
616  " and will be removed in a future version. Use " #replace " instead.")
617 # define itkGenericLegacyBodyMacro(method, version) \
618  itkGenericOutputMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
619 # define itkGenericLegacyReplaceBodyMacro(method, version, replace) \
620  itkGenericOutputMacro(#method " was deprecated for ITK " #version \
621  " and will be removed in a future version. Use " #replace " instead.")
622 #endif
623 
624 // Most modern x86 CPUs have 64 byte aligned blocks which are used for
625 // the cache lines. By aligning multi-threaded structures with the
626 // cache lines, false shared can be reduced, and performance
627 // increased.
628 #define ITK_CACHE_LINE_ALIGNMENT 64
629 
630 //
631 // itkPadStruct will add padding to a structure to ensure a minimum size
632 // for ensuring that adjacent structures do not share CACHE lines.
633 // Each struct will take up some multiple of cacheline sizes.
634 // This is particularly useful for arrays of thread private variables.
635 //
636 #define itkPadStruct(mincachesize, oldtype, newtype) \
637  struct newtype : public oldtype \
638  { \
639  char _StructPadding[mincachesize - (sizeof(oldtype) % mincachesize)]; \
640  };
641 
642 //
643 // itkAlignedTypedef is a macro which creates a new type to make a
644 // data structure aligned.
645 //
646 #if defined(ITK_HAS_GNU_ATTRIBUTE_ALIGNED)
647 # define itkAlignedTypedef(alignment, oldtype, newtype) using newtype = oldtype __attribute__((aligned(alignment)))
648 #elif defined(_MSC_VER)
649 # define itkAlignedTypedef(alignment, oldtype, newtype) using newtype = __declspec(align(alignment)) oldtype
650 #else
651 # define itkAlignedTypedef(alignment, oldtype, newtype) using newtype = oldtype
652 #endif
653 
654 #if defined(ITK_FUTURE_LEGACY_REMOVE)
655 //=============================================================================
656 /*
657 NOTE: DEPRECATED - This macro is not longer needed to support modern
658 compilers.
659 
660  Define a common way of declaring a templated function as a friend inside a class.
661  - ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENTS(T)
662 
663  The following templated function
664 
665  template <T>
666  T add(const T & a, const T & b);
667 
668  is declared as friend with
669 
670  class A
671  {
672  public:
673  friend Self add<>( const Self & a, const Self & b );
674  }
675 
676 */
677 # define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T) < >
678 #else // LEGACY_REMOVE
679 # define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T) "Macro remove use C++11 compliant declaration of "
680 #endif
681 
682 //--------------------------------------------------------------------------------
683 // Helper macros for Template Meta-Programming techniques of for-loops
684 // unrolling
685 //--------------------------------------------------------------------------------
686 
687 //--------------------------------------------------------------------------------
688 // Macro that generates an unrolled for loop for assigning elements of one array
689 // to elements of another array The array are assumed to be of same length
690 // (dimension), and this is also assumed to be the value of NumberOfIterations.
691 // No verification of size is performed. Casting is performed as part of the
692 // assignment, by using the DestinationElementType as the casting type.
693 // Source and destination array types must have defined operator[] in their
694 // API.
695 #define itkForLoopAssignmentMacro( \
696  DestinationType, SourceType, DestinationElementType, DestinationArray, SourceArray, NumberOfIterations) \
697  for (unsigned int i = 0; i < NumberOfIterations; ++i) \
698  { \
699  DestinationArray[i] = static_cast<DestinationElementType>(SourceArray[i]); \
700  }
701 
702 //--------------------------------------------------------------------------------
703 // Macro that generates an unrolled for loop for rounding and assigning
704 // elements of one array to elements of another array The array are assumed to
705 // be of same length (dimension), and this is also assumed to be the value of
706 // NumberOfIterations. No verification of size is performed. Casting is
707 // performed as part of the assignment, by using the DestinationElementType as
708 // the casting type.
709 // Source and destination array types must have defined operator[] in their
710 // API.
711 #define itkForLoopRoundingAndAssignmentMacro( \
712  DestinationType, Sourcrnd_halfintup, DestinationElementType, DestinationArray, SourceArray, NumberOfIterations) \
713  for (unsigned int i = 0; i < NumberOfIterations; ++i) \
714  { \
715  DestinationArray[i] = itk::Math::Round<DestinationElementType>(SourceArray[i]); \
716  }
717 
718 // end of Template Meta Programming helper macros
719 
720 #ifndef NDEBUG
721 
722 # ifdef _POSIX_SOURCE
723 # define itkAssertInDebugOrThrowInReleaseMacro(msg) __assert_fail(msg, __FILE__, __LINE__, __ASSERT_FUNCTION);
724 # else
725 # define itkAssertInDebugOrThrowInReleaseMacro(msg) itkGenericExceptionMacro(<< msg);
726 # endif
727 
728 #else
729 # define itkAssertInDebugOrThrowInReleaseMacro(msg) itkGenericExceptionMacro(<< msg);
730 #endif
731 
732 #define itkAssertOrThrowMacro(test, message) \
733  if (!(test)) \
734  { \
735  std::ostringstream msgstr; \
736  msgstr << message; \
737  itkAssertInDebugOrThrowInReleaseMacro(msgstr.str().c_str()); \
738  }
739 
740 #ifndef NDEBUG
741 # define itkAssertInDebugAndIgnoreInReleaseMacro(X) assert(X)
742 #else
743 # define itkAssertInDebugAndIgnoreInReleaseMacro(X)
744 #endif
745 
746 
747 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
748 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
749 // !! The ITK Get/Set Macros for various types !!
750 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
751 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
766 #define itkStaticConstMacro(name, type, value) static constexpr type name = value
767 
768 #define itkGetStaticConstMacro(name) (Self::name)
769 
771 #define itkSetInputMacro(name, type) \
772  virtual void Set##name(const type * _arg) \
773  { \
774  itkDebugMacro("setting input " #name " to " << _arg); \
775  if (_arg != itkDynamicCastInDebugMode<type *>(this->ProcessObject::GetInput(#name))) \
776  { \
777  this->ProcessObject::SetInput(#name, const_cast<type *>(_arg)); \
778  this->Modified(); \
779  } \
780  }
781 
782 
784 #define itkGetInputMacro(name, type) \
785  virtual const type * Get##name() const \
786  { \
787  itkDebugMacro("returning input " << #name " of " << this->ProcessObject::GetInput(#name)); \
788  return itkDynamicCastInDebugMode<const type *>(this->ProcessObject::GetInput(#name)); \
789  }
790 
791 
793 #define itkSetDecoratedInputMacro(name, type) \
794  virtual void Set##name##Input(const SimpleDataObjectDecorator<type> * _arg) \
795  { \
796  itkDebugMacro("setting input " #name " to " << _arg); \
797  if (_arg != itkDynamicCastInDebugMode<SimpleDataObjectDecorator<type> *>(this->ProcessObject::GetInput(#name))) \
798  { \
799  this->ProcessObject::SetInput(#name, const_cast<SimpleDataObjectDecorator<type> *>(_arg)); \
800  this->Modified(); \
801  } \
802  } \
803  virtual void Set##name(const SimpleDataObjectDecorator<type> * _arg) { this->Set##name##Input(_arg); } \
804  virtual void Set##name(const type & _arg) \
805  { \
806  using DecoratorType = SimpleDataObjectDecorator<type>; \
807  itkDebugMacro("setting input " #name " to " << _arg); \
808  const DecoratorType * oldInput = \
809  itkDynamicCastInDebugMode<const DecoratorType *>(this->ProcessObject::GetInput(#name)); \
810  CLANG_PRAGMA_PUSH \
811  CLANG_SUPPRESS_Wfloat_equal if (oldInput && oldInput->Get() == _arg) CLANG_PRAGMA_POP { return; } \
812  typename DecoratorType::Pointer newInput = DecoratorType::New(); \
813  newInput->Set(_arg); \
814  this->Set##name##Input(newInput); \
815  }
816 
817 
819 #define itkGetDecoratedInputMacro(name, type) \
820  virtual const SimpleDataObjectDecorator<type> * Get##name##Input() const \
821  { \
822  itkDebugMacro("returning input " << #name " of " << this->ProcessObject::GetInput(#name)); \
823  return itkDynamicCastInDebugMode<const SimpleDataObjectDecorator<type> *>(this->ProcessObject::GetInput(#name)); \
824  } \
825  virtual const type & Get##name() const \
826  { \
827  itkDebugMacro("Getting input " #name); \
828  using DecoratorType = SimpleDataObjectDecorator<type>; \
829  const DecoratorType * input = \
830  itkDynamicCastInDebugMode<const DecoratorType *>(this->ProcessObject::GetInput(#name)); \
831  if (input == nullptr) \
832  { \
833  itkExceptionMacro(<< "input" #name " is not set"); \
834  } \
835  return input->Get(); \
836  }
837 
838 
841 #define itkSetGetDecoratedInputMacro(name, type) \
842  itkSetDecoratedInputMacro(name, type) itkGetDecoratedInputMacro(name, type)
843 
848 #define itkSetDecoratedObjectInputMacro(name, type) \
849  virtual void Set##name##Input(const DataObjectDecorator<type> * _arg) \
850  { \
851  itkDebugMacro("setting input " #name " to " << _arg); \
852  if (_arg != itkDynamicCastInDebugMode<DataObjectDecorator<type> *>(this->ProcessObject::GetInput(#name))) \
853  { \
854  this->ProcessObject::SetInput(#name, const_cast<DataObjectDecorator<type> *>(_arg)); \
855  this->Modified(); \
856  } \
857  } \
858  virtual void Set##name(const type * _arg) \
859  { \
860  using DecoratorType = DataObjectDecorator<type>; \
861  itkDebugMacro("setting input " #name " to " << _arg); \
862  const DecoratorType * oldInput = \
863  itkDynamicCastInDebugMode<const DecoratorType *>(this->ProcessObject::GetInput(#name)); \
864  if (oldInput && oldInput->Get() == _arg) \
865  { \
866  return; \
867  } \
868  typename DecoratorType::Pointer newInput = DecoratorType::New(); \
869  newInput->Set(_arg); \
870  this->Set##name##Input(newInput); \
871  }
872 
873 
878 #define itkGetDecoratedObjectInputMacro(name, type) \
879  virtual const DataObjectDecorator<type> * Get##name##Input() const \
880  { \
881  itkDebugMacro("returning input " << #name " of " << this->ProcessObject::GetInput(#name)); \
882  return itkDynamicCastInDebugMode<const DataObjectDecorator<type> *>(this->ProcessObject::GetInput(#name)); \
883  } \
884  virtual const type * Get##name() const \
885  { \
886  itkDebugMacro("Getting input " #name); \
887  using DecoratorType = DataObjectDecorator<type>; \
888  const DecoratorType * input = \
889  itkDynamicCastInDebugMode<const DecoratorType *>(this->ProcessObject::GetInput(#name)); \
890  if (input == nullptr) \
891  { \
892  return nullptr; \
893  } \
894  return input->Get(); \
895  }
896 
897 
900 #define itkSetGetDecoratedObjectInputMacro(name, type) \
901  itkSetDecoratedObjectInputMacro(name, type) itkGetDecoratedObjectInputMacro(name, type)
902 
904 #define itkSetMacro(name, type) \
905  virtual void Set##name(const type _arg) \
906  { \
907  itkDebugMacro("setting " #name " to " << _arg); \
908  CLANG_PRAGMA_PUSH \
909  CLANG_SUPPRESS_Wfloat_equal if (this->m_##name != _arg) \
910  { \
911  this->m_##name = _arg; \
912  this->Modified(); \
913  } \
914  CLANG_PRAGMA_POP \
915  }
916 
917 
919 #define itkGetMacro(name, type) \
920  virtual type Get##name() { return this->m_##name; }
921 
922 
926 #define itkGetConstMacro(name, type) \
927  virtual type Get##name() const { return this->m_##name; }
928 
929 
934 #define itkGetConstReferenceMacro(name, type) \
935  virtual const type & Get##name() const { return this->m_##name; }
936 
937 
942 #define itkSetEnumMacro(name, type) \
943  virtual void Set##name(const type _arg) \
944  { \
945  itkDebugMacro("setting " #name " to " << static_cast<long>(_arg)); \
946  if (this->m_##name != _arg) \
947  { \
948  this->m_##name = _arg; \
949  this->Modified(); \
950  } \
951  }
952 
953 
958 #define itkGetEnumMacro(name, type) \
959  virtual type Get##name() const { return this->m_##name; }
960 
961 
965 #define itkSetStringMacro(name) \
966  virtual void Set##name(const char * _arg) \
967  { \
968  if (_arg && (_arg == this->m_##name)) \
969  { \
970  return; \
971  } \
972  if (_arg) \
973  { \
974  this->m_##name = _arg; \
975  } \
976  else \
977  { \
978  this->m_##name = ""; \
979  } \
980  this->Modified(); \
981  } \
982  virtual void Set##name(const std::string & _arg) { this->Set##name(_arg.c_str()); }
983 
984 
985 
989 #define itkGetStringMacro(name) \
990  virtual const char * Get##name() const { return this->m_##name.c_str(); }
991 
995 #define itkSetClampMacro(name, type, min, max) \
996  virtual void Set##name(type _arg) \
997  { \
998  const type temp_extrema = (_arg < min ? min : (_arg > max ? max : _arg)); \
999  itkDebugMacro("setting " << #name " to " << _arg); \
1000  CLANG_PRAGMA_PUSH \
1001  CLANG_SUPPRESS_Wfloat_equal if (this->m_##name != temp_extrema) \
1002  { \
1003  this->m_##name = temp_extrema; \
1004  this->Modified(); \
1005  } \
1006  CLANG_PRAGMA_POP \
1007  }
1008 
1009 
1010 // NOTE: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
1015 #define itkSetObjectMacro(name, type) \
1016  virtual void Set##name(type * _arg) \
1017  { \
1018  itkDebugMacro("setting " << #name " to " << _arg); \
1019  CLANG_PRAGMA_PUSH \
1020  CLANG_SUPPRESS_Wfloat_equal if (this->m_##name != _arg) \
1021  { \
1022  this->m_##name = _arg; \
1023  this->Modified(); \
1024  } \
1025  CLANG_PRAGMA_POP \
1026  }
1027 
1028 
1037 // NOTE: A class can use either itkGetModifiableObjectMacro
1038 // or itkGetObjectMacro, but not both.
1039 // A class can use either itkGetModifiableObjectMacro
1040 // or itkGetConstObjectMacro, but not both.
1041 // If the desired behavior is to only provide const
1042 // access to the itkObject ivar, then use itkGetConstObjectMacro,
1043 // else use itkGetModifiableObjectMacro for read/write access to
1044 // the ivar.
1045 // It is permissible to use both itkGetObjectMacro and itkGetConstObjectMacro
1046 // for backwards compatibility.
1047 // If the ITK_LEGACY_REMOVE=FALSE, then it is
1048 // permissible to use itkGetObjectMacro which
1049 // defines both signatures itk::GetXXX() and
1050 // itk::GetModifiableXXX()
1051 
1054 #define itkGetConstObjectMacro(name, type) \
1055  virtual const type * Get##name() const { return this->m_##name.GetPointer(); }
1056 
1057 
1058 #if defined(ITK_FUTURE_LEGACY_REMOVE)
1059 // In the future, the itkGetObjectMacro will be deprecated with the ITK_LEGACY_REMOVE
1060 // flag. For now, this very advanced feature is only available
1061 // through manual setting of a compiler define -DITK_FUTURE_LEGACY_REMOVE
1062 // ("/DITK_FUTURE_LEGACY_REMOVE /EHsc" with Visual Studio)
1063 // to ease the transition from the historical GetObjectMacro to the GetModifiableObjectMacro
1064 # define itkGetObjectMacro(name, type) \
1065  virtual type * Get##name() \
1066  { \
1067  purposeful_error("itkGetObjectMacro should be replaced with itkGetModifiableObjectMacro."); \
1068  }
1069 
1070 # define itkGetModifiableObjectMacro(name, type) \
1071  virtual type * GetModifiable##name() { return this->m_##name.GetPointer(); } \
1072  itkGetConstObjectMacro(name, type)
1073 
1074 #else // defined ( ITK_FUTURE_LEGACY_REMOVE )
1075 
1077 # define itkGetObjectMacro(name, type) \
1078  virtual type * Get##name() { return this->m_##name.GetPointer(); }
1079 # define itkGetModifiableObjectMacro(name, type) \
1080  virtual type * GetModifiable##name() { return this->m_##name.GetPointer(); } \
1081  itkGetConstObjectMacro(name, type) itkGetObjectMacro(name, type)
1082 #endif // defined ( ITK_FUTURE_LEGACY_REMOVE )
1083 
1084 
1085 // For backwards compatibility define ITK_EXPORT to nothing
1086 #define ITK_EXPORT
1087 
1088 
1091 #define itkGetConstReferenceObjectMacro(name, type) \
1092  virtual const typename type::Pointer & Get##name() const { return this->m_##name; }
1093 
1098 #define itkSetConstObjectMacro(name, type) \
1099  virtual void Set##name(const type * _arg) \
1100  { \
1101  itkDebugMacro("setting " << #name " to " << _arg); \
1102  if (this->m_##name != _arg) \
1103  { \
1104  this->m_##name = _arg; \
1105  this->Modified(); \
1106  } \
1107  }
1108 
1109 
1112 #define itkBooleanMacro(name) \
1113  virtual void name##On() { this->Set##name(true); } \
1114  virtual void name##Off() { this->Set##name(false); }
1115 
1116 
1120 #define itkSetVectorMacro(name, type, count) \
1121  virtual void Set##name(type data[]) \
1122  { \
1123  unsigned int i; \
1124  for (i = 0; i < count; i++) \
1125  { \
1126  CLANG_PRAGMA_PUSH \
1127  CLANG_SUPPRESS_Wfloat_equal if (data[i] != this->m_##name[i]) CLANG_PRAGMA_POP { break; } \
1128  } \
1129  if (i < count) \
1130  { \
1131  this->Modified(); \
1132  for (i = 0; i < count; i++) \
1133  { \
1134  this->m_##name[i] = data[i]; \
1135  } \
1136  } \
1137  }
1138 
1139 
1142 #define itkGetVectorMacro(name, type, count) \
1143  virtual type * Get##name() const { return this->m_##name; }
1144 
1149 #define itkGPUKernelClassMacro(kernel) \
1150 
1151 
1156 #define itkGPUKernelClassMacro(kernel) \
1157  * Workaround KWstyle bug \ \
1158  * \ingroup ITKCommon \ \
1159  */ \
1160  class kernel \
1161  { \
1162  public: \
1163  static const char * \
1164  GetOpenCLSource(); \
1165  \
1166  private: \
1167  kernel(); \
1168  virtual ~kernel(); \
1169  kernel(const kernel &); \
1170  void \
1171  operator=(const kernel &); \
1172  };
1173 
1174 
1175 #define itkGetOpenCLSourceFromKernelMacro(kernel) \
1176  static const char * GetOpenCLSource() { return kernel::GetOpenCLSource(); }
1177 
1178 // A useful macro in the PrintSelf method for printing member variables
1179 // which are pointers to object based on the LightObject class.
1180 #define itkPrintSelfObjectMacro(name) \
1181  if (static_cast<const LightObject *>(this->m_##name) == nullptr) \
1182  { \
1183  os << indent << #name << ": (null)" << std::endl; \
1184  } \
1185  else \
1186  { \
1187  os << indent << #name << ": " << std::endl; \
1188  this->m_##name->Print(os, indent.GetNextIndent()); \
1189  }
1190 
1191 
1193 #define itkSetDecoratedOutputMacro(name, type) \
1194  virtual void Set##name##Output(const SimpleDataObjectDecorator<type> * _arg) \
1195  { \
1196  itkDebugMacro("setting output " #name " to " << _arg); \
1197  if (_arg != itkDynamicCastInDebugMode<SimpleDataObjectDecorator<type> *>(this->ProcessObject::GetOutput(#name))) \
1198  { \
1199  this->ProcessObject::SetOutput(#name, const_cast<SimpleDataObjectDecorator<type> *>(_arg)); \
1200  this->Modified(); \
1201  } \
1202  } \
1203  virtual void Set##name(const type & _arg) \
1204  { \
1205  using DecoratorType = SimpleDataObjectDecorator<type>; \
1206  itkDebugMacro("setting output " #name " to " << _arg); \
1207  DecoratorType * output = itkDynamicCastInDebugMode<DecoratorType *>(this->ProcessObject::GetOutput(#name)); \
1208  if (output) \
1209  { \
1210  if (output->Get() == _arg) \
1211  { \
1212  return; \
1213  } \
1214  else \
1215  { \
1216  output->Set(_arg); \
1217  } \
1218  } \
1219  else \
1220  { \
1221  typename DecoratorType::Pointer newOutput = DecoratorType::New(); \
1222  newOutput->Set(_arg); \
1223  this->Set##name##Output(newOutput); \
1224  } \
1225  }
1226 
1227 
1229 #define itkGetDecoratedOutputMacro(name, type) \
1230  virtual const SimpleDataObjectDecorator<type> * Get##name##Output() const \
1231  { \
1232  itkDebugMacro("returning output " << #name " of " << this->ProcessObject::GetOutput(#name)); \
1233  return itkDynamicCastInDebugMode<const SimpleDataObjectDecorator<type> *>(this->ProcessObject::GetOutput(#name)); \
1234  } \
1235  virtual const type & Get##name() const \
1236  { \
1237  itkDebugMacro("Getting output " #name); \
1238  using DecoratorType = SimpleDataObjectDecorator<type>; \
1239  const DecoratorType * output = \
1240  itkDynamicCastInDebugMode<const DecoratorType *>(this->ProcessObject::GetOutput(#name)); \
1241  if (output == nullptr) \
1242  { \
1243  itkExceptionMacro(<< "output" #name " is not set"); \
1244  } \
1245  return output->Get(); \
1246  }
1247 
1248 
1249 
1259 #if defined(ITKV4_COMPATIBILITY)
1260 # define ITK_ITERATOR_VIRTUAL virtual
1261 # define ITK_ITERATOR_OVERRIDE override
1262 # define ITK_ITERATOR_FINAL
1263 #elif !defined(ITK_LEGACY_REMOVE)
1264 # define ITK_ITERATOR_VIRTUAL virtual
1265 # define ITK_ITERATOR_OVERRIDE override
1266 # define ITK_ITERATOR_FINAL final
1267 #else
1268 # define ITK_ITERATOR_VIRTUAL
1269 # define ITK_ITERATOR_OVERRIDE
1270 # define ITK_ITERATOR_FINAL
1271 #endif
1272 
1273 #if defined(ITKV4_COMPATIBILITY)
1274 // A macro for methods which are const in ITKv5, but not in ITKv4
1275 # define ITKv5_CONST
1276 #else
1277 // A macro for methods which are const in ITKv5, but not in ITKv4
1278 # define ITKv5_CONST const
1279 #endif
1280 
1281 #define itkExceptionObject_h
1282 #include "itkExceptionObject.h"
1283 #undef itkExceptionObject_h
1284 
1292 template <typename TTarget, typename TSource>
1293 TTarget
1294 itkDynamicCastInDebugMode(TSource x)
1295 {
1296 #ifndef NDEBUG
1297  if (x == nullptr)
1298  {
1299  return nullptr;
1300  }
1301  TTarget rval = dynamic_cast<TTarget>(x);
1302  if (rval == nullptr)
1303  {
1304  itkGenericExceptionMacro(<< "Failed dynamic cast to " << typeid(TTarget).name()
1305  << " object type = " << x->GetNameOfClass());
1306  }
1307  return rval;
1308 #else
1309  return static_cast<TTarget>(x);
1310 #endif
1311 }
1313 
1314 #endif // end of itkMacro.h
itkExceptionObject.h
itkWin32Header.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26