ITK  4.10.0
Insight Segmentation and Registration Toolkit
itkMacro.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
38 #ifndef itkMacro_h
39 #define itkMacro_h
40 
41 #include "itkWin32Header.h"
42 #include "itkConfigure.h"
43 #include "ITKCommonExport.h"
44 
45 #include <typeinfo>
46 
47 #include <string>
48 #include <cstdlib>
49 #ifndef NDEBUG
50 #include <cassert>
51 #endif
52 
53 #include <sstream>
54 
59 namespace itk
60 {
61 // end namespace itk - this is here for documentation purposes
62 }
63 
66 #define itkNotUsed(x)
67 
68 // Define ITK_PRAGMA macro.
69 //
70 // It sets "#pragma" preprocessor directives without expecting the arguments
71 // to be quoted.
72 #define ITK_PRAGMA(x) _Pragma (#x)
73 
74 // The clang compiler has many useful non-default compiler warnings
75 // that tend to have a high false positive rate.
76 // The following set of defines allows us to suppress false positives
77 // and still track down suspicious code
78 #if defined(__clang__) && defined(__has_warning)
79 #define CLANG_PRAGMA_PUSH ITK_PRAGMA(clang diagnostic push)
80 #define CLANG_PRAGMA_POP ITK_PRAGMA(clang diagnostic pop)
81 # if __has_warning("-Wfloat-equal")
82 #define CLANG_SUPPRESS_Wfloat_equal ITK_PRAGMA( clang diagnostic ignored "-Wfloat-equal" )
83 # endif
84 #else
85 #define CLANG_PRAGMA_PUSH
86 #define CLANG_PRAGMA_POP
87 #define CLANG_SUPPRESS_Wfloat_equal
88 #endif
89 
90 // Define ITK_GCC_PRAGMA_DIAG(param1 [param2 [...]]) macro.
91 //
92 // This macros sets a pragma diagnostic if it supported by the version
93 // of GCC being used otherwise it is a no-op.
94 //
95 // GCC diagnostics pragma supported only with GCC >= 4.2
96 #if defined( __GNUC__ ) && !defined( __INTEL_COMPILER )
97 # if ( __GNUC__ > 4 ) || (( __GNUC__ >= 4 ) && ( __GNUC_MINOR__ >= 2 ))
98 # define ITK_GCC_PRAGMA_DIAG(x) ITK_PRAGMA(GCC diagnostic x)
99 # else
100 # define ITK_GCC_PRAGMA_DIAG(x)
101 # endif
102 #else
103 # define ITK_GCC_PRAGMA_DIAG(x)
104 #endif
105 
106 // Define ITK_GCC_PRAGMA_DIAG_(PUSH|POP) macros.
107 //
108 // These macros respectively push and pop the diagnostic context
109 // if it is supported by the version of GCC being used
110 // otherwise it is a no-op.
111 //
112 // GCC push/pop diagnostics pragma are supported only with GCC >= 4.6
113 //
114 // Define macro ITK_HAS_GCC_PRAGMA_DIAG_PUSHPOP if it is supported.
115 #if defined( __GNUC__ ) && !defined( __INTEL_COMPILER )
116 # if ( __GNUC__ > 4 ) || (( __GNUC__ >= 4 ) && ( __GNUC_MINOR__ >= 6 ))
117 # define ITK_GCC_PRAGMA_DIAG_PUSH() ITK_GCC_PRAGMA_DIAG(push)
118 # define ITK_GCC_PRAGMA_DIAG_POP() ITK_GCC_PRAGMA_DIAG(pop)
119 # define ITK_HAS_GCC_PRAGMA_DIAG_PUSHPOP
120 # else
121 # define ITK_GCC_PRAGMA_DIAG_PUSH()
122 # define ITK_GCC_PRAGMA_DIAG_POP()
123 # endif
124 #else
125 # define ITK_GCC_PRAGMA_DIAG_PUSH()
126 # define ITK_GCC_PRAGMA_DIAG_POP()
127 #endif
128 
129 /*
130  * ITK only supports MSVC++ 7.1 and greater
131  * MSVC++ 11.0 _MSC_VER = 1700
132  * MSVC++ 10.0 _MSC_VER = 1600
133  * MSVC++ 9.0 _MSC_VER = 1500
134  * MSVC++ 8.0 _MSC_VER = 1400
135  * MSVC++ 7.1 _MSC_VER = 1310
136  * MSVC++ 7.0 _MSC_VER = 1300
137  * MSVC++ 6.0 _MSC_VER = 1200
138  * MSVC++ 5.0 _MSC_VER = 1100
139 */
140 #if defined( _MSC_VER ) && ( _MSC_VER < 1310 )
141 //#error "_MSC_VER < 1310 (MSVC++ 7.1) not supported under ITKv4"
142 #endif
143 #if defined( __SUNPRO_CC ) && ( __SUNPRO_CC < 0x590 )
144 #error "__SUNPRO_CC < 0x590 not supported under ITKv4"
145 #endif
146 #if defined( __CYGWIN__ )
147 #error "The Cygwin compiler is not supported in ITKv4 and above"
148 #endif
149 #if defined( __BORLANDC__ )
150 #error "The Borland C compiler is not supported in ITKv4 and above"
151 #endif
152 #if defined( __MWERKS__ )
153 #error "The MetroWerks compiler is not supported in ITKv4 and above"
154 #endif
155 #if defined( __GNUC__ ) && ( __GNUC__ < 3 )
156 #error "The __GNUC__ version 2.95 compiler is not supprted under ITKv4 and above"
157 #if defined( __sgi )
158 //This is true for IRIX 6.5.18m with MIPSPro 7.3.1.3m.
159 //TODO: At some future point, it may be necessary to
160 //define a minimum __sgi version that will work.
161 #error "The __sgi compiler is not supprted under ITKv4 and above"
162 #endif
163 #endif
164 
165 // Setup symbol exports
166 #if defined( _WIN32 ) || defined ( WIN32 )
167  #define ITK_ABI_IMPORT __declspec(dllimport)
168  #define ITK_ABI_EXPORT __declspec(dllexport)
169  #define ITK_ABI_HIDDEN
170 #else
171  #if __GNUC__ >= 4
172  #define ITK_ABI_IMPORT __attribute__ ((visibility ("default")))
173  #define ITK_ABI_EXPORT __attribute__ ((visibility ("default")))
174  #define ITK_ABI_HIDDEN __attribute__ ((visibility ("hidden")))
175  #else
176  #define ITK_ABI_IMPORT
177  #define ITK_ABI_EXPORT
178  #define ITK_ABI_HIDDEN
179  #endif
180 #endif
181 
182 #if ITK_COMPILED_CXX_VERSION >= 201103L
183 // In c++11 the override keyword allows you to explicity define that a function
184 // is intended to override the base-class version. This makes the code more
185 // managable and fixes a set of common hard-to-find bugs.
186 #define ITK_OVERRIDE override
187 // In functions that should not be implemented, use the C++11 mechanism
188 // to ensure that thye are purposely not implemented
189 #define ITK_DELETE_FUNCTION =delete
190 // In c++11 there is an explicit nullptr type that introduces a new keyword to
191 // serve as a distinguished null pointer constant: nullptr. It is of type
192 // nullptr_t, which is implicitly convertible and comparable to any pointer type
193 // or pointer-to-member type. It is not implicitly convertible or comparable to
194 // integral types, except for bool.
195 #define ITK_NULLPTR nullptr
196 // In C++11 the throw-list specification has been deprecated,
197 // replaces with the noexcept specifier. Using this function
198 // specification adds the run-time check that the method does not
199 // throw, if it does throw then std::terminate will be called.
200 // Use cautiously.
201 #define ITK_NOEXCEPT noexcept
202 #define ITK_HAS_CXX11_STATIC_ASSERT
203 #define ITK_HAS_CXX11_RVREF
204 #define ITK_CONSTEXPR constexpr
205 #define ITK_CONSTEXPR_FUNC constexpr
206 #else
207 #define ITK_OVERRIDE
208 #define ITK_DELETE_FUNCTION
209 #define ITK_NULLPTR NULL
210 #define ITK_NOEXCEPT throw()
211 #define ITK_CONSTEXPR const
212 #define ITK_CONSTEXPR_FUNC inline
213 #endif
214 
215 // Use "ITK_FALLTHROUGH;" to annotate deliberate fall-through in switches,
216 // use it analogously to "break;". The trailing semi-colon is required.
217 #if ITK_COMPILED_CXX_VERSION >= 201103L && defined(__has_warning)
218 # if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
219 # define ITK_FALLTHROUGH [[clang::fallthrough]]
220 # endif
221 #endif
222 
223 #ifndef ITK_FALLTHROUGH
224 # define ITK_FALLTHROUGH ((void)0)
225 #endif
226 
247 #define itkNewMacro(x) \
248  itkSimpleNewMacro(x) \
249  itkCreateAnotherMacro(x) \
250  itkCloneMacro(x)
251 
252 #define itkSimpleNewMacro(x) \
253  static Pointer New(void) \
254  { \
255  Pointer smartPtr = ::itk::ObjectFactory< x >::Create(); \
256  if ( smartPtr.GetPointer() == ITK_NULLPTR ) \
257  { \
258  smartPtr = new x; \
259  } \
260  smartPtr->UnRegister(); \
261  return smartPtr; \
262  }
263 
264 #define itkCreateAnotherMacro(x) \
265  virtual ::itk::LightObject::Pointer CreateAnother(void) const ITK_OVERRIDE \
266  { \
267  ::itk::LightObject::Pointer smartPtr; \
268  smartPtr = x::New().GetPointer(); \
269  return smartPtr; \
270  }
271 
272 #define itkCloneMacro(x) \
273  Pointer Clone() const \
274  { \
275  Pointer rval = \
276  dynamic_cast<x *>(this->InternalClone().GetPointer()); \
277  return rval; \
278  }
279 
296 #define itkFactorylessNewMacro(x) \
297  static Pointer New(void) \
298  { \
299  Pointer smartPtr; \
300  x * rawPtr = new x; \
301  smartPtr = rawPtr; \
302  rawPtr->UnRegister(); \
303  return smartPtr; \
304  } \
305  virtual ::itk::LightObject::Pointer CreateAnother(void) const ITK_OVERRIDE \
306  { \
307  ::itk::LightObject::Pointer smartPtr; \
308  smartPtr = x::New().GetPointer(); \
309  return smartPtr; \
310  }
311 
312 
315 #define itkTypeMacro(thisClass, superclass) \
316  virtual const char *GetNameOfClass() const ITK_OVERRIDE \
317  { \
318  return #thisClass; \
319  }
320 
321 #define itkTypeMacroNoParent(thisClass) \
322  virtual const char *GetNameOfClass() const \
323  { \
324  return #thisClass; \
325  }
326 
327 namespace itk
328 {
335 extern ITKCommon_EXPORT void OutputWindowDisplayText(const char *);
337 
338 extern ITKCommon_EXPORT void OutputWindowDisplayErrorText(const char *);
339 
340 extern ITKCommon_EXPORT void OutputWindowDisplayWarningText(const char *);
341 
342 extern ITKCommon_EXPORT void OutputWindowDisplayGenericOutputText(const char *);
343 
344 extern ITKCommon_EXPORT void OutputWindowDisplayDebugText(const char *);
345 } // end namespace itk
346 
350 #if defined( NDEBUG )
351 #define itkDebugMacro(x)
352 #define itkDebugStatement(x)
353 #else
354 #define itkDebugMacro(x) \
355  { \
356  if ( this->GetDebug() && ::itk::Object::GetGlobalWarningDisplay() ) \
357  { \
358  std::ostringstream itkmsg; \
359  itkmsg << "Debug: In " __FILE__ ", line " << __LINE__ << "\n" \
360  << this->GetNameOfClass() << " (" << this << "): " x \
361  << "\n\n"; \
362  ::itk::OutputWindowDisplayDebugText( itkmsg.str().c_str() ); \
363  } \
364  }
365 
366 
367 //The itkDebugStatement is to be used to protect code that is only
368 //used in the itkDebugMacro
369 #define itkDebugStatement(x) x
370 #endif
371 
375 #define itkWarningMacro(x) \
376  { \
377  if ( ::itk::Object::GetGlobalWarningDisplay() ) \
378  { \
379  std::ostringstream itkmsg; \
380  itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
381  << this->GetNameOfClass() << " (" << this << "): " x \
382  << "\n\n"; \
383  ::itk::OutputWindowDisplayWarningText( itkmsg.str().c_str() ); \
384  } \
385  }
386 
387 
388 //The itkDebugStatement is to be used ot protect code that is only
389 //used in the itkDebugMacro
390 #define itkWarningStatement(x) x
391 
392 #if defined( ITK_CPP_FUNCTION )
393  #if defined( _WIN32 ) && !defined( __MINGW32__ ) && !defined( ITK_WRAPPING_PARSER )
394  #define ITK_LOCATION __FUNCSIG__
395  #elif defined( __GNUC__ )
396  #define ITK_LOCATION __PRETTY_FUNCTION__
397  #else
398  #define ITK_LOCATION __FUNCTION__
399  #endif
400 #else
401  #define ITK_LOCATION "unknown"
402 #endif
403 
404 #include "itkExceptionObject.h"
405 
409 #define itkExceptionMacro(x) \
410  { \
411  std::ostringstream message; \
412  message << "itk::ERROR: " << this->GetNameOfClass() \
413  << "(" << this << "): " x; \
414  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(), ITK_LOCATION); \
415  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
416  }
417 
418 
419 #define itkGenericExceptionMacro(x) \
420  { \
421  std::ostringstream message; \
422  message << "itk::ERROR: " x; \
423  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(), ITK_LOCATION); \
424  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
425  }
426 
427 #define itkDeclareExceptionMacro(newexcp,parentexcp,whatmessage) \
428 namespace itk { \
429 class newexcp : public parentexcp \
430 { \
431 public: \
432 newexcp( const char *file, unsigned int lineNumber ) : \
433 parentexcp( file, lineNumber ) \
434 { \
435  this->SetDescription( whatmessage ); \
436 } \
437 newexcp( const std::string & file, unsigned int lineNumber ) : \
438 parentexcp( file, lineNumber ) \
439 { \
440  this->SetDescription( whatmessage ); \
441 } \
442 itkTypeMacro(newexcp, parentexcp); \
443 }; \
444 }
445 
446 #define itkSpecializedExceptionMacro(exceptiontype) \
447  { \
448  ::itk::exceptiontype e_(__FILE__, __LINE__); \
449  e_.SetLocation(ITK_LOCATION); \
450  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
451  }
452 
453 #define itkSpecializedMessageExceptionMacro(exceptiontype,x) \
454  { \
455  ::itk::exceptiontype e_(__FILE__, __LINE__); \
456  std::ostringstream message; \
457  message << "itk::ERROR: " x; \
458  e_.SetDescription(message.str().c_str()); \
459  e_.SetLocation(ITK_LOCATION); \
460  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
461  }
462 
463 
464 #define itkGenericOutputMacro(x) \
465  { \
466  if ( ::itk::Object::GetGlobalWarningDisplay() ) \
467  { \
468  std::ostringstream itkmsg; \
469  itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
470  x << "\n\n"; \
471  ::itk::OutputWindowDisplayGenericOutputText( itkmsg.str().c_str() ); \
472  } \
473  }
474 
475 //----------------------------------------------------------------------------
476 // Macros for simplifying the use of logging
477 //
478 #define itkLogMacro(x, y) \
479  { \
480  if ( this->GetLogger() ) \
481  { \
482  this->GetLogger()->Write(::itk::LoggerBase::x, y); \
483  } \
484  }
485 
486 #define itkLogMacroStatic(obj, x, y) \
487  { \
488  if ( obj->GetLogger() ) \
489  { \
490  obj->GetLogger()->Write(::itk::LoggerBase::x, y); \
491  } \
492  }
493 
494 //----------------------------------------------------------------------------
495 // Setup legacy code policy.
496 //
497 // CMake options ITK_LEGACY_REMOVE and ITK_LEGACY_SILENT are converted
498 // They may be used to completely remove legacy code or silence the
499 // warnings. The default is to warn about their use.
500 //
501 // Source files that test the legacy code may define ITK_LEGACY_TEST
502 // like this:
503 //
504 // #define ITK_LEGACY_TEST
505 // #include "itkClassWithDeprecatedMethod.h"
506 //
507 // in order to silence the warnings for calling deprecated methods.
508 // No other source files in ITK should call the methods since they are
509 // provided only for compatibility with older user code.
510 
511 // Define itkLegacyMacro to mark legacy methods where they are
512 // declared in their class. Example usage:
513 //
514 // // \deprecated Replaced by MyOtherMethod() as of ITK 2.0.
515 // itkLegacyMacro(void MyMethod());
516 //
517 // See below for what to do for the method definition.
518 #if defined( ITK_LEGACY_REMOVE )
519 #define itkLegacyMacro(method) /* no ';' */
520 #elif defined( ITK_LEGACY_SILENT ) || defined( ITK_LEGACY_TEST ) || defined( ITK_WRAPPING_PARSER )
521 // Provide legacy methods with no warnings.
522 #define itkLegacyMacro(method) method
523 #else
524 // Setup compile-time warnings for uses of deprecated methods if
525 // possible on this compiler.
526 #if defined( __GNUC__ ) && !defined( __INTEL_COMPILER ) && ( __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 1 ) )
527 #define itkLegacyMacro(method) method __attribute__( ( deprecated ) )
528 #elif defined( _MSC_VER )
529 #define itkLegacyMacro(method) __declspec(deprecated) method
530 #else
531 #define itkLegacyMacro(method) method
532 #endif
533 #endif
534 
535 // Macros to create runtime deprecation warning messages in function
536 // bodies. Example usage:
537 //
538 // #if !defined( ITK_LEGACY_REMOVE )
539 // void itkMyClass::MyOldMethod()
540 // {
541 // itkLegacyBodyMacro(itkMyClass::MyOldMethod, 2.0);
542 // }
543 //
544 // void itkMyClass::MyMethod()
545 // {
546 // itkLegacyReplaceBodyMacro(itkMyClass::MyMethod, 2.0,
547 // itkMyClass::MyOtherMethod);
548 // }
549 // #endif
550 #if defined( ITK_LEGACY_REMOVE ) || defined( ITK_LEGACY_SILENT )
551 #define itkLegacyBodyMacro(method, version)
552 #define itkLegacyReplaceBodyMacro(method, version, replace)
553 #define itkGenericLegacyBodyMacro(method, version)
554 #define itkGenericLegacyReplaceBodyMacro(method, version, replace)
555 #else
556 #define itkLegacyBodyMacro(method, version) \
557  itkWarningMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
558 #define itkLegacyReplaceBodyMacro(method, version, replace) \
559  itkWarningMacro( \
560  #method " was deprecated for ITK " #version " and will be removed in a future version. Use " #replace \
561  " instead.")
562 #define itkGenericLegacyBodyMacro(method, version) \
563  itkGenericOutputMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
564 #define itkGenericLegacyReplaceBodyMacro(method, version, replace) \
565  itkGenericOutputMacro( \
566  #method " was deprecated for ITK " #version " and will be removed in a future version. Use " #replace \
567  " instead.")
568 #endif
569 
570 #if defined ( ITK_LEGACY_REMOVE )
571 #define ITK_TEMPLATE_TXX "error ITK_TEMPLATE_TXX is no longer a supported identifier, you should replace with ITK_MANUAL_INSTANTIATION as a replacement"
572 #else
573 #define ITK_TEMPLATE_TXX 1
574 #endif
575 
576 
577 // Most modern x86 CPUs have 64 byte aligned blocks which are used for
578 // the cache lines. By aligning multi-threaded structures with the
579 // cache lines, false shared can be reduced, and performance
580 // increased.
581 #define ITK_CACHE_LINE_ALIGNMENT 64
582 
583 //
584 // itkPadStruct will add padding to a structure to ensure a minimum size
585 // for ensuring that adjacent structures do not share CACHE lines.
586 // Each struct will take up some multiple of cacheline sizes.
587 // This is particularly useful for arrays of thread private variables.
588 //
589 #define itkPadStruct( mincachesize, oldtype, newtype ) \
590  struct newtype: public oldtype \
591  { \
592  char _StructPadding[mincachesize - (sizeof(oldtype)%mincachesize) ]; \
593  };
594 
595 //
596 // itkAlignedTypedef is a macro which creates a new typedef to make a
597 // data structure aligned.
598 //
599 #if defined( ITK_HAS_GNU_ATTRIBUTE_ALIGNED )
600 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
601  typedef oldtype newtype __attribute__((aligned(alignment)))
602 #elif defined ( _MSC_VER )
603 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
604  typedef __declspec(align( alignment )) oldtype newtype
605 #else
606 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
607  typedef oldtype newtype
608 #endif
609 
610 //=============================================================================
611 /* Define a common way of declaring a templated function as a friend inside a class.
612  - ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENTS(T)
613 
614  The following templated function
615 
616  template <T>
617  T add(const T & a, const T & b);
618 
619  is declared as friend in some compilers as:
620 
621  class A
622  {
623  public:
624  friend Self add<Self>( const Self & a, const Self & b );
625  }
626 
627  while other compilers will do
628 
629  class A
630  {
631  public:
632  friend Self add<>( const Self & a, const Self & b );
633  }
634 
635  This characteristic of the compiler is checked by a TRY_COMPILE
636  command defined in Insight/CMake/itkTestFriendTemplatedFunction.cxx
637 
638 */
639 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_NULL_STRING )
640 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T)
641 #else
642 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_EMPTY_BRACKETS )
643 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T) < >
644 #else
645 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_TEMPLATE_ARGUMENTS )
646 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T) < T >
647 #endif
648 #endif
649 #endif
650 // THIS IS A TEMPORARY PATCH FOR Visual Studio 10. The correct solution must
651 // be implemented in Insight/CMake/itkTestFriendTemplatedFunction.cxx
652 #if ( defined ( _MSC_VER ) && ( _MSC_VER >= 1600 ) )
653 #ifdef ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT
654 #undef ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT
655 #endif
656 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T)
657 #endif
658 
659 //--------------------------------------------------------------------------------
660 // Helper macros for Template Meta-Programming techniques of for-loops
661 // unrolling
662 //--------------------------------------------------------------------------------
663 
664 //--------------------------------------------------------------------------------
665 // Macro that generates an unrolled for loop for assigning elements of one array
666 // to elements of another array The array are assumed to be of same length
667 // (dimension), and this is also assumed to be the value of NumberOfIterations.
668 // No verification of size is performed. Casting is perfomed as part of the
669 // assignment, by using the DestinationElementType as the casting type.
670 // Source and destination array types must have defined opearator[] in their
671 // API.
672 #define itkForLoopAssignmentMacro(DestinationType, \
673  SourceType, \
674  DestinationElementType, \
675  DestinationArray, \
676  SourceArray, \
677  NumberOfIterations) \
678  for ( unsigned int i = 0; i < NumberOfIterations; ++i ) \
679  { \
680  DestinationArray[i] = static_cast< DestinationElementType >( SourceArray[i] ); \
681  }
682 
683 //--------------------------------------------------------------------------------
684 // Macro that generates an unrolled for loop for rounding and assigning
685 // elements of one array to elements of another array The array are assumed to
686 // be of same length (dimension), and this is also assumed to be the value of
687 // NumberOfIterations. No verification of size is performed. Casting is
688 // perfomed as part of the assignment, by using the DestinationElementType as
689 // the casting type.
690 // Source and destination array types must have defined opearator[] in their
691 // API.
692 #define itkForLoopRoundingAndAssignmentMacro(DestinationType, \
693  Sourcrnd_halfintup, \
694  DestinationElementType, \
695  DestinationArray, \
696  SourceArray, \
697  NumberOfIterations) \
698  for ( unsigned int i = 0; i < NumberOfIterations; ++i ) \
699  { \
700  DestinationArray[i] = itk::Math::Round< DestinationElementType >(SourceArray[i]); \
701  }
702 
703 // end of Template Meta Programming helper macros
704 
705 #ifndef NDEBUG
706 
707 #ifdef _POSIX_SOURCE
708 #define itkAssertInDebugOrThrowInReleaseMacro(msg) __assert_fail (msg, __FILE__, __LINE__, __ASSERT_FUNCTION);
709 #else
710 #define itkAssertInDebugOrThrowInReleaseMacro(msg) itkGenericExceptionMacro(<< msg);
711 #endif
712 
713 #else
714 #define itkAssertInDebugOrThrowInReleaseMacro(msg) itkGenericExceptionMacro(<< msg);
715 #endif
716 
717 #define itkAssertOrThrowMacro(test, message) \
718  if ( !( test ) ) \
719  { \
720  std::ostringstream msgstr; \
721  msgstr << message; \
722  itkAssertInDebugOrThrowInReleaseMacro( msgstr.str().c_str() ); \
723  }
724 
725 #ifndef NDEBUG
726 #define itkAssertInDebugAndIgnoreInReleaseMacro(X) assert(X)
727 #else
728 #define itkAssertInDebugAndIgnoreInReleaseMacro(X)
729 #endif
730 
731 #ifdef ITKV3_COMPATIBILITY
732 // As of MSVS++ 7.1 and greater, typename is supported in templates
733 // All ITKv4 compilers support the typename keyword, but this is
734 // needed to ease transition from ITKv3.
735 #define ITK_TYPENAME typename
736 #endif
737 
741 template <typename TTarget, typename TSource>
742 TTarget itkDynamicCastInDebugMode(TSource x)
743 {
744 #ifndef NDEBUG
745  if(x == 0)
746  {
747  return 0;
748  }
749  TTarget rval = dynamic_cast<TTarget>(x);
750  if(rval == 0)
751  {
752  itkGenericExceptionMacro(<< "Failed dynamic cast to "
753  << typeid(TTarget).name()
754  << " object type = "
755  << x->GetNameOfClass());
756  }
757  return rval;
758 #else
759  return static_cast<TTarget>(x);
760 #endif
761 }
763 
764 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
765 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
766 // !! The ITK Get/Set Macros for various types !!
767 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
768 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
783 #if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__ ) < 405 && !defined( __clang__ ) && !defined( __INTEL_COMPILER )
784 # define itkStaticConstMacro(name,type,value) enum { name = value }
785 #else
786 # define itkStaticConstMacro(name,type,value) static ITK_CONSTEXPR type name = value
787 #endif
788 
789 #define itkGetStaticConstMacro(name) (Self::name)
790 
792 #define itkSetInputMacro(name, type) \
793  virtual void Set##name(const type *_arg) \
794  { \
795  itkDebugMacro("setting input " #name " to " << _arg); \
796  if ( _arg != itkDynamicCastInDebugMode< type * >( this->ProcessObject::GetInput(#name) ) ) \
797  { \
798  this->ProcessObject::SetInput( #name, const_cast< type * >( _arg ) ); \
799  this->Modified(); \
800  } \
801  }
802 
803 
805 #define itkGetInputMacro(name, type) \
806  virtual const type * Get##name() const \
807  { \
808  itkDebugMacro( "returning input " << #name " of " << this->ProcessObject::GetInput(#name) ); \
809  return itkDynamicCastInDebugMode< const type * >( this->ProcessObject::GetInput(#name) ); \
810  }
811 
812 
814 #define itkSetDecoratedInputMacro(name, type) \
815  virtual void Set##name##Input(const SimpleDataObjectDecorator< type > *_arg) \
816  { \
817  itkDebugMacro("setting input " #name " to " << _arg); \
818  if ( _arg != itkDynamicCastInDebugMode< SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ) \
819  { \
820  this->ProcessObject::SetInput( #name, const_cast< SimpleDataObjectDecorator< type > * >( _arg ) ); \
821  this->Modified(); \
822  } \
823  } \
824  virtual void Set##name(const SimpleDataObjectDecorator< type > *_arg) \
825  { \
826  this->Set##name##Input(_arg); \
827  } \
828  virtual void Set##name(const type &_arg) \
829  { \
830  typedef SimpleDataObjectDecorator< type > DecoratorType; \
831  itkDebugMacro("setting input " #name " to " << _arg); \
832  const DecoratorType *oldInput = \
833  itkDynamicCastInDebugMode< const DecoratorType * >( \
834  this->ProcessObject::GetInput(#name) ); \
835 CLANG_PRAGMA_PUSH \
836 CLANG_SUPPRESS_Wfloat_equal \
837  if ( oldInput && oldInput->Get() == _arg ) \
838 CLANG_PRAGMA_POP \
839  { \
840  return; \
841  } \
842  typename DecoratorType::Pointer newInput = DecoratorType::New(); \
843  newInput->Set(_arg); \
844  this->Set##name##Input(newInput); \
845  }
846 
847 
849 #define itkGetDecoratedInputMacro(name, type) \
850  virtual const SimpleDataObjectDecorator< type > * Get##name##Input() const \
851  { \
852  itkDebugMacro( "returning input " << #name " of " << this->ProcessObject::GetInput(#name) ); \
853  return itkDynamicCastInDebugMode< const SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ); \
854  } \
855  virtual const type & Get##name() const \
856  { \
857  itkDebugMacro("Getting input " #name); \
858  typedef SimpleDataObjectDecorator< type > DecoratorType; \
859  const DecoratorType *input = \
860  itkDynamicCastInDebugMode< const DecoratorType * >( \
861  this->ProcessObject::GetInput(#name) ); \
862  if( input == ITK_NULLPTR ) \
863  { \
864  itkExceptionMacro(<<"input" #name " is not set"); \
865  } \
866  return input->Get(); \
867  }
868 
869 
872 #define itkSetGetDecoratedInputMacro(name, type) \
873  itkSetDecoratedInputMacro(name, type) \
874  itkGetDecoratedInputMacro(name, type)
875 
880 #define itkSetDecoratedObjectInputMacro(name, type) \
881  virtual void Set##name##Input(const DataObjectDecorator< type > *_arg) \
882  { \
883  itkDebugMacro("setting input " #name " to " << _arg); \
884  if ( _arg != itkDynamicCastInDebugMode< DataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ) \
885  { \
886  this->ProcessObject::SetInput( #name, const_cast< DataObjectDecorator< type > * >( _arg ) ); \
887  this->Modified(); \
888  } \
889  } \
890  virtual void Set##name(const type * _arg) \
891  { \
892  typedef DataObjectDecorator< type > DecoratorType; \
893  itkDebugMacro("setting input " #name " to " << _arg); \
894  const DecoratorType *oldInput = \
895  itkDynamicCastInDebugMode< const DecoratorType * >( \
896  this->ProcessObject::GetInput(#name) ); \
897  if ( oldInput && oldInput->Get() == _arg ) \
898  { \
899  return; \
900  } \
901  typename DecoratorType::Pointer newInput = DecoratorType::New(); \
902  newInput->Set(_arg); \
903  this->Set##name##Input(newInput); \
904  }
905 
906 
911 #define itkGetDecoratedObjectInputMacro(name, type) \
912  virtual const DataObjectDecorator< type > * Get##name##Input() const \
913  { \
914  itkDebugMacro( "returning input " << #name " of "<< this->ProcessObject::GetInput(#name) ); \
915  return itkDynamicCastInDebugMode< const DataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ); \
916  } \
917  virtual const type * Get##name() const \
918  { \
919  itkDebugMacro("Getting input " #name); \
920  typedef DataObjectDecorator< type > DecoratorType; \
921  const DecoratorType *input = \
922  itkDynamicCastInDebugMode< const DecoratorType * >( \
923  this->ProcessObject::GetInput(#name) ); \
924  if( input == ITK_NULLPTR ) \
925  { \
926  return ITK_NULLPTR; \
927  } \
928  return input->Get(); \
929  }
930 
931 
934 #define itkSetGetDecoratedObjectInputMacro(name, type) \
935  itkSetDecoratedObjectInputMacro(name, type) \
936  itkGetDecoratedObjectInputMacro(name, type)
937 
939 #define itkSetMacro(name, type) \
940  virtual void Set##name (const type _arg) \
941  { \
942  itkDebugMacro("setting " #name " to " << _arg); \
943 CLANG_PRAGMA_PUSH \
944 CLANG_SUPPRESS_Wfloat_equal \
945  if ( this->m_##name != _arg ) \
946  { \
947  this->m_##name = _arg; \
948  this->Modified(); \
949  } \
950 CLANG_PRAGMA_POP \
951  }
952 
953 
955 #define itkGetMacro(name, type) \
956  virtual type Get##name () \
957  { \
958  return this->m_##name; \
959  }
960 
961 
965 #define itkGetConstMacro(name, type) \
966  virtual type Get##name () const \
967  { \
968  return this->m_##name; \
969  }
970 
971 
976 #define itkGetConstReferenceMacro(name, type) \
977  virtual const type &Get##name () const \
978  { \
979  return this->m_##name; \
980  }
981 
982 
987 #define itkSetEnumMacro(name, type) \
988  virtual void Set##name (const type _arg) \
989  { \
990  itkDebugMacro( "setting " #name " to " << static_cast< long >( _arg ) ); \
991  if ( this->m_##name != _arg ) \
992  { \
993  this->m_##name = _arg; \
994  this->Modified(); \
995  } \
996  }
997 
998 
1003 #define itkGetEnumMacro(name, type) \
1004  virtual type Get##name () const \
1005  { \
1006  return this->m_##name; \
1007  }
1008 
1009 
1013 #define itkSetStringMacro(name) \
1014  virtual void Set##name (const char *_arg) \
1015  { \
1016  if ( _arg && ( _arg == this->m_##name ) ) { return; } \
1017  if ( _arg ) \
1018  { \
1019  this->m_##name = _arg; \
1020  } \
1021  else \
1022  { \
1023  this->m_##name = ""; \
1024  } \
1025  this->Modified(); \
1026  } \
1027  virtual void Set##name (const std::string & _arg) \
1028  { \
1029  this->Set##name( _arg.c_str() ); \
1030  } \
1031 
1032 
1033 
1037 #define itkGetStringMacro(name) \
1038  virtual const char *Get##name () const \
1039  { \
1040  return this->m_##name.c_str(); \
1041  }
1042 
1046 #define itkSetClampMacro(name, type, min, max) \
1047  virtual void Set##name (type _arg) \
1048  { \
1049  const type temp_extrema=( _arg < min ? min : ( _arg > max ? max : _arg ) );\
1050  itkDebugMacro("setting " << #name " to " << _arg); \
1051 CLANG_PRAGMA_PUSH \
1052 CLANG_SUPPRESS_Wfloat_equal \
1053  if ( this->m_##name != temp_extrema ) \
1054  { \
1055  this->m_##name = temp_extrema; \
1056  this->Modified(); \
1057  } \
1058 CLANG_PRAGMA_POP \
1059  }
1060 
1061 
1062 //NOTE: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
1067 #define itkSetObjectMacro(name, type) \
1068  virtual void Set##name (type * _arg) \
1069  { \
1070  itkDebugMacro("setting " << #name " to " << _arg); \
1071 CLANG_PRAGMA_PUSH \
1072 CLANG_SUPPRESS_Wfloat_equal \
1073  if ( this->m_##name != _arg ) \
1074  { \
1075  this->m_##name = _arg; \
1076  this->Modified(); \
1077  } \
1078 CLANG_PRAGMA_POP \
1079  }
1080 
1081 
1090 // NOTE: A class can use either itkGetModifiableObjectMacro
1091 // or itkGetObjectMacro, but not both.
1092 // A class can use either itkGetModifiableObjectMacro
1093 // or itkGetConstObjectMacro, but not both.
1094 // If the desired behavior is to only provide const
1095 // access to the itkObject ivar, then use itkGetConstObjectMacro,
1096 // else use itkGetModifiableObjectMacro for read/write access to
1097 // the ivar.
1098 // It is permissable to use both itkGetObjectMacro and itkGetConstObjectMacro
1099 // for backwards compatibility.
1100 // If the ITK_LEGACY_REMOVE=FALSE, then it is
1101 // permissable to use itkGetObjectMacro which
1102 // defines both signatures itk::GetXXX() and
1103 // itk::GetModifiableXXX()
1104 
1107 #define itkGetConstObjectMacro(name, type) \
1108  virtual const type * Get##name () const \
1109  { \
1110  return this->m_##name.GetPointer(); \
1111  }
1112 
1113 
1114 #if defined ( ITK_FUTURE_LEGACY_REMOVE )
1115 // In the future, the itkGetObjectMacro will be deprecated with the ITK_LEGACY_REMOVE
1116 // flag. For now, this very advanced feature is only available
1117 // through manual setting of a compiler define -DITK_FUTURE_LEGACY_REMOVE
1118 // ("/DITK_FUTURE_LEGACY_REMOVE /EHsc" with Visual Studio)
1119 // to ease the transition from the historical GetObjectMacro to the GetModifiableObjectMacro
1120 # define itkGetObjectMacro(name, type) \
1121  virtual type * Get##name () \
1122  { \
1123  purposeful_error("itkGetObjectMacro should be replaced with itkGetModifiableObjectMacro."); \
1124  }
1125 
1126 # define itkGetModifiableObjectMacro(name, type) \
1127  virtual type * GetModifiable##name () \
1128  { \
1129  return this->m_##name.GetPointer(); \
1130  } \
1131  itkGetConstObjectMacro(name, type)
1132 
1133 #else // defined ( ITK_FUTURE_LEGACY_REMOVE )
1134 
1136 # define itkGetObjectMacro(name, type) \
1137  virtual type * Get##name () \
1138  { \
1139  return this->m_##name.GetPointer(); \
1140  }
1141 # define itkGetModifiableObjectMacro(name, type) \
1142  virtual type * GetModifiable##name () \
1143  { \
1144  return this->m_##name.GetPointer(); \
1145  } \
1146  itkGetConstObjectMacro(name, type) \
1147  itkGetObjectMacro(name, type)
1148 #endif // defined ( ITK_FUTURE_LEGACY_REMOVE )
1149 
1150 
1151 // For backwards compatibility define ITK_EXPORT to nothing
1152 #define ITK_EXPORT
1153 
1154 
1157 #define itkGetConstReferenceObjectMacro(name, type) \
1158  virtual const typename type::Pointer & Get##name () const \
1159  { \
1160  return this->m_##name; \
1161  }
1162 
1167 #define itkSetConstObjectMacro(name, type) \
1168  virtual void Set##name (const type * _arg) \
1169  { \
1170  itkDebugMacro("setting " << #name " to " << _arg); \
1171  if ( this->m_##name != _arg ) \
1172  { \
1173  this->m_##name = _arg; \
1174  this->Modified(); \
1175  } \
1176  }
1177 
1178 
1181 #define itkBooleanMacro(name) \
1182  virtual void name##On () \
1183  { \
1184  this->Set##name(true); \
1185  } \
1186  virtual void name##Off () \
1187  { \
1188  this->Set##name(false); \
1189  }
1190 
1191 
1195 #define itkSetVectorMacro(name, type, count) \
1196  virtual void Set##name(type data[]) \
1197  { \
1198  unsigned int i; \
1199  for ( i = 0; i < count; i++ ) \
1200  { \
1201 CLANG_PRAGMA_PUSH \
1202 CLANG_SUPPRESS_Wfloat_equal \
1203  if ( data[i] != this->m_##name[i] ) \
1204 CLANG_PRAGMA_POP \
1205  { \
1206  break; \
1207  } \
1208  } \
1209  if ( i < count ) \
1210  { \
1211  this->Modified(); \
1212  for ( i = 0; i < count; i++ ) \
1213  { \
1214  this->m_##name[i] = data[i]; \
1215  } \
1216  } \
1217  }
1218 
1219 
1222 #define itkGetVectorMacro(name, type, count) \
1223  virtual type * Get##name () const \
1224  { \
1225  return this->m_##name; \
1226  }
1227 
1232 #define itkGPUKernelClassMacro(kernel) \
1233 
1234 
1239 
1254 
1276 
1311 
1313 
1333 
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes...
Definition: itkArray.h:30