9 #ifndef H5DATATYPE_MISC_HPP
10 #define H5DATATYPE_MISC_HPP
16 #include <H5Ppublic.h>
17 #include <H5Tpublic.h>
23 inline DataTypeClass convert_type_class(
const H5T_class_t& tclass);
25 inline hid_t create_string(std::size_t length);
29 return _hid == H5I_INVALID_HID;
33 return convert_type_class(H5Tget_class(
_hid));
37 return H5Tget_size(
_hid);
41 return (H5Tequal(
_hid, other.
_hid) > 0);
45 return !(*
this == other);
49 auto var_value = H5Tis_variable_str(
_hid);
51 HDF5ErrMapper::ToException<DataTypeException>(
52 "Unable to define datatype size to variable");
54 return static_cast<bool>(var_value);
62 return H5Tequal(
_hid, H5T_STD_REF_OBJ) > 0;
72 _hid = H5Tcopy(H5T_NATIVE_CHAR);
77 _hid = H5Tcopy(H5T_NATIVE_CHAR);
82 _hid = H5Tcopy(H5T_NATIVE_UCHAR);
88 _hid = H5Tcopy(H5T_NATIVE_SHORT);
93 _hid = H5Tcopy(H5T_NATIVE_USHORT);
99 _hid = H5Tcopy(H5T_NATIVE_INT);
104 _hid = H5Tcopy(H5T_NATIVE_UINT);
110 _hid = H5Tcopy(H5T_NATIVE_LONG);
115 _hid = H5Tcopy(H5T_NATIVE_ULONG);
121 _hid = H5Tcopy(H5T_NATIVE_LLONG);
126 _hid = H5Tcopy(H5T_NATIVE_ULLONG);
132 _hid = H5Tcopy(H5T_NATIVE_FLOAT);
137 _hid = H5Tcopy(H5T_NATIVE_DOUBLE);
143 _hid = H5Tcopy(H5T_NATIVE_HBOOL);
149 _hid = create_string(H5T_VARIABLE);
154 template <
size_t StrLen>
160 template <
size_t StrLen>
168 static struct ComplexType :
public Object {
170 _hid = H5Tcreate(H5T_COMPOUND,
sizeof(std::complex<double>));
172 H5Tinsert(_hid,
"r", 0, H5T_NATIVE_DOUBLE);
173 H5Tinsert(_hid,
"i",
sizeof(
double), H5T_NATIVE_DOUBLE);
176 _hid = H5Tcopy(complexType.getId());
180 template <
typename T>
183 "Atomic types cant be arrays, except for char[] (fixed-len strings)");
190 template <std::
size_t N>
193 datavec.resize(length);
194 std::memcpy(datavec[0].data(), array[0].data(), N * length);
197 template <std::
size_t N>
200 datavec.resize(
static_cast<std::size_t
>(iter_end - iter_begin));
201 for (
auto& dst_array : datavec) {
202 const char* src = (iter_begin++)->c_str();
203 const size_t length = std::min(N - 1 , std::strlen(src));
204 std::memcpy(dst_array.data(), src, length);
205 dst_array[length] = 0;
209 template <std::
size_t N>
214 template <std::
size_t N>
219 template <std::
size_t N>
221 datavec.emplace_back();
222 const size_t length = std::min(N - 1 , src.length());
223 std::memcpy(datavec.back().data(), src.c_str(), length);
224 datavec.back()[length] = 0;
227 template <std::
size_t N>
229 datavec.emplace_back();
230 std::copy(src.begin(), src.end(), datavec.back().data());
233 template <std::
size_t N>
242 _hid = H5Tcopy(H5T_STD_REF_OBJ);
247 #define _H5_STRUCT_PADDING(current_size, member_size) (((member_size) - (current_size)) % (member_size))
249 inline void CompoundType::create(
size_t size) {
251 size_t current_size = 0, max_type_size = 0;
254 for (
auto& member: members) {
255 size_t member_size = H5Tget_size(member.base_type.getId());
256 if (member_size == 0) {
257 throw DataTypeException(
"Cannot get size of DataType with hid: " +
258 std::to_string(member.base_type.getId()));
266 current_size = member.offset + member_size;
268 max_type_size = std::max(max_type_size, member_size);
275 if((
_hid = H5Tcreate(H5T_COMPOUND, size)) < 0) {
276 HDF5ErrMapper::ToException<DataTypeException>(
277 "Could not create new compound datatype");
281 for (
const auto& member: members) {
282 if(H5Tinsert(
_hid, member.name.c_str(), member.offset, member.base_type.getId()) < 0) {
283 HDF5ErrMapper::ToException<DataTypeException>(
284 "Could not add new member to datatype"
290 #undef _H5_STRUCT_PADDING
293 H5Tcommit2(
object.
getId(), name.c_str(),
getId(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
299 if((_hid = H5Tenum_create(
AtomicType<
typename std::underlying_type<T>::type>{}.getId())) < 0) {
300 HDF5ErrMapper::ToException<DataTypeException>(
301 "Could not create new enum datatype");
305 for (
const auto& member: members) {
306 if(H5Tenum_insert(_hid, member.name.c_str(), &(member.value)) < 0) {
307 HDF5ErrMapper::ToException<DataTypeException>(
308 "Could not add new member to this enum datatype"
316 H5Tcommit2(
object.getId(), name.c_str(), getId(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
321 inline hid_t create_string(
size_t length){
322 hid_t _hid = H5Tcopy(H5T_C_S1);
323 if (H5Tset_size(_hid, length) < 0) {
324 HDF5ErrMapper::ToException<DataTypeException>(
325 "Unable to define datatype size to variable");
328 H5Tset_cset(_hid, H5T_CSET_UTF8);
333 inline DataTypeClass convert_type_class(
const H5T_class_t& tclass) {
398 template <
typename T>
405 template <
typename T>
416 std::ostringstream ss;
417 ss <<
"Size of array type " <<
sizeof(T)
418 <<
" != that of memory datatype " << t.
getSize()
429 #endif // H5DATATYPE_MISC_HPP