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