ITK  5.4.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  itkOverrideGetNameOfClassMacro(MetaDataObject);
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 
223 template <typename T>
224 inline void
225 EncapsulateMetaData(MetaDataDictionary & Dictionary, const std::string & key, const T & invalue)
226 {
228  temp->SetMetaDataObjectValue(invalue);
229  Dictionary[key] = temp;
230 }
233 template <typename T>
234 inline void
235 EncapsulateMetaData(MetaDataDictionary & Dictionary, const char * key, const T & invalue)
236 {
237  EncapsulateMetaData(Dictionary, std::string(key), invalue);
238 }
239 
249 template <typename T>
250 inline bool
251 ExposeMetaData(const MetaDataDictionary & Dictionary, const std::string key, T & outval)
252 {
253  auto keyIter = Dictionary.Find(key);
254  if (keyIter == Dictionary.End())
255  {
256  return false;
257  }
260  auto const * const TempMetaDataObject = dynamic_cast<MetaDataObject<T> const *>(keyIter->second.GetPointer());
261  if (TempMetaDataObject == nullptr)
262  {
263  return false;
264  }
265 
266  outval = TempMetaDataObject->GetMetaDataObjectValue();
267  return true;
268 }
269 
270 } // end namespace itk
271 
279 #define ITK_NATIVE_TYPE_METADATAPRINT(TYPE_NAME) \
280  template <> \
281  void itk::MetaDataObject<TYPE_NAME>::Print(std::ostream & os) const \
282  { \
283  os << this->m_MetaDataObjectValue << std::endl; \
284  }
285 
294 #define ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(TYPE_NAME_PART1, TYPE_NAME_PART2) \
295  template <> \
296  void itk::MetaDataObject<TYPE_NAME_PART1, TYPE_NAME_PART2>::Print(std::ostream & os) const \
297  { \
298  this->m_MetaDataObjectValue->Print(os); \
299  }
300 
308 #define ITK_IMAGE_TYPE_METADATAPRINT(STORAGE_TYPE) \
309  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 1>::Pointer) \
310  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 2>::Pointer) \
311  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 3>::Pointer) \
312  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 4>::Pointer) \
313  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 5>::Pointer) \
314  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 6>::Pointer) \
315  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 7>::Pointer) \
316  ITK_OBJECT_TYPE_METADATAPRINT_1COMMA(itk::Image<STORAGE_TYPE, 8>::Pointer)
317 
318 #ifndef ITK_MANUAL_INSTANTIATION
319 # include "itkMetaDataObject.hxx"
320 #endif
321 
322 #endif // itkMetaDataObject_h
323 
325 #ifndef ITK_TEMPLATE_EXPLICIT_MetaDataObject
326 // Explicit instantiation is required to ensure correct dynamic_cast
327 // behavior across shared libraries.
328 //
329 // IMPORTANT: Since within the same compilation unit,
330 // ITK_TEMPLATE_EXPLICIT_<classname> defined and undefined states
331 // need to be considered. This code *MUST* be *OUTSIDE* the header
332 // guards.
333 //
334 #if defined(ITKCommon_EXPORTS)
335 // We are building this library
336 # define ITKCommon_EXPORT_EXPLICIT ITK_TEMPLATE_EXPORT
337 #else
338 // We are using this library
339 # define ITKCommon_EXPORT_EXPLICIT ITKCommon_EXPORT
340 #endif
341 namespace itk
342 {
343 
344 ITK_GCC_PRAGMA_DIAG_PUSH()
345 ITK_GCC_PRAGMA_DIAG(ignored "-Wattributes")
346 
347 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<bool>;
348 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned char>;
349 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<char>;
350 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<signed char>;
351 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned short>;
352 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<short>;
353 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned int>;
354 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<int>;
355 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned long>;
356 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<long>;
357 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned long long>;
358 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<long long>;
359 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<float>;
360 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<double>;
361 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::string>;
362 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<float>>;
363 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<double>>;
364 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<std::vector<float>>>;
365 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<std::vector<double>>>;
366 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<char>>;
367 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<int>>;
368 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<float>>;
369 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<double>>;
370 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Matrix<float, 4, 4>>;
371 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Matrix<double>>;
372 
373 ITK_GCC_PRAGMA_DIAG_POP()
374 
375 } // end namespace itk
376 #undef ITKCommon_EXPORT_EXPLICIT
377 #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:225
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:251
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:339
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