Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkVectorPropertySerializer.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkVectorPropertySerializer_h
14 #define mitkVectorPropertySerializer_h
15 
17 #include <mitkVectorProperty.h>
18 #include <mitkLexicalCast.h>
19 #include <tinyxml2.h>
20 
21 namespace mitk
22 {
50  template <typename DATATYPE>
52  {
53  public:
54  // Expand manually most of mitkClassMacro:
55  // mitkClassMacro(VectorProperty<DATATYPE>, mitk::BaseProperty);
56  // This manual expansion is done to override explicitly
57  // the GetNameOfClass methods
63  std::vector<std::string> GetClassHierarchy() const override { return mitk::GetClassHierarchy<Self>(); }
64  // This function must return different
65  // strings in function of the template parameter!
66  // Serialization depends on this feature.
67  static const char *GetStaticNameOfClass()
68  {
69  // concatenate a prefix dependent on the template type and our own classname
70  static std::string nameOfClass =
71  std::string(VectorPropertyDataType<DATATYPE>::prefix()) + "VectorPropertySerializer";
72  return nameOfClass.c_str();
73  }
74 
75  const char *GetNameOfClass() const override { return this->GetStaticNameOfClass(); }
76  itkFactorylessNewMacro(Self);
77  itkCloneMacro(Self);
78 
90  tinyxml2::XMLElement* Serialize(tinyxml2::XMLDocument& doc) override
91  {
92  auto *listElement = doc.NewElement("Values");
93 
94  if (const PropertyType *prop = dynamic_cast<const PropertyType *>(m_Property.GetPointer()))
95  {
96  typename PropertyType::VectorType elements = prop->GetValue();
97  unsigned int index(0);
98  for (auto listEntry : elements)
99  {
100  std::stringstream indexS;
101  indexS << index++;
102 
103  auto *entryElement = doc.NewElement("Value");
104  entryElement->SetAttribute("idx", indexS.str().c_str());
105  entryElement->SetAttribute("value", boost::lexical_cast<std::string>(listEntry).c_str());
106  listElement->InsertEndChild(entryElement);
107  }
108 
109  return listElement;
110  }
111  else
112  {
113  return nullptr;
114  }
115  }
116 
128  BaseProperty::Pointer Deserialize(const tinyxml2::XMLElement *listElement) override
129  {
130  typename PropertyType::VectorType datalist;
131 
132  if (listElement)
133  {
134  std::string valueString;
135  DATATYPE value;
136  for (auto *valueElement = listElement->FirstChildElement("Value"); valueElement;
137  valueElement = valueElement->NextSiblingElement("Value"))
138  {
139  valueString = valueElement->Attribute("value");
140  if (valueString.empty())
141  {
142  MITK_ERROR << "Missing value attribute in <Values> list";
143  return nullptr;
144  }
145 
146  try
147  {
148  value = boost::lexical_cast<DATATYPE>(valueString);
149  }
150  catch (boost::bad_lexical_cast &e)
151  {
152  MITK_ERROR << "Could not parse '" << valueString << "' as number: " << e.what();
153  return nullptr;
154  }
155 
156  datalist.push_back(value);
157  }
158 
159  typename PropertyType::Pointer property = PropertyType::New();
160  property->SetValue(datalist);
161  return property.GetPointer();
162  }
163  else
164  {
165  MITK_ERROR << "Missing <Values> tag.";
166  }
167 
168  return nullptr;
169  }
170  };
171 
176 
177 } // namespace
178 
179 #endif
#define MITKSCENESERIALIZATIONBASE_EXPORT
Base class for objects that serialize and deserialize BaseProperty types to/from XML.
Serializes and deserializes VectorProperty instances to/from XML.
std::vector< std::string > GetClassHierarchy() const override
itk::SmartPointer< const Self > ConstPointer
BaseProperty::Pointer Deserialize(const tinyxml2::XMLElement *listElement) override
Deserializes an XML "Values" element back into a VectorProperty.
VectorPropertySerializer< DATATYPE > Self
const char * GetNameOfClass() const override
tinyxml2::XMLElement * Serialize(tinyxml2::XMLDocument &doc) override
Serializes the VectorProperty into an XML "Values" element.
Property for storing a std::vector of values.
std::vector< DATATYPE > VectorType
The type of the std::vector stored by this property.
Provides a robust lexical cast from strings to numeric types with a fallback for edge-case values.
#define MITK_ERROR
Log an error message.
Definition: mitkLog.h:372
Find image slices visible on a given plane.
VectorPropertySerializer< int > IntVectorPropertySerializer
Convenience typedef for a VectorPropertySerializer specialized for int values.
VectorPropertySerializer< double > DoubleVectorPropertySerializer
Convenience typedef for a VectorPropertySerializer specialized for double values.
Helper trait for VectorProperty to determine a good ITK class name prefix.