Line data Source code
1 : #include "dbe/Conversion.hpp"
2 : #include "dbe/Exceptions.hpp"
3 :
4 : namespace dbe {
5 : namespace convert {
6 :
7 0 : template<> QStringList to<QStringList>(std::vector<std::string> const & x) {
8 0 : QStringList ret;
9 :
10 0 : for(std::string const & a : x) {
11 0 : ret.append(QString::fromStdString(a));
12 : }
13 :
14 0 : return ret;
15 0 : }
16 :
17 : template<>
18 0 : std::string to<std::string>(QStringList const & DataList) {
19 0 : std::string rString;
20 :
21 0 : for(int i = 0; i < DataList.size(); ++i) {
22 0 : rString = DataList.at(i).toStdString();
23 : }
24 :
25 0 : return rString;
26 0 : }
27 :
28 : template<>
29 0 : std::vector<std::string> to<std::vector<std::string> >(QStringList const & DataList) {
30 0 : std::vector<std::string> rVector;
31 :
32 0 : for(int i = 0; i < DataList.size(); ++i) {
33 0 : rVector.push_back(DataList.at(i).toStdString());
34 : }
35 :
36 0 : return rVector;
37 0 : }
38 :
39 : template<>
40 0 : std::string to<std::string>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
41 0 : Q_UNUSED (Format)
42 :
43 0 : std::string rString;
44 :
45 0 : for(int i = 0; i < DataList.size(); ++i) {
46 0 : rString = DataList.at(i).toStdString();
47 : }
48 :
49 0 : return rString;
50 0 : }
51 :
52 : template<>
53 0 : std::vector<std::string> to<std::vector<std::string>>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
54 0 : Q_UNUSED (Format)
55 :
56 0 : std::vector<std::string> rVector;
57 :
58 0 : for(int i = 0; i < DataList.size(); ++i) {
59 0 : rVector.push_back(DataList.at(i).toStdString());
60 : }
61 :
62 0 : return rVector;
63 0 : }
64 :
65 : template<>
66 0 : u_int8_t to<u_int8_t>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
67 0 : bool ok = false;
68 0 : u_int8_t uChar = 0;
69 :
70 0 : if(DataList.size() > 0) {
71 0 : if(Format == dunedaq::conffwk::dec_int_format) {
72 0 : uChar = (u_int8_t) (DataList.at(0).toULong(&ok, 10));
73 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
74 0 : uChar = (u_int8_t) (DataList.at(0).toULong(&ok, 16));
75 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
76 0 : uChar = (u_int8_t) (DataList.at(0).toULong(&ok, 8));
77 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
78 0 : uChar = (u_int8_t) (DataList.at(0).toULong(&ok));
79 : }
80 : }
81 :
82 0 : return uChar;
83 : }
84 :
85 : template<>
86 0 : std::vector<u_int8_t> to<std::vector<u_int8_t> >(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
87 0 : std::vector<u_int8_t> rVector;
88 :
89 0 : for(int i = 0; i < DataList.size(); ++i) {
90 0 : bool ok = false;
91 0 : u_int8_t uChar = 0;
92 :
93 0 : if(Format == dunedaq::conffwk::dec_int_format) {
94 0 : uChar = (u_int8_t) (DataList.at(i).toULong(&ok, 10));
95 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
96 0 : uChar = (u_int8_t) (DataList.at(i).toULong(&ok, 16));
97 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
98 0 : uChar = (u_int8_t) (DataList.at(i).toULong(&ok, 8));
99 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
100 0 : uChar = (u_int8_t) (DataList.at(i).toULong(&ok));
101 : }
102 :
103 0 : rVector.push_back(uChar);
104 : }
105 :
106 0 : return rVector;
107 0 : }
108 :
109 : template<>
110 0 : int8_t to<int8_t>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
111 0 : bool ok = false;
112 0 : int8_t sChar = 0;
113 :
114 0 : if(DataList.size() > 0) {
115 0 : if(Format == dunedaq::conffwk::dec_int_format) {
116 0 : sChar = (int8_t) (DataList.at(0).toLong(&ok, 10));
117 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
118 0 : sChar = (int8_t) (DataList.at(0).toLong(&ok, 16));
119 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
120 0 : sChar = (int8_t) (DataList.at(0).toLong(&ok, 8));
121 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
122 0 : sChar = (int8_t) (DataList.at(0).toLong(&ok));
123 : }
124 : }
125 :
126 0 : return sChar;
127 : }
128 :
129 : template<>
130 0 : std::vector<int8_t> to<std::vector<int8_t>>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
131 0 : std::vector<int8_t> rVector;
132 :
133 0 : for(int i = 0; i < DataList.size(); ++i) {
134 0 : bool ok = false;
135 0 : int8_t sChar = 0;
136 :
137 0 : if(Format == dunedaq::conffwk::dec_int_format) {
138 0 : sChar = (int8_t) (DataList.at(i).toLong(&ok, 10));
139 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
140 0 : sChar = (int8_t) (DataList.at(i).toLong(&ok, 16));
141 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
142 0 : sChar = (int8_t) (DataList.at(i).toLong(&ok, 8));
143 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
144 0 : sChar = (int8_t) (DataList.at(i).toLong(&ok));
145 : }
146 :
147 0 : rVector.push_back(sChar);
148 : }
149 :
150 0 : return rVector;
151 0 : }
152 :
153 : template<>
154 0 : u_int16_t to<u_int16_t>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
155 0 : bool ok = false;
156 0 : u_int16_t uShort = 0;
157 :
158 0 : if(DataList.size() > 0) {
159 0 : if(Format == dunedaq::conffwk::dec_int_format) {
160 0 : uShort = DataList.at(0).toUShort(&ok, 10);
161 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
162 0 : uShort = DataList.at(0).toUShort(&ok, 16);
163 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
164 0 : uShort = DataList.at(0).toUShort(&ok, 8);
165 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
166 0 : uShort = DataList.at(0).toUShort(&ok);
167 : }
168 : }
169 :
170 0 : return uShort;
171 : }
172 :
173 : template<>
174 0 : std::vector<u_int16_t> to<std::vector<u_int16_t>>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
175 0 : std::vector<u_int16_t> rVector;
176 :
177 0 : for(int i = 0; i < DataList.size(); ++i) {
178 0 : bool ok = false;
179 0 : u_int16_t uShort = 0;
180 :
181 0 : if(Format == dunedaq::conffwk::dec_int_format) {
182 0 : uShort = DataList.at(i).toUShort(&ok, 10);
183 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
184 0 : uShort = DataList.at(i).toUShort(&ok, 16);
185 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
186 0 : uShort = DataList.at(i).toUShort(&ok, 8);
187 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
188 0 : uShort = DataList.at(i).toUShort(&ok);
189 : }
190 :
191 0 : rVector.push_back(uShort);
192 : }
193 :
194 0 : return rVector;
195 0 : }
196 :
197 : template<>
198 0 : int16_t to<int16_t>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
199 0 : bool ok = false;
200 0 : int16_t sShort = 0;
201 :
202 0 : if(DataList.size() > 0) {
203 0 : if(Format == dunedaq::conffwk::dec_int_format) {
204 0 : sShort = DataList.at(0).toShort(&ok, 10);
205 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
206 0 : sShort = DataList.at(0).toShort(&ok, 16);
207 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
208 0 : sShort = DataList.at(0).toShort(&ok, 8);
209 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
210 0 : sShort = DataList.at(0).toShort(&ok);
211 : }
212 : }
213 :
214 0 : return sShort;
215 : }
216 :
217 : template<>
218 0 : std::vector<int16_t> to<std::vector<int16_t>>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
219 0 : std::vector<int16_t> rVector;
220 :
221 0 : for(int i = 0; i < DataList.size(); ++i) {
222 0 : bool ok = false;
223 0 : int16_t sShort = 0;
224 :
225 0 : if(Format == dunedaq::conffwk::dec_int_format) {
226 0 : sShort = DataList.at(i).toShort(&ok, 10);
227 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
228 0 : sShort = DataList.at(i).toShort(&ok, 16);
229 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
230 0 : sShort = DataList.at(i).toShort(&ok, 8);
231 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
232 0 : sShort = DataList.at(i).toShort(&ok);
233 : }
234 :
235 0 : rVector.push_back(sShort);
236 : }
237 :
238 0 : return rVector;
239 0 : }
240 :
241 : template<>
242 0 : u_int32_t to<u_int32_t>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
243 0 : bool ok = false;
244 0 : u_int32_t uLong = 0;
245 :
246 0 : if(DataList.size() > 0) {
247 0 : if(Format == dunedaq::conffwk::dec_int_format) {
248 0 : uLong = DataList.at(0).toULong(&ok, 10);
249 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
250 0 : uLong = DataList.at(0).toULong(&ok, 16);
251 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
252 0 : uLong = DataList.at(0).toULong(&ok, 8);
253 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
254 0 : uLong = DataList.at(0).toULong(&ok);
255 : }
256 : }
257 :
258 0 : return uLong;
259 : }
260 :
261 : template<>
262 0 : std::vector<u_int32_t> to<std::vector<u_int32_t>>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
263 0 : std::vector<u_int32_t> rVector;
264 :
265 0 : for(int i = 0; i < DataList.size(); ++i) {
266 0 : bool ok = false;
267 0 : u_int32_t uLong = 0;
268 :
269 0 : if(Format == dunedaq::conffwk::dec_int_format) {
270 0 : uLong = DataList.at(i).toULong(&ok, 10);
271 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
272 0 : uLong = DataList.at(i).toULong(&ok, 16);
273 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
274 0 : uLong = DataList.at(i).toULong(&ok, 8);
275 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
276 0 : uLong = DataList.at(i).toULong(&ok);
277 : }
278 :
279 0 : rVector.push_back(uLong);
280 : }
281 :
282 0 : return rVector;
283 0 : }
284 :
285 : template<>
286 0 : int32_t to<int32_t>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
287 0 : bool ok = false;
288 0 : int32_t sLong = 0;
289 :
290 0 : if(DataList.size() > 0) {
291 0 : if(Format == dunedaq::conffwk::dec_int_format) {
292 0 : sLong = DataList.at(0).toLong(&ok, 10);
293 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
294 0 : sLong = DataList.at(0).toLong(&ok, 16);
295 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
296 0 : sLong = DataList.at(0).toLong(&ok, 8);
297 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
298 0 : sLong = DataList.at(0).toLong(&ok);
299 : }
300 : }
301 :
302 0 : return sLong;
303 : }
304 :
305 : template<>
306 0 : std::vector<int32_t> to<std::vector<int32_t>>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
307 0 : std::vector<int32_t> rVector;
308 :
309 0 : for(int i = 0; i < DataList.size(); ++i) {
310 0 : bool ok = false;
311 0 : int32_t sLong = 0;
312 :
313 0 : if(Format == dunedaq::conffwk::dec_int_format) {
314 0 : sLong = DataList.at(i).toLong(&ok, 10);
315 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
316 0 : sLong = DataList.at(i).toLong(&ok, 16);
317 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
318 0 : sLong = DataList.at(i).toLong(&ok, 8);
319 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
320 0 : sLong = DataList.at(i).toLong(&ok, 10);
321 : }
322 :
323 0 : rVector.push_back(sLong);
324 : }
325 :
326 0 : return rVector;
327 0 : }
328 :
329 : template<>
330 0 : u_int64_t to<u_int64_t>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
331 0 : bool ok = false;
332 0 : u_int64_t u64 = 0;
333 :
334 0 : if(DataList.size() > 0) {
335 0 : if(Format == dunedaq::conffwk::dec_int_format) {
336 0 : u64 = (uint64_t) (DataList.at(0).toULongLong(&ok, 10));
337 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
338 0 : u64 = (uint64_t) (DataList.at(0).toULongLong(&ok, 16));
339 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
340 0 : u64 = (uint64_t) (DataList.at(0).toULongLong(&ok, 8));
341 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
342 0 : u64 = (uint64_t) (DataList.at(0).toULongLong(&ok, 10));
343 : }
344 : }
345 :
346 0 : return u64;
347 : }
348 :
349 : template<>
350 0 : std::vector<u_int64_t> to<std::vector<u_int64_t>>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
351 0 : std::vector<u_int64_t> rVector;
352 :
353 0 : for(int i = 0; i < DataList.size(); ++i) {
354 0 : bool ok = false;
355 0 : u_int64_t u64 = 0;
356 :
357 0 : if(Format == dunedaq::conffwk::dec_int_format) {
358 0 : u64 = (uint64_t) (DataList.at(i).toULongLong(&ok, 10));
359 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
360 0 : u64 = (uint64_t) (DataList.at(i).toULongLong(&ok, 16));
361 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
362 0 : u64 = (uint64_t) (DataList.at(i).toULongLong(&ok, 8));
363 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
364 0 : u64 = (uint64_t) (DataList.at(i).toULongLong(&ok, 10));
365 : }
366 :
367 0 : rVector.push_back(u64);
368 : }
369 :
370 0 : return rVector;
371 0 : }
372 :
373 : template<>
374 0 : int64_t to<int64_t>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
375 0 : bool ok = false;
376 0 : int64_t i64 = 0;
377 :
378 0 : if(DataList.size() > 0) {
379 0 : if(Format == dunedaq::conffwk::dec_int_format) {
380 0 : i64 = (int64_t) (DataList.at(0).toLongLong(&ok, 10));
381 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
382 0 : i64 = (int64_t) (DataList.at(0).toLongLong(&ok, 16));
383 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
384 0 : i64 = (int64_t) (DataList.at(0).toLongLong(&ok, 8));
385 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
386 0 : i64 = (int64_t) (DataList.at(0).toLongLong(&ok, 10));
387 : }
388 : }
389 :
390 0 : return i64;
391 : }
392 :
393 : template<>
394 0 : std::vector<int64_t> to<std::vector<int64_t>>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
395 0 : std::vector<int64_t> rVector;
396 :
397 0 : for(int i = 0; i < DataList.size(); ++i) {
398 0 : bool ok = false;
399 0 : int64_t i64 = 0;
400 :
401 0 : if(Format == dunedaq::conffwk::dec_int_format) {
402 0 : i64 = (int64_t) (DataList.at(i).toLongLong(&ok, 10));
403 0 : } else if(Format == dunedaq::conffwk::hex_int_format) {
404 0 : i64 = (int64_t) (DataList.at(i).toLongLong(&ok, 16));
405 0 : } else if(Format == dunedaq::conffwk::oct_int_format) {
406 0 : i64 = (int64_t) (DataList.at(i).toLongLong(&ok, 8));
407 0 : } else if(Format == dunedaq::conffwk::na_int_format) {
408 0 : i64 = (int64_t) (DataList.at(i).toLongLong(&ok, 10));
409 : }
410 :
411 0 : rVector.push_back(i64);
412 : }
413 :
414 0 : return rVector;
415 0 : }
416 :
417 : template<>
418 0 : float to<float>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
419 0 : Q_UNUSED (Format)
420 :
421 0 : float f {0};
422 :
423 0 : for(auto i = 0; i < DataList.size(); ++i) {
424 0 : f = DataList.at(i).toFloat();
425 : }
426 :
427 0 : return f;
428 : }
429 :
430 : template<>
431 0 : std::vector<float> to<std::vector<float>>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
432 0 : Q_UNUSED (Format)
433 :
434 0 : std::vector<float> rVector;
435 :
436 0 : for(int i = 0; i < DataList.size(); ++i) {
437 0 : rVector.push_back(DataList.at(i).toFloat());
438 : }
439 :
440 0 : return rVector;
441 0 : }
442 :
443 : template<>
444 0 : double to<double>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
445 0 : Q_UNUSED (Format)
446 :
447 0 : double d {0};
448 :
449 0 : for(int i = 0; i < DataList.size(); ++i) {
450 0 : d = DataList.at(i).toDouble();
451 : }
452 :
453 0 : return d;
454 : }
455 :
456 : template<>
457 0 : std::vector<double> to<std::vector<double>>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
458 0 : Q_UNUSED (Format)
459 :
460 0 : std::vector<double> rVector;
461 :
462 0 : for(int i = 0; i < DataList.size(); ++i) {
463 0 : rVector.push_back(DataList.at(i).toDouble());
464 : }
465 :
466 0 : return rVector;
467 0 : }
468 :
469 : template<>
470 0 : bool to<bool>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
471 0 : bool b = false;
472 0 : Q_UNUSED (Format)
473 :
474 0 : for(int i = 0; i < DataList.size(); ++i) {
475 0 : if(QString::compare(DataList.at(i), QString("true"), Qt::CaseInsensitive) == 0
476 0 : || QString::compare(DataList.at(i), QString("1"), Qt::CaseInsensitive) == 0) {
477 : b = true;
478 0 : } else if(QString::compare(DataList.at(i), QString("false"), Qt::CaseInsensitive) == 0
479 0 : || QString::compare(DataList.at(i), QString("0"), Qt::CaseInsensitive) == 0) {
480 : b = false;
481 : } else {
482 0 : std::string message = "Conversion to enum of " + DataList.at(i).toStdString() + " is not possible!";
483 0 : throw daq::dbe::BadConversion( ERS_HERE, message.c_str() );
484 0 : }
485 : }
486 :
487 0 : return b;
488 : }
489 :
490 : template<>
491 0 : std::vector<bool> to<std::vector<bool>>(QStringList const & DataList, dunedaq::conffwk::int_format_t Format) {
492 0 : std::vector<bool> rVector;
493 0 : Q_UNUSED (Format)
494 :
495 0 : for(int i = 0; i < DataList.size(); ++i) {
496 0 : if(QString::compare(DataList.at(i), QString("true"), Qt::CaseInsensitive) == 0
497 0 : || QString::compare(DataList.at(i), QString("1"), Qt::CaseInsensitive) == 0) {
498 0 : rVector.push_back(true);
499 0 : } else if(QString::compare(DataList.at(i), QString("false"), Qt::CaseInsensitive) == 0
500 0 : || QString::compare(DataList.at(i), QString("0"), Qt::CaseInsensitive) == 0) {
501 0 : rVector.push_back(false);
502 : } else {
503 0 : std::string message = "Conversion to enum of " + DataList.at(i).toStdString() + " is not possible!";
504 0 : throw daq::dbe::BadConversion( ERS_HERE, message.c_str() );
505 0 : }
506 : }
507 :
508 0 : return rVector;
509 0 : }
510 :
511 : }
512 : }
|