ChimeraTK-DeviceAccess  03.18.00
DataDescriptor.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 
4 #include "DataDescriptor.h"
5 namespace ChimeraTK {
6 
7  /********************************************************************************************************************/
8 
9  DataDescriptor::DataDescriptor(FundamentalType fundamentalType_, bool isIntegral_, bool isSigned_, size_t nDigits_,
10  size_t nFractionalDigits_, DataType rawDataType_, DataType transportLayerDataType_)
11  : _fundamentalType(fundamentalType_), _rawDataType(rawDataType_), _transportLayerDataType(transportLayerDataType_),
12  _isIntegral(isIntegral_), _isSigned(isSigned_), _nDigits(nDigits_), _nFractionalDigits(nFractionalDigits_) {}
13 
14  /********************************************************************************************************************/
15 
17  if(type.isNumeric()) {
18  _fundamentalType = FundamentalType::numeric;
19  }
20  else {
21  _fundamentalType = FundamentalType::string;
22  }
23  _isIntegral = type.isIntegral();
24  _isSigned = type.isSigned();
25 
26  // the defaults
27  _nDigits = 0; // invalid for non handled types
28  _nFractionalDigits = 0; // 0 for integers
29 
30  if(type == DataType::int8) { // 8 bit signed
31  _nDigits = 4; // -127 .. 128
32  }
33  else if(type == DataType::uint8) { // 8 bit unsigned
34  _nDigits = 3; // 0..256
35  }
36  else if(type == DataType::int16) { // 16 bit signed
37  _nDigits = 6; // -32000 .. 32000 (approx)
38  }
39  else if(type == DataType::uint16) { // 16 bit unsigned
40  _nDigits = 5; // 0 .. 65000 (approx)
41  }
42  else if(type == DataType::int32) { // 32 bit signed
43  _nDigits = 11; // -2e9 .. 2e9 (approx)
44  }
45  else if(type == DataType::uint32) { // 32 bit unsigned
46  _nDigits = 10; // 0 .. 4e9 (approx)
47  }
48  else if(type == DataType::int64) { // 64 bit signed // NOLINT(bugprone-branch-clone)
49  _nDigits = 20; // -9e18 .. 9e18 (approx)
50  }
51  else if(type == DataType::uint64) { // 64 bit unsigned
52  _nDigits = 20; // 0 .. 2e19 (approx)
53  }
54  else if(type == DataType::float32) { // 32 bit float IEEE 754
55  _nDigits = 3 + 45; // todo. fix comments. see RegisterInfoMap::RegisterInfo::RegisterInfo
56  _nFractionalDigits = 45;
57  }
58  else if(type == DataType::float64) { // 64 bit float IEEE 754
59  _nDigits = 3 + 325; // todo. fix comments. see RegisterInfoMap::RegisterInfo::RegisterInfo
60  _nFractionalDigits = 325;
61  }
62  else if(type == DataType::Boolean) {
63  _fundamentalType = FundamentalType::boolean;
64  }
65  else if(type == DataType::Void) {
66  _fundamentalType = FundamentalType::nodata;
67  }
68  }
69 
70  /********************************************************************************************************************/
71 
73  : _fundamentalType(FundamentalType::undefined), _isIntegral(false), _isSigned(false), _nDigits(0),
74  _nFractionalDigits(0) {}
75 
76  /********************************************************************************************************************/
77 
79  return _fundamentalType;
80  }
81 
82  /********************************************************************************************************************/
83 
84  bool DataDescriptor::isSigned() const {
85  assert(_fundamentalType == FundamentalType::numeric);
86  return _isSigned;
87  }
88 
89  /********************************************************************************************************************/
90 
92  assert(_fundamentalType == FundamentalType::numeric);
93  return _isIntegral;
94  }
95 
96  /********************************************************************************************************************/
97 
98  size_t DataDescriptor::nDigits() const {
99  assert(_fundamentalType == FundamentalType::numeric);
100  return _nDigits;
101  }
102 
103  /********************************************************************************************************************/
104 
106  assert(_fundamentalType == FundamentalType::numeric && !_isIntegral);
107  return _nFractionalDigits;
108  }
109 
110  /********************************************************************************************************************/
111 
113  return _rawDataType;
114  }
115 
117  _rawDataType = d;
118  }
119 
120  /********************************************************************************************************************/
121 
123  return _transportLayerDataType;
124  }
125 
126  /********************************************************************************************************************/
127 
128  bool DataDescriptor::operator==(const DataDescriptor& other) const {
129  return _fundamentalType == other._fundamentalType && _rawDataType == other._rawDataType &&
130  _transportLayerDataType == other._transportLayerDataType && _isIntegral == other._isIntegral &&
131  _isSigned == other._isSigned && _nDigits == other._nDigits && _nFractionalDigits == other._nFractionalDigits;
132  }
133 
134  /********************************************************************************************************************/
135 
136  bool DataDescriptor::operator!=(const DataDescriptor& other) const {
137  return !operator==(other);
138  }
139 
140  /********************************************************************************************************************/
141 
144  if(isIntegral()) {
145  if(isSigned()) {
146  if(nDigits() > 11) {
147  return typeid(int64_t);
148  }
149  if(nDigits() > 6) {
150  return typeid(int32_t);
151  }
152  if(nDigits() > 4) {
153  return typeid(int16_t);
154  }
155  return typeid(int8_t);
156  }
157  if(nDigits() > 10) {
158  return typeid(uint64_t);
159  }
160  if(nDigits() > 5) {
161  return typeid(uint32_t);
162  }
163  if(nDigits() > 3) {
164  return typeid(uint16_t);
165  }
166  return typeid(uint8_t);
167  }
168  // fractional:
169  // Maximum number of decimal digits to display a float without loss in non-exponential display, including
170  // sign, leading 0, decimal dot and one extra digit to avoid rounding issues (hence the +4).
171  // This computation matches the one performed in the NumericAddressedBackend catalogue.
172  size_t floatMaxDigits = 4 +
173  size_t(std::max(
174  std::log10(std::numeric_limits<float>::max()), -std::log10(std::numeric_limits<float>::denorm_min())));
175  if(nDigits() > floatMaxDigits) {
176  return typeid(double);
177  }
178  return typeid(float);
179  }
181  return typeid(ChimeraTK::Boolean);
182  }
184  return typeid(std::string);
185  }
187  return typeid(ChimeraTK::Void);
188  }
189  return {};
190  }
191 
192  /********************************************************************************************************************/
193  /********************************************************************************************************************/
194 
195  std::ostream& operator<<(std::ostream& stream, const DataDescriptor::FundamentalType& fundamentalType) {
196  if(fundamentalType == DataDescriptor::FundamentalType::nodata) {
197  stream << "nodata";
198  }
199  else if(fundamentalType == DataDescriptor::FundamentalType::boolean) {
200  stream << "boolean";
201  }
202  else if(fundamentalType == DataDescriptor::FundamentalType::numeric) {
203  stream << "numeric";
204  }
205  else if(fundamentalType == DataDescriptor::FundamentalType::string) {
206  stream << "string";
207  }
208  else if(fundamentalType == DataDescriptor::FundamentalType::undefined) {
209  stream << "undefined";
210  }
211  return stream;
212  }
213  /********************************************************************************************************************/
214 
215 } // namespace ChimeraTK
ChimeraTK::Void
Wrapper Class for void.
Definition: SupportedUserTypes.h:71
DataDescriptor.h
ChimeraTK::DataType::uint8
@ uint8
Unsigned 8 bit integer.
Definition: SupportedUserTypes.h:608
ChimeraTK::DataType::isIntegral
bool isIntegral() const
Return whether the raw data type is an integer.
Definition: SupportedUserTypes.h:643
ChimeraTK::DataType::float64
@ float64
Double precision float.
Definition: SupportedUserTypes.h:616
ChimeraTK::DataType::isSigned
bool isSigned() const
Return whether the raw data type is signed.
Definition: SupportedUserTypes.h:664
ChimeraTK::DataType::float32
@ float32
Single precision float.
Definition: SupportedUserTypes.h:615
ChimeraTK::DataDescriptor::operator!=
bool operator!=(const DataDescriptor &other) const
Definition: DataDescriptor.cpp:136
ChimeraTK::DataType::int32
@ int32
Signed 32 bit integer.
Definition: SupportedUserTypes.h:611
ChimeraTK::DataDescriptor::setRawDataType
void setRawDataType(const DataType &d)
Set the raw data type.
Definition: DataDescriptor.cpp:116
ChimeraTK::DataDescriptor::transportLayerDataType
DataType transportLayerDataType() const
Get the data type on the transport layer.
Definition: DataDescriptor.cpp:122
ChimeraTK::DataDescriptor::FundamentalType::nodata
@ nodata
ChimeraTK::DataType::Boolean
@ Boolean
Boolean.
Definition: SupportedUserTypes.h:618
ChimeraTK::DataDescriptor::minimumDataType
DataType minimumDataType() const
Get the minimum data type required to represent the described data type in the host CPU.
Definition: DataDescriptor.cpp:142
ChimeraTK::DataType::uint32
@ uint32
Unsigned 32 bit integer.
Definition: SupportedUserTypes.h:612
ChimeraTK::DataDescriptor
Class describing the actual payload data format of a register in an abstract manner.
Definition: DataDescriptor.h:19
ChimeraTK::DataDescriptor::fundamentalType
FundamentalType fundamentalType() const
Get the fundamental data type.
Definition: DataDescriptor.cpp:78
ChimeraTK::DataDescriptor::nFractionalDigits
size_t nFractionalDigits() const
Approximate maximum number of digits after decimal dot (of base 10) needed to represent the value (ex...
Definition: DataDescriptor.cpp:105
ChimeraTK::DataDescriptor::FundamentalType::boolean
@ boolean
ChimeraTK::DataDescriptor::isSigned
bool isSigned() const
Return whether the data is signed or not.
Definition: DataDescriptor.cpp:84
ChimeraTK::DataDescriptor::operator==
bool operator==(const DataDescriptor &other) const
Definition: DataDescriptor.cpp:128
ChimeraTK::DataType::Void
@ Void
Void.
Definition: SupportedUserTypes.h:619
ChimeraTK::DataType::uint16
@ uint16
Unsigned 16 bit integer.
Definition: SupportedUserTypes.h:610
ChimeraTK::DataDescriptor::FundamentalType::undefined
@ undefined
ChimeraTK::DataType::isNumeric
bool isNumeric() const
Returns whether the data type is numeric.
Definition: SupportedUserTypes.h:681
ChimeraTK::DataDescriptor::FundamentalType::numeric
@ numeric
ChimeraTK::DataType
A class to describe which of the supported data types is used.
Definition: SupportedUserTypes.h:599
ChimeraTK::DataDescriptor::rawDataType
DataType rawDataType() const
Get the raw data type.
Definition: DataDescriptor.cpp:112
ChimeraTK::DataType::int64
@ int64
Signed 64 bit integer.
Definition: SupportedUserTypes.h:613
ChimeraTK::DataDescriptor::FundamentalType
FundamentalType
Enum for the fundamental data types.
Definition: DataDescriptor.h:25
ChimeraTK::DataType::int8
@ int8
Signed 8 bit integer.
Definition: SupportedUserTypes.h:607
ChimeraTK::operator<<
std::ostream & operator<<(std::ostream &stream, const DataDescriptor::FundamentalType &fundamentalType)
Definition: DataDescriptor.cpp:195
ChimeraTK::DataDescriptor::isIntegral
bool isIntegral() const
Return whether the data is integral or not (e.g.
Definition: DataDescriptor.cpp:91
ChimeraTK::DataDescriptor::nDigits
size_t nDigits() const
Return the approximate maximum number of digits (of base 10) needed to represent the value (including...
Definition: DataDescriptor.cpp:98
ChimeraTK::DataDescriptor::DataDescriptor
DataDescriptor()
Default constructor sets fundamental type to "undefined".
Definition: DataDescriptor.cpp:72
ChimeraTK::DataType::int16
@ int16
Signed 16 bit integer.
Definition: SupportedUserTypes.h:609
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::Boolean
Wrapper Class to avoid vector<bool> problems.
Definition: SupportedUserTypes.h:21
ChimeraTK::DataType::uint64
@ uint64
Unsigned 64 bit integer.
Definition: SupportedUserTypes.h:614
ChimeraTK::DataDescriptor::FundamentalType::string
@ string