1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkMetaDataDictionaryTest.cxx,v $
Language: C++
Date: $Date: 2007-04-08 17:08:29 $
Version: $Revision: 1.14 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include "itkMetaDataDictionary.h"
#include "itkMetaDataObject.h"
#include "itkImage.h"
#include "itkObject.h"
#include <iostream>
#include <complex>
#if 0
//================================================================================
//================================================================================
//================================================================================
//================================================================================
// The behavior of the MetaDataObject<Type>::Print() function has many plausible
// application dependant implementations. The default implementation prints the
// a string "UNKNOWN PRINT CHARACTERISTICS]" that is applicable to all possible
// MetaDataObject types.
//
// The application developer may overload the default implementation to provide
// a specialization that produces results desirable for their applicaiton.
//
// Below is one possible implementation that may be used.
/**
* NATIVE_TYPE_METADATAPRINT
* An ungly macro to facilitate creating a simple implementation of
* the MetaDataObject<Type>::Print() function for types that
* have operator<< defined.
* \param TYPE_NAME the native type parameter type
*/
#define NATIVE_TYPE_METADATAPRINT(TYPE_NAME) \
void \
itk::MetaDataObject<TYPE_NAME> \
::Print(std::ostream& os) const \
{ \
os << this->m_MetaDataObjectValue << std::endl; \
} \
void \
itk::MetaDataObject<const TYPE_NAME> \
::Print(std::ostream& os) const \
{ \
os << this->m_MetaDataObjectValue << std::endl; \
}
/**
* ITK_OBJECT_TYPE_METADATAPRINT_1COMMA
* An ungly macro to facilitate creating a simple implementation of
* the MetaDataObject<Type>::Print() function for
* itk::Objects that have 1 comma in their type definition
* \param TYPE_NAME_PART1
* \param TYPE_NAME_PART2
*/
#define ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(TYPE_NAME_PART1,TYPE_NAME_PART2) \
void \
itk::MetaDataObject<TYPE_NAME_PART1,TYPE_NAME_PART2> \
::Print(std::ostream& os) const \
{ \
this->m_MetaDataObjectValue->Print(os); \
} \
void \
itk::MetaDataObject<const TYPE_NAME_PART1,TYPE_NAME_PART2> \
::Print(std::ostream& os) const \
{ \
this->m_MetaDataObjectValue->Print(os); \
}
/**
* ITK_IMAGE_TYPE_METADATAPRINT
* An ungly macro to facilitate creating a simple implementation of
* the MetaDataObject<Type>::Print() function for
* itk::Objects that have 1 comma in their type definition
* \param STORAGE_TYPE The storage type of the image type to print.
*/
#define ITK_IMAGE_TYPE_METADATAPRINT(STORAGE_TYPE) \
ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE,1>::Pointer) \
ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE,2>::Pointer) \
ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE,3>::Pointer) \
ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE,4>::Pointer) \
NATIVE_TYPE_METADATAPRINT(char)
NATIVE_TYPE_METADATAPRINT(char *)
NATIVE_TYPE_METADATAPRINT(char * const)
NATIVE_TYPE_METADATAPRINT(unsigned char)
NATIVE_TYPE_METADATAPRINT(short int)
NATIVE_TYPE_METADATAPRINT(unsigned short int)
NATIVE_TYPE_METADATAPRINT(int)
NATIVE_TYPE_METADATAPRINT(unsigned int)
NATIVE_TYPE_METADATAPRINT(long int)
NATIVE_TYPE_METADATAPRINT(unsigned long int)
NATIVE_TYPE_METADATAPRINT(float)
NATIVE_TYPE_METADATAPRINT(double)
NATIVE_TYPE_METADATAPRINT(std::string)
NATIVE_TYPE_METADATAPRINT(std::complex<float>)
NATIVE_TYPE_METADATAPRINT(std::complex<double>)
ITK_IMAGE_TYPE_METADATAPRINT(char)
ITK_IMAGE_TYPE_METADATAPRINT(char *)
ITK_IMAGE_TYPE_METADATAPRINT(char * const)
ITK_IMAGE_TYPE_METADATAPRINT(unsigned char)
ITK_IMAGE_TYPE_METADATAPRINT(short int)
ITK_IMAGE_TYPE_METADATAPRINT(unsigned short int)
ITK_IMAGE_TYPE_METADATAPRINT(int)
ITK_IMAGE_TYPE_METADATAPRINT(unsigned int)
ITK_IMAGE_TYPE_METADATAPRINT(long int)
ITK_IMAGE_TYPE_METADATAPRINT(unsigned long int)
ITK_IMAGE_TYPE_METADATAPRINT(float)
ITK_IMAGE_TYPE_METADATAPRINT(double)
ITK_IMAGE_TYPE_METADATAPRINT(std::string)
ITK_IMAGE_TYPE_METADATAPRINT(std::complex<float>)
ITK_IMAGE_TYPE_METADATAPRINT(std::complex<double>)
//================================================================================
//================================================================================
//================================================================================
//================================================================================
#endif
int itkMetaDataDictionaryTest(int , char * [])
{
//This is a demo program to show how to put data into a dictionary.
itk::MetaDataDictionary MyDictionary;
//------------------------Testing of native types
//-------Floats
itk::EncapsulateMetaData<float>(MyDictionary,"ASimpleFloatInitalized",static_cast<float>(1.234560F));
{
float tempfloat = 0.0;
const bool IsValidReturn=itk::ExposeMetaData<float>(MyDictionary,"ASimpleFloatInitalized",tempfloat);
if(IsValidReturn == true)
{
std::cout << tempfloat << std::endl;
}
else
{
std::cout << "Invalid key, or invalid type specified." << std::endl;
}
}
itk::EncapsulateMetaData<float>(MyDictionary,"ASimpleFloatChanged",static_cast<float>(-1000.234560F));
itk::EncapsulateMetaData<double>(MyDictionary,"ASimpleFloatChanged",static_cast<float>(-0.000000001F));
//-------Char pointers -- These can be tricky, so be careful!
itk::EncapsulateMetaData<const char *>(MyDictionary,"charconst*","Value String");
const char * value="Value String";
itk::EncapsulateMetaData<const char *>(MyDictionary,"charconst*2",value);
itk::EncapsulateMetaData<std::string>(MyDictionary,"srtringfromcharconst*",std::string("Value Never Seen"));
//Other gotchas with the Dictionary
char * StrandedMemory=new char[2345];
strcpy(StrandedMemory,"XXXXXXXXXXXXThis is stranded memory that will not be released when the Dictionary is cleaned up");
//NOTE: Only the pointer is copied, not the data withing the pointer!
itk::EncapsulateMetaData<char *>(MyDictionary,"MemoryChangedOutsideOfDictionary",StrandedMemory);
{
char * temp = NULL;
itk::ExposeMetaData<char *>(MyDictionary,"MemoryChangedOutsideOfDictionary",temp);
std::cout << "Memory Before Change: "<<temp <<std::endl;
}
strcpy(StrandedMemory,"------------This this was changed outside the class, and may cause all types of errors.");
{
char * temp = NULL;
itk::ExposeMetaData<char *>(MyDictionary,"MemoryChangedOutsideOfDictionary",temp);
std::cout << "Memory After Change: "<<temp <<std::endl;
}
//Print functionality Test
std::cout << "===========================================================" << std::endl;
std::cout << "Printing Dictionary" << std::endl;
MyDictionary.Print(std::cout);
// Iterator are broken on VS6
#if !(defined(_MSC_VER) && _MSC_VER < 1300)
std::cout << "Exercise the Iterator access" << std::endl;
try
{
itk::MetaDataDictionary::Iterator itr = MyDictionary.Begin();
itk::MetaDataDictionary::Iterator end = MyDictionary.End();
while( itr != end )
{
std::cout << "Key = " << itr->first << std::endl;
std::cout << "Value = ";
itr->second->Print( std::cout );
std::cout << std::endl;
++itr;
}
}
catch( itk::ExceptionObject & excp )
{
std::cerr << "Exception Thrown." << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
std::cout << "Exercise the const Iterator access" << std::endl;
try
{
const itk::MetaDataDictionary & MyConstDictionary = MyDictionary;
itk::MetaDataDictionary::ConstIterator itr = MyConstDictionary.Begin();
itk::MetaDataDictionary::ConstIterator end = MyConstDictionary.End();
while( itr != end )
{
std::cout << "Key = " << itr->first << std::endl;
std::cout << "Value = ";
itr->second->Print( std::cout );
std::cout << std::endl;
++itr;
}
}
catch( itk::ExceptionObject & excp )
{
std::cerr << "Exception Thrown." << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
#endif
//NOTE: Must clean up memory allocated with char * StrandedMemory=new char[2345];
delete [] StrandedMemory;
return EXIT_SUCCESS;
}
|