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