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