ITK  6.0.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 {
67 template <typename MetaDataObjectType>
68 class ITK_TEMPLATE_EXPORT MetaDataObject : public MetaDataObjectBase
69 {
70 public:
71  ITK_DISALLOW_COPY_AND_MOVE(MetaDataObject);
72 
78 
80  itkFactorylessNewMacro(Self);
81 
83  itkOverrideGetNameOfClassMacro(MetaDataObject);
84 
91  const char *
92  GetMetaDataObjectTypeName() const override;
93 
100  const std::type_info &
101  GetMetaDataObjectTypeInfo() const override;
102 
108  const MetaDataObjectType &
109  GetMetaDataObjectValue() const;
110 
116  void
117  SetMetaDataObjectValue(const MetaDataObjectType & newValue);
118 
123  void
124  Print(std::ostream & os) const override;
125 
127  friend bool
128  operator==(const Self & lhs, const Self & rhs)
129  {
130  return Self::EqualValues(lhs.m_MetaDataObjectValue, rhs.m_MetaDataObjectValue);
131  }
132 
134  friend bool
135  operator!=(const Self & lhs, const Self & rhs)
136  {
137  return !(lhs == rhs);
138  }
139 
140 protected:
141  MetaDataObject() = default;
142  ~MetaDataObject() override = default;
143 
144 private:
147  template <typename TValue>
148  static auto
149  Assign(TValue & target, const TValue & source) -> decltype(target = source)
150  {
151  return target = source;
152  }
153 
155  template <typename TValue, size_t VNumberOfElements>
156  static void
157  Assign(TValue (&target)[VNumberOfElements], const TValue (&source)[VNumberOfElements])
158  {
159  for (size_t i = 0; i < VNumberOfElements; ++i)
160  {
161  Self::Assign(target[i], source[i]);
162  }
163  }
169  template <typename TValue>
170  static auto
171  EqualValues(const TValue & lhs, const TValue & rhs) -> decltype(lhs == rhs)
172  {
173  return lhs == rhs;
174  }
175 
177  template <typename TValue, size_t VNumberOfElements>
178  static bool
179  EqualValues(const TValue (&lhs)[VNumberOfElements], const TValue (&rhs)[VNumberOfElements])
180  {
181  for (size_t i = 0; i < VNumberOfElements; ++i)
182  {
183  if (!Self::EqualValues(lhs[i], rhs[i]))
184  {
185  return false;
186  }
187  }
188  return true;
189  }
194  bool
195  Equal(const MetaDataObjectBase & metaDataObjectBase) const override
196  {
197  const auto metaDataObject = dynamic_cast<const Self *>(&metaDataObjectBase);
198  return (metaDataObject != nullptr) && (*this == *metaDataObject);
199  }
206  MetaDataObjectType m_MetaDataObjectValue{};
207 };
208 
216 template <typename T>
217 inline void
218 EncapsulateMetaData(MetaDataDictionary & Dictionary, const std::string & key, const T & invalue)
219 {
221  temp->SetMetaDataObjectValue(invalue);
222  Dictionary[key] = temp;
223 }
226 template <typename T>
227 inline void
228 EncapsulateMetaData(MetaDataDictionary & Dictionary, const char * key, const T & invalue)
229 {
230  EncapsulateMetaData(Dictionary, std::string(key), invalue);
231 }
232 
242 template <typename T>
243 inline bool
244 ExposeMetaData(const MetaDataDictionary & Dictionary, const std::string key, T & outval)
245 {
246  auto keyIter = Dictionary.Find(key);
247  if (keyIter == Dictionary.End())
248  {
249  return false;
250  }
253  auto const * const TempMetaDataObject = dynamic_cast<MetaDataObject<T> const *>(keyIter->second.GetPointer());
254  if (TempMetaDataObject == nullptr)
255  {
256  return false;
257  }
258 
259  outval = TempMetaDataObject->GetMetaDataObjectValue();
260  return true;
261 }
262 
263 } // end namespace itk
264 
265 #ifndef ITK_MANUAL_INSTANTIATION
266 # include "itkMetaDataObject.hxx"
267 #endif
268 
269 #endif // itkMetaDataObject_h
270 
272 #ifndef ITK_TEMPLATE_EXPLICIT_MetaDataObject
273 // Explicit instantiation is required to ensure correct dynamic_cast
274 // behavior across shared libraries.
275 //
276 // IMPORTANT: Since within the same compilation unit,
277 // ITK_TEMPLATE_EXPLICIT_<classname> defined and undefined states
278 // need to be considered. This code *MUST* be *OUTSIDE* the header
279 // guards.
280 //
281 #if defined(ITKCommon_EXPORTS)
282 // We are building this library
283 # define ITKCommon_EXPORT_EXPLICIT ITK_TEMPLATE_EXPORT
284 #else
285 // We are using this library
286 # define ITKCommon_EXPORT_EXPLICIT ITKCommon_EXPORT
287 #endif
288 namespace itk
289 {
290 
291 ITK_GCC_PRAGMA_DIAG_PUSH()
292 ITK_GCC_PRAGMA_DIAG(ignored "-Wattributes")
293 
294 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<bool>;
295 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned char>;
296 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<char>;
297 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<signed char>;
298 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned short>;
299 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<short>;
300 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned int>;
301 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<int>;
302 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned long>;
303 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<long>;
304 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned long long>;
305 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<long long>;
306 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<float>;
307 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<double>;
308 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::string>;
309 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<float>>;
310 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<double>>;
311 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<std::vector<float>>>;
312 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<std::vector<double>>>;
313 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<char>>;
314 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<int>>;
315 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<float>>;
316 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<double>>;
317 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Matrix<float, 4, 4>>;
318 extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Matrix<double>>;
319 
320 ITK_GCC_PRAGMA_DIAG_POP()
321 
322 } // end namespace itk
323 #undef ITKCommon_EXPORT_EXPLICIT
324 #endif
itk::MetaDataObject
Allows arbitrary data types to be stored as MetaDataObjectBase types, and to be stored in a MetaDataD...
Definition: itkMetaDataObject.h:68
itk::MetaDataObject::GetMetaDataObjectValue
const MetaDataObjectType & GetMetaDataObjectValue() const
itk::EncapsulateMetaData
void EncapsulateMetaData(MetaDataDictionary &Dictionary, const std::string &key, const T &invalue)
Definition: itkMetaDataObject.h:218
itkMatrix.h
itk::MetaDataObject::EqualValues
static bool EqualValues(const TValue(&lhs)[VNumberOfElements], const TValue(&rhs)[VNumberOfElements])
Definition: itkMetaDataObject.h:179
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:128
itk::MetaDataObject::operator!=
friend bool operator!=(const Self &lhs, const Self &rhs)
Definition: itkMetaDataObject.h:135
itk::SmartPointer< Self >
itk::MetaDataDictionary
Provides a mechanism for storing a collection of arbitrary data types.
Definition: itkMetaDataDictionary.h:54
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
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:244
itk::MetaDataObject::Equal
bool Equal(const MetaDataObjectBase &metaDataObjectBase) const override
Definition: itkMetaDataObject.h:195
itkArray.h
itkMetaDataDictionary.h
itk::MetaDataDictionary::End
Iterator End()
ITKCommon_EXPORT_EXPLICIT
#define ITKCommon_EXPORT_EXPLICIT
Definition: itkMetaDataObject.h:286
itk::MetaDataObject::Assign
static void Assign(TValue(&target)[VNumberOfElements], const TValue(&source)[VNumberOfElements])
Definition: itkMetaDataObject.h:157
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:171
itk::MetaDataObject::Assign
static auto Assign(TValue &target, const TValue &source) -> decltype(target=source)
Definition: itkMetaDataObject.h:149