ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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 {
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 }
164
165
168 template <typename TValue>
169 static auto
170 EqualValues(const TValue & lhs, const TValue & rhs) -> decltype(lhs == rhs)
171 {
172 return lhs == rhs;
173 }
174
176 template <typename TValue, size_t VNumberOfElements>
177 static bool
178 EqualValues(const TValue (&lhs)[VNumberOfElements], const TValue (&rhs)[VNumberOfElements])
179 {
180 for (size_t i = 0; i < VNumberOfElements; ++i)
181 {
182 if (!Self::EqualValues(lhs[i], rhs[i]))
183 {
184 return false;
185 }
186 }
187 return true;
188 }
189
190
192 bool
193 Equal(const MetaDataObjectBase & metaDataObjectBase) const override
194 {
195 const auto metaDataObject = dynamic_cast<const Self *>(&metaDataObjectBase);
196 return (metaDataObject != nullptr) && (*this == *metaDataObject);
197 }
198
203 MetaDataObjectType m_MetaDataObjectValue{};
204};
205
213template <typename T>
214inline void
215EncapsulateMetaData(MetaDataDictionary & Dictionary, const std::string & key, const T & invalue)
216{
217 auto temp = MetaDataObject<T>::New();
218 temp->SetMetaDataObjectValue(invalue);
219 Dictionary[key] = temp;
220}
221
222template <typename T>
223inline void
224EncapsulateMetaData(MetaDataDictionary & Dictionary, const char * key, const T & invalue)
225{
226 EncapsulateMetaData(Dictionary, std::string(key), invalue);
227}
228
238template <typename T>
239inline bool
240ExposeMetaData(const MetaDataDictionary & Dictionary, const std::string key, T & outval)
241{
242 auto keyIter = Dictionary.Find(key);
243 if (keyIter == Dictionary.End())
244 {
245 return false;
246 }
247
248 const auto * const TempMetaDataObject = dynamic_cast<const MetaDataObject<T> *>(keyIter->second.GetPointer());
249 if (TempMetaDataObject == nullptr)
250 {
251 return false;
252 }
253
254 outval = TempMetaDataObject->GetMetaDataObjectValue();
255 return true;
256}
257
258} // end namespace itk
259
260#ifndef ITK_MANUAL_INSTANTIATION
261# include "itkMetaDataObject.hxx"
262#endif
263
264#endif // itkMetaDataObject_h
265
267#ifndef ITK_TEMPLATE_EXPLICIT_MetaDataObject
268// Explicit instantiation is required to ensure correct dynamic_cast
269// behavior across shared libraries.
270//
271// IMPORTANT: Since within the same compilation unit,
272// ITK_TEMPLATE_EXPLICIT_<classname> defined and undefined states
273// need to be considered. This code *MUST* be *OUTSIDE* the header
274// guards.
275//
276#if defined(ITKCommon_EXPORTS)
277// We are building this library
278# define ITKCommon_EXPORT_EXPLICIT ITK_TEMPLATE_EXPORT
279#else
280// We are using this library
281# define ITKCommon_EXPORT_EXPLICIT ITKCommon_EXPORT
282#endif
283namespace itk
284{
285
286ITK_GCC_PRAGMA_DIAG_PUSH()
287ITK_GCC_PRAGMA_DIAG(ignored "-Wattributes")
288
289extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<bool>;
290extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned char>;
291extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<char>;
292extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<signed char>;
293extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned short>;
294extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<short>;
295extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned int>;
296extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<int>;
297extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned long>;
298extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<long>;
299extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<unsigned long long>;
300extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<long long>;
301extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<float>;
302extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<double>;
303extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::string>;
304extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<float>>;
305extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<double>>;
306extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<std::vector<float>>>;
307extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<std::vector<std::vector<double>>>;
308extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<char>>;
309extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<int>>;
310extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<float>>;
311extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Array<double>>;
312extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Matrix<float, 4, 4>>;
313extern template class ITKCommon_EXPORT_EXPLICIT MetaDataObject<Matrix<double>>;
314
315ITK_GCC_PRAGMA_DIAG_POP()
316
317} // end namespace itk
318#undef ITKCommon_EXPORT_EXPLICIT
319#endif
Provides a mechanism for storing a collection of arbitrary data types.
Iterator Find(const std::string &key)
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)
MetaDataObjectType m_MetaDataObjectValue
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
SmartPointer< Self > Pointer
MetaDataObjectBase Superclass
static void Assign(TValue(&target)[VNumberOfElements], const TValue(&source)[VNumberOfElements])
SmartPointer< const Self > ConstPointer
const std::type_info & GetMetaDataObjectTypeInfo() const override
Implements transparent reference counting.
#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)