Medical Imaging Interaction Toolkit  2018.4.99-9a29ffc6
Medical Imaging Interaction Toolkit
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 (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 
14 #include <algorithm>
15 
17  : m_CurrentValue(0)
18 {
19 }
20 
22  : BaseProperty(other),
23  m_CurrentValue(other.m_CurrentValue),
24  m_IdMap(other.m_IdMap),
25  m_NameMap(other.m_NameMap)
26 {
27 }
28 
29 bool mitk::EnumerationProperty::AddEnum(const std::string &name, const IdType &id)
30 {
31  if (false == this->IsValidEnumerationValue(id) &&
32  false == this->IsValidEnumerationValue(name))
33  {
34  this->GetEnumIds().insert(std::make_pair(id, name));
35  this->GetEnumStrings().insert(std::make_pair(name, id));
36 
37  return true;
38  }
39 
40  return false;
41 }
42 
43 bool mitk::EnumerationProperty::SetValue(const std::string &name)
44 {
45  if (this->IsValidEnumerationValue(name))
46  {
47  m_CurrentValue = this->GetEnumId(name);
48  this->Modified();
49 
50  return true;
51  }
52 
53  return false;
54 }
55 
57 {
58  if (this->IsValidEnumerationValue(id))
59  {
60  m_CurrentValue = id;
61  this->Modified();
62 
63  return true;
64  }
65 
66  return false;
67 }
68 
70 {
71  return m_CurrentValue;
72 }
73 
75 {
76  return this->GetEnumString(m_CurrentValue);
77 }
78 
80 {
81  this->GetEnumIds().clear();
82  this->GetEnumStrings().clear();
83 
84  m_CurrentValue = 0;
85 }
86 
87 mitk::EnumerationProperty::EnumIdsContainerType::size_type mitk::EnumerationProperty::Size() const
88 {
89  return this->GetEnumIds().size();
90 }
91 
93 {
94  return this->GetEnumIds().cbegin();
95 }
96 
98 {
99  return this->GetEnumIds().cend();
100 }
101 
103 {
104  return this->IsValidEnumerationValue(id)
105  ? this->GetEnumIds().find(id)->second
106  : std::string("invalid enum id or enums empty");
107 }
108 
110 {
111  return this->IsValidEnumerationValue(name)
112  ? this->GetEnumStrings().find(name)->second
113  : 0;
114 }
115 
117 {
118  const auto &other = static_cast<const Self &>(property);
119 
120  return
121  this->Size() == other.Size() &&
122  this->GetValueAsId() == other.GetValueAsId() &&
123  std::equal(this->Begin(), this->End(), other.Begin());
124 }
125 
127 {
128  const auto &other = static_cast<const Self &>(property);
129 
130  this->GetEnumIds() = other.GetEnumIds();
131  this->GetEnumStrings() = other.GetEnumStrings();
132 
133  m_CurrentValue = other.m_CurrentValue;
134 
135  return true;
136 }
137 
139 {
140  return this->GetEnumIds().end() != this->GetEnumIds().find(id);
141 }
142 
143 bool mitk::EnumerationProperty::IsValidEnumerationValue(const std::string &name) const
144 {
145  return this->GetEnumStrings().end() != this->GetEnumStrings().find(name);
146 }
147 
149 {
150  return m_IdMap;
151 }
152 
154 {
155  return m_IdMap;
156 }
157 
159 {
160  return m_NameMap;
161 }
162 
164 {
165  return m_NameMap;
166 }
virtual IdType GetValueAsId() const
virtual bool SetValue(const std::string &name)
virtual EnumIdsContainerType::size_type Size() const
std::string GetValueAsString() const override
bool IsEqual(const BaseProperty &property) const override
bool Assign(const BaseProperty &property) override
virtual IdType GetEnumId(const std::string &name) const
virtual bool AddEnum(const std::string &name, const IdType &id)
const EnumStringsContainerType & GetEnumStrings() const
virtual EnumConstIterator End() const
const EnumIdsContainerType & GetEnumIds() const
Abstract base class for properties.
EnumIdsContainerType::const_iterator EnumConstIterator
std::map< IdType, std::string > EnumIdsContainerType
virtual EnumConstIterator Begin() const
virtual bool IsValidEnumerationValue(const IdType &id) const
std::map< std::string, IdType > EnumStringsContainerType
virtual std::string GetEnumString(const IdType &id) const