Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef mitkVectorPropertySerializer_h
18 #define mitkVectorPropertySerializer_h
19 
21 
22 #include "mitkVectorProperty.h"
23 
24 #include <boost/lexical_cast.hpp>
25 
26 namespace mitk
27 {
47  template <typename DATATYPE>
49  {
50  public:
51  // Expand manually most of mitkClassMacro:
52  // mitkClassMacro(VectorProperty<DATATYPE>, mitk::BaseProperty);
53  // This manual expansion is done to override explicitely
54  // the GetNameOfClass methods
60  virtual std::vector<std::string> GetClassHierarchy() const override { return mitk::GetClassHierarchy<Self>(); }
61  // This function must return different
62  // strings in function of the template parameter!
63  // Serialization depends on this feature.
64  static const char *GetStaticNameOfClass()
65  {
66  // concatenate a prefix dependent on the template type and our own classname
67  static std::string nameOfClass =
68  std::string(VectorPropertyDataType<DATATYPE>::prefix()) + "VectorPropertySerializer";
69  return nameOfClass.c_str();
70  }
71 
72  virtual const char *GetNameOfClass() const override { return this->GetStaticNameOfClass(); }
73  itkFactorylessNewMacro(Self);
74  itkCloneMacro(Self)
75 
76 
77  virtual TiXmlElement *Serialize() override
78  {
79  auto listElement = new TiXmlElement("Values");
80 
81  if (const PropertyType *prop = dynamic_cast<const PropertyType *>(m_Property.GetPointer()))
82  {
83  typename PropertyType::VectorType elements = prop->GetValue();
84  unsigned int index(0);
85  for (auto listEntry : elements)
86  {
87  std::stringstream indexS;
88  indexS << index++;
89 
90  auto entryElement = new TiXmlElement("Value");
91  entryElement->SetAttribute("idx", indexS.str());
92  entryElement->SetAttribute("value", boost::lexical_cast<std::string>(listEntry));
93  listElement->LinkEndChild(entryElement);
94  }
95 
96  return listElement;
97  }
98  else
99  {
100  return nullptr;
101  }
102  }
103 
105  virtual BaseProperty::Pointer Deserialize(TiXmlElement *listElement) override
106  {
107  typename PropertyType::VectorType datalist;
108 
109  if (listElement)
110  {
111  MITK_DEBUG << "Deserializing " << *listElement;
112 
113  unsigned int index(0);
114  std::string valueString;
115  DATATYPE value;
116  for (TiXmlElement *valueElement = listElement->FirstChildElement("Value"); valueElement;
117  valueElement = valueElement->NextSiblingElement("Value"))
118  {
119  if (valueElement->QueryValueAttribute("value", &valueString) != TIXML_SUCCESS)
120  {
121  MITK_ERROR << "Missing value attribute in <Values> list";
122  return nullptr;
123  }
124 
125  try
126  {
127  value = boost::lexical_cast<DATATYPE>(valueString);
128  }
129  catch (boost::bad_lexical_cast &e)
130  {
131  MITK_ERROR << "Could not parse '" << valueString << "' as number: " << e.what();
132  return nullptr;
133  }
134 
135  datalist.push_back(value);
136  ++index;
137  }
138 
139  typename PropertyType::Pointer property = PropertyType::New();
140  property->SetValue(datalist);
141  return property.GetPointer();
142  }
143  else
144  {
145  MITK_ERROR << "Missing <Values> tag.";
146  }
147 
148  return nullptr;
149  }
150  };
151 
154 
155 } // namespace
156 
157 #endif
Helper for VectorProperty to determine a good ITK ClassName.
#define MITK_ERROR
Definition: mitkLogMacros.h:24
itk::SmartPointer< const Self > ConstPointer
virtual const char * GetNameOfClass() const override
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
DataCollection - Class to facilitate loading/accessing structured data.
VectorPropertySerializer< DATATYPE > Self
VectorPropertySerializer< double > DoubleVectorPropertySerializer
static const char * GetStaticNameOfClass()
virtual BaseProperty::Pointer Deserialize(TiXmlElement *listElement) override
Construct a property from an XML serialization.
Base class for objects that serialize BaseProperty types.
std::vector< DATATYPE > VectorType
Providing a std::vector as property.
#define MITKSCENESERIALIZATIONBASE_EXPORT
VectorPropertySerializer< int > IntVectorPropertySerializer
virtual std::vector< std::string > GetClassHierarchy() const override
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.