| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkMetaDataObject.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:42 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
Portions of this code are covered under the VTK copyright. |
| 13 |
|
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details. |
| 14 |
|
|
| 15 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 16 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 17 |
IND |
*****PURPOSE. See the above copyright notices for more information. |
| 18 |
|
|
| 19 |
|
=========================================================================*/ |
| 20 |
|
#ifndef __itkMetaDataObject_h |
| 21 |
|
#define __itkMetaDataObject_h |
| 22 |
|
|
| 23 |
|
#include "itkMetaDataDictionary.h" |
| 24 |
|
#include "itkMacro.h" |
| 25 |
|
#include "itkObjectFactory.h" |
| 26 |
|
#include "itkCommand.h" |
| 27 |
|
#include "itkFastMutexLock.h" |
| 28 |
|
|
| 29 |
|
#include <string> |
| 30 |
|
|
| 31 |
|
namespace itk |
| 32 |
|
{ |
| 33 |
IND |
**/** |
| 34 |
IND |
**** \author Hans J. Johnson |
| 35 |
IND |
**** The MetaDataObject class is a templated class that |
| 36 |
IND |
**** is a specialization of the MetaDataObjectBase type. |
| 37 |
IND |
**** This class allows arbitrary data types to be |
| 38 |
IND |
**** stored as MetaDataObjectBase types, and to be stored in |
| 39 |
IND |
**** a MetaDataDictionary. |
| 40 |
IND |
**** |
| 41 |
IND |
**** Any class or built in type that has valid copy constructor and operator= |
| 42 |
IND |
**** can be wrapped directly with this simple template type. |
| 43 |
IND |
**** |
| 44 |
LEN,IND |
**** Classes or built in types that do not have valid copy constructors or operator= |
| 45 |
LEN,IND |
**** implemented will have to implement those functions by deriving from MetaDataObject<MetaDataObjectType> |
| 46 |
LEN,IND |
**** and redefining the copy constructor and initializing constructor and the Get/Set functions |
| 47 |
IND |
**** to work around those deficiencies. |
| 48 |
IND |
**** |
| 49 |
LEN,IND |
**** The behavior of the MetaDataObject<Type>::Print() function has many plausible |
| 50 |
LEN,IND |
**** application dependant implementations. The default implementation prints the |
| 51 |
IND |
**** string "[UNKNOWN PRINT CHARACTERISTICS]" that works for all possible |
| 52 |
IND |
**** MetaDataObject types. |
| 53 |
IND |
**** |
| 54 |
LEN,IND |
**** The application developer may overload the default implementation to provide |
| 55 |
LEN,IND |
**** a specialized Print() characteristics to produce results desirable for their application. |
| 56 |
LEN,IND |
**** A set of very crude Macros {NATIVE_TYPE_METADATAPRINT, ITK_OBJECT_TYPE_METADATAPRINT_1COMMA, ITK_IMAGE_TYPE_METADATAPRINT } |
| 57 |
IND |
**** are provided to facilitate a very simple implementation, and as an example. |
| 58 |
|
|
| 59 |
IND |
****/ |
| 60 |
IND |
**template <class MetaDataObjectType> |
| 61 |
MCM,IND |
****class ITK_EXPORT MetaDataObject: public itk::MetaDataObjectBase |
| 62 |
IND |
****{ |
| 63 |
IND |
******public: |
| 64 |
IND |
********/** Smart pointer typedef support. */ |
| 65 |
IND |
********typedef MetaDataObject Self; |
| 66 |
TDA,IND |
********typedef MetaDataObjectBase Superclass; |
| 67 |
TDA,IND |
********typedef SmartPointer<Self> Pointer; |
| 68 |
TDA,IND |
********typedef SmartPointer<const Self> ConstPointer; |
| 69 |
|
|
| 70 |
IND |
********/** Method for creation through the object factory. */ |
| 71 |
IND |
********itkNewMacro(Self); |
| 72 |
|
|
| 73 |
IND |
********/** Run-time type information (and related methods). */ |
| 74 |
IND |
********itkTypeMacro(MetaDataObject, MetaDataObjectBase); |
| 75 |
|
|
| 76 |
IND |
********/** |
| 77 |
IND |
********** \author Hans J. Johnson |
| 78 |
IND |
********** Default constructor with no initialization. |
| 79 |
IND |
**********/ |
| 80 |
IND |
********MetaDataObject(void); |
| 81 |
IND |
********/** \author Hans J. Johnson |
| 82 |
IND |
********** Default virtual Destructor |
| 83 |
IND |
**********/ |
| 84 |
IND |
********virtual ~MetaDataObject(void); |
| 85 |
IND |
********/** |
| 86 |
IND |
********** \author Hans J. Johnson |
| 87 |
LEN,IND |
********** Initializer constructor that sets m_MetaDataObjectValue to InitializerValue |
| 88 |
IND |
**********/ |
| 89 |
IND |
********MetaDataObject(const MetaDataObjectType InitializerValue); |
| 90 |
IND |
********/** |
| 91 |
IND |
********** \author Hans J. Johnson |
| 92 |
LEN,IND |
********** Copy constructor that sets m_MetaDataObjectValue to TemplateObject.m_MetaDataObjectValue |
| 93 |
IND |
**********/ |
| 94 |
LEN,IND |
********MetaDataObject(const MetaDataObject<MetaDataObjectType> &TemplateObject); |
| 95 |
IND |
********/** |
| 96 |
IND |
********** \author Hans J. Johnson |
| 97 |
IND |
********** |
| 98 |
IND |
********** The definition of this function is necessary to fulfill |
| 99 |
IND |
********** the interface of the MetaDataObjectBase |
| 100 |
LEN,IND |
********** \return A pointer to a const char array containing the unique type name. |
| 101 |
IND |
**********/ |
| 102 |
IND |
********virtual const char * GetMetaDataObjectTypeName(void) const; |
| 103 |
IND |
********/** |
| 104 |
IND |
********** \author Hans J. Johnson |
| 105 |
IND |
********** |
| 106 |
IND |
********** The definition of this function is necessary to fulfill |
| 107 |
IND |
********** the interface of the MetaDataObjectBase |
| 108 |
IND |
********** \return A constant reference to a std::type_info object |
| 109 |
IND |
**********/ |
| 110 |
IND |
********virtual const std::type_info & GetMetaDataObjectTypeInfo(void) const; |
| 111 |
IND |
********/** |
| 112 |
IND |
********** \author Hans J. Johnson |
| 113 |
IND |
********** Function to return the stored value of type MetaDataObjectType. |
| 114 |
IND |
********** \return a constant reference to a MetaDataObjectType |
| 115 |
IND |
**********/ |
| 116 |
IND |
********const MetaDataObjectType & GetMetaDataObjectValue(void) const; |
| 117 |
IND |
********/** |
| 118 |
IND |
********** \author Hans J. Johnson |
| 119 |
IND |
********** Function to set the stored value of type MetaDataObjectType. |
| 120 |
IND |
********** \param A constant reference to at MetaDataObjectType. |
| 121 |
IND |
**********/ |
| 122 |
IND |
********void SetMetaDataObjectValue(const MetaDataObjectType & NewValue ); |
| 123 |
IND |
********/** |
| 124 |
IND |
********** Defines the default behavior for printing out this element |
| 125 |
IND |
********** \param os An output stream |
| 126 |
IND |
**********/ |
| 127 |
IND |
********virtual void Print(std::ostream& os) const; |
| 128 |
IND |
******private: |
| 129 |
LEN,IND |
********//This is made private to force the use of the MetaDataObject<MetaDataObjectType>::New() operator! |
| 130 |
IND |
********//void * operator new(size_t nothing) {};//purposefully not implemented |
| 131 |
IND |
********/** |
| 132 |
IND |
********** \author Hans J. Johnson |
| 133 |
IND |
********** A variable to store this derived type. |
| 134 |
IND |
**********/ |
| 135 |
IND |
********MetaDataObjectType m_MetaDataObjectValue; |
| 136 |
IND |
****}; |
| 137 |
|
|
| 138 |
|
|
| 139 |
IND |
**/** |
| 140 |
LEN,IND |
**** EncapsulateMetaData is a convenience function that encapsulates raw MetaData into a |
| 141 |
IND |
**** MetaDataObject that can be put into the MetaDataDictionary. |
| 142 |
IND |
**** \param value the value of type T that is to be encapsulated. |
| 143 |
IND |
**** \return A smartpointer ot a MetaDataObject that is suitable for |
| 144 |
IND |
**** insertion into a MetaDataDictionary. |
| 145 |
IND |
****/ |
| 146 |
IND |
**template <class T> |
| 147 |
LEN,IND |
****inline void EncapsulateMetaData(MetaDataDictionary &Dictionary, const std::string & key, const T &invalue) |
| 148 |
IND |
****{ |
| 149 |
IND |
******typename MetaDataObject<T>::Pointer temp=MetaDataObject<T>::New(); |
| 150 |
IND |
******temp->SetMetaDataObjectValue(invalue); |
| 151 |
IND |
******Dictionary[key] = temp; |
| 152 |
IND |
****} |
| 153 |
|
|
| 154 |
IND |
**template <class T> |
| 155 |
LEN,IND |
****inline void EncapsulateMetaData(MetaDataDictionary &Dictionary, const char *key, const T &invalue) |
| 156 |
IND |
****{ |
| 157 |
IND |
******EncapsulateMetaData(Dictionary, std::string(key), invalue); |
| 158 |
IND |
****} |
| 159 |
|
|
| 160 |
IND |
**/** |
| 161 |
IND |
**** FindValInDictionary provides a shortcut for pulling a value of type |
| 162 |
IND |
**** T out of a MetaDataDictionary. |
| 163 |
IND |
**** If Dictionary[key] isn't set, return false, otherwise copy into |
| 164 |
IND |
**** outval reference and return true. |
| 165 |
IND |
**** \param Dictionary -- reference to a dictionary |
| 166 |
IND |
**** \param key -- string identifier for this object |
| 167 |
IND |
**** \param outval -- where to store value found in table. |
| 168 |
IND |
****/ |
| 169 |
IND |
**template <class T> |
| 170 |
LEN,IND |
****inline bool ExposeMetaData(MetaDataDictionary &Dictionary, const std::string key, T &outval) |
| 171 |
IND |
****{ |
| 172 |
IND |
******if(!Dictionary.HasKey(key)) |
| 173 |
IND |
******{ |
| 174 |
IND |
********return false; |
| 175 |
IND |
******} |
| 176 |
|
|
| 177 |
IND |
******MetaDataObjectBase::Pointer baseObjectSmartPointer = Dictionary[key]; |
| 178 |
|
|
| 179 |
LEN,IND |
******if(strcmp(typeid(T).name(),baseObjectSmartPointer->GetMetaDataObjectTypeName()) != 0) |
| 180 |
IND |
******{ |
| 181 |
IND |
********return false; |
| 182 |
IND |
******} |
| 183 |
IND |
******//The following is necessary for getting this to work on |
| 184 |
IND |
******//kitware's SGI computers. It is not necessary for |
| 185 |
IND |
******//for IRIX 6.5.18m with MIPSPro 7.3.1.3m. |
| 186 |
|
#if (defined(__sgi) && !defined(__GNUC__)) |
| 187 |
IND |
******/* |
| 188 |
IND |
******** from page 10.4.11 pg 256 of the Stroustrup book: |
| 189 |
LEN,IND |
******** ======================================================================== |
| 190 |
LEN,IND |
******** The reinterpret_cast is the crudest and potentially nastiest of the type |
| 191 |
IND |
******** conversion operators. In most caes, it simply yeilds a value with the |
| 192 |
IND |
******** same bit pattern as it's argument wit the type required . Thus, it can |
| 193 |
IND |
******** be used for the inherently implementation-depend, dangerous, and |
| 194 |
LEN,IND |
******** occasionally absolutely necessary activity of converting interger values |
| 195 |
IND |
******** to pointers, and vice versa. |
| 196 |
IND |
********/ |
| 197 |
IND |
******outval = |
| 198 |
LEN,IND |
********reinterpret_cast<MetaDataObject <T> *>(Dictionary[key].GetPointer())->GetMetaDataObjectValue(); |
| 199 |
IND |
#else |
| 200 |
IND |
******{ |
| 201 |
LEN,IND |
********if(MetaDataObject <T> * TempMetaDataObject =dynamic_cast<MetaDataObject <T> *>(Dictionary[key].GetPointer())) |
| 202 |
IND |
********{ |
| 203 |
IND |
**********outval = TempMetaDataObject->GetMetaDataObjectValue(); |
| 204 |
IND |
********} |
| 205 |
IND |
********else |
| 206 |
IND |
********{ |
| 207 |
IND |
**********return false; |
| 208 |
IND |
********} |
| 209 |
IND |
******} |
| 210 |
|
#endif |
| 211 |
IND |
******// --------------- ^^^^^^^^^^^^ |
| 212 |
IND |
******// SmartPointer MetaDataObject<T>* |
| 213 |
IND |
******return true; |
| 214 |
IND |
****} |
| 215 |
|
|
| 216 |
LEN,IND |
**//This is only necessary to make the borland compiler happy. It should not be necesary for most compilers. |
| 217 |
LEN,IND |
**//This should not change the behavior, it just adds an extra level of complexity to using the ExposeMetaData |
| 218 |
IND |
**//with const char * keys. |
| 219 |
|
template <class T> |
| 220 |
LEN,IND |
**inline bool ExposeMetaData(MetaDataDictionary &Dictionary, const char * const key, T &outval) |
| 221 |
IND |
**{ |
| 222 |
IND |
****return ExposeMetaData(Dictionary, std::string(key), outval); |
| 223 |
IND |
**} |
| 224 |
|
|
| 225 |
|
} // end namespace itk |
| 226 |
|
|
| 227 |
|
/** |
| 228 |
|
* NATIVE_TYPE_METADATAPRINT |
| 229 |
|
* An ugly macro to facilitate creating a simple implementation of |
| 230 |
|
* the MetaDataObject<Type>::Print() function for types that |
| 231 |
|
* have operator<< defined. |
| 232 |
|
* \param TYPE_NAME the native type parameter type |
| 233 |
|
*/ |
| 234 |
|
#define NATIVE_TYPE_METADATAPRINT(TYPE_NAME) \ |
| 235 |
|
template <> \ |
| 236 |
|
void \ |
| 237 |
IND |
**itk::MetaDataObject< TYPE_NAME > \ |
| 238 |
IND |
**::Print(std::ostream& os) const \ |
| 239 |
IND |
{ \ |
| 240 |
|
os << this->m_MetaDataObjectValue << std::endl; \ |
| 241 |
|
} \ |
| 242 |
|
template <> \ |
| 243 |
|
void \ |
| 244 |
IND |
**itk::MetaDataObject< const TYPE_NAME > \ |
| 245 |
IND |
**::Print(std::ostream& os) const \ |
| 246 |
IND |
{ \ |
| 247 |
|
os << this->m_MetaDataObjectValue << std::endl; \ |
| 248 |
|
} |
| 249 |
|
|
| 250 |
|
/** |
| 251 |
|
* ITK_OBJECT_TYPE_METADATAPRINT_1COMMA |
| 252 |
|
* An ugly macro to facilitate creating a simple implementation of |
| 253 |
|
* the MetaDataObject< Type >::Print() function for |
| 254 |
|
* itk::Objects that have 1 comma in their type definition |
| 255 |
|
* \param TYPE_NAME_PART1 |
| 256 |
|
* \param TYPE_NAME_PART2 |
| 257 |
|
*/ |
| 258 |
LEN |
#define ITK_OBJECT_TYPE_METADATAPRINT_1COMMA( TYPE_NAME_PART1 , TYPE_NAME_PART2 ) \ |
| 259 |
|
template <> \ |
| 260 |
|
void \ |
| 261 |
IND |
**itk::MetaDataObject< TYPE_NAME_PART1 , TYPE_NAME_PART2 > \ |
| 262 |
IND |
**::Print(std::ostream& os) const \ |
| 263 |
IND |
{ \ |
| 264 |
|
this->m_MetaDataObjectValue->Print(os); \ |
| 265 |
IND |
} \ |
| 266 |
|
template <> \ |
| 267 |
|
void \ |
| 268 |
IND |
**itk::MetaDataObject< const TYPE_NAME_PART1 , TYPE_NAME_PART2 > \ |
| 269 |
IND |
**::Print(std::ostream& os) const \ |
| 270 |
IND |
{ \ |
| 271 |
|
this->m_MetaDataObjectValue->Print(os); \ |
| 272 |
IND |
} |
| 273 |
|
|
| 274 |
|
/** |
| 275 |
|
* ITK_IMAGE_TYPE_METADATAPRINT |
| 276 |
|
* An ugly macro to facilitate creating a simple implementation of |
| 277 |
|
* the MetaDataObject<Type>::Print() function for |
| 278 |
|
* itk::Image<STORAGE_TYPE,[1-8]>::Pointer |
| 279 |
|
* \param STORAGE_TYPE The storage type of the image type to print. |
| 280 |
|
*/ |
| 281 |
|
#define ITK_IMAGE_TYPE_METADATAPRINT(STORAGE_TYPE) \ |
| 282 |
LEN,IND |
**ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 1 >::Pointer) \ |
| 283 |
LEN,IND |
**ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 2 >::Pointer) \ |
| 284 |
LEN,IND |
**ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 3 >::Pointer) \ |
| 285 |
LEN,IND |
**ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 4 >::Pointer) \ |
| 286 |
LEN,IND |
**ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 5 >::Pointer) \ |
| 287 |
LEN,IND |
**ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 6 >::Pointer) \ |
| 288 |
LEN,IND |
**ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 7 >::Pointer) \ |
| 289 |
LEN,IND |
**ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE , 8 >::Pointer) \ |
| 290 |
|
|
| 291 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 292 |
|
#include "itkMetaDataObject.txx" |
| 293 |
|
#endif |
| 294 |
|
|
| 295 |
|
#endif //itkMetaDataObject_h |
| 296 |
|
|
| 297 |
EOF |
|