Medical Imaging Interaction Toolkit  2021.02.99-9ccbc4d1
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 {
42  template <typename DATATYPE>
44  {
45  public:
46  // Expand manually most of mitkClassMacro:
47  // mitkClassMacro(VectorProperty<DATATYPE>, mitk::BaseProperty);
48  // This manual expansion is done to override explicitely
49  // the GetNameOfClass methods
55  std::vector<std::string> GetClassHierarchy() const override { return mitk::GetClassHierarchy<Self>(); }
56  // This function must return different
57  // strings in function of the template parameter!
58  // Serialization depends on this feature.
59  static const char *GetStaticNameOfClass()
60  {
61  // concatenate a prefix dependent on the template type and our own classname
62  static std::string nameOfClass =
63  std::string(VectorPropertyDataType<DATATYPE>::prefix()) + "VectorPropertySerializer";
64  return nameOfClass.c_str();
65  }
66 
67  const char *GetNameOfClass() const override { return this->GetStaticNameOfClass(); }
68  itkFactorylessNewMacro(Self);
69  itkCloneMacro(Self);
70 
72  tinyxml2::XMLElement* Serialize(tinyxml2::XMLDocument& doc) override
73  {
74  auto *listElement = doc.NewElement("Values");
75 
76  if (const PropertyType *prop = dynamic_cast<const PropertyType *>(m_Property.GetPointer()))
77  {
78  typename PropertyType::VectorType elements = prop->GetValue();
79  unsigned int index(0);
80  for (auto listEntry : elements)
81  {
82  std::stringstream indexS;
83  indexS << index++;
84 
85  auto *entryElement = doc.NewElement("Value");
86  entryElement->SetAttribute("idx", indexS.str().c_str());
87  entryElement->SetAttribute("value", boost::lexical_cast<std::string>(listEntry).c_str());
88  listElement->InsertEndChild(entryElement);
89  }
90 
91  return listElement;
92  }
93  else
94  {
95  return nullptr;
96  }
97  }
98 
100  BaseProperty::Pointer Deserialize(const tinyxml2::XMLElement *listElement) override
101  {
102  typename PropertyType::VectorType datalist;
103 
104  if (listElement)
105  {
106  MITK_DEBUG << "Deserializing " << *listElement;
107 
108  unsigned int index(0);
109  std::string valueString;
110  DATATYPE value;
111  for (auto *valueElement = listElement->FirstChildElement("Value"); valueElement;
112  valueElement = valueElement->NextSiblingElement("Value"))
113  {
114  valueString = valueElement->Attribute("value");
115  if (valueString.empty())
116  {
117  MITK_ERROR << "Missing value attribute in <Values> list";
118  return nullptr;
119  }
120 
121  try
122  {
123  value = boost::lexical_cast<DATATYPE>(valueString);
124  }
125  catch (boost::bad_lexical_cast &e)
126  {
127  MITK_ERROR << "Could not parse '" << valueString << "' as number: " << e.what();
128  return nullptr;
129  }
130 
131  datalist.push_back(value);
132  ++index;
133  }
134 
135  typename PropertyType::Pointer property = PropertyType::New();
136  property->SetValue(datalist);
137  return property.GetPointer();
138  }
139  else
140  {
141  MITK_ERROR << "Missing <Values> tag.";
142  }
143 
144  return nullptr;
145  }
146  };
147 
150 
151 } // namespace
152 
153 #endif
Helper for VectorProperty to determine a good ITK ClassName.
BaseProperty::Pointer Deserialize(const tinyxml2::XMLElement *listElement) override
Construct a property from an XML serialization.
#define MITK_ERROR
Definition: mitkLogMacros.h:20
itk::SmartPointer< const Self > ConstPointer
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
tinyxml2::XMLElement * Serialize(tinyxml2::XMLDocument &doc) override
Build an XML version of this property.
DataCollection - Class to facilitate loading/accessing structured data.
VectorPropertySerializer< DATATYPE > Self
std::vector< std::string > GetClassHierarchy() const override
const char * GetNameOfClass() const override
Target lexical_cast(const std::string &arg)
VectorPropertySerializer< double > DoubleVectorPropertySerializer
Base class for objects that serialize BaseProperty types.
std::vector< DATATYPE > VectorType
Providing a std::vector as property.
#define MITKSCENESERIALIZATIONBASE_EXPORT
VectorPropertySerializer< int > IntVectorPropertySerializer