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