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
mitkIntLookupTablePropertySerializer.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 mitkIntLookupTablePropertySerializer_h_included
18 #define mitkIntLookupTablePropertySerializer_h_included
19 
21 
22 #include "mitkProperties.h"
23 
24 namespace mitk
25 {
26  class IntLookupTablePropertySerializer : public BasePropertySerializer
27  {
28  public:
29  mitkClassMacro(IntLookupTablePropertySerializer, BasePropertySerializer);
30  itkFactorylessNewMacro(Self) itkCloneMacro(Self)
31 
32  virtual TiXmlElement *Serialize() override
33  {
34  const IntLookupTableProperty *prop = dynamic_cast<const IntLookupTableProperty *>(m_Property.GetPointer());
35  if (prop == nullptr)
36  return nullptr;
37  IntLookupTable lut = prop->GetValue();
38  // if (lut.IsNull())
39  // return NULL; // really?
40  const IntLookupTable::LookupTableType &map = lut.GetLookupTable();
41 
42  auto element = new TiXmlElement("IntLookupTableTable");
43  for (auto it = map.begin(); it != map.end(); ++it)
44  {
45  auto tableEntry = new TiXmlElement("LUTValue");
46  tableEntry->SetAttribute("id", it->first);
47  tableEntry->SetAttribute("value", it->second);
48  element->LinkEndChild(tableEntry);
49  }
50  return element;
51  }
52 
53  virtual BaseProperty::Pointer Deserialize(TiXmlElement *element) override
54  {
55  if (!element)
56  return nullptr;
57 
58  IntLookupTable lut;
59  for (TiXmlElement *child = element->FirstChildElement("LUTValue"); child != nullptr;
60  child = child->NextSiblingElement("LUTValue"))
61  {
62  int temp;
63  if (child->QueryIntAttribute("id", &temp) == TIXML_WRONG_TYPE)
64  return nullptr; // TODO: can we do a better error handling?
66  if (child->QueryIntAttribute("value", &temp) == TIXML_WRONG_TYPE)
67  return nullptr; // TODO: can we do a better error handling?
69  lut.SetTableValue(id, val);
70  }
71  return IntLookupTableProperty::New(lut).GetPointer();
72  }
73 
74  protected:
75  IntLookupTablePropertySerializer() {}
76  virtual ~IntLookupTablePropertySerializer() {}
77  };
78 } // namespace
79 // important to put this into the GLOBAL namespace (because it starts with 'namespace mitk')
80 MITK_REGISTER_SERIALIZER(IntLookupTablePropertySerializer);
81 #endif
std::map< IdentifierType, ValueType > LookupTableType
MITK_REGISTER_SERIALIZER(IntLookupTablePropertySerializer)
DataCollection - Class to facilitate loading/accessing structured data.
itk::SmartPointer< Self > Pointer
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:44