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
mitkEnumerationProperty.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 
18 #include <algorithm>
19 
20 // static map members of EnumerationProperty. These Maps point to per-classname-maps of ID <-> String. Accessed by
21 // GetEnumIds() and GetEnumString().
22 mitk::EnumerationProperty::IdMapForClassNameContainerType mitk::EnumerationProperty::s_IdMapForClassName;
23 mitk::EnumerationProperty::StringMapForClassNameContainerType mitk::EnumerationProperty::s_StringMapForClassName;
24 
26 {
27  m_CurrentValue = 0;
28 }
29 
31  : BaseProperty(other), m_CurrentValue(other.m_CurrentValue)
32 {
33 }
34 
35 bool mitk::EnumerationProperty::AddEnum(const std::string &name, const IdType &id)
36 {
37  if ((!IsValidEnumerationValue(name)) && (!IsValidEnumerationValue(id)))
38  {
39  GetEnumIds().insert(std::make_pair(id, name));
40  GetEnumStrings().insert(std::make_pair(name, id));
41  return true;
42  }
43  else
44  {
45  return false;
46  }
47 }
48 
49 bool mitk::EnumerationProperty::SetValue(const std::string &name)
50 {
51  if (IsValidEnumerationValue(name))
52  {
53  m_CurrentValue = GetEnumId(name);
54  Modified();
55  return true;
56  }
57  else
58  {
59  return false;
60  }
61 }
62 
64 {
65  if (IsValidEnumerationValue(id))
66  {
67  m_CurrentValue = id;
68  Modified();
69  return true;
70  }
71  else
72  {
73  return false;
74  }
75 }
76 
78 {
79  return m_CurrentValue;
80 }
81 
83 {
84  return GetEnumString(m_CurrentValue);
85 }
86 
88 {
89  GetEnumIds().clear();
90  GetEnumStrings().clear();
91  m_CurrentValue = 0;
92 }
93 
94 mitk::EnumerationProperty::EnumIdsContainerType::size_type mitk::EnumerationProperty::Size() const
95 {
96  return GetEnumIds().size();
97 }
98 
100 {
101  return GetEnumIds().begin();
102 }
103 
105 {
106  return GetEnumIds().end();
107 }
108 
110 {
111  if (IsValidEnumerationValue(id))
112  {
113  return GetEnumIds().find(id)->second;
114  }
115  else
116  {
117  return "invalid enum id or enums empty";
118  }
119 }
120 
122 {
123  if (IsValidEnumerationValue(name))
124  {
125  return GetEnumStrings().find(name)->second;
126  }
127  else
128  {
129  return 0;
130  }
131 }
132 
134 {
135  const Self &other = static_cast<const Self &>(property);
136  return this->Size() == other.Size() && this->GetValueAsId() == other.GetValueAsId() &&
137  std::equal(this->Begin(), this->End(), other.Begin());
138 }
139 
141 {
142  const Self &other = static_cast<const Self &>(property);
143  this->GetEnumIds() = other.GetEnumIds();
144  this->GetEnumStrings() = other.GetEnumStrings();
145  this->m_CurrentValue = other.m_CurrentValue;
146  this->Size() == other.Size() && this->GetValueAsId() == other.GetValueAsId() &&
147  std::equal(this->Begin(), this->End(), other.Begin());
148  return true;
149 }
150 
152 {
153  return (GetEnumIds().find(val) != GetEnumIds().end());
154 }
155 
156 bool mitk::EnumerationProperty::IsValidEnumerationValue(const std::string &val) const
157 {
158  return (GetEnumStrings().find(val) != GetEnumStrings().end());
159 }
160 
162 {
163  std::string className = this->GetNameOfClass(); // virtual!
164  return s_IdMapForClassName[className];
165 }
166 
168 {
169  std::string className = this->GetNameOfClass(); // virtual!
170  return s_IdMapForClassName[className];
171 }
172 
174 {
175  std::string className = this->GetNameOfClass(); // virtual!
176  return s_StringMapForClassName[className];
177 }
178 
180 {
181  std::string className = this->GetNameOfClass(); // virtual!
182  return s_StringMapForClassName[className];
183 }
184 
186 {
187  itk::LightObject::Pointer result(new Self(*this));
188  result->UnRegister();
189  return result;
190 }
virtual bool SetValue(const std::string &name)
virtual std::string GetValueAsString() const override
virtual bool IsEqual(const BaseProperty &property) const override
itk::SmartPointer< Self > Pointer
virtual bool Assign(const BaseProperty &property) override
virtual bool IsValidEnumerationValue(const IdType &val) const
itk::LightObject::Pointer InternalClone() const override
virtual bool AddEnum(const std::string &name, const IdType &id)
const EnumStringsContainerType & GetEnumStrings() const
Abstract base class for properties.
EnumIdsContainerType::const_iterator EnumConstIterator
std::map< IdType, std::string > EnumIdsContainerType
virtual EnumConstIterator End() const
virtual EnumIdsContainerType::size_type Size() const
virtual IdType GetEnumId(const std::string &name) const
const EnumIdsContainerType & GetEnumIds() const
virtual EnumConstIterator Begin() const
virtual std::string GetEnumString(const IdType &id) const
std::map< std::string, IdType > EnumStringsContainerType
virtual IdType GetValueAsId() const