DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Conversion.hpp
Go to the documentation of this file.
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: yes (from dbe/Conversion.h to include/dbe/Conversion.hpp).
5
6#ifndef DBE_CONVERSION_H_
7#define DBE_CONVERSION_H_
8
9#include "dbe/Exceptions.hpp"
10
11#include "conffwk/Schema.hpp"
12#include "logging/Logging.hpp"
13
14#include <sstream>
15#include <string>
16#include <type_traits>
17#include <vector>
18
19#include <QStringList>
20
21namespace dbe {
22
23namespace convert {
24//------------------------------------------------------------------------------------------
31template<typename T>
32inline std::string
33valtostr(T const& value)
34{
35 return std::to_string(value);
36}
37
38template<>
39inline std::string
40valtostr<std::string>(std::string const& x)
41{
42 return x;
43}
44
45template<>
46inline std::string
47valtostr<bool>(bool const& s)
48{
49 std::ostringstream ss;
50 ss << std::boolalpha << s;
51 return ss.str();
52}
53//------------------------------------------------------------------------------------------
54
55//------------------------------------------------------------------------------------------
62template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
63inline std::string
64valtostr(T const& value, dunedaq::conffwk::int_format_t const format)
65{
67 return std::to_string(value);
68 } else {
69 std::ostringstream ss;
70
72 ss << std::hex << std::showbase << +((typename std::make_unsigned<T>::type)value);
73 } else if (format == dunedaq::conffwk::oct_int_format) {
74 ss << std::oct << std::showbase << +((typename std::make_unsigned<T>::type)value);
75 } else if (format == dunedaq::conffwk::na_int_format) {
76 ss << value;
77 TLOG_DEBUG(1) << " This conversion should not happen for value " << ss.str();
78 }
79
80 return ss.str();
81 }
82}
83
84// Specialization for boolean (the std::make_unsigned is not defined for booleans)
85template<>
86inline std::string
87valtostr<bool>(bool const& value, dunedaq::conffwk::int_format_t const format)
88{
89 Q_UNUSED(format);
90 return valtostr<bool>(value);
91}
92
93// This is for floating point types
94template<typename T, typename std::enable_if<std::is_floating_point<T>::value, int>::type = 0>
95inline std::string
96valtostr(T const& value, dunedaq::conffwk::int_format_t const format)
97{
98 Q_UNUSED(format);
99 return std::to_string(value);
100}
101
102// This is for simple strings
103inline std::string
104valtostr(std::string const& value, dunedaq::conffwk::int_format_t const format)
105{
106 Q_UNUSED(format);
107 return value;
108}
109//------------------------------------------------------------------------------------------
110
111//------------------------------------------------------------------------------------------
112template<typename T>
113T
114to(QStringList const& DataList)
115{
116 Q_UNUSED(DataList)
117
118 T Dummy;
119 return Dummy;
120}
121
122template<typename T>
123T
124to(QStringList const& DataList, dunedaq::conffwk::int_format_t Format)
125{
126 Q_UNUSED(Format)
127 Q_UNUSED(DataList)
128
129 T Dummy;
130 return Dummy;
131}
132
133template<typename T>
134T
135to(std::vector<std::string> const& x)
136{
137 return x;
138}
139//------------------------------------------------------------------------------------------
140
141//------------------------------------------------------------------------------------------
143
144template<>
145QStringList
146to(std::vector<std::string> const& DataList);
147
148template<>
149bool
150to<bool>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
151template<>
152std::vector<bool>
153to<std::vector<bool>>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
154template<>
155std::string
156to<std::string>(QStringList const& DataList);
157template<>
158std::vector<std::string>
159to<std::vector<std::string>>(QStringList const& DataList);
160template<>
161std::string
162to<std::string>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
163template<>
164std::vector<std::string>
165to<std::vector<std::string>>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
166template<>
167u_int8_t
168to<u_int8_t>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
169template<>
170std::vector<u_int8_t>
171to<std::vector<u_int8_t>>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
172template<>
173int8_t
174to<int8_t>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
175template<>
176std::vector<int8_t>
177to<std::vector<int8_t>>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
178template<>
179u_int16_t
180to<u_int16_t>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
181template<>
182std::vector<u_int16_t>
183to<std::vector<u_int16_t>>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
184template<>
185int16_t
186to<int16_t>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
187template<>
188std::vector<int16_t>
189to<std::vector<int16_t>>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
190template<>
191u_int32_t
192to<u_int32_t>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
193template<>
194std::vector<u_int32_t>
195to<std::vector<u_int32_t>>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
196template<>
197int32_t
198to<int32_t>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
199template<>
200std::vector<int32_t>
201to<std::vector<int32_t>>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
202template<>
203u_int64_t
204to<u_int64_t>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
205template<>
206std::vector<u_int64_t>
207to<std::vector<u_int64_t>>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
208template<>
209int64_t
210to<int64_t>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
211template<>
212std::vector<int64_t>
213to<std::vector<int64_t>>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
214template<>
215float
216to<float>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
217template<>
218std::vector<float>
219to<std::vector<float>>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
220template<>
221double
222to<double>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
223template<>
224std::vector<double>
225to<std::vector<double>>(QStringList const& DataList, dunedaq::conffwk::int_format_t Format);
226//------------------------------------------------------------------------------------------
227
228}
229}
230
231#endif // DBE_CONVERSION_H_
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:116
std::string valtostr(T const &value)
T to(QStringList const &DataList)
std::string to< std::string >(QStringList const &DataList)
std::string valtostr< std::string >(std::string const &x)
Include QT Headers.