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