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

Generated at Mon Apr 14 13:32:22 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000