Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 
18 #include "mitkVectorProperty.h"
19 
20 #include <mitkLexicalCast.h>
21 
22 namespace mitk
23 {
43  template <typename DATATYPE>
45  {
46  public:
47  // Expand manually most of mitkClassMacro:
48  // mitkClassMacro(VectorProperty<DATATYPE>, mitk::BaseProperty);
49  // This manual expansion is done to override explicitely
50  // the GetNameOfClass methods
56  std::vector<std::string> GetClassHierarchy() const override { return mitk::GetClassHierarchy<Self>(); }
57  // This function must return different
58  // strings in function of the template parameter!
59  // Serialization depends on this feature.
60  static const char *GetStaticNameOfClass()
61  {
62  // concatenate a prefix dependent on the template type and our own classname
63  static std::string nameOfClass =
64  std::string(VectorPropertyDataType<DATATYPE>::prefix()) + "VectorPropertySerializer";
65  return nameOfClass.c_str();
66  }
67 
68  const char *GetNameOfClass() const override { return this->GetStaticNameOfClass(); }
69  itkFactorylessNewMacro(Self);
70  itkCloneMacro(Self);
71 
73  TiXmlElement *Serialize() override
74  {
75  auto listElement = new TiXmlElement("Values");
76 
77  if (const PropertyType *prop = dynamic_cast<const PropertyType *>(m_Property.GetPointer()))
78  {
79  typename PropertyType::VectorType elements = prop->GetValue();
80  unsigned int index(0);
81  for (auto listEntry : elements)
82  {
83  std::stringstream indexS;
84  indexS << index++;
85 
86  auto entryElement = new TiXmlElement("Value");
87  entryElement->SetAttribute("idx", indexS.str());
88  entryElement->SetAttribute("value", boost::lexical_cast<std::string>(listEntry));
89  listElement->LinkEndChild(entryElement);
90  }
91 
92  return listElement;
93  }
94  else
95  {
96  return nullptr;
97  }
98  }
99 
101  BaseProperty::Pointer Deserialize(TiXmlElement *listElement) override
102  {
103  typename PropertyType::VectorType datalist;
104 
105  if (listElement)
106  {
107  MITK_DEBUG << "Deserializing " << *listElement;
108 
109  unsigned int index(0);
110  std::string valueString;
111  DATATYPE value;
112  for (TiXmlElement *valueElement = listElement->FirstChildElement("Value"); valueElement;
113  valueElement = valueElement->NextSiblingElement("Value"))
114  {
115  if (valueElement->QueryValueAttribute("value", &valueString) != TIXML_SUCCESS)
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.
#define MITK_ERROR
Definition: mitkLogMacros.h:20
itk::SmartPointer< const Self > ConstPointer
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
DataCollection - Class to facilitate loading/accessing structured data.
VectorPropertySerializer< DATATYPE > Self
TiXmlElement * Serialize() override
Build an XML version of this property.
std::vector< std::string > GetClassHierarchy() const override
const char * GetNameOfClass() const override
Target lexical_cast(const std::string &arg)
BaseProperty::Pointer Deserialize(TiXmlElement *listElement) override
Construct a property from an XML serialization.
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