NGen
Loading...
Searching...
No Matches
bmi_primitive_type.hpp
1#ifndef NGEN_BMI_PRIMITIVE_TYPE_HPP
2#define NGEN_BMI_PRIMITIVE_TYPE_HPP
3
4#include <cstddef>
5#include <cstdint>
6#include <limits>
7#include <stdexcept>
8#include <string>
9#include <string_view>
10#include <type_traits>
11
12/*
13 * The type of one item of a BMI variable, resolved from the name a model reports.
14 *
15 * primitive_type the item types a variable can have, independent of any language's spelling
16 * parse_bmi_type() a type name to its primitive_type; the one table of spellings
17 * with_primitive_type() call a callable with the C++ type a primitive_type denotes
18 *
19 * Kept apart from bmi_utilities.hpp, which builds typed access to model variables on top of this,
20 * so that Bmi_Adapter can name a primitive_type without depending on those utilities.
21 */
22
23namespace models {
24 namespace bmi {
26 struct unknown_bmi_type : std::runtime_error {
27 explicit unknown_bmi_type(std::string_view type_name)
28 : std::runtime_error("No C++ type corresponds to the BMI type name '" + std::string(type_name) + "'")
30
31 std::string type_name;
32 };
33
50 enum class primitive_type : std::uint8_t {
51 i8, u8, i16, u16, i32, u32, i64, u64,
52 f32, f64,
54 f128,
55 };
56
62 inline constexpr bool long_double_is_float128 = std::numeric_limits<long double>::digits == 113;
63
65 constexpr const char *to_string(primitive_type type)
66 {
67 switch (type) {
68 case primitive_type::i8: return "i8";
69 case primitive_type::u8: return "u8";
70 case primitive_type::i16: return "i16";
71 case primitive_type::u16: return "u16";
72 case primitive_type::i32: return "i32";
73 case primitive_type::u32: return "u32";
74 case primitive_type::i64: return "i64";
75 case primitive_type::u64: return "u64";
76 case primitive_type::f32: return "f32";
77 case primitive_type::f64: return "f64";
78 case primitive_type::long_double: return "long_double";
79 case primitive_type::f128: return "f128";
80 }
81 return "?";
82 }
83
85 constexpr size_t size_of(primitive_type type)
86 {
87 switch (type) {
88 case primitive_type::i8: case primitive_type::u8: return 1;
89 case primitive_type::i16: case primitive_type::u16: return 2;
92 case primitive_type::long_double: return sizeof(long double);
93 case primitive_type::f128: return 16;
94 }
95 return 0;
96 }
97
104 template <typename T>
106 {
107 if constexpr (std::is_same_v<T, long double>) {
109 } else if constexpr (std::is_same_v<T, double>) {
110 return primitive_type::f64;
111 } else if constexpr (std::is_same_v<T, float>) {
112 return primitive_type::f32;
113 } else {
114 static_assert(std::is_integral_v<T> && !std::is_same_v<T, bool>, "No primitive_type for this type");
115 static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8,
116 "No primitive_type of this integer's width");
117 constexpr bool is_signed = std::is_signed_v<T>;
118 if constexpr (sizeof(T) == 1) return is_signed ? primitive_type::i8 : primitive_type::u8;
119 else if constexpr (sizeof(T) == 2) return is_signed ? primitive_type::i16 : primitive_type::u16;
120 else if constexpr (sizeof(T) == 4) return is_signed ? primitive_type::i32 : primitive_type::u32;
121 else return is_signed ? primitive_type::i64 : primitive_type::u64;
122 }
123 }
124
126 struct unsupported_primitive_type : std::runtime_error {
128 : std::runtime_error(std::string("No C++ type for primitive type ") + to_string(type) +
129 " on this platform")
130 , type(type) {}
131
133 };
134
171 template <typename F>
173 {
174 switch (type) {
175 case primitive_type::i8: return f.template operator()<std::int8_t>();
176 case primitive_type::u8: return f.template operator()<std::uint8_t>();
177 case primitive_type::i16: return f.template operator()<std::int16_t>();
178 case primitive_type::u16: return f.template operator()<std::uint16_t>();
179 case primitive_type::i32: return f.template operator()<std::int32_t>();
180 case primitive_type::u32: return f.template operator()<std::uint32_t>();
181 case primitive_type::i64: return f.template operator()<std::int64_t>();
182 case primitive_type::u64: return f.template operator()<std::uint64_t>();
183 case primitive_type::f32: return f.template operator()<float>();
184 case primitive_type::f64: return f.template operator()<double>();
185 case primitive_type::long_double: return f.template operator()<long double>();
187 // Where long double is not binary128, GCC and Clang offer __float128 on x86-64, and C++23
188 // std::float128_t where <stdfloat> defines it. Supporting f128 there means dispatching to
189 // that type here, which every callable then has to compile for, as for long double.
190 if constexpr (long_double_is_float128) {
191 return f.template operator()<long double>();
192 } else {
193 break;
194 }
195 }
196 throw unsupported_primitive_type(type);
197 }
198
211 inline primitive_type parse_bmi_type(std::string_view type)
212 {
213 if (type == "double" || type == "double precision")
214 return primitive_type::f64;
215 if (type == "long double")
217 if (type == "float" || type == "real")
218 return primitive_type::f32;
219
220 if (type == "int8" || type == "int8_t")
221 return primitive_type::i8;
222 if (type == "uint8" || type == "uint8_t")
223 return primitive_type::u8;
224 if (type == "int16" || type == "int16_t")
225 return primitive_type::i16;
226 if (type == "uint16" || type == "uint16_t")
227 return primitive_type::u16;
228 if (type == "int32" || type == "int32_t")
229 return primitive_type::i32;
230 if (type == "uint32" || type == "uint32_t")
231 return primitive_type::u32;
232 if (type == "int64" || type == "int64_t")
233 return primitive_type::i64;
234 if (type == "uint64" || type == "uint64_t")
235 return primitive_type::u64;
236
237 if (type == "short" || type == "short int" || type == "signed short" || type == "signed short int")
239 if (type == "unsigned short" || type == "unsigned short int")
241 if (type == "int" || type == "signed" || type == "signed int" || type == "integer")
242 return primitive_type_of<int>();
243 if (type == "unsigned" || type == "unsigned int")
245 if (type == "long" || type == "long int" || type == "signed long" || type == "signed long int")
247 if (type == "unsigned long" || type == "unsigned long int")
249 if (type == "long long" || type == "long long int" || type == "signed long long" || type == "signed long long int")
251 if (type == "unsigned long long" || type == "unsigned long long int")
253
254 throw unknown_bmi_type(type);
255 }
256 }
257}
258
259#endif //NGEN_BMI_PRIMITIVE_TYPE_HPP
Definition deserialization.hpp:40
constexpr const char * to_string(primitive_type type)
The enumerator's name, e.g.
Definition bmi_primitive_type.hpp:65
auto with_primitive_type(primitive_type type, F &&f)
Call f with the C++ type that type denotes.
Definition bmi_primitive_type.hpp:172
primitive_type
The type of one item of a BMI variable, independent of how any language spells it.
Definition bmi_primitive_type.hpp:50
constexpr primitive_type primitive_type_of()
The primitive_type of the C++ arithmetic type.
Definition bmi_primitive_type.hpp:105
constexpr bool long_double_is_float128
Whether long double is IEEE binary128, the one format with a 113-bit significand.
Definition bmi_primitive_type.hpp:62
primitive_type parse_bmi_type(std::string_view type)
The primitive_type a BMI type name denotes.
Definition bmi_primitive_type.hpp:211
constexpr size_t size_of(primitive_type type)
The size in bytes of one item of type.
Definition bmi_primitive_type.hpp:85
Definition AbstractCLibBmiAdapter.hpp:6
STL namespace.
Thrown when a BMI type name has no C++ type behind it.
Definition bmi_primitive_type.hpp:26
std::string type_name
Definition bmi_primitive_type.hpp:31
unknown_bmi_type(std::string_view type_name)
Definition bmi_primitive_type.hpp:27
Thrown when a primitive_type has no C++ type on this platform.
Definition bmi_primitive_type.hpp:126
unsupported_primitive_type(primitive_type type)
Definition bmi_primitive_type.hpp:127
primitive_type type
Definition bmi_primitive_type.hpp:132