ITK  4.4.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 
44 #include <typeinfo>
45 
46 #include <string>
47 #include <cstdlib>
48 #ifndef NDEBUG
49 #include <cassert>
50 #endif
51 
52 #include <sstream>
53 
58 namespace itk
59 {
60 // end namespace itk - this is here for documentation purposes
61 }
62 
65 #define itkNotUsed(x)
66 
67 /*
68  * ITK only supports MSVC++ 7.1 and greater
69  * MSVC++ 11.0 _MSC_VER = 1700
70  * MSVC++ 10.0 _MSC_VER = 1600
71  * MSVC++ 9.0 _MSC_VER = 1500
72  * MSVC++ 8.0 _MSC_VER = 1400
73  * MSVC++ 7.1 _MSC_VER = 1310
74  * MSVC++ 7.0 _MSC_VER = 1300
75  * MSVC++ 6.0 _MSC_VER = 1200
76  * MSVC++ 5.0 _MSC_VER = 1100
77 */
78 #if defined( _MSC_VER ) && ( _MSC_VER < 1310 )
79 //#error "_MSC_VER < 1310 (MSVC++ 7.1) not supported under ITKv4"
80 #endif
81 #if defined( __SUNPRO_CC ) && ( __SUNPRO_CC < 0x590 )
82 #error "__SUNPRO_CC < 0x590 not supported under ITKv4"
83 #endif
84 #if defined( __CYGWIN__ )
85 #error "The Cygwin compiler is not supported in ITKv4 and above"
86 #endif
87 #if defined( __BORLANDC__ )
88 #error "The Borland C compiler is not supported in ITKv4 and above"
89 #endif
90 #if defined( __MWERKS__ )
91 #error "The MetroWerks compiler is not supported in ITKv4 and above"
92 #endif
93 #if defined( __GNUC__ ) && ( __GNUC__ < 3 )
94 #error "The __GNUC__ version 2.95 compiler is not supprted under ITKv4 and above"
95 #if defined( __sgi )
96 //This is true for IRIX 6.5.18m with MIPSPro 7.3.1.3m.
97 //TODO: At some future point, it may be necessary to
98 //define a minimum __sgi version that will work.
99 #error "The __sgi compiler is not supprted under ITKv4 and above"
100 #endif
101 #endif
102 
103 // Setup symbol exports
104 //
105 // When a class definition has ITK_EXPORT, the class will be
106 // checked automatically, by Utilities/Dart/PrintSelfCheck.tcl
107 #define ITK_EXPORT
108 
109 #if defined( _WIN32 ) || defined ( WIN32 )
110  #define ITK_ABI_IMPORT __declspec(dllimport)
111  #define ITK_ABI_EXPORT __declspec(dllexport)
112  #define ITK_ABI_HIDDEN
113 #else
114  #if __GNUC__ >= 4
115  #define ITK_ABI_IMPORT __attribute__ ((visibility ("default")))
116  #define ITK_ABI_EXPORT __attribute__ ((visibility ("default")))
117  #define ITK_ABI_HIDDEN __attribute__ ((visibility ("hidden")))
118  #else
119  #define ITK_ABI_IMPORT
120  #define ITK_ABI_EXPORT
121  #define ITK_ABI_HIDDEN
122  #endif
123 #endif
124 
125 #define ITKCommon_HIDDEN ITK_ABI_HIDDEN
126 
127 #if !defined( ITKSTATIC )
128  #ifdef ITKCommon_EXPORTS
129  #define ITKCommon_EXPORT ITK_ABI_EXPORT
130  #else
131  #define ITKCommon_EXPORT ITK_ABI_IMPORT
132  #endif /* ITKCommon_EXPORTS */
133 #else
134  /* ITKCommon is build as a static lib */
135  #if __GNUC__ >= 4
136  // Don't hide symbols in the static ITKCommon library in case
137  // -fvisibility=hidden is used
138  #define ITKCommon_EXPORT ITK_ABI_EXPORT
139  #else
140  #define ITKCommon_EXPORT
141  #endif
142 #endif
143 
164 #define itkNewMacro(x) \
165  itkSimpleNewMacro(x) \
166  itkCreateAnotherMacro(x) \
167  itkCloneMacro(x)
168 
169 #define itkSimpleNewMacro(x) \
170  static Pointer New(void) \
171  { \
172  Pointer smartPtr = ::itk::ObjectFactory< x >::Create(); \
173  if ( smartPtr.GetPointer() == NULL ) \
174  { \
175  smartPtr = new x; \
176  } \
177  smartPtr->UnRegister(); \
178  return smartPtr; \
179  }
180 
181 #define itkCreateAnotherMacro(x) \
182  virtual::itk::LightObject::Pointer CreateAnother(void) const \
183  { \
184  ::itk::LightObject::Pointer smartPtr; \
185  smartPtr = x::New().GetPointer(); \
186  return smartPtr; \
187  }
188 
189 #define itkCloneMacro(x) \
190  Pointer Clone() const \
191  { \
192  Pointer rval = \
193  dynamic_cast<x *>(this->InternalClone().GetPointer()); \
194  return rval; \
195  }
196 
213 #define itkFactorylessNewMacro(x) \
214  static Pointer New(void) \
215  { \
216  Pointer smartPtr; \
217  x * rawPtr = new x; \
218  smartPtr = rawPtr; \
219  rawPtr->UnRegister(); \
220  return smartPtr; \
221  } \
222  virtual::itk::LightObject::Pointer CreateAnother(void) const \
223  { \
224  ::itk::LightObject::Pointer smartPtr; \
225  smartPtr = x::New().GetPointer(); \
226  return smartPtr; \
227  }
228 
229 
232 #define itkTypeMacro(thisClass, superclass) \
233  virtual const char *GetNameOfClass() const \
234  { \
235  return #thisClass; \
236  }
237 
238 namespace itk
239 {
246 extern ITKCommon_EXPORT void OutputWindowDisplayText(const char *);
248 
249 extern ITKCommon_EXPORT void OutputWindowDisplayErrorText(const char *);
250 
251 extern ITKCommon_EXPORT void OutputWindowDisplayWarningText(const char *);
252 
253 extern ITKCommon_EXPORT void OutputWindowDisplayGenericOutputText(const char *);
254 
255 extern ITKCommon_EXPORT void OutputWindowDisplayDebugText(const char *);
256 } // end namespace itk
257 
261 #if defined( NDEBUG )
262 #define itkDebugMacro(x)
263 #define itkDebugStatement(x)
264 #else
265 #define itkDebugMacro(x) \
266  { \
267  if ( this->GetDebug() && ::itk::Object::GetGlobalWarningDisplay() ) \
268  { \
269  std::ostringstream itkmsg; \
270  itkmsg << "Debug: In " __FILE__ ", line " << __LINE__ << "\n" \
271  << this->GetNameOfClass() << " (" << this << "): " x \
272  << "\n\n"; \
273  ::itk::OutputWindowDisplayDebugText( itkmsg.str().c_str() ); \
274  } \
275  }
276 
277 
278 //The itkDebugStatement is to be used to protect code that is only
279 //used in the itkDebugMacro
280 #define itkDebugStatement(x) x
281 #endif
282 
286 #define itkWarningMacro(x) \
287  { \
288  if ( ::itk::Object::GetGlobalWarningDisplay() ) \
289  { \
290  std::ostringstream itkmsg; \
291  itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
292  << this->GetNameOfClass() << " (" << this << "): " x \
293  << "\n\n"; \
294  ::itk::OutputWindowDisplayWarningText( itkmsg.str().c_str() ); \
295  } \
296  }
297 
298 
299 //The itkDebugStatement is to be used ot protect code that is only
300 //used in the itkDebugMacro
301 #define itkWarningStatement(x) x
302 
303 #if defined( ITK_CPP_FUNCTION )
304  #if defined( _WIN32 ) && !defined( __MINGW32__ ) && !defined( CABLE_CONFIGURATION ) \
305  && !defined( CSWIG )
306  #define ITK_LOCATION __FUNCSIG__
307  #elif defined( __GNUC__ )
308  #define ITK_LOCATION __PRETTY_FUNCTION__
309  #else
310  #define ITK_LOCATION __FUNCTION__
311  #endif
312 #else
313  #define ITK_LOCATION "unknown"
314 #endif
315 
316 #include "itkExceptionObject.h"
317 
321 #define itkExceptionMacro(x) \
322  { \
323  std::ostringstream message; \
324  message << "itk::ERROR: " << this->GetNameOfClass() \
325  << "(" << this << "): " x; \
326  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(), ITK_LOCATION); \
327  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
328  }
329 
330 
331 #define itkGenericExceptionMacro(x) \
332  { \
333  std::ostringstream message; \
334  message << "itk::ERROR: " x; \
335  ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(), ITK_LOCATION); \
336  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
337  }
338 
339 #define itkDeclareExceptionMacro(newexcp,parentexcp,whatmessage) \
340 namespace itk { \
341 class ITK_EXPORT newexcp : public parentexcp \
342 { \
343 public: \
344 newexcp( const char *file, unsigned int lineNumber ) : \
345 parentexcp( file, lineNumber ) \
346 { \
347  this->SetDescription( whatmessage ); \
348 } \
349 newexcp( const std::string & file, unsigned int lineNumber ) : \
350 parentexcp( file, lineNumber ) \
351 { \
352  this->SetDescription( whatmessage ); \
353 } \
354 itkTypeMacro(newexcp, parentexcp); \
355 }; \
356 }
357 
358 #define itkSpecializedExceptionMacro(exceptiontype) \
359  { \
360  ::itk::exceptiontype e_(__FILE__, __LINE__); \
361  e_.SetLocation(ITK_LOCATION); \
362  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
363  }
364 
365 #define itkSpecializedMessageExceptionMacro(exceptiontype,x) \
366  { \
367  ::itk::exceptiontype e_(__FILE__, __LINE__); \
368  std::ostringstream message; \
369  message << "itk::ERROR: " x; \
370  e_.SetDescription(message.str().c_str()); \
371  e_.SetLocation(ITK_LOCATION); \
372  throw e_; /* Explicit naming to work around Intel compiler bug. */ \
373  }
374 
375 
376 #define itkGenericOutputMacro(x) \
377  { \
378  if ( ::itk::Object::GetGlobalWarningDisplay() ) \
379  { \
380  std::ostringstream itkmsg; \
381  itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
382  x << "\n\n"; \
383  ::itk::OutputWindowDisplayGenericOutputText( itkmsg.str().c_str() ); \
384  } \
385  }
386 
387 //----------------------------------------------------------------------------
388 // Macros for simplifying the use of logging
389 //
390 #define itkLogMacro(x, y) \
391  { \
392  if ( this->GetLogger() ) \
393  { \
394  this->GetLogger()->Write(::itk::LoggerBase::x, y); \
395  } \
396  }
397 
398 #define itkLogMacroStatic(obj, x, y) \
399  { \
400  if ( obj->GetLogger() ) \
401  { \
402  obj->GetLogger()->Write(::itk::LoggerBase::x, y); \
403  } \
404  }
405 
406 //----------------------------------------------------------------------------
407 // Setup legacy code policy.
408 //
409 // CMake options ITK_LEGACY_REMOVE and ITK_LEGACY_SILENT are converted
410 // They may be used to completely remove legacy code or silence the
411 // warnings. The default is to warn about their use.
412 //
413 // Source files that test the legacy code may define ITK_LEGACY_TEST
414 // like this:
415 //
416 // #define ITK_LEGACY_TEST
417 // #include "itkClassWithDeprecatedMethod.h"
418 //
419 // in order to silence the warnings for calling deprecated methods.
420 // No other source files in ITK should call the methods since they are
421 // provided only for compatibility with older user code.
422 
423 // Define itkLegacyMacro to mark legacy methods where they are
424 // declared in their class. Example usage:
425 //
426 // // @deprecated Replaced by MyOtherMethod() as of ITK 2.0.
427 // itkLegacyMacro(void MyMethod());
428 #if defined( ITK_LEGACY_REMOVE )
429 #define itkLegacyMacro(method) /* no ';' */
430 #elif defined( ITK_LEGACY_SILENT ) || defined( ITK_LEGACY_TEST ) || defined( CSWIG )
431 // Provide legacy methods with no warnings.
432 #define itkLegacyMacro(method) method
433 #else
434 // Setup compile-time warnings for uses of deprecated methods if
435 // possible on this compiler.
436 #if defined( __GNUC__ ) && !defined( __INTEL_COMPILER ) && ( __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 1 ) )
437 #define itkLegacyMacro(method) method __attribute__( ( deprecated ) )
438 #elif defined( _MSC_VER )
439 #define itkLegacyMacro(method) __declspec(deprecated) method
440 #else
441 #define itkLegacyMacro(method) method
442 #endif
443 #endif
444 
445 // Macros to create runtime deprecation warning messages in function
446 // bodies. Example usage:
447 //
448 // void itkMyClass::MyOldMethod()
449 // {
450 // itkLegacyBodyMacro(itkMyClass::MyOldMethod, 2.0);
451 // }
452 //
453 // void itkMyClass::MyMethod()
454 // {
455 // itkLegacyReplaceBodyMacro(itkMyClass::MyMethod, 2.0,
456 // itkMyClass::MyOtherMethod);
457 // }
458 #if defined( ITK_LEGACY_REMOVE ) || defined( ITK_LEGACY_SILENT )
459 #define itkLegacyBodyMacro(method, version)
460 #define itkLegacyReplaceBodyMacro(method, version, replace)
461 #define itkGenericLegacyBodyMacro(method, version)
462 #define itkGenericLegacyReplaceBodyMacro(method, version, replace)
463 #else
464 #define itkLegacyBodyMacro(method, version) \
465  itkWarningMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
466 #define itkLegacyReplaceBodyMacro(method, version, replace) \
467  itkWarningMacro( \
468  #method " was deprecated for ITK " #version " and will be removed in a future version. Use " #replace \
469  " instead.")
470 #define itkGenericLegacyBodyMacro(method, version) \
471  itkGenericOutputMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
472 #define itkGenericLegacyReplaceBodyMacro(method, version, replace) \
473  itkGenericOutputMacro( \
474  #method " was deprecated for ITK " #version " and will be removed in a future version. Use " #replace \
475  " instead.")
476 #endif
477 
478 #if defined ( ITK_LEGACY_REMOVE )
479 #define ITK_TEMPLATE_TXX "error ITK_TEMPLATE_TXX is no longer a supported identifier, you should replace with ITK_MANUAL_INSTANTIATION as a replacement"
480 #else
481 #define ITK_TEMPLATE_TXX 1
482 #endif
483 
484 
485 // Most modern x86 CPUs have 64 byte aligned blocks which are used for
486 // the cache lines. By aligning multi-threaded structures with the
487 // cache lines, false shared can be reduced, and performance
488 // increased.
489 #define ITK_CACHE_LINE_ALIGNMENT 64
490 
491 //
492 // itkAlignedTypedef is a macro which creates a new typedef to make a
493 // data structure aligned.
494 //
495 #if defined ( ITK_HAS_CPP11_ALIGNAS )
496 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
497  typedef oldtype newtype alignas(alignment)
498 #elif defined( ITK_HAS_GNU_ATTRIBUTE_ALIGNED )
499 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
500  typedef oldtype newtype __attribute__((aligned(alignment)))
501 #elif defined ( _MSC_VER )
502 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
503  typedef __declspec(align( alignment )) struct oldtype newtype
504 #else
505 # define itkAlignedTypedef( alignment, oldtype, newtype ) \
506  typedef oldtype newtype
507 #endif
508 
509 //=============================================================================
510 /* Define a common way of declaring a templated function as a friend inside a class.
511  - ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENTS(T)
512 
513  The following templated function
514 
515  template <T>
516  T add(const T & a, const T & b);
517 
518  is declared as friend in some compilers as:
519 
520  class A
521  {
522  public:
523  friend Self add<Self>( const Self & a, const Self & b );
524  }
525 
526  while other compilers will do
527 
528  class A
529  {
530  public:
531  friend Self add<>( const Self & a, const Self & b );
532  }
533 
534  This characteristic of the compiler is checked by a TRY_COMPILE
535  command defined in Insight/CMake/itkTestFriendTemplatedFunction.cxx
536 
537 */
538 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_NULL_STRING )
539 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T)
540 #else
541 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_EMPTY_BRACKETS )
542 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T) < >
543 #else
544 #if defined( ITK_SUPPORTS_TEMPLATED_FRIEND_FUNCTION_WITH_TEMPLATE_ARGUMENTS )
545 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T) < T >
546 #endif
547 #endif
548 #endif
549 // THIS IS A TEMPORARY PATCH FOR Visual Studio 10. The correct solution must
550 // be implemented in Insight/CMake/itkTestFriendTemplatedFunction.cxx
551 #if ( defined ( _MSC_VER ) && ( _MSC_VER >= 1600 ) )
552 #ifdef ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT
553 #undef ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT
554 #endif
555 #define ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(T)
556 #endif
557 
558 //=============================================================================
559 /* Choose a way to prevent template instantiation on this platform.
560  - ITK_TEMPLATE_DO_NOT_INSTANTIATE = use #pragma do_not_instantiate to
561  prevent instantiation
562  - ITK_TEMPLATE_EXTERN = use extern template to prevent instantiation
563 */
564 #if defined( __INTEL_COMPILER ) && __INTEL_COMPILER >= 700
565 #define ITK_TEMPLATE_EXTERN 1
566 #elif defined( __GNUC__ ) && __GNUC__ >= 3
567 #define ITK_TEMPLATE_EXTERN 1
568 #elif defined( _MSC_VER )
569 #define ITK_TEMPLATE_EXTERN 1
570 #endif
571 #if !defined( ITK_TEMPLATE_DO_NOT_INSTANTIATE )
572 #define ITK_TEMPLATE_DO_NOT_INSTANTIATE 0
573 #endif
574 #if !defined( ITK_TEMPLATE_EXTERN )
575 #define ITK_TEMPLATE_EXTERN 0
576 #endif
577 
578 /* Define a macro to explicitly instantiate a template.
579  - ITK_TEMPLATE_EXPORT(X) =
580  Explicitly instantiate X, where X is of the form N(a1[,a2...,aN]).
581  examples: ITK_TEMPLATE_EXPORT(1(class Foo<int>))
582  ITK_TEMPLATE_EXPORT(2(class Bar<int, char>))
583  Use one level of expansion delay to allow user code to have
584  a macro determining the number of arguments. */
585 #define ITK_TEMPLATE_EXPORT(x) ITK_TEMPLATE_EXPORT_DELAY(x)
586 #define ITK_TEMPLATE_EXPORT_DELAY(x) template ITK_TEMPLATE_##x;
587 
588 /* Define a macro to prevent template instantiations.
589  - ITK_TEMPLATE_IMPORT(X) =
590  Prevent instantiation of X, where X is of the form N(a1[,a2...,aN]).
591  examples: ITK_TEMPLATE_IMPORT(1(class Foo<int>))
592  ITK_TEMPLATE_IMPORT(2(class Bar<int, char>))
593  Use one level of expansion delay to allow user code to have
594  a macro determining the number of arguments.
595 */
596 #if ITK_TEMPLATE_EXTERN
597 #define ITK_TEMPLATE_IMPORT_DELAY(x) extern template ITK_TEMPLATE_##x;
598 #elif ITK_TEMPLATE_DO_NOT_INSTANTIATE
599 #define ITK_TEMPLATE_IMPORT_DELAY(x) \
600  ITK_TEMPLATE_IMPORT_IMPL(do_not_instantiate ITK_TEMPLATE_##x)
601 #define ITK_TEMPLATE_IMPORT_IMPL(x) _Pragma(#x)
602 #endif
603 #if defined( ITK_TEMPLATE_IMPORT_DELAY )
604 #define ITK_TEMPLATE_IMPORT(x) ITK_TEMPLATE_IMPORT_DELAY(x)
605 #define ITK_TEMPLATE_IMPORT_WORKS 1
606 #else
607 #define ITK_TEMPLATE_IMPORT(x)
608 #define ITK_TEMPLATE_IMPORT_WORKS 0
609 #endif
610 
611 //=============================================================================
612 
613 /* Define macros to export and import template instantiations for each
614  library in ITK. */
615 #define ITK_EXPORT_ITKCommon(c, x, n) \
616  ITK_EXPORT_TEMPLATE(ITKCommon_EXPORT, c, x, n)
617 #define ITK_IMPORT_ITKCommon(c, x, n) \
618  ITK_IMPORT_TEMPLATE(ITKCommon_EXPORT, c, x, n)
619 
620 //--------------------------------------------------------------------------------
621 // Helper macros for Template Meta-Programming techniques of for-loops
622 // unrolling
623 //--------------------------------------------------------------------------------
624 
625 //--------------------------------------------------------------------------------
626 // Macro that generates an unrolled for loop for assigning elements of one array
627 // to elements of another array The array are assumed to be of same length
628 // (dimension), and this is also assumed to be the value of NumberOfIterations.
629 // No verification of size is performed. Casting is perfomed as part of the
630 // assignment, by using the DestinationElementType as the casting type.
631 // Source and destination array types must have defined opearator[] in their
632 // API.
633 #define itkForLoopAssignmentMacro(DestinationType, \
634  SourceType, \
635  DestinationElementType, \
636  DestinationArray, \
637  SourceArray, \
638  NumberOfIterations) \
639  for ( unsigned int i = 0; i < NumberOfIterations; ++i ) \
640  { \
641  DestinationArray[i] = static_cast< DestinationElementType >( SourceArray[i] ); \
642  }
643 
644 //--------------------------------------------------------------------------------
645 // Macro that generates an unrolled for loop for rounding and assigning
646 // elements of one array to elements of another array The array are assumed to
647 // be of same length (dimension), and this is also assumed to be the value of
648 // NumberOfIterations. No verification of size is performed. Casting is
649 // perfomed as part of the assignment, by using the DestinationElementType as
650 // the casting type.
651 // Source and destination array types must have defined opearator[] in their
652 // API.
653 #define itkForLoopRoundingAndAssignmentMacro(DestinationType, \
654  Sourcrnd_halfintup, \
655  DestinationElementType, \
656  DestinationArray, \
657  SourceArray, \
658  NumberOfIterations) \
659  for ( unsigned int i = 0; i < NumberOfIterations; ++i ) \
660  { \
661  DestinationArray[i] = itk::Math::Round< DestinationElementType >(SourceArray[i]); \
662  }
663 
664 // end of Template Meta Programming helper macros
665 
666 #ifndef NDEBUG
667 
668 #ifdef _POSIX_SOURCE
669 #define itkAssertInDebugOrThrowInReleaseMacro(msg) __assert_fail (msg, __FILE__, __LINE__, __ASSERT_FUNCTION);
670 #else
671 #define itkAssertInDebugOrThrowInReleaseMacro(msg) itkGenericExceptionMacro(<< msg);
672 #endif
673 
674 #else
675 #define itkAssertInDebugOrThrowInReleaseMacro(msg) itkGenericExceptionMacro(<< msg);
676 #endif
677 
678 #define itkAssertOrThrowMacro(test, message) \
679  if ( !( test ) ) \
680  { \
681  std::ostringstream msgstr; \
682  msgstr << message; \
683  itkAssertInDebugOrThrowInReleaseMacro( msgstr.str().c_str() ); \
684  }
685 
686 #ifndef NDEBUG
687 #define itkAssertInDebugAndIgnoreInReleaseMacro(X) assert(X)
688 #else
689 #define itkAssertInDebugAndIgnoreInReleaseMacro(X)
690 #endif
691 
692 #ifdef ITKV3_COMPATIBILITY
693 // As of MSVS++ 7.1 and greater, typename is supported in templates
694 // All ITKv4 compilers support the typename keyword, but this is
695 // needed to ease transition from ITKv3.
696 #define ITK_TYPENAME typename
697 #endif
698 
702 template <typename TTarget, typename TSource>
703 TTarget itkDynamicCastInDebugMode(TSource x)
704 {
705 #ifndef NDEBUG
706  if(x == 0)
707  {
708  return 0;
709  }
710  TTarget rval = dynamic_cast<TTarget>(x);
711  if(rval == 0)
712  {
713  itkGenericExceptionMacro(<< "Failed dynamic cast to "
714  << typeid(TTarget).name()
715  << " object type = "
716  << x->GetNameOfClass());
717  }
718  return rval;
719 #else
720  return static_cast<TTarget>(x);
721 #endif
722 
723 
724 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
725 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
726 // !! The ITK Get/Set Macros for various types !!
727 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
728 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
729 //This is probably better, but requires a lot of extra work
730 //for gettting ExplicitInstantiation to work properly. \#define
731 // itkStaticConstMacro(name, type, value) static const type name = value
732 #define itkStaticConstMacro(name, type, value) enum { name = value }
733 
734 #define itkGetStaticConstMacro(name) (Self::name)
735 
737 #define itkSetInputMacro(name, type) \
738  virtual void Set##name(const type *_arg) \
739  { \
740  itkDebugMacro("setting input " #name " to " << _arg); \
741  if ( _arg != static_cast< type * >( this->ProcessObject::GetInput(#name) ) ) \
742  { \
743  this->ProcessObject::SetInput( #name, const_cast< type * >( _arg ) ); \
744  this->Modified(); \
745  } \
746  }
747 
748 
750 #define itkGetInputMacro(name, type) \
751  virtual const type * Get##name() const \
752  { \
753  itkDebugMacro( "returning input " << #name " of " \
754  << static_cast< const type * >( this->ProcessObject::GetInput(#name) ) ); \
755  return static_cast< const type * >( this->ProcessObject::GetInput(#name) ); \
756  }
757 
758 
760 #define itkSetDecoratedInputMacro(name, type) \
761  virtual void Set##name##Input(const SimpleDataObjectDecorator< type > *_arg) \
762  { \
763  itkDebugMacro("setting input " #name " to " << _arg); \
764  if ( _arg != static_cast< SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ) \
765  { \
766  this->ProcessObject::SetInput( #name, const_cast< SimpleDataObjectDecorator< type > * >( _arg ) ); \
767  this->Modified(); \
768  } \
769  } \
770  virtual void Set##name(const type &_arg) \
771  { \
772  typedef SimpleDataObjectDecorator< type > DecoratorType; \
773  itkDebugMacro("setting input " #name " to " << _arg); \
774  const DecoratorType *oldInput = \
775  static_cast< const DecoratorType * >( \
776  this->ProcessObject::GetInput(#name) ); \
777  if ( oldInput && oldInput->Get() == _arg ) \
778  { \
779  return; \
780  } \
781  typename DecoratorType::Pointer newInput = DecoratorType::New(); \
782  newInput->Set(_arg); \
783  this->Set##name##Input(newInput); \
784  }
785 
786 
788 #define itkGetDecoratedInputMacro(name, type) \
789  virtual const SimpleDataObjectDecorator< type > * Get##name##Input() const \
790  { \
791  itkDebugMacro( "returning input " << #name " of " \
792  << static_cast< const SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ); \
793  return static_cast< const SimpleDataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ); \
794  } \
795  virtual const type & Get##name() const \
796  { \
797  itkDebugMacro("Getting input " #name); \
798  typedef SimpleDataObjectDecorator< type > DecoratorType; \
799  const DecoratorType *input = \
800  static_cast< const DecoratorType * >( \
801  this->ProcessObject::GetInput(#name) ); \
802  if( input == NULL ) \
803  { \
804  itkExceptionMacro(<<"input" #name " is not set"); \
805  } \
806  return input->Get(); \
807  }
808 
809 
812 #define itkSetGetDecoratedInputMacro(name, type) \
813  itkSetDecoratedInputMacro(name, type) \
814  itkGetDecoratedInputMacro(name, type)
815 
820 #define itkSetDecoratedObjectInputMacro(name, type) \
821  virtual void Set##name##Input(const DataObjectDecorator< type > *_arg) \
822  { \
823  itkDebugMacro("setting input " #name " to " << _arg); \
824  if ( _arg != static_cast< DataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ) \
825  { \
826  this->ProcessObject::SetInput( #name, const_cast< DataObjectDecorator< type > * >( _arg ) ); \
827  this->Modified(); \
828  } \
829  } \
830  virtual void Set##name(const type * _arg) \
831  { \
832  typedef DataObjectDecorator< type > DecoratorType; \
833  itkDebugMacro("setting input " #name " to " << _arg); \
834  const DecoratorType *oldInput = \
835  static_cast< const DecoratorType * >( \
836  this->ProcessObject::GetInput(#name) ); \
837  if ( oldInput && oldInput->Get() == _arg ) \
838  { \
839  return; \
840  } \
841  typename DecoratorType::Pointer newInput = DecoratorType::New(); \
842  newInput->Set(_arg); \
843  this->Set##name##Input(newInput); \
844  }
845 
846 
851 #define itkGetDecoratedObjectInputMacro(name, type) \
852  virtual const DataObjectDecorator< type > * Get##name##Input() const \
853  { \
854  itkDebugMacro( "returning input " << #name " of " \
855  << static_cast< const DataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ) ); \
856  return static_cast< const DataObjectDecorator< type > * >( this->ProcessObject::GetInput(#name) ); \
857  } \
858  virtual const type * Get##name() const \
859  { \
860  itkDebugMacro("Getting input " #name); \
861  typedef DataObjectDecorator< type > DecoratorType; \
862  const DecoratorType *input = \
863  static_cast< const DecoratorType * >( \
864  this->ProcessObject::GetInput(#name) ); \
865  if( input == NULL ) \
866  { \
867  itkExceptionMacro(<<"input" #name " is not set"); \
868  } \
869  return input->Get(); \
870  }
871 
872 
875 #define itkSetGetDecoratedObjectInputMacro(name, type) \
876  itkSetDecoratedObjectInputMacro(name, type) \
877  itkGetDecoratedObjectInputMacro(name, type)
878 
880 #define itkSetMacro(name, type) \
881  virtual void Set##name (const type _arg) \
882  { \
883  itkDebugMacro("setting " #name " to " << _arg); \
884  if ( this->m_##name != _arg ) \
885  { \
886  this->m_##name = _arg; \
887  this->Modified(); \
888  } \
889  }
890 
891 
893 #define itkGetMacro(name, type) \
894  virtual type Get##name () \
895  { \
896  return this->m_##name; \
897  }
898 
899 
903 #define itkGetConstMacro(name, type) \
904  virtual type Get##name () const \
905  { \
906  return this->m_##name; \
907  }
908 
909 
914 #define itkGetConstReferenceMacro(name, type) \
915  virtual const type &Get##name () const \
916  { \
917  return this->m_##name; \
918  }
919 
920 
925 #define itkSetEnumMacro(name, type) \
926  virtual void Set##name (const type _arg) \
927  { \
928  itkDebugMacro( "setting " #name " to " << static_cast< long >( _arg ) ); \
929  if ( this->m_##name != _arg ) \
930  { \
931  this->m_##name = _arg; \
932  this->Modified(); \
933  } \
934  }
935 
936 
941 #define itkGetEnumMacro(name, type) \
942  virtual type Get##name () const \
943  { \
944  return this->m_##name; \
945  }
946 
947 
951 #define itkSetStringMacro(name) \
952  virtual void Set##name (const char *_arg) \
953  { \
954  if ( _arg && ( _arg == this->m_##name ) ) { return; } \
955  if ( _arg ) \
956  { \
957  this->m_##name = _arg; \
958  } \
959  else \
960  { \
961  this->m_##name = ""; \
962  } \
963  this->Modified(); \
964  } \
965  virtual void Set##name (const std::string & _arg) \
966  { \
967  this->Set##name( _arg.c_str() ); \
968  } \
969 
970 
971 
975 #define itkGetStringMacro(name) \
976  virtual const char *Get##name () const \
977  { \
978  return this->m_##name.c_str(); \
979  }
980 
984 #define itkSetClampMacro(name, type, min, max) \
985  virtual void Set##name (type _arg) \
986  { \
987  itkDebugMacro("setting " << #name " to " << _arg); \
988  if ( this->m_##name != ( _arg < min ? min : ( _arg > max ? max : _arg ) ) ) \
989  { \
990  this->m_##name = ( _arg < min ? min : ( _arg > max ? max : _arg ) ); \
991  this->Modified(); \
992  } \
993  }
994 
995 
1000 #define itkSetObjectMacro(name, type) \
1001  virtual void Set##name (type * _arg) \
1002  { \
1003  itkDebugMacro("setting " << #name " to " << _arg); \
1004  if ( this->m_##name != _arg ) \
1005  { \
1006  this->m_##name = _arg; \
1007  this->Modified(); \
1008  } \
1009  }
1010 
1011 
1020 // NOTE: A class can use either itkGetModifiableObjectMacro
1021 // or itkGetObjectMacro, but not both.
1022 // A class can use either itkGetModifiableObjectMacro
1023 // or itkGetConstObjectMacro, but not both.
1024 // If the desired behavior is to only provide const
1025 // access to the itkObject ivar, then use itkGetConstObjectMacro,
1026 // else use itkGetModifiableObjectMacro for read/write access to
1027 // the ivar.
1028 // It is permissable to use both itkGetObjectMacro and itkGetConstObjectMacro
1029 // for backwards compatibility.
1030 // If the ITK_LEGACY_REMOVE=FALSE, then it is
1031 // permissable to use itkGetObjectMacro which
1032 // defines both signatures itk::GetXXX() and
1033 // itk::GetModifiableXXX()
1034 
1037 #define itkGetConstObjectMacro(name, type) \
1038  virtual const type * Get##name () const \
1039  { \
1040  return this->m_##name.GetPointer(); \
1041  }
1042 
1043 
1044 #if defined ( ITK_FUTURE_LEGACY_REMOVE )
1045 // In the future, the itkGetObjectMacro will be deprecated with the ITK_LEGACY_REMOVE
1046 // flag. For now, this very advanced feature is only available
1047 // through manual setting of a compiler define -DITK_FUTURE_LEGACY_REMOVE
1048 // ("/DITK_FUTURE_LEGACY_REMOVE /EHsc" with Visual Studio)
1049 // to ease the transition from the historical GetObjectMacro to the GetModifiableObjectMacro
1050 # define itkGetObjectMacro(name, type) \
1051  virtual type * Get##name () \
1052  { \
1053  purposeful_error("itkGetObjectMacro should be replaced with itkGetModifiableObjectMacro."); \
1054  }
1055 
1056 # define itkGetModifiableObjectMacro(name, type) \
1057  virtual type * GetModifiable##name () \
1058  { \
1059  return this->m_##name.GetPointer(); \
1060  } \
1061  itkGetConstObjectMacro(name, type)
1062 
1063 #else // defined ( ITK_FUTURE_LEGACY_REMOVE )
1064 
1066 # define itkGetObjectMacro(name, type) \
1067  virtual type * Get##name () \
1068  { \
1069  return this->m_##name.GetPointer(); \
1070  }
1071 # define itkGetModifiableObjectMacro(name, type) \
1072  virtual type * GetModifiable##name () \
1073  { \
1074  return this->m_##name.GetPointer(); \
1075  } \
1076  itkGetConstObjectMacro(name, type) \
1077  itkGetObjectMacro(name, type)
1078 #endif // defined ( ITK_FUTURE_LEGACY_REMOVE )
1079 
1080 
1083 #define itkGetConstReferenceObjectMacro(name, type) \
1084  virtual const typename type::Pointer & Get##name () const \
1085  { \
1086  return this->m_##name; \
1087  }
1088 
1093 #define itkSetConstObjectMacro(name, type) \
1094  virtual void Set##name (const type * _arg) \
1095  { \
1096  itkDebugMacro("setting " << #name " to " << _arg); \
1097  if ( this->m_##name != _arg ) \
1098  { \
1099  this->m_##name = _arg; \
1100  this->Modified(); \
1101  } \
1102  }
1103 
1104 
1107 #define itkBooleanMacro(name) \
1108  virtual void name##On () \
1109  { \
1110  this->Set##name(true); \
1111  } \
1112  virtual void name##Off () \
1113  { \
1114  this->Set##name(false); \
1115  }
1116 
1117 
1121 #define itkSetVectorMacro(name, type, count) \
1122  virtual void Set##name(type data[]) \
1123  { \
1124  unsigned int i; \
1125  for ( i = 0; i < count; i++ ) \
1126  { \
1127  if ( data[i] != this->m_##name[i] ) \
1128  { \
1129  break; \
1130  } \
1131  } \
1132  if ( i < count ) \
1133  { \
1134  this->Modified(); \
1135  for ( i = 0; i < count; i++ ) \
1136  { \
1137  this->m_##name[i] = data[i]; \
1138  } \
1139  } \
1140  }
1141 
1142 
1145 #define itkGetVectorMacro(name, type, count) \
1146  virtual type * Get##name () const \
1147  { \
1148  return this->m_##name; \
1149  }
1150 
1154 #define itkGPUKernelClassMacro(kernel) \
1155 class ITK_EXPORT kernel \
1156  { \
1157  public: \
1158  static const char* GetOpenCLSource(); \
1159  private: \
1160  kernel(); \
1161  virtual ~kernel(); \
1162  kernel(const kernel &); \
1163  void operator=(const kernel &); \
1164  };
1165 
1166 
1167 #define itkGetOpenCLSourceFromKernelMacro(kernel) \
1168  static const char* GetOpenCLSource() \
1169  { \
1170  return kernel::GetOpenCLSource(); \
1171  }
1172 
1173 }
1174 
1175 #endif //end of itkMacro.h
1176