Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkConfigurationHolder.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 
19 #include <mitkExceptionMacro.h>
20 #include <sstream>
21 
23  m_ValueType(DT_UNINIZIALIZED)
24 {
25  m_GroupValue.clear();
26 }
27 
29 {
30  m_BoolValue = value;
31  m_ValueType = DT_BOOL;
32 }
33 
35 {
36  m_UIntValue = value;
37  m_ValueType = DT_UINT;
38 }
39 
41 {
42  m_IntValue = value;
43  m_ValueType = DT_INT;
44 }
45 
47 {
48  m_DoubleValue = value;
49  m_ValueType = DT_DOUBLE;
50 }
51 
52 void mitk::ConfigurationHolder::SetString(std::string value)
53 {
54  m_StringValue = value;
55  m_ValueType = DT_STRING;
56 }
57 
59 {
60  m_GroupValue.clear();
61  m_ValueType = DT_GROUP;
62 }
63 
65 {
66  m_GroupValue[id] = value;
67  m_ValueType = DT_GROUP;
68 }
69 
71 {
72  switch (m_ValueType)
73  {
74  case DT_UNINIZIALIZED:
75  mitkThrow() << "No value stored";
76  break;
77  case DT_BOOL:
78  return m_BoolValue;
79  break;
80  case DT_UINT:
81  return m_UIntValue;
82  break;
83  case DT_INT:
84  return m_IntValue;
85  break;
86  case DT_DOUBLE:
87  return (m_DoubleValue > 1 || m_DoubleValue < -1);
88  break;
89  case DT_STRING:
90  return (m_StringValue != "0");
91  break;
92  case DT_GROUP:
93  mitkThrow() << "Cannot convert group data to bool";
94  break;
95  default:
96  mitkThrow() << "Unkown Data Type.";
97  break;
98  }
99 }
100 
102 {
103  unsigned int result;
104  std::istringstream ss(m_StringValue);
105  ss >> result;
106 
107  switch (m_ValueType)
108  {
109  case DT_UNINIZIALIZED:
110  mitkThrow() << "No value stored";
111  break;
112  case DT_BOOL:
113  return m_BoolValue;
114  break;
115  case DT_UINT:
116  return m_UIntValue;
117  break;
118  case DT_INT:
119  return m_IntValue;
120  break;
121  case DT_DOUBLE:
122  return m_DoubleValue;
123  break;
124  case DT_STRING:
125  return result;
126  break;
127  case DT_GROUP:
128  mitkThrow() << "Cannot convert group data to bool";
129  break;
130  default:
131  mitkThrow() << "Unkown Data Type.";
132  break;
133  }
134 }
135 
137 {
138  int result;
139  std::istringstream ss(m_StringValue);
140  ss >> result;
141 
142  switch (m_ValueType)
143  {
144  case DT_UNINIZIALIZED:
145  mitkThrow() << "No value stored";
146  break;
147  case DT_BOOL:
148  return m_BoolValue;
149  break;
150  case DT_UINT:
151  return m_UIntValue;
152  break;
153  case DT_INT:
154  return m_IntValue;
155  break;
156  case DT_DOUBLE:
157  return m_DoubleValue;
158  break;
159  case DT_STRING:
160  return result;
161  break;
162  case DT_GROUP:
163  mitkThrow() << "Cannot convert group data to bool";
164  break;
165  default:
166  mitkThrow() << "Unkown Data Type.";
167  break;
168  }
169 }
170 
172 {
173  double result;
174  std::istringstream ss(m_StringValue);
175  ss >> result;
176 
177  switch (m_ValueType)
178  {
179  case DT_UNINIZIALIZED:
180  mitkThrow() << "No value stored";
181  break;
182  case DT_BOOL:
183  return m_BoolValue;
184  break;
185  case DT_UINT:
186  return m_UIntValue;
187  break;
188  case DT_INT:
189  return m_IntValue;
190  break;
191  case DT_DOUBLE:
192  return m_DoubleValue > 1;
193  break;
194  case DT_STRING:
195  return result;
196  break;
197  case DT_GROUP:
198  mitkThrow() << "Cannot convert group data to bool";
199  break;
200  default:
201  mitkThrow() << "Unkown Data Type.";
202  break;
203  }
204 }
205 
207 
208 {
209  std::ostringstream strs;
210 
211  switch (m_ValueType)
212  {
213  case DT_UNINIZIALIZED:
214  mitkThrow() << "No value stored";
215  break;
216  case DT_BOOL:
217  strs << m_BoolValue;
218  break;
219  case DT_UINT:
220  strs << m_UIntValue;
221  break;
222  case DT_INT:
223  strs << m_IntValue;
224  break;
225  case DT_DOUBLE:
226  strs << m_DoubleValue;
227  break;
228  case DT_STRING:
229  return m_StringValue;
230  break;
231  case DT_GROUP:
232  mitkThrow() << "Cannot convert group data to bool";
233  break;
234  default:
235  mitkThrow() << "Unkown Data Type.";
236  break;
237  }
238  return strs.str();
239 }
240 
242 {
243  if (m_ValueType != DT_GROUP)
244  mitkThrow() << "No Group Data, cannot convert to String Vector";
245 
246  std::vector<std::string> result;
247  for (auto iter = m_GroupValue.begin(); iter != m_GroupValue.end(); ++iter)
248  {
249  result.push_back((*iter).second.AsString());
250  }
251  return result;
252 }
253 
255 {
256  return m_GroupValue[id];
257 }
258 
260 {
261  try {
262  return this->AsBool();
263  }
264  catch (mitk::Exception)
265  {
266  return value;
267  }
268 }
269 
270 unsigned int mitk::ConfigurationHolder::AsUnsignedInt(unsigned int value)
271 {
272  try {
273  return this->AsUnsignedInt();
274  }
275  catch (mitk::Exception)
276  {
277  return value;
278  }
279 }
280 
282 {
283  try {
284  return this->AsInt();
285  }
286  catch (mitk::Exception)
287  {
288  return value;
289  }
290 }
291 
293 {
294  try {
295  return this->AsDouble();
296  }
297  catch (mitk::Exception)
298  {
299  return value;
300  }
301 }
302 
303 std::string mitk::ConfigurationHolder::AsString(std::string value)
304 {
305  try {
306  return this->AsString();
307  }
308  catch (mitk::Exception)
309  {
310  return value;
311  }
312 }
ConfigurationHolder & At(std::string id)
#define DT_INT
#define DT_DOUBLE
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
#define mitkThrow()
std::vector< std::string > AsStringVector()
void AddToGroup(std::string id, const ConfigurationHolder &value)
void SetString(std::string value)
void SetUnsignedInt(unsigned int value)