ITK  5.3.0
Insight Toolkit
itkMetaDataObject.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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  * https://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 "itkArray.h"
34 #include "itkMatrix.h"
35 
36 #include <cstring>
37 
38 namespace itk
39 {
74 template <typename MetaDataObjectType>
75 class ITK_TEMPLATE_EXPORT MetaDataObject : public MetaDataObjectBase
76 {
77 public:
78  ITK_DISALLOW_COPY_AND_MOVE(MetaDataObject);
79 
85 
87  itkFactorylessNewMacro(Self);
88 
90  itkTypeMacro(MetaDataObject, MetaDataObjectBase);
91 
98  const char *
99  GetMetaDataObjectTypeName() const override;
100 
107  const std::type_info &
108  GetMetaDataObjectTypeInfo() const override;
109 
115  const MetaDataObjectType &
116  GetMetaDataObjectValue() const;
117 
123  void
124  SetMetaDataObjectValue(const MetaDataObjectType & newValue);
125 
130  void
131  Print(std::ostream & os) const override;
132 
134  friend bool
135  operator==(const Self & lhs, const Self & rhs)
136  {
137  return Self::EqualValues(lhs.m_MetaDataObjectValue, rhs.m_MetaDataObjectValue);
138  }
139 
141  friend bool
142  operator!=(const Self & lhs, const Self & rhs)
143  {
144  return !(lhs == rhs);
145  }
146 
147 protected:
148  MetaDataObject() = default;
149  ~MetaDataObject() override = default;
150 
151 private:
154  template <typename TValue>
155  static auto
156  Assign(TValue & target, const TValue & source) -> decltype(target = source)
157  {
158  return target = source;
159  }
160 
162  template <typename TValue, size_t VNumberOfElements>
163  static void
164  Assign(TValue (&target)[VNumberOfElements], const TValue (&source)[VNumberOfElements])
165  {
166  for (size_t i = 0; i < VNumberOfElements; ++i)
167  {
168  Self::Assign(target[i], source[i]);
169  }
170  }
176  template <typename TValue>
177  static auto
178  EqualValues(const TValue & lhs, const TValue & rhs) -> decltype(lhs == rhs)
179  {
180  return lhs == rhs;
181  }
182 
184  template <typename TValue, size_t VNumberOfElements>
185  static bool
186  EqualValues(const TValue (&lhs)[VNumberOfElements], const TValue (&rhs)[VNumberOfElements])
187  {
188  for (size_t i = 0; i < VNumberOfElements; ++i)
189  {
190  if (!Self::EqualValues(lhs[i], rhs[i]))
191  {
192  return false;
193  }
194  }
195  return true;
196  }
201  bool
202  Equal(const MetaDataObjectBase & metaDataObjectBase) const override
203  {
204  const auto metaDataObject = dynamic_cast<const Self *>(&metaDataObjectBase);
205  return (metaDataObject != nullptr) && (*this == *metaDataObject);
206  }
213  MetaDataObjectType m_MetaDataObjectValue;
214 };
215 
225 template <typename T>
226 inline void
227 EncapsulateMetaData(MetaDataDictionary & Dictionary, const std::string & key, const T & invalue)
228 {
230  temp->SetMetaDataObjectValue(invalue);
231  Dictionary[key] = temp;
232 }
235 template <typename T>
236 inline void
237 EncapsulateMetaData(MetaDataDictionary & Dictionary, const char * key, const T & invalue)
238 {
239  EncapsulateMetaData(Dictionary, std::string(key), invalue);
240 }
241 
251 template <typename T>
252 inline bool
253 ExposeMetaData(const MetaDataDictionary & Dictionary, const std::string key, T & outval)
254 {
255  auto keyIter = Dictionary.Find(key);
256  if (keyIter == Dictionary.End())
257  {
258  return false;
259  }
262  auto const * const TempMetaDataObject = dynamic_cast<MetaDataObject<T> const *>(keyIter->second.GetPointer());
263  if (TempMetaDataObject == nullptr)
264  {
265  return false;
266  }
267 
268  outval = TempMetaDataObject->GetMetaDataObjectValue();
269  return true;
270 }
271 
272 } // end namespace itk
273 
281 #define ITK_NATIVE_TYPE_METADATAPRINT(TYPE_NAME) \
282  template <> \
283  void itk::MetaDataObject<TYPE_NAME>::Print(std::ostream & os) const \
284  { \
285  os << this->m_MetaDataObjectValue << std::endl; \
286  }
287 
296 #define ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(TYPE_NAME_PART1, TYPE_NAME_PART2) \
297  template <> \
298  void itk::MetaDataObject<TYPE_NAME_PART1, TYPE_NAME_PART2>::Print(std::ostream & os) const \
299  { \
300  this->m_MetaDataObjectValue->Print(os); \
301  }
302 
310 #define ITK_IMAGE_TYPE_METADATAPRINT(STORAGE_TYPE) \
311  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 1>::Pointer) \
312  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 2>::Pointer) \
313  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 3>::Pointer) \
314  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 4>::Pointer) \
315  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 5>::Pointer) \
316  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 6>::Pointer) \
317  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 7>::Pointer) \
318  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 8>::Pointer)
319 
320 #ifndef ITK_MANUAL_INSTANTIATION
321 # include "itkMetaDataObject.hxx"
322 #endif
323 
324 #endif // itkMetaDataObject_h
325 
327 #ifndef ITK_TEMPLATE_EXPLICIT_MetaDataObject
328 // Explicit instantiation is required to ensure correct dynamic_cast
329 // behavior across shared libraries.
330 //
331 // IMPORTANT: Since within the same compilation unit,
332 // ITK_TEMPLATE_EXPLICIT_<classname> defined and undefined states
333 // need to be considered. This code *MUST* be *OUTSIDE* the header
334 // guards.
335 //
336 #if defined(ITKCommon_EXPORTS)
337 // We are building this library
338 # define ITKCommon_EXPORT_EXPLICIT ITK_TEMPLATE_EXPORT
339 #else
340 // We are using this library
341 # define ITKCommon_EXPORT_EXPLICIT ITKCommon_EXPORT
342 #endif
343 namespace itk
344 {
345 
346 ITK_GCC_PRAGMA_DIAG_PUSH()
347 ITK_GCC_PRAGMA_DIAG(ignored "-Wattributes")
348 
349 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<bool>;
350 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned char>;
351 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<char>;
352 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<signed char>;
353 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned short>;
354 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<short>;
355 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned int>;
356 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<int>;
357 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned long>;
358 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<long>;
359 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned long long>;
360 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<long long>;
361 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<float>;
362 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<double>;
363 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::string>;
364 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<float>>;
365 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<double>>;
366 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<std::vector<float>>>;
367 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<std::vector<double>>>;
368 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<char>>;
369 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<int>>;
370 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<float>>;
371 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<double>>;
372 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Matrix<float, 4, 4>>;
373 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Matrix<double>>;
374 
375 ITK_GCC_PRAGMA_DIAG_POP()
376 
377 } // end namespace itk
378 #undef ITKCommon_EXPORT_EXPLICIT
379 #endif
itk::MetaDataObject
Allows arbitrary data types to be stored as MetaDataObjectBase types, and to be stored in a MetaDataD...
Definition: itkMetaDataObject.h:75
itk::MetaDataObject::GetMetaDataObjectValue
const MetaDataObjectType & GetMetaDataObjectValue() const
itk::EncapsulateMetaData
void EncapsulateMetaData(MetaDataDictionary &Dictionary, const std::string &key, const T &invalue)
Definition: itkMetaDataObject.h:227
itkMatrix.h
itk::MetaDataObject::EqualValues
static bool EqualValues(const TValue(&lhs)[VNumberOfElements], const TValue(&rhs)[VNumberOfElements])
Definition: itkMetaDataObject.h:186
itk::MetaDataObjectBase
The common interface for MetaDataObject's.
Definition: itkMetaDataObjectBase.h:47
itk::MetaDataObject::operator==
friend bool operator==(const Self &lhs, const Self &rhs)
Definition: itkMetaDataObject.h:135
itk::MetaDataObject::operator!=
friend bool operator!=(const Self &lhs, const Self &rhs)
Definition: itkMetaDataObject.h:142
itk::SmartPointer< Self >
itk::MetaDataObject::m_MetaDataObjectValue
MetaDataObjectType m_MetaDataObjectValue
Definition: itkMetaDataObject.h:213
itk::MetaDataDictionary
Provides a mechanism for storing a collection of arbitrary data types.
Definition: itkMetaDataDictionary.h:54
itk::MetaDataDictionary::Find
Iterator Find(const std::string &key)
itkMacro.h
itk::ExposeMetaData
bool ExposeMetaData(const MetaDataDictionary &Dictionary, const std::string key, T &outval)
Definition: itkMetaDataObject.h:253
itk::MetaDataObject::Equal
bool Equal(const MetaDataObjectBase &metaDataObjectBase) const override
Definition: itkMetaDataObject.h:202
itkArray.h
itkMetaDataDictionary.h
itk::MetaDataDictionary::End
Iterator End()
ITKCommon_EXPORT_EXPLICIT
#define ITKCommon_EXPORT_EXPLICIT
Definition: itkMetaDataObject.h:341
itk::MetaDataObject::Assign
static void Assign(TValue(&target)[VNumberOfElements], const TValue(&source)[VNumberOfElements])
Definition: itkMetaDataObject.h:164
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::MetaDataObject::New
static Pointer New()
itk::MetaDataObject::EqualValues
static auto EqualValues(const TValue &lhs, const TValue &rhs) -> decltype(lhs==rhs)
Definition: itkMetaDataObject.h:178
itk::MetaDataObject::Assign
static auto Assign(TValue &target, const TValue &source) -> decltype(target=source)
Definition: itkMetaDataObject.h:156