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
32#include "itkMacro.h"
33#include "itkArray.h"
34#include "itkMatrix.h"
35
36#include <cstring>
37
38namespace itk
39{
74template <typename MetaDataObjectType>
75class ITK_TEMPLATE_EXPORT MetaDataObject : public MetaDataObjectBase
76{
77public:
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 &
109
115 const MetaDataObjectType &
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
147protected:
148 MetaDataObject() = default;
149 ~MetaDataObject() override = default;
150
151private:
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
223template <typename T>
224inline void
225EncapsulateMetaData(MetaDataDictionary & Dictionary, const std::string & key, const T & invalue)
226{
228 temp->SetMetaDataObjectValue(invalue);
229 Dictionary[key] = temp;
230}
233template <typename T>
234inline void
235EncapsulateMetaData(MetaDataDictionary & Dictionary, const char * key, const T & invalue)
236{
237 EncapsulateMetaData(Dictionary, std::string(key), invalue);
238}
239
249template <typename T>
250inline bool
251ExposeMetaData(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
341namespace itk
342{
343
344ITK_GCC_PRAGMA_DIAG_PUSH()
345ITK_GCC_PRAGMA_DIAG(ignored "-Wattributes")
346
347extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<bool>;
348extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned char>;
349extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<char>;
350extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<signed char>;
351extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned short>;
352extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<short>;
353extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned int>;
354extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<int>;
355extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned long>;
356extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<long>;
357extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned long long>;
358extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<long long>;
359extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<float>;
360extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<double>;
361extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::string>;
362extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<float>>;
363extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<double>>;
364extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<std::vector<float>>>;
365extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<std::vector<double>>>;
366extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<char>>;
367extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<int>>;
368extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<float>>;
369extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<double>>;
370extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Matrix<float, 4, 4>>;
371extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Matrix<double>>;
372
373ITK_GCC_PRAGMA_DIAG_POP()
374
375} // end namespace itk
376#undef ITKCommon_EXPORT_EXPLICIT
377#endif
Light weight base class for most itk classes.
Provides a mechanism for storing a collection of arbitrary data types.
Iterator Find(const std::string &key)
The common interface for MetaDataObject's.
Allows arbitrary data types to be stored as MetaDataObjectBase types, and to be stored in a MetaDataD...
friend bool operator==(const Self &lhs, const Self &rhs)
void Print(std::ostream &os) const override
MetaDataObject()=default
void SetMetaDataObjectValue(const MetaDataObjectType &newValue)
bool Equal(const MetaDataObjectBase &metaDataObjectBase) const override
friend bool operator!=(const Self &lhs, const Self &rhs)
static bool EqualValues(const TValue(&lhs)[VNumberOfElements], const TValue(&rhs)[VNumberOfElements])
const char * GetMetaDataObjectTypeName() const override
const MetaDataObjectType & GetMetaDataObjectValue() const
static Pointer New()
static auto Assign(TValue &target, const TValue &source) -> decltype(target=source)
static auto EqualValues(const TValue &lhs, const TValue &rhs) -> decltype(lhs==rhs)
~MetaDataObject() override=default
static void Assign(TValue(&target)[VNumberOfElements], const TValue(&source)[VNumberOfElements])
const std::type_info & GetMetaDataObjectTypeInfo() const override
#define ITKCommon_EXPORT_EXPLICIT
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void EncapsulateMetaData(MetaDataDictionary &Dictionary, const std::string &key, const T &invalue)
bool ExposeMetaData(const MetaDataDictionary &Dictionary, const std::string key, T &outval)