ITK  4.2.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 <string.h>
37 #include <cstring>
38 
39 namespace itk
40 {
73 template< class MetaDataObjectType >
74 class ITK_EXPORT MetaDataObject:public MetaDataObjectBase
75 {
76 public:
77 
83 
85  itkFactorylessNewMacro(Self);
86 
88  itkTypeMacro(MetaDataObject, MetaDataObjectBase);
89 
94  MetaDataObject(void);
95 
99  virtual ~MetaDataObject(void);
100 
105  MetaDataObject(const MetaDataObjectType InitializerValue);
106 
112 
120  virtual const char * GetMetaDataObjectTypeName(void) const;
121 
129  virtual const std::type_info & GetMetaDataObjectTypeInfo(void) const;
130 
136  const MetaDataObjectType & GetMetaDataObjectValue(void) const;
137 
143  void SetMetaDataObjectValue(const MetaDataObjectType & NewValue);
144 
149  virtual void Print(std::ostream & os) const;
150 
151 private:
152  //This is made private to force the use of the
153  // MetaDataObject<MetaDataObjectType>::New() operator!
154  //void * operator new(SizeValueType nothing) {};//purposefully not implemented
159  MetaDataObjectType m_MetaDataObjectValue;
160 };
161 
171 template< class T >
172 inline void EncapsulateMetaData(MetaDataDictionary & Dictionary, const std::string & key, const T & invalue)
173 {
175  temp->SetMetaDataObjectValue(invalue);
176  Dictionary[key] = temp;
177 }
179 
180 template< class T >
181 inline void EncapsulateMetaData(MetaDataDictionary & Dictionary, const char *key, const T & invalue)
182 {
183  EncapsulateMetaData(Dictionary, std::string(key), invalue);
184 }
185 
195 template< class T >
196 inline bool ExposeMetaData(MetaDataDictionary & Dictionary, const std::string key, T & outval)
197 {
198  if ( !Dictionary.HasKey(key) )
199  {
200  return false;
201  }
202 
203  MetaDataObjectBase::Pointer baseObjectSmartPointer = Dictionary[key];
204 
205  if ( strcmp( typeid( T ).name(), baseObjectSmartPointer->GetMetaDataObjectTypeName() ) != 0 )
206  {
207  return false;
208  }
209  {
210  if ( MetaDataObject< T > *TempMetaDataObject = dynamic_cast< MetaDataObject< T > * >( Dictionary[key].GetPointer() ) )
211  {
212  outval = TempMetaDataObject->GetMetaDataObjectValue();
213  }
214  else
215  {
216  return false;
217  }
218  }
219  return true;
220 }
221 
222 // This should not change the behavior, it just adds an extra level of complexity
223 // to using the ExposeMetaData with const char * keys.
224 template< class T >
225 inline bool ExposeMetaData(MetaDataDictionary & Dictionary, const char *const key, T & outval)
226 {
227  return ExposeMetaData(Dictionary, std::string(key), outval);
228 }
229 
230 // const versions of ExposeMetaData just to make life easier for enduser
231 // programmers, and to maintain backwards compatibility.
232 // The other option is to cast away constness in the main function.
233 template< class T >
234 inline bool ExposeMetaData(const MetaDataDictionary & Dictionary, const std::string key, T & outval)
235 {
236  MetaDataDictionary NonConstVersion = Dictionary;
237 
238  return ExposeMetaData(NonConstVersion, key, outval);
239 }
240 
241 template< class T >
242 inline bool ExposeMetaData(const MetaDataDictionary & Dictionary, const char *const key, T & outval)
243 {
244  MetaDataDictionary NonConstVersion = Dictionary;
245 
246  return ExposeMetaData(Dictionary, std::string(key), outval);
247 }
248 } // end namespace itk
249 
257 #define NATIVE_TYPE_METADATAPRINT(TYPE_NAME) \
258  template< > \
259  void \
260  itk::MetaDataObject< TYPE_NAME > \
261  ::Print(std::ostream & os) const \
262  { \
263  os << this->m_MetaDataObjectValue << std::endl; \
264  } \
265  template< > \
266  void \
267  itk::MetaDataObject< const TYPE_NAME > \
268  ::Print(std::ostream & os) const \
269  { \
270  os << this->m_MetaDataObjectValue << std::endl; \
271  }
272 
273 
282 #define ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(TYPE_NAME_PART1, TYPE_NAME_PART2) \
283  template< > \
284  void \
285  itk::MetaDataObject< TYPE_NAME_PART1, TYPE_NAME_PART2 > \
286  ::Print(std::ostream & os) const \
287  { \
288  this->m_MetaDataObjectValue->Print(os); \
289  } \
290  template< > \
291  void \
292  itk::MetaDataObject< const TYPE_NAME_PART1, TYPE_NAME_PART2 > \
293  ::Print(std::ostream & os) const \
294  { \
295  this->m_MetaDataObjectValue->Print(os); \
296  }
297 
298 
306 #define ITK_IMAGE_TYPE_METADATAPRINT(STORAGE_TYPE) \
307  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 1 >::Pointer) \
308  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 2 >::Pointer) \
309  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 3 >::Pointer) \
310  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 4 >::Pointer) \
311  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 5 >::Pointer) \
312  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 6 >::Pointer) \
313  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 7 >::Pointer) \
314  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image< STORAGE_TYPE, 8 >::Pointer) \
315 
316 // Define instantiation macro for this template.
317 #define ITK_TEMPLATE_MetaDataObject(_, EXPORT, TypeX, TypeY) \
318  namespace itk \
319  { \
320  _( 1 ( class EXPORT MetaDataObject< ITK_TEMPLATE_1 TypeX > ) ) \
321  namespace Templates \
322  { \
323  typedef MetaDataObject< ITK_TEMPLATE_1 TypeX > \
324  MetaDataObject##TypeY; \
325  } \
326  }
327 
328 #if ITK_TEMPLATE_EXPLICIT
329 #include "Templates/itkMetaDataObject+-.h"
330 #endif
331 
332 #if ITK_TEMPLATE_TXX
333 #include "itkMetaDataObject.hxx"
334 #endif
335 
336 #endif //itkMetaDataObject_h
337