Medical Imaging Interaction Toolkit  2024.06.99-60d9b802
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 explicitly
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  std::string valueString;
107  DATATYPE value;
108  for (auto *valueElement = listElement->FirstChildElement("Value"); valueElement;
109  valueElement = valueElement->NextSiblingElement("Value"))
110  {
111  valueString = valueElement->Attribute("value");
112  if (valueString.empty())
113  {
114  MITK_ERROR << "Missing value attribute in <Values> list";
115  return nullptr;
116  }
117 
118  try
119  {
120  value = boost::lexical_cast<DATATYPE>(valueString);
121  }
122  catch (boost::bad_lexical_cast &e)
123  {
124  MITK_ERROR << "Could not parse '" << valueString << "' as number: " << e.what();
125  return nullptr;
126  }
127 
128  datalist.push_back(value);
129  }
130 
131  typename PropertyType::Pointer property = PropertyType::New();
132  property->SetValue(datalist);
133  return property.GetPointer();
134  }
135  else
136  {
137  MITK_ERROR << "Missing <Values> tag.";
138  }
139 
140  return nullptr;
141  }
142  };
143 
146 
147 } // namespace
148 
149 #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:145
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:144
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