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