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