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
32#include "itkMacro.h"
33#include "itkArray.h"
34#include "itkMatrix.h"
35
36#include <cstring>
37
38namespace itk
39{
67template <typename MetaDataObjectType>
68class ITK_TEMPLATE_EXPORT MetaDataObject : public MetaDataObjectBase
69{
70public:
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 &
102
108 const MetaDataObjectType &
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
140protected:
141 MetaDataObject() = default;
142 ~MetaDataObject() override = default;
143
144private:
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
216template <typename T>
217inline void
218EncapsulateMetaData(MetaDataDictionary & Dictionary, const std::string & key, const T & invalue)
219{
220 auto temp = MetaDataObject<T>::New();
221 temp->SetMetaDataObjectValue(invalue);
222 Dictionary[key] = temp;
223}
226template <typename T>
227inline void
228EncapsulateMetaData(MetaDataDictionary & Dictionary, const char * key, const T & invalue)
229{
230 EncapsulateMetaData(Dictionary, std::string(key), invalue);
231}
232
242template <typename T>
243inline bool
244ExposeMetaData(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
288namespace itk
289{
290
291ITK_GCC_PRAGMA_DIAG_PUSH()
292ITK_GCC_PRAGMA_DIAG(ignored "-Wattributes")
293
294extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<bool>;
295extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned char>;
296extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<char>;
297extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<signed char>;
298extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned short>;
299extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<short>;
300extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned int>;
301extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<int>;
302extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned long>;
303extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<long>;
304extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned long long>;
305extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<long long>;
306extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<float>;
307extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<double>;
308extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::string>;
309extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<float>>;
310extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<double>>;
311extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<std::vector<float>>>;
312extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<std::vector<double>>>;
313extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<char>>;
314extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<int>>;
315extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<float>>;
316extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<double>>;
317extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Matrix<float, 4, 4>>;
318extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Matrix<double>>;
319
320ITK_GCC_PRAGMA_DIAG_POP()
321
322} // end namespace itk
323#undef ITKCommon_EXPORT_EXPLICIT
324#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)