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