Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkMacro.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkMacro.h,v $
00005   Language:  C++
00006   Date:      $Date: 2005/11/05 13:10:24 $
00007   Version:   $Revision: 1.57 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012   Portions of this code are covered under the VTK copyright.
00013   See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
00014 
00015      This software is distributed WITHOUT ANY WARRANTY; without even 
00016      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00017      PURPOSE.  See the above copyright notices for more information.
00018 
00019 =========================================================================*/
00030 #ifndef __itkMacro_h
00031 #define __itkMacro_h
00032 
00033 #include "itkWin32Header.h"
00034 #include "itkConfigure.h"
00035 
00036 #include <string>
00037 
00038 // Determine type of string stream to use.
00039 #if !defined(CMAKE_NO_ANSI_STRING_STREAM)
00040 #  include <sstream>
00041 #elif !defined(CMAKE_NO_ANSI_STREAM_HEADERS)
00042 #  include <strstream>
00043 #  define ITK_NO_ANSI_STRING_STREAM
00044 #else
00045 #  include <strstream.h>
00046 #  define ITK_NO_ANSI_STRING_STREAM
00047 #endif
00048 
00053 namespace itk
00054 {
00055 } // end namespace itk - this is here for documentation purposes
00056 
00059 #define itkNotUsed(x)
00060 
00075 #if defined(_MSC_VER) && (_MSC_VER <= 1300) 
00076 #   define ITK_NO_INCLASS_MEMBER_INITIALIZATION
00077 #endif
00078 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x540)
00079 #   define ITK_NO_INCLASS_MEMBER_INITIALIZATION
00080 #endif
00081 #if defined(__SVR4) && !defined(__SUNPRO_CC)
00082 #   define ITK_NO_INCLASS_MEMBER_INITIALIZATION
00083 #endif
00084 
00085 #if defined(_MSC_VER) && (_MSC_VER <= 1300) 
00086 #define ITK_NO_SELF_AS_TRAIT_IN_TEMPLATE_ARGUMENTS
00087 #endif
00088 
00089 #ifdef ITK_NO_INCLASS_MEMBER_INITIALIZATION
00090 #   define itkStaticConstMacro(name,type,value) enum { name = value }
00091 #else
00092 #   define itkStaticConstMacro(name,type,value) static const type name = value
00093 #endif
00094 
00095 #ifdef ITK_NO_SELF_AS_TRAIT_IN_TEMPLATE_ARGUMENTS
00096 #   define itkGetStaticConstMacro(name) name
00097 #else
00098 #   define itkGetStaticConstMacro(name) (Self::name)
00099 #endif
00100 
00102 #define itkSetInputMacro(name, type, number) \
00103   virtual void Set##name##Input(const type *_arg) \
00104   { \
00105     itkDebugMacro("setting input " #name " to " << _arg); \
00106     if (_arg != static_cast<type *>(this->ProcessObject::GetInput( number ))) \
00107       { \
00108       this->ProcessObject::SetNthInput( number, const_cast<type *>(_arg) ); \
00109       this->Modified(); \
00110       } \
00111   } \
00112   virtual void SetInput##number(const type *_arg) \
00113   { \
00114     itkDebugMacro("setting input " #number " to " << _arg); \
00115     if (_arg != static_cast<type *>(this->ProcessObject::GetInput( number ))) \
00116       { \
00117       this->ProcessObject::SetNthInput( number, const_cast<type *>(_arg) ); \
00118       this->Modified(); \
00119       } \
00120   } 
00121 
00122 
00124 #define itkGetInputMacro(name, type, number) \
00125   virtual const type * Get##name##Input() const \
00126   { \
00127     itkDebugMacro("returning input " << #name " of " << static_cast<const type *>(this->ProcessObject::GetInput( number )) ); \
00128     return static_cast<const type *>(this->ProcessObject::GetInput( number )); \
00129   } \
00130   virtual const type * GetInput##number() const \
00131   { \
00132     itkDebugMacro("returning input " << #number " of " << static_cast<const type *>(this->ProcessObject::GetInput( number )) ); \
00133     return static_cast<const type *>(this->ProcessObject::GetInput( number )); \
00134   } 
00135 
00136 
00138 #define itkSetMacro(name,type) \
00139   virtual void Set##name (const type _arg) \
00140   { \
00141     itkDebugMacro("setting " #name " to " << _arg); \
00142     if (this->m_##name != _arg) \
00143       { \
00144       this->m_##name = _arg; \
00145       this->Modified(); \
00146       } \
00147   } 
00148 
00150 #define itkGetMacro(name,type) \
00151   virtual type Get##name () \
00152   { \
00153     itkDebugMacro("returning " << #name " of " << this->m_##name ); \
00154     return this->m_##name; \
00155   }
00156 
00160 #define itkGetConstMacro(name,type) \
00161   virtual type Get##name () const \
00162   { \
00163     itkDebugMacro("returning " << #name " of " << this->m_##name ); \
00164     return this->m_##name; \
00165   }
00166 
00171 #define itkGetConstReferenceMacro(name,type) \
00172   virtual const type & Get##name () const \
00173   { \
00174     itkDebugMacro("returning " << #name " of " << this->m_##name ); \
00175     return this->m_##name; \
00176   }
00177 
00181 #define itkSetStringMacro(name) \
00182   virtual void Set##name (const char* _arg) \
00183   { \
00184     if ( _arg && (_arg == this->m_##name) ) { return;} \
00185     if (_arg) \
00186       { \
00187       this->m_##name = _arg;\
00188       } \
00189      else \
00190       { \
00191       this->m_##name = ""; \
00192       } \
00193     this->Modified(); \
00194   } 
00195 
00196 
00200 #define itkGetStringMacro(name) \
00201   virtual const char* Get##name () const \
00202   { \
00203     return this->m_##name.c_str(); \
00204   } 
00205 
00209 #define itkSetClampMacro(name,type,min,max) \
00210   virtual void Set##name (type _arg) \
00211   { \
00212     itkDebugMacro("setting " << #name " to " << _arg ); \
00213     if (this->m_##name != (_arg<min?min:(_arg>max?max:_arg))) \
00214       { \
00215       this->m_##name = (_arg<min?min:(_arg>max?max:_arg)); \
00216       this->Modified(); \
00217       } \
00218   } 
00219 
00224 #define itkSetObjectMacro(name,type) \
00225   virtual void Set##name (type* _arg) \
00226   { \
00227     itkDebugMacro("setting " << #name " to " << _arg ); \
00228     if (this->m_##name != _arg) \
00229       { \
00230       this->m_##name = _arg; \
00231       this->Modified(); \
00232       } \
00233   } 
00234 
00237 #define itkGetObjectMacro(name,type) \
00238   virtual type * Get##name () \
00239   { \
00240     itkDebugMacro("returning " #name " address " << this->m_##name ); \
00241     return this->m_##name.GetPointer(); \
00242   } 
00243 
00248 #define itkSetConstObjectMacro(name,type) \
00249   virtual void Set##name (const type* _arg) \
00250   { \
00251     itkDebugMacro("setting " << #name " to " << _arg ); \
00252     if (this->m_##name != _arg) \
00253       { \
00254       this->m_##name = _arg; \
00255       this->Modified(); \
00256       } \
00257   } 
00258 
00259 
00262 #define itkGetConstObjectMacro(name,type) \
00263   virtual const type * Get##name () const \
00264   { \
00265     itkDebugMacro("returning " #name " address " << this->m_##name ); \
00266     return this->m_##name.GetPointer(); \
00267   } 
00268 
00271 #define itkGetConstReferenceObjectMacro(name,type) \
00272   virtual const typename type::Pointer & Get##name () const \
00273   { \
00274     itkDebugMacro("returning " #name " address " << this->m_##name ); \
00275     return this->m_##name; \
00276   } 
00277 
00280 #define itkBooleanMacro(name) \
00281   virtual void name##On () { this->Set##name(true);} \
00282   virtual void name##Off () { this->Set##name(false);}
00283 
00287 #define itkSetVectorMacro(name,type,count) \
00288   virtual void Set##name(type data[]) \
00289   { \
00290     unsigned int i; \
00291     for (i=0; i<count; i++) { if ( data[i] != this->m_##name[i] ) { break; }} \
00292     if ( i < count ) \
00293       { \
00294       this->Modified(); \
00295       for (i=0; i<count; i++) { this->m_##name[i] = data[i]; }\
00296       } \
00297   }
00298 
00301 #define itkGetVectorMacro(name,type,count) \
00302   virtual type *Get##name () const \
00303   { \
00304     return this->m_##name; \
00305   } 
00306 
00323 #define itkNewMacro(x) \
00324 static Pointer New(void) \
00325 { \
00326   Pointer smartPtr; \
00327   x *rawPtr = ::itk::ObjectFactory<x>::Create(); \
00328   if(rawPtr == NULL) \
00329     { \
00330     rawPtr = new x; \
00331     } \
00332   smartPtr = rawPtr; \
00333   rawPtr->UnRegister(); \
00334   return smartPtr; \
00335 } \
00336 virtual ::itk::LightObject::Pointer CreateAnother(void) const \
00337 { \
00338   ::itk::LightObject::Pointer smartPtr; \
00339   smartPtr = x::New().GetPointer(); \
00340   return smartPtr; \
00341 }
00342 
00343 
00360 #define itkFactorylessNewMacro(x) \
00361 static Pointer New(void) \
00362 { \
00363   Pointer smartPtr; \
00364   x *rawPtr = new x; \
00365   smartPtr = rawPtr; \
00366   rawPtr->UnRegister(); \
00367   return smartPtr; \
00368 } \
00369 virtual LightObject::Pointer CreateAnother(void) const \
00370 { \
00371   LightObject::Pointer smartPtr; \
00372   smartPtr = x::New().GetPointer(); \
00373   return smartPtr; \
00374 }
00375 
00378 #define itkTypeMacro(thisClass,superclass) \
00379     virtual const char *GetNameOfClass() const \
00380         {return #thisClass;} 
00381 
00382 
00383 namespace itk
00384 {
00391 extern ITKCommon_EXPORT void OutputWindowDisplayText(const char*);
00392 extern ITKCommon_EXPORT void OutputWindowDisplayErrorText(const char*);
00393 extern ITKCommon_EXPORT void OutputWindowDisplayWarningText(const char*);
00394 extern ITKCommon_EXPORT void OutputWindowDisplayGenericOutputText(const char*);
00395 extern ITKCommon_EXPORT void OutputWindowDisplayDebugText(const char*);
00396 } // end namespace itk
00397 
00401 #if defined(ITK_LEAN_AND_MEAN) || defined(__BORLANDC__)
00402 #define itkDebugMacro(x)
00403 #else
00404 #define itkDebugMacro(x) \
00405   { if (this->GetDebug() && ::itk::Object::GetGlobalWarningDisplay())   \
00406     { ::itk::OStringStream itkmsg; \
00407       itkmsg << "Debug: In " __FILE__ ", line " << __LINE__ << "\n" \
00408              << this->GetNameOfClass() << " (" << this << "): " x  \
00409              << "\n\n"; \
00410       ::itk::OutputWindowDisplayDebugText(itkmsg.str().c_str());} \
00411 }
00412 #endif
00413 
00414 
00418 #ifdef ITK_LEAN_AND_MEAN
00419 #define itkWarningMacro(x)
00420 #else
00421 #define itkWarningMacro(x) \
00422 { if (itk::Object::GetGlobalWarningDisplay()) \
00423     { ::itk::OStringStream itkmsg; \
00424       itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
00425              << this->GetNameOfClass() << " (" << this << "): " x  \
00426              << "\n\n"; \
00427       itk::OutputWindowDisplayWarningText(itkmsg.str().c_str());} \
00428 }
00429 #endif
00430 
00431 namespace itk
00432 {
00433 
00439 #if !defined(ITK_NO_ANSI_STRING_STREAM)
00440 class OStringStream: public std::ostringstream
00441 {
00442 public:
00443   OStringStream() {}
00444 private:
00445   OStringStream(const OStringStream&);
00446   void operator=(const OStringStream&);
00447 };
00448 #else
00449 namespace OStringStreamDetail
00450 {
00451   class Cleanup
00452   {
00453   public:
00454     Cleanup(std::ostrstream& ostr): m_OStrStream(ostr) {}
00455     ~Cleanup() { m_OStrStream.rdbuf()->freeze(0); }
00456     static void IgnoreUnusedVariable(const Cleanup&) {}
00457   protected:
00458     std::ostrstream& m_OStrStream;
00459   };
00460 }//namespace OStringStreamDetail
00461 
00462 class OStringStream: public std::ostrstream
00463 {
00464 public:
00465   typedef std::ostrstream Superclass;
00466   OStringStream() {}
00467   std::string str()
00468     {
00469       OStringStreamDetail::Cleanup cleanup(*this);
00470       OStringStreamDetail::Cleanup::IgnoreUnusedVariable(cleanup);
00471       int pcount = this->pcount();
00472       const char* ptr = this->Superclass::str();
00473       return std::string(ptr?ptr:"", pcount);
00474     }
00475 private:
00476   OStringStream(const OStringStream&);
00477   void operator=(const OStringStream&);
00478 };
00479 #endif
00480 
00481 }//namespace itk
00482 
00483 #include "itkExceptionObject.h"
00484   
00488 #define itkExceptionMacro(x) \
00489   { \
00490   ::itk::OStringStream message; \
00491   message << "itk::ERROR: " << this->GetNameOfClass() \
00492           << "(" << this << "): " x; \
00493   ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str()); \
00494   throw e_; /* Explicit naming to work around Intel compiler bug.  */ \
00495   }
00496 
00497 #define itkGenericExceptionMacro(x) \
00498   { \
00499   ::itk::OStringStream message; \
00500   message << "itk::ERROR: " x; \
00501   ::itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str()); \
00502   throw e_; /* Explicit naming to work around Intel compiler bug.  */ \
00503   }
00504 
00505 #ifdef ITK_LEAN_AND_MEAN
00506 #define itkGenericOutputMacro(x)
00507 #else
00508 #define itkGenericOutputMacro(x) \
00509 { if (::itk::Object::GetGlobalWarningDisplay()) \
00510     { ::itk::OStringStream itkmsg; \
00511       itkmsg << "WARNING: In " __FILE__ ", line " << __LINE__ << "\n" \
00512              x << "\n\n"; \
00513       itk::OutputWindowDisplayGenericOutputText(itkmsg.str().c_str());} \
00514 }
00515 #endif
00516 
00517 
00518 
00519 //----------------------------------------------------------------------------
00520 // Macros for simplifying the use of logging
00521 //
00522 #define itkLogMacro( x, y)  \
00523 {         \
00524   if (this->GetLogger() ) \
00525     {  \
00526     this->GetLogger()->Write(::itk::LoggerBase::x, y); \
00527     }  \
00528 }
00529 
00530 
00531 #define itkLogMacroStatic( obj, x, y)  \
00532 {         \
00533   if (obj->GetLogger() ) \
00534     {  \
00535     obj->GetLogger()->Write(::itk::LoggerBase::x, y); \
00536     }  \
00537 }
00538 
00539 
00540 //----------------------------------------------------------------------------
00541 // Setup legacy code policy.
00542 
00543 // Define itkLegacy macro to mark legacy methods where they are
00544 // declared in their class.  Example usage:
00545 //
00546 //   // @deprecated Replaced by MyOtherMethod() as of ITK 2.0.
00547 //   itkLegacy(void MyMethod());
00548 #if defined(ITK_LEGACY_REMOVE)
00549   // Remove legacy methods completely.
00550 # define itkLegacy(method)
00551 #elif defined(ITK_LEGACY_SILENT) || defined(ITK_WRAPPING_CXX)
00552   // Provide legacy methods with no warnings.
00553 # define itkLegacy(method) method
00554 #else
00555   // Setup compile-time warnings for uses of deprecated methods if
00556   // possible on this compiler.
00557 # if defined(__GNUC__) && !defined(__INTEL_COMPILER) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
00558 #  define itkLegacy(method) method __attribute__((deprecated))
00559 # elif defined(_MSC_VER) && _MSC_VER >= 1300
00560 #  define itkLegacy(method) __declspec(deprecated) method
00561 # else
00562 #  define itkLegacy(method) method
00563 # endif
00564 #endif
00565 
00566 // Macros to create runtime deprecation warning messages in function
00567 // bodies.  Example usage:
00568 //
00569 //   void itkMyClass::MyOldMethod()
00570 //   {
00571 //     itkLegacyBody(itkMyClass::MyOldMethod, 2.0);
00572 //   }
00573 //
00574 //   void itkMyClass::MyMethod()
00575 //   {
00576 //     itkLegacyReplaceBody(itkMyClass::MyMethod, 2.0,
00577 //                          itkMyClass::MyOtherMethod);
00578 //   }
00579 #if defined(ITK_LEGACY_REMOVE) || defined(ITK_LEGACY_SILENT)
00580 # define itkLegacyBody(method, version)
00581 # define itkLegacyReplaceBody(method, version, replace)
00582 #else
00583 # define itkLegacyBody(method, version) \
00584   itkWarningMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.")
00585 # define itkLegacyReplaceBody(method, version, replace) \
00586   itkWarningMacro(#method " was deprecated for ITK " #version " and will be removed in a future version.  Use " #replace " instead.")
00587 #endif
00588 
00589 
00590 #endif //end of itkMacro.h
00591 
00592 

Generated at Wed May 24 23:37:02 2006 for ITK by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2000