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
mitkFloatPropertySerializer.cpp
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 mitkFloatPropertySerializer_h_included
18 #define mitkFloatPropertySerializer_h_included
19 
21 
22 #include "mitkProperties.h"
23 #include "mitkStringsToNumbers.h"
24 #include <mitkLocaleSwitch.h>
25 
26 namespace mitk
27 {
28  class FloatPropertySerializer : public BasePropertySerializer
29  {
30  public:
31  mitkClassMacro(FloatPropertySerializer, BasePropertySerializer);
32  itkFactorylessNewMacro(Self) itkCloneMacro(Self)
33 
34  virtual TiXmlElement *Serialize() override
35  {
36  if (const FloatProperty *prop = dynamic_cast<const FloatProperty *>(m_Property.GetPointer()))
37  {
38  LocaleSwitch localeSwitch("C");
39 
40  auto element = new TiXmlElement("float");
41  element->SetAttribute("value", boost::lexical_cast<std::string>(prop->GetValue()));
42  return element;
43  }
44  else
45  return nullptr;
46  }
47 
48  virtual BaseProperty::Pointer Deserialize(TiXmlElement *element) override
49  {
50  if (!element)
51  return nullptr;
52 
53  LocaleSwitch localeSwitch("C");
54 
55  std::string f_string;
56  if (element->QueryStringAttribute("value", &f_string) == TIXML_SUCCESS)
57  {
58  try
59  {
60  return FloatProperty::New(boost::lexical_cast<float>(f_string)).GetPointer();
61  }
62  catch (boost::bad_lexical_cast &e)
63  {
64  MITK_ERROR << "Could not parse string as number: " << e.what();
65  return nullptr;
66  }
67  }
68  else
69  {
70  return nullptr;
71  }
72  }
73 
74  protected:
75  FloatPropertySerializer() {}
76  virtual ~FloatPropertySerializer() {}
77  };
78 
79 } // namespace
80 
81 // important to put this into the GLOBAL namespace (because it starts with 'namespace mitk')
82 MITK_REGISTER_SERIALIZER(FloatPropertySerializer);
83 
84 #endif
#define MITK_ERROR
Definition: mitkLogMacros.h:24
DataCollection - Class to facilitate loading/accessing structured data.
itk::SmartPointer< Self > Pointer
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:44
static Pointer New()
MITK_REGISTER_SERIALIZER(FloatPropertySerializer)