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