ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkMetaDataObject.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #ifndef __itkMetaDataObject_h
29 #define __itkMetaDataObject_h
30 
31 #include "itkMetaDataDictionary.h"
32 #include "itkMacro.h"
33 #include "itkCommand.h"
34 #include "itkFastMutexLock.h"
35 
36 #include <cstring>
37 
38 namespace itk
39 {
72 template< class MetaDataObjectType >
73 class ITK_EXPORT MetaDataObject:public MetaDataObjectBase
74 {
75 public:
76 
82 
84  itkFactorylessNewMacro(Self);
85 
87  itkTypeMacro(MetaDataObject, MetaDataObjectBase);
88 
93  MetaDataObject(void);
94 
98  virtual ~MetaDataObject(void);
99 
104  MetaDataObject(const MetaDataObjectType InitializerValue);
105 
111 
119  virtual const char * GetMetaDataObjectTypeName(void) const;
120 
128  virtual const std::type_info & GetMetaDataObjectTypeInfo(void) const;
129 
135  const MetaDataObjectType & GetMetaDataObjectValue(void) const;
136 
142  void SetMetaDataObjectValue(const MetaDataObjectType & NewValue);
143 
148  virtual void Print(std::ostream & os) const;
149 
150 private:
151  //This is made private to force the use of the
152  // MetaDataObject<MetaDataObjectType>::New() operator!
153  //void * operator new(SizeValueType nothing) {};//purposefully not implemented
158  MetaDataObjectType m_MetaDataObjectValue;
159 };
160 
170 template< class T >
171 inline void EncapsulateMetaData(MetaDataDictionary & Dictionary, const std::string & key, const T & invalue)
172 {
174  temp->SetMetaDataObjectValue(invalue);
175  Dictionary[key] = temp;
176 }
178 
179 template< class T >
180 inline void EncapsulateMetaData(MetaDataDictionary & Dictionary, const char *key, const T & invalue)
181 {
182  EncapsulateMetaData(Dictionary, std::string(key), invalue);
183 }
184 
194 template< class T >
195 inline bool ExposeMetaData(MetaDataDictionary & Dictionary, const std::string key, T & outval)
196 {
197  if ( !Dictionary.HasKey(key) )
198  {
199  return false;
200  }
201 
202  MetaDataObjectBase::Pointer baseObjectSmartPointer = Dictionary[key];
203 
204  if ( strcmp( typeid( T ).name(), baseObjectSmartPointer->GetMetaDataObjectTypeName() ) != 0 )
205  {
206  return false;
207  }
208  {
209  if ( MetaDataObject< T > *TempMetaDataObject = dynamic_cast< MetaDataObject< T > * >( Dictionary[key].GetPointer() ) )
210  {
211  outval = TempMetaDataObject->GetMetaDataObjectValue();
212  }
213  else
214  {
215  return false;
216  }
217  }
218  return true;
219 }
220 
221 // This should not change the behavior, it just adds an extra level of complexity
222 // to using the ExposeMetaData with const char * keys.
223 template< class T >
224 inline bool ExposeMetaData(MetaDataDictionary & Dictionary, const char *const key, T & outval)
225 {
226  return ExposeMetaData(Dictionary, std::string(key), outval);
227 }
228 
229 // const versions of ExposeMetaData just to make life easier for enduser
230 // programmers, and to maintain backwards compatibility.
231 // The other option is to cast away constness in the main function.
232 template< class T >
233 inline bool ExposeMetaData(const MetaDataDictionary & Dictionary, const std::string key, T & outval)
234 {
235  MetaDataDictionary NonConstVersion = Dictionary;
236 
237  return ExposeMetaData(NonConstVersion, key, outval);
238 }
239 
240 template< class T >
241 inline bool ExposeMetaData(const MetaDataDictionary & Dictionary, const char *const key, T & outval)
242 {
243  MetaDataDictionary NonConstVersion = Dictionary;
244 
245  return ExposeMetaData(Dictionary, std::string(key), outval);
246 }
247 } // end namespace itk
248 
256 #define NATIVE_TYPE_METADATAPRINT(TYPE_NAME) \
257  template< > \
258  void \
259  itk::MetaDataObject< TYPE_NAME > \
260  ::Print(std::ostream & os) const \
261  { \
262  os << this->m_MetaDataObjectValue << std::endl; \
263  } \
264  template< > \
265  void \
266  itk::MetaDataObject< const TYPE_NAME > \
267  ::Print(std::ostream & os) const \
268  { \
269  os << this->m_MetaDataObjectValue << std::endl; \
270  }
271 
272 
281 #define ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(TYPE_NAME_PART1, TYPE_NAME_PART2) \
282  template< > \
283  void \
284  itk::MetaDataObject< TYPE_NAME_PART1, TYPE_NAME_PART2 > \
285  ::Print(std::ostream & os) const \
286  { \
287  this->m_MetaDataObjectValue->Print(os); \
288  } \
289  template< > \
290  void \
291  itk::MetaDataObject< const TYPE_NAME_PART1, TYPE_NAME_PART2 > \
292  ::Print(std::ostream & os) const \
293  { \
294  this->m_MetaDataObjectValue->Print(os); \
295  }
296 
297 
305 #define ITK_IMAGE_TYPE_METADATAPRINT(STORAGE_TYPE) \
306  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 1 >::Pointer) \
307  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 2 >::Pointer) \
308  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 3 >::Pointer) \
309  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 4 >::Pointer) \
310  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 5 >::Pointer) \
311  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 6 >::Pointer) \
312  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 7 >::Pointer) \
313  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 8 >::Pointer) \
314 
315 #ifndef ITK_MANUAL_INSTANTIATION
316 #include "itkMetaDataObject.hxx"
317 #endif
318 
319 #endif //itkMetaDataObject_h
320