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