Medical Imaging Interaction Toolkit  2023.12.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 {
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  unsigned int index(0);
107  std::string valueString;
108  DATATYPE value;
109  for (auto *valueElement = listElement->FirstChildElement("Value"); valueElement;
110  valueElement = valueElement->NextSiblingElement("Value"))
111  {
112  valueString = valueElement->Attribute("value");
113  if (valueString.empty())
114  {
115  MITK_ERROR << "Missing value attribute in <Values> list";
116  return nullptr;
117  }
118 
119  try
120  {
121  value = boost::lexical_cast<DATATYPE>(valueString);
122  }
123  catch (boost::bad_lexical_cast &e)
124  {
125  MITK_ERROR << "Could not parse '" << valueString << "' as number: " << e.what();
126  return nullptr;
127  }
128 
129  datalist.push_back(value);
130  ++index;
131  }
132 
133  typename PropertyType::Pointer property = PropertyType::New();
134  property->SetValue(datalist);
135  return property.GetPointer();
136  }
137  else
138  {
139  MITK_ERROR << "Missing <Values> tag.";
140  }
141 
142  return nullptr;
143  }
144  };
145 
148 
149 } // namespace
150 
151 #endif
mitk::VectorPropertySerializer::Deserialize
BaseProperty::Pointer Deserialize(const tinyxml2::XMLElement *listElement) override
Construct a property from an XML serialization.
Definition: mitkVectorPropertySerializer.h:100
mitk::VectorPropertySerializer::GetStaticNameOfClass
static const char * GetStaticNameOfClass()
Definition: mitkVectorPropertySerializer.h:59
mitkBasePropertySerializer.h
mitk::VectorPropertySerializer
Serializes a VectorProperty.
Definition: mitkVectorPropertySerializer.h:43
mitk::IntVectorPropertySerializer
VectorPropertySerializer< int > IntVectorPropertySerializer
Definition: mitkVectorPropertySerializer.h:147
mitk::BasePropertySerializer
Base class for objects that serialize BaseProperty types.
Definition: mitkBasePropertySerializer.h:41
MITK_ERROR
#define MITK_ERROR
Definition: mitkLog.h:211
itk::SmartPointer< Self >
mitkLexicalCast.h
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::VectorPropertySerializer::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: mitkVectorPropertySerializer.h:54
mitk::VectorPropertyDataType
Helper for VectorProperty to determine a good ITK ClassName.
Definition: mitkVectorProperty.h:32
mitk::VectorPropertySerializer::GetNameOfClass
const char * GetNameOfClass() const override
Definition: mitkVectorPropertySerializer.h:67
mitk::VectorPropertySerializer::GetClassHierarchy
std::vector< std::string > GetClassHierarchy() const override
Definition: mitkVectorPropertySerializer.h:55
mitk::VectorPropertySerializer::PropertyType
VectorProperty< DATATYPE > PropertyType
Definition: mitkVectorPropertySerializer.h:50
mitk::DoubleVectorPropertySerializer
VectorPropertySerializer< double > DoubleVectorPropertySerializer
Definition: mitkVectorPropertySerializer.h:146
mitk::VectorPropertySerializer::Pointer
itk::SmartPointer< Self > Pointer
Definition: mitkVectorPropertySerializer.h:53
mitk::VectorProperty
Providing a std::vector as property.
Definition: mitkVectorProperty.h:52
mitk::VectorPropertySerializer::Serialize
tinyxml2::XMLElement * Serialize(tinyxml2::XMLDocument &doc) override
Build an XML version of this property.
Definition: mitkVectorPropertySerializer.h:72
mitk::VectorPropertySerializer::Self
VectorPropertySerializer< DATATYPE > Self
Definition: mitkVectorPropertySerializer.h:51
mitkVectorProperty.h
MITKSCENESERIALIZATIONBASE_EXPORT
#define MITKSCENESERIALIZATIONBASE_EXPORT
Definition: MitkSceneSerializationBaseExports.h:15
mitk::VectorPropertySerializer::SuperClass
BasePropertySerializer SuperClass
Definition: mitkVectorPropertySerializer.h:52
mitk::VectorProperty::VectorType
std::vector< DATATYPE > VectorType
Definition: mitkVectorProperty.h:55