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

itkMetaDataObject.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkMetaDataObject.h,v $
00005   Language:  C++
00006   Date:      $Date: 2008-05-26 02:36:52 $
00007   Version:   $Revision: 1.22 $
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 =========================================================================*/
00020 #ifndef __itkMetaDataObject_h
00021 #define __itkMetaDataObject_h
00022 
00023 #include "itkMetaDataDictionary.h"
00024 #include "itkMacro.h"
00025 #include "itkObjectFactory.h"
00026 #include "itkCommand.h"
00027 #include "itkFastMutexLock.h"
00028 
00029 #include <string>
00030 #include <cstring>
00031 
00032 namespace itk
00033 {
00061   template <class MetaDataObjectType>
00062     class ITK_EXPORT MetaDataObject: public MetaDataObjectBase
00063     {
00064       public:
00066         typedef MetaDataObject  Self;
00067         typedef MetaDataObjectBase  Superclass;
00068         typedef SmartPointer<Self>  Pointer;
00069         typedef SmartPointer<const Self>  ConstPointer;
00070 
00072         itkFactorylessNewMacro(Self);
00073 
00075         itkTypeMacro(MetaDataObject, MetaDataObjectBase);
00076 
00081         MetaDataObject(void);
00082 
00086         virtual ~MetaDataObject(void);
00087 
00092         MetaDataObject(const MetaDataObjectType InitializerValue);
00093 
00098         MetaDataObject(const MetaDataObject<MetaDataObjectType> &TemplateObject);
00099 
00107         virtual const char * GetMetaDataObjectTypeName(void) const;
00108 
00116         virtual const std::type_info & GetMetaDataObjectTypeInfo(void) const;
00117 
00123         const MetaDataObjectType & GetMetaDataObjectValue(void) const;
00124 
00130         void SetMetaDataObjectValue(const MetaDataObjectType & NewValue );
00131 
00136         virtual void Print(std::ostream& os) const;
00137       private:
00138         //This is made private to force the use of the MetaDataObject<MetaDataObjectType>::New() operator!
00139         //void * operator new(size_t nothing) {};//purposefully not implemented
00140 
00145         MetaDataObjectType m_MetaDataObjectValue;
00146     };
00147 
00148 
00156   template <class T>
00157     inline void EncapsulateMetaData(MetaDataDictionary &Dictionary, const std::string & key, const T &invalue)
00158     {
00159       typename MetaDataObject<T>::Pointer temp=MetaDataObject<T>::New();
00160       temp->SetMetaDataObjectValue(invalue);
00161       Dictionary[key] = temp;
00162     }
00164 
00165   template <class T>
00166     inline void EncapsulateMetaData(MetaDataDictionary &Dictionary, const char *key, const T &invalue)
00167     {
00168       EncapsulateMetaData(Dictionary, std::string(key), invalue);
00169     }
00170 
00180   template <class T>
00181     inline bool ExposeMetaData(MetaDataDictionary &Dictionary, const std::string key, T &outval)
00182     {
00183       if(!Dictionary.HasKey(key))
00184       {
00185         return false;
00186       }
00187 
00188       MetaDataObjectBase::Pointer baseObjectSmartPointer = Dictionary[key];
00189 
00190       if(strcmp(typeid(T).name(),baseObjectSmartPointer->GetMetaDataObjectTypeName()) != 0)
00191       {
00192         return false;
00193       }
00194       //The following is necessary for getting this to work on
00195       //kitware's SGI computers.  It is not necessary for
00196       //for IRIX 6.5.18m with MIPSPro 7.3.1.3m.
00197 #if (defined(__sgi) && !defined(__GNUC__))
00198       /*
00199        * from page 10.4.11 pg 256 of the Stroustrup book:
00200        * ========================================================================
00201        * The reinterpret_cast is the crudest and potentially nastiest of the type
00202        * conversion operators.  In most caes, it simply yeilds a value with the
00203        * same bit pattern as it's argument wit the type required .  Thus, it can
00204        * be used for the inherently implementation-depend, dangerous, and
00205        * occasionally absolutely necessary activity of converting interger values
00206        * to pointers, and  vice versa.
00207        */
00208       outval =
00209         reinterpret_cast<MetaDataObject <T> *>(Dictionary[key].GetPointer())->GetMetaDataObjectValue();
00210 #else
00211       {
00212         if(MetaDataObject <T> * TempMetaDataObject =dynamic_cast<MetaDataObject <T> *>(Dictionary[key].GetPointer()))
00213         {
00214           outval = TempMetaDataObject->GetMetaDataObjectValue();
00215         }
00216         else
00217         {
00218           return false;
00219         }
00220       }
00221 #endif
00222       //                                 --------------- ^^^^^^^^^^^^
00223       //                                 SmartPointer    MetaDataObject<T>*
00224       return true;
00225     }
00226 
00227   //This is only necessary to make the borland compiler happy.  It should not be necesary for most compilers.
00228   //This should not change the behavior, it just adds an extra level of complexity to using the ExposeMetaData
00229   //with const char * keys.
00230 template <class T>
00231   inline bool ExposeMetaData(MetaDataDictionary &Dictionary, const char * const key, T &outval)
00232   {
00233     return ExposeMetaData(Dictionary, std::string(key), outval);
00234   }
00235 // const versions of ExposeMetaData just to make life easier for enduser programmers, and to maintain backwards compatibility.
00236 // The other option is to cast away constness in the main function.
00237 template <class T>
00238   inline bool ExposeMetaData(const MetaDataDictionary &Dictionary, const std::string key, T &outval)
00239     {
00240     MetaDataDictionary NonConstVersion=Dictionary;
00241     return ExposeMetaData(NonConstVersion,key,outval);
00242     }
00243 
00244 template <class T>
00245   inline bool ExposeMetaData(const MetaDataDictionary &Dictionary, const char * const key, T &outval)
00246     {
00247     MetaDataDictionary NonConstVersion=Dictionary;
00248     return ExposeMetaData(Dictionary, std::string(key), outval);
00249     }
00250 
00251 } // end namespace itk
00252 
00260 #define NATIVE_TYPE_METADATAPRINT(TYPE_NAME) \
00261 template <> \
00262 void \
00263   itk::MetaDataObject< TYPE_NAME > \
00264   ::Print(std::ostream& os) const \
00265 { \
00266   os << this->m_MetaDataObjectValue << std::endl; \
00267 } \
00268 template <> \
00269 void \
00270   itk::MetaDataObject< const TYPE_NAME > \
00271   ::Print(std::ostream& os) const \
00272 { \
00273   os << this->m_MetaDataObjectValue << std::endl; \
00274 }
00275 
00276 
00285 #define ITK_OBJECT_TYPE_METADATAPRINT_1COMMA( TYPE_NAME_PART1 , TYPE_NAME_PART2 ) \
00286 template <> \
00287 void \
00288   itk::MetaDataObject< TYPE_NAME_PART1 , TYPE_NAME_PART2 > \
00289   ::Print(std::ostream& os) const \
00290 { \
00291   this->m_MetaDataObjectValue->Print(os); \
00292 } \
00293 template <> \
00294 void \
00295   itk::MetaDataObject< const TYPE_NAME_PART1 , TYPE_NAME_PART2 > \
00296   ::Print(std::ostream& os) const \
00297 { \
00298   this->m_MetaDataObjectValue->Print(os); \
00299 }
00300 
00301 
00309 #define ITK_IMAGE_TYPE_METADATAPRINT(STORAGE_TYPE) \
00310   ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 1 >::Pointer) \
00311   ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 2 >::Pointer) \
00312   ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 3 >::Pointer) \
00313   ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 4 >::Pointer) \
00314   ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 5 >::Pointer) \
00315   ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 6 >::Pointer) \
00316   ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 7 >::Pointer) \
00317   ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 8 >::Pointer) \
00318 
00319 // Define instantiation macro for this template.
00320 #define ITK_TEMPLATE_MetaDataObject(_, EXPORT, x, y) namespace itk { \
00321   _(1(class EXPORT MetaDataObject< ITK_TEMPLATE_1 x >)) \
00322   namespace Templates { typedef MetaDataObject< ITK_TEMPLATE_1 x > \
00323                                          MetaDataObject##y; } \
00324   }
00325 
00326 #if ITK_TEMPLATE_EXPLICIT
00327 # include "Templates/itkMetaDataObject+-.h"
00328 #endif
00329 
00330 #if ITK_TEMPLATE_TXX
00331 # include "itkMetaDataObject.txx"
00332 #endif
00333 
00334 
00335 #endif //itkMetaDataObject_h
00336 
00337 

Generated at Wed Nov 5 22:51:34 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000