DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
conversions-impl.hh
Go to the documentation of this file.
1
2// Implementations of Item value conversions
3//
4// note that these are classes declared in a source file;
5// ItemConversion::FromString is the one and only interface
6// for creating conversions
7
8
10 pass,
11(
12public:
13 void init (unordered_map<std::string, std::string> params) {
14 (void) params; // to make compiler not complain about unused arguments
15 data_size = sizeof(uint32_t);
16 data_description = "i";
17 }
18 void Convert(uint32_t src, void *dest) {
19 uint32_t *val = (uint32_t *)dest;
20 *val = src;
21 }
22))
23
25 linear,
26(
27 double scale;
28 double offset;
29public:
30 void init (unordered_map<std::string, std::string> params) {
31 data_size = sizeof(double);
32 data_description = "d";
33 std::string scale_str = params["scale"];
34 std::string offset_str = params["offset"];
35 sscanf(scale_str.c_str(), "%le", &scale);
36 sscanf(offset_str.c_str(), "%le", &offset);
37 };
38 void Convert(uint32_t src, void *dest) {
39 double *val = (double *)dest;
40 *val = ((double)src) * scale + offset;
41 }
42))
43
45 enum,
46(
47 unordered_map<uint32_t, std::string> names;
48public:
49 void init (unordered_map<std::string, std::string> params) {
50 size_t length = 16; // enough for "???? 0x00000000\0"
51 BOOST_FOREACH( STRIP((std::pair<std::string, std::string>)) i, params ) {
52 long int number = 0;
53 std::string key = i.first;
54 std::string value = i.second;
55 if (1 != sscanf(key.c_str(), "%li", &number)) {
56 // error: unrecognized number
57 continue;
58 }
59 names[(uint32_t)number] = value;
60 size_t newlength = value.size();
61 length = (newlength > length ? newlength : length);
62 }
63 data_size = length;
64 const size_t descrlen = 16;
65 char descr[descrlen];
66 snprintf(descr, descrlen, "c:%zu;", length);
67 data_description = std::string(descr);
68 };
69 void Convert(uint32_t src, void *dest) {
70 char *val = (char *)dest;
71 if (names.count(src)) {
72 std::string name = names[src];
73 // strncpy pads to end of buffer with null byte
74 strncpy(val, name.c_str(), data_size);
75 } else {
76 // due to snprintf limitation we can't write to the last byte
77 // thus the formatted string uses 15 out of 16 characters
78 int i = snprintf(val, data_size, "???? 0x%08X", src);
79 // pad to end of buffer with null byte
80 while (i < (int)data_size) {val[i] = '\0'; i++;}
81 }
82 }
83))
84
double offset
void Convert(uint32_t src, void *dest)
double scale
void init(unordered_map< std::string, std::string > params)
#define STRIP(x)
#define DEFINE_CONVERSION_CLASS(name, body)