libadc-cxx 1.0.0
Structured logging for scientific computing
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1/* Copyright 2025 NTESS. See the top-level LICENSE.txt file for details.
2 *
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#ifndef adc_types_hpp
6#define adc_types_hpp
7#include <memory>
8#include <string>
9#include <string_view>
10#include <vector>
11#include <list>
12#include <set>
13#include <complex>
14#include <variant>
15#include <array>
16#include <sys/time.h>
17
18#include "adc/adc_config.h"
19
20namespace adc {
21
22/** \addtogroup API
23 * @{
24 */
25
26/*! \brief A version with tags list.
27 */
28struct ADC_VISIBLE version {
29 const std::string name;
30 const std::vector <std::string> tags;
31 version(const std::string n, std::vector <std::string> t) : name(n), tags(t) {}
32};
33
34/// \brief Set to 1 if 8/16 float types for gpus are supported.
35/// ADC_SUPPORT_GPU_FLOATS should be defined by build-time configuration
36#define ADC_SUPPORT_GPU_FLOATS 0
37
38/// \brief Set to 1 if 80 bit floats for cpus are supported.
39/// ADC_SUPPORT_EXTENDED_FLOATS should be defined by build-time configuration
40#define ADC_SUPPORT_EXTENDED_FLOATS 0
41
42/// \brief Set to 1 if 128 bit floats for cpus are supported.
43/// ADC_SUPPORT_QUAD_FLOATS should be defined by build-time configuration
44#define ADC_SUPPORT_QUAD_FLOATS 0
45
46/// \brief include boost::json support in the API
47/// ADC_BOOST_JSON_PUBLIC could be defined by build-time configuration.
48/// If it is, this library forces boost::json and other boost dependencies
49/// on the build of any application which uses it.
50#define ADC_BOOST_JSON_PUBLIC 0
51
52/*! \brief the version number of enum scalar_type and object_type
53 */
54inline version enum_version("1.0.0", {"none"});
55
56/*! \brief field types for scientific data encode/decode with json.
57 *
58 * Bit precision and C vs specialized strings are preserved
59 * when data is tagged following this enum.
60 * The add() functions on the builder api automatically tag with this.
61 */
64 cp_bool, //!< bool (true/false,1/0)
65 cp_char, //!< char (8 bit)
66 cp_char16, //!< char16_t
67 cp_char32, //!< char32_t
68 // string is problematic. For embedded nul data or utf-8, use array of char8
69 cp_cstr, //!< c null-terminated string
70 cp_json_str, //!< c null-terminated string that contains valid json
71 cp_yaml_str, //!< c null-terminated string that contains valid yaml
72 cp_xml_str, //!< c null-terminated string that contains valid xml
73 cp_json, //!< json value (object, list, etc)
74 cp_path, //!< c null-terminated string which names a file-system path
75 cp_number_str, //!< c null-terminated string containing an exact decimal representation of arbitrary precision
76 // unsigned int types
77 cp_uint8, //!< uint8_t
78 cp_uint16, //!< uint16_t
79 cp_uint32, //!< uint32_t
80 cp_uint64, //!< uint64_t
81 // signed int
82 cp_int8, //!< int8_t
83 cp_int16, //!< int16_t
84 cp_int32, //!< int32_t
85 cp_int64, //!< int64_t
86 // float
87 cp_f32, //!< 32 bit float
88 cp_f64, //!< 64 bit float
89 cp_f80, //!< 80 bit float; requires ADC_SUPPORT_EXTENDED_FLOATS support
90 cp_f128, //!< 128 bit float; requires ADC_SUPPORT_QUAD_FLOATS support
91 // mini float types
92 cp_f8_e4m3, //!< 8 bit float (3 mantissa, 4 exponent); requires ADC_SUPPORT_GPU_FLOATS support
93 cp_f8_e5m2, //!< 8 bit float (2 mantissa, 5 exponent); requires ADC_SUPPORT_GPU_FLOATS support
94 cp_f16_e5m10, //!< 16 bit float (10 mantissa, 5 exponent); requires ADC_SUPPORT_GPU_FLOATS support
95 cp_f16_e8m7, //!< 16 bit bfloat (7 mantissa, 8 exponent); requires ADC_SUPPORT_GPU_FLOATS support
96 // complex float types
97 cp_c_f32, //!< complex<float>
98 cp_c_f64, //!< complex<double>
99 cp_c_f80, //!< complex<extended>; requires ADC_SUPPORT_EXTENDED_FLOATS support
100 cp_c_f128, //!< complex<quad>; requires ADC_SUPPORT_QUAD_FLOATS support
101 // time types
102 cp_timespec, //!< (second, nanosecond) as int64_t, int64_t pair from clock_gettime
103 cp_timeval, //!< gettimeofday struct timeval (second, microsecond) as int64_t pair
104 cp_epoch, //!< time(NULL) seconds since the epoch (UNIX) as int64_t
105 // reserved but not yet supported
106 cp_char8, //!< unsigned 8-bit char; reserved
107 // end mark
108 cp_last
110/// when expanding scalar_type, always update enum.ipp to match.
111
117
118/*! \brief variant for querying builder data.
119 *
120 * Any changes here must be reflected in the var_string operations.
121 */
122typedef std::variant<
123 bool,
124 char,
125 char16_t,
126 char32_t,
127 int8_t,
128 int16_t,
129 int32_t,
130 int64_t,
131 uint8_t,
132 uint16_t,
133 uint32_t,
134 uint64_t,
135 float,
136 double,
137 std::complex<float>,
138 std::complex<double>,
139 std::array<int64_t, 2>,
140 std::string,
141 std::shared_ptr<bool[]>,
142 std::shared_ptr<char[]>,
143 std::shared_ptr<char16_t[]>,
144 std::shared_ptr<char32_t[]>,
145 std::shared_ptr<int8_t[]>,
146 std::shared_ptr<int16_t[]>,
147 std::shared_ptr<int32_t[]>,
148 std::shared_ptr<int64_t[]>,
149 std::shared_ptr<uint8_t[]>,
150 std::shared_ptr<uint16_t[]>,
151 std::shared_ptr<uint32_t[]>,
152 std::shared_ptr<uint64_t[]>,
153 std::shared_ptr<float[]>,
154 std::shared_ptr<double[]>,
155 std::shared_ptr<std::complex<float>[]>,
156 std::shared_ptr<std::complex<double>[]>,
157 std::shared_ptr<std::string[]>
158 // fixme: need to ifdef extended/quad/gpu types here in the variant definition.
160
161// these indicate how to handle a data pointer returned from builder lookups.
162// vp is often pointing into data, but not in all cases (string, 8byte types).
163struct field {
164 key_type kt; //!< kind of data associated with the name queried
165 scalar_type st; //!< scalar type of the data as published,
166 const void *vp; //!< address of data to be cast according to st for use with c/fortran
167 size_t count; //!< number of elements in vp.
168 std::string container; //!< name of the container variety given to see builder::add_array
170};
171
172/*! \brief get the string representation of a scalar_type value */
173ADC_VISIBLE const std::string to_string(scalar_type st);
174
175/*! \brief get string of float using to_chars. */
176ADC_VISIBLE const std::string to_string(float);
177
178/*! \brief get string of double using to_chars. */
179ADC_VISIBLE const std::string to_string(double);
180
181/*! \brief get string of array */
182ADC_VISIBLE const std::string to_string(void *data, scalar_type cptype, size_t count);
183
184/*! \brief get the enum representation of a scalar_type string */
185ADC_VISIBLE scalar_type scalar_type_from_name(const std::string& name);
186
187/*! \brief return non-zero if to_string and enum scalar_type are inconsisent. */
188ADC_VISIBLE int test_enum_strings();
189
190/*! \brief classification of json-adjacent structure elements.
191 * This is not currently in use and may be retired soon.
192 */
194 co_list = cp_last, //!< ordered list of arbitrary values
195 co_map, //!< string keyed map of arbitrary values
196 co_array, //!< 0-indexed continguous array of type-identical values
197 co_scalar //!< single value
199
200/*! \brief return string for printing from variant v. */
202public:
203 std::string operator()(const bool x) const { return std::string(x ? "true": "false"); }
204 std::string operator()(char x) const { return std::string(1, x); }
205 std::string operator()(char16_t x) const { return std::to_string((uint16_t) x); }
206 std::string operator()(char32_t x) const { return std::to_string((uint32_t) x); }
207 std::string operator()(int8_t x) const { return std::to_string(x); }
208 std::string operator()(int16_t x) const { return std::to_string(x); }
209 std::string operator()(int32_t x) const { return std::to_string(x); }
210 std::string operator()(int64_t x) const { return std::to_string(x); }
211 std::string operator()(uint8_t x) const { return std::to_string(x); }
212 std::string operator()(uint16_t x) const { return std::to_string(x); }
213 std::string operator()(uint32_t x) const { return std::to_string(x); }
214 std::string operator()(uint64_t x) const { return std::to_string(x); }
215 std::string operator()(float x) const { return to_string(x); }
216 std::string operator()(double x) const { return to_string(x); }
217 std::string operator()(std::complex<float> x) const { return to_string(x.real()) + ",i" + to_string(x.imag()); }
218 std::string operator()(std::complex<double> x) const { return to_string(x.real()) + ",i" + to_string(x.imag()); }
219 std::string operator()(std::array<int64_t, 2> x) const { return std::to_string(x[0]) + "," + std::to_string(x[1]); }
220 std::string operator()(std::string x) const { return std::string(x); }
221 std::string operator()(std::shared_ptr<bool[]> x) const { return to_string((void *)x.get(), cp_bool, count); }
222 std::string operator()(std::shared_ptr<char[]> x) const { return to_string((void *)x.get(), cp_char, count); }
223 std::string operator()(std::shared_ptr<char16_t[]> x) const { return to_string((void *)x.get(), cp_char16, count); }
224 std::string operator()(std::shared_ptr<char32_t[]> x) const { return to_string((void *)x.get(), cp_char32, count); }
225 std::string operator()(std::shared_ptr<int8_t[]> x) const { return to_string((void *)x.get(), cp_int8, count); }
226 std::string operator()(std::shared_ptr<int16_t[]> x) const { return to_string((void *)x.get(), cp_int16, count); }
227 std::string operator()(std::shared_ptr<int32_t[]> x) const { return to_string((void *)x.get(), cp_int32, count); }
228 std::string operator()(std::shared_ptr<int64_t[]> x) const { return to_string((void *)x.get(), cp_int64, count); }
229 std::string operator()(std::shared_ptr<uint8_t[]> x) const { return to_string((void *)x.get(), cp_uint8, count); }
230 std::string operator()(std::shared_ptr<uint16_t[]> x) const { return to_string((void *)x.get(), cp_uint16, count); }
231 std::string operator()(std::shared_ptr<uint32_t[]> x) const { return to_string((void *)x.get(), cp_uint32, count); }
232 std::string operator()(std::shared_ptr<uint64_t[]> x) const { return to_string((void *)x.get(), cp_uint64, count); }
233 std::string operator()(std::shared_ptr<float[]> x) const { return to_string((void *)x.get(), cp_f32, count); }
234 std::string operator()(std::shared_ptr<double[]> x) const { return to_string((void *)x.get(), cp_f64, count); }
235 std::string operator()(std::shared_ptr<std::complex<float>[]> x) const { return to_string((void *)x.get(), cp_c_f32, count); }
236 std::string operator()(std::shared_ptr<std::complex<double>[]> x) const { return to_string((void *)x.get(), cp_c_f64, count); }
237 std::string operator()(std::shared_ptr<std::string[]> x) const { return to_string((void *)x.get(), cp_cstr, count); }
238 // fixme: need to ifdef extended/quad/gpu types here in the variant definition.
239
240 var_string() : count(1) {}
241 var_string(size_t count) : count(count) {}
242
243private:
244 size_t count;
245};
246
247/** @}*/
248};
249#endif // adc_types_hpp
return string for printing from variant v.
Definition types.hpp:201
std::string operator()(char32_t x) const
Definition types.hpp:206
std::string operator()(int64_t x) const
Definition types.hpp:210
std::string operator()(std::shared_ptr< int16_t[]> x) const
Definition types.hpp:226
std::string operator()(std::complex< double > x) const
Definition types.hpp:218
std::string operator()(std::shared_ptr< int64_t[]> x) const
Definition types.hpp:228
std::string operator()(std::array< int64_t, 2 > x) const
Definition types.hpp:219
std::string operator()(int8_t x) const
Definition types.hpp:207
std::string operator()(uint32_t x) const
Definition types.hpp:213
std::string operator()(int32_t x) const
Definition types.hpp:209
std::string operator()(uint8_t x) const
Definition types.hpp:211
std::string operator()(std::shared_ptr< std::complex< double >[]> x) const
Definition types.hpp:236
std::string operator()(std::shared_ptr< std::string[]> x) const
Definition types.hpp:237
std::string operator()(uint64_t x) const
Definition types.hpp:214
std::string operator()(std::shared_ptr< std::complex< float >[]> x) const
Definition types.hpp:235
std::string operator()(char x) const
Definition types.hpp:204
std::string operator()(float x) const
Definition types.hpp:215
std::string operator()(std::shared_ptr< int32_t[]> x) const
Definition types.hpp:227
std::string operator()(int16_t x) const
Definition types.hpp:208
std::string operator()(char16_t x) const
Definition types.hpp:205
std::string operator()(std::shared_ptr< uint64_t[]> x) const
Definition types.hpp:232
std::string operator()(std::shared_ptr< uint32_t[]> x) const
Definition types.hpp:231
std::string operator()(uint16_t x) const
Definition types.hpp:212
std::string operator()(std::shared_ptr< float[]> x) const
Definition types.hpp:233
std::string operator()(std::shared_ptr< uint16_t[]> x) const
Definition types.hpp:230
var_string(size_t count)
Definition types.hpp:241
std::string operator()(std::shared_ptr< char[]> x) const
Definition types.hpp:222
std::string operator()(double x) const
Definition types.hpp:216
std::string operator()(std::shared_ptr< int8_t[]> x) const
Definition types.hpp:225
std::string operator()(std::shared_ptr< char32_t[]> x) const
Definition types.hpp:224
std::string operator()(const bool x) const
Definition types.hpp:203
std::string operator()(std::string x) const
Definition types.hpp:220
std::string operator()(std::shared_ptr< bool[]> x) const
Definition types.hpp:221
std::string operator()(std::shared_ptr< char16_t[]> x) const
Definition types.hpp:223
std::string operator()(std::shared_ptr< double[]> x) const
Definition types.hpp:234
std::string operator()(std::complex< float > x) const
Definition types.hpp:217
std::string operator()(std::shared_ptr< uint8_t[]> x) const
Definition types.hpp:229
int test_enum_strings()
return non-zero if to_string and enum scalar_type are inconsisent.
Definition enums.ipp:364
key_type
when expanding scalar_type, always update enum.ipp to match.
Definition types.hpp:112
scalar_type scalar_type_from_name(const std::string &name)
get the enum representation of a scalar_type string
Definition enums.ipp:104
version enum_version("1.0.0", {"none"})
the version number of enum scalar_type and object_type
std::variant< bool, char, char16_t, char32_t, int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t, float, double, std::complex< float >, std::complex< double >, std::array< int64_t, 2 >, std::string, std::shared_ptr< bool[]>, std::shared_ptr< char[]>, std::shared_ptr< char16_t[]>, std::shared_ptr< char32_t[]>, std::shared_ptr< int8_t[]>, std::shared_ptr< int16_t[]>, std::shared_ptr< int32_t[]>, std::shared_ptr< int64_t[]>, std::shared_ptr< uint8_t[]>, std::shared_ptr< uint16_t[]>, std::shared_ptr< uint32_t[]>, std::shared_ptr< uint64_t[]>, std::shared_ptr< float[]>, std::shared_ptr< double[]>, std::shared_ptr< std::complex< float >[]>, std::shared_ptr< std::complex< double >[]>, std::shared_ptr< std::string[]> > variant
variant for querying builder data.
Definition types.hpp:159
object_type
classification of json-adjacent structure elements. This is not currently in use and may be retired s...
Definition types.hpp:193
const std::string to_string(float f)
get string of float using to_chars.
Definition enums.ipp:132
scalar_type
field types for scientific data encode/decode with json.
Definition types.hpp:62
@ k_value
Definition types.hpp:115
@ k_none
Definition types.hpp:113
@ k_section
Definition types.hpp:114
@ co_map
string keyed map of arbitrary values
Definition types.hpp:195
@ co_scalar
single value
Definition types.hpp:197
@ co_list
ordered list of arbitrary values
Definition types.hpp:194
@ co_array
0-indexed continguous array of type-identical values
Definition types.hpp:196
@ cp_xml_str
c null-terminated string that contains valid xml
Definition types.hpp:72
@ cp_int64
int64_t
Definition types.hpp:85
@ cp_epoch
time(NULL) seconds since the epoch (UNIX) as int64_t
Definition types.hpp:104
@ cp_f16_e8m7
16 bit bfloat (7 mantissa, 8 exponent); requires ADC_SUPPORT_GPU_FLOATS support
Definition types.hpp:95
@ cp_int32
int32_t
Definition types.hpp:84
@ cp_c_f32
complex<float>
Definition types.hpp:97
@ cp_char
char (8 bit)
Definition types.hpp:65
@ cp_path
c null-terminated string which names a file-system path
Definition types.hpp:74
@ cp_char16
char16_t
Definition types.hpp:66
@ cp_json_str
c null-terminated string that contains valid json
Definition types.hpp:70
@ cp_timespec
(second, nanosecond) as int64_t, int64_t pair from clock_gettime
Definition types.hpp:102
@ cp_timeval
gettimeofday struct timeval (second, microsecond) as int64_t pair
Definition types.hpp:103
@ cp_c_f64
complex<double>
Definition types.hpp:98
@ cp_bool
bool (true/false,1/0)
Definition types.hpp:64
@ cp_int8
int8_t
Definition types.hpp:82
@ cp_char8
unsigned 8-bit char; reserved
Definition types.hpp:106
@ cp_f128
128 bit float; requires ADC_SUPPORT_QUAD_FLOATS support
Definition types.hpp:90
@ cp_uint16
uint16_t
Definition types.hpp:78
@ cp_json
json value (object, list, etc)
Definition types.hpp:73
@ cp_c_f80
complex<extended>; requires ADC_SUPPORT_EXTENDED_FLOATS support
Definition types.hpp:99
@ cp_number_str
c null-terminated string containing an exact decimal representation of arbitrary precision
Definition types.hpp:75
@ cp_uint32
uint32_t
Definition types.hpp:79
@ cp_int16
int16_t
Definition types.hpp:83
@ cp_f64
64 bit float
Definition types.hpp:88
@ cp_f80
80 bit float; requires ADC_SUPPORT_EXTENDED_FLOATS support
Definition types.hpp:89
@ cp_none
Definition types.hpp:63
@ cp_f32
32 bit float
Definition types.hpp:87
@ cp_yaml_str
c null-terminated string that contains valid yaml
Definition types.hpp:71
@ cp_f16_e5m10
16 bit float (10 mantissa, 5 exponent); requires ADC_SUPPORT_GPU_FLOATS support
Definition types.hpp:94
@ cp_c_f128
complex<quad>; requires ADC_SUPPORT_QUAD_FLOATS support
Definition types.hpp:100
@ cp_cstr
c null-terminated string
Definition types.hpp:69
@ cp_char32
char32_t
Definition types.hpp:67
@ cp_uint64
uint64_t
Definition types.hpp:80
@ cp_uint8
uint8_t
Definition types.hpp:77
@ cp_f8_e5m2
8 bit float (2 mantissa, 5 exponent); requires ADC_SUPPORT_GPU_FLOATS support
Definition types.hpp:93
@ cp_f8_e4m3
8 bit float (3 mantissa, 4 exponent); requires ADC_SUPPORT_GPU_FLOATS support
Definition types.hpp:92
@ cp_last
Definition types.hpp:108
Definition adc.hpp:82
std::string container
name of the container variety given to see builder::add_array
Definition types.hpp:168
scalar_type st
scalar type of the data as published,
Definition types.hpp:165
size_t count
number of elements in vp.
Definition types.hpp:167
variant data
Definition types.hpp:169
key_type kt
kind of data associated with the name queried
Definition types.hpp:164
const void * vp
address of data to be cast according to st for use with c/fortran
Definition types.hpp:166
A version with tags list.
Definition types.hpp:28
version(const std::string n, std::vector< std::string > t)
Definition types.hpp:31
const std::string name
Definition types.hpp:29
const std::vector< std::string > tags
Definition types.hpp:30