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