9#ifndef H5DATATYPE_MISC_HPP
10#define H5DATATYPE_MISC_HPP
23inline DataTypeClass convert_type_class(
const H5T_class_t& tclass);
25inline 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_SCHAR);
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);
154template <
size_t StrLen>
160template <
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());
182 static_assert(details::inspector<T>::recursive_ndim == 0,
183 "Atomic types cant be arrays, except for char[] (fixed-length strings)");
184 static_assert(details::inspector<T>::recursive_ndim > 0,
"Type not supported");
190template <std::
size_t N>
194 std::memcpy(datavec[0].data(), array[0].data(), N * length);
197template <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;
209template <std::
size_t N>
214template <std::
size_t N>
219template <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;
227template <std::
size_t N>
229 datavec.emplace_back();
230 std::copy(src.begin(), src.end(), datavec.back().data());
233template <std::
size_t N>
235 return std::string(datavec[i].data());
242 _hid = H5Tcopy(H5T_STD_REF_OBJ);
248 if (H5Tget_class(hid) != H5T_COMPOUND) {
249 return H5Tget_size(hid);
252 auto number_of_members = H5Tget_nmembers(hid);
253 if (number_of_members == -1) {
255 std::to_string(hid));
257 if (number_of_members == 0) {
259 std::to_string(hid));
262 auto member_type = H5Tget_member_type(hid, 0);
264 H5Tclose(member_type);
269#define _H5_STRUCT_PADDING(current_size, member_size) (((member_size) - (current_size)) % (member_size))
271inline void CompoundType::create(
size_t size) {
273 size_t current_size = 0, max_atomic_size = 0;
276 for (
auto& member: members) {
277 size_t member_size = H5Tget_size(member.base_type.getId());
279 if (member_size == 0) {
280 throw DataTypeException(
"Cannot get size of DataType with hid: " +
281 std::to_string(member.base_type.getId()));
292 current_size = member.offset + member_size;
296 max_atomic_size = std::max(max_atomic_size, first_atomic_size);
303 if((
_hid = H5Tcreate(H5T_COMPOUND, size)) < 0) {
304 HDF5ErrMapper::ToException<DataTypeException>(
305 "Could not create new compound datatype");
309 for (
const auto& member: members) {
310 if(H5Tinsert(
_hid, member.name.c_str(), member.offset, member.base_type.getId()) < 0) {
311 HDF5ErrMapper::ToException<DataTypeException>(
312 "Could not add new member to datatype"
318#undef _H5_STRUCT_PADDING
321 H5Tcommit2(
object.
getId(), name.c_str(),
getId(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
327 if((_hid = H5Tenum_create(
AtomicType<
typename std::underlying_type<T>::type>{}.
getId())) < 0) {
328 HDF5ErrMapper::ToException<DataTypeException>(
329 "Could not create new enum datatype");
333 for (
const auto& member: members) {
334 if(H5Tenum_insert(_hid, member.name.c_str(), &(member.value)) < 0) {
335 HDF5ErrMapper::ToException<DataTypeException>(
336 "Could not add new member to this enum datatype"
344 H5Tcommit2(
object.getId(), name.c_str(), getId(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
349inline hid_t create_string(
size_t length){
350 hid_t _hid = H5Tcopy(H5T_C_S1);
351 if (H5Tset_size(_hid, length) < 0) {
352 HDF5ErrMapper::ToException<DataTypeException>(
353 "Unable to define datatype size to variable");
356 H5Tset_cset(_hid, H5T_CSET_UTF8);
361inline DataTypeClass convert_type_class(
const H5T_class_t& tclass) {
451 if (
sizeof(T) != t.
getSize()) {
452 std::ostringstream ss;
453 ss <<
"Size of array type " <<
sizeof(T)
454 <<
" != that of memory datatype " << t.
getSize()
#define _H5_STRUCT_PADDING(current_size, member_size)
Definition: H5DataType_misc.hpp:269
AtomicType()
Definition: H5DataType_misc.hpp:163
AtomicType()
Definition: H5DataType_misc.hpp:157
create an HDF5 DataType from a C++ type
Definition: H5DataType.hpp:100
AtomicType()
Definition: H5DataType_misc.hpp:181
void commit(const Object &object, const std::string &name) const
Commit datatype into the given Object.
Definition: H5DataType_misc.hpp:320
Exception specific to HighFive DataType interface.
Definition: H5Exception.hpp:83
HDF5 Data Type.
Definition: H5DataType.hpp:42
bool operator==(const DataType &other) const
Definition: H5DataType_misc.hpp:40
bool isFixedLenStr() const
Returns whether the type is a fixed-length string.
Definition: H5DataType_misc.hpp:57
size_t getSize() const
Returns the length (in bytes) of this type elements.
Definition: H5DataType_misc.hpp:36
bool isVariableStr() const
Returns whether the type is a variable-length string.
Definition: H5DataType_misc.hpp:48
bool empty() const noexcept
Check the DataType was default constructed. Such value might represent auto-detection of the datatype...
Definition: H5DataType_misc.hpp:28
std::string string() const
Returns a friendly description of the type (e.g. Float32)
Definition: H5DataType_misc.hpp:65
DataTypeClass getClass() const
Return the fundamental type.
Definition: H5DataType_misc.hpp:32
bool isReference() const
Returns whether the type is a Reference.
Definition: H5DataType_misc.hpp:61
bool operator!=(const DataType &other) const
Definition: H5DataType_misc.hpp:44
Create a enum HDF5 datatype.
Definition: H5DataType.hpp:188
void commit(const Object &object, const std::string &name) const
Commit datatype into the given Object.
Definition: H5DataType_misc.hpp:343
A structure representing a set of fixed-length strings.
Definition: H5DataType.hpp:239
void resize(std::size_t n)
Definition: H5DataType.hpp:284
std::string getString(std::size_t index) const
Retrieve a string from the structure as std::string.
Definition: H5DataType_misc.hpp:234
void push_back(const std::string &)
Append an std::string to the buffer structure.
Definition: H5DataType_misc.hpp:220
Definition: H5Object.hpp:36
hid_t getId() const noexcept
getId
Definition: H5Object_misc.hpp:55
hid_t _hid
Definition: H5Object.hpp:87
Definition: H5_definitions.hpp:15
DataType create_and_check_datatype()
Create a DataType instance representing type T and perform a sanity check on its size.
Definition: H5DataType_misc.hpp:434
DataType create_datatype()
Create a DataType instance representing type T.
Definition: H5DataType_misc.hpp:427
size_t find_first_atomic_member_size(hid_t hid)
Definition: H5DataType_misc.hpp:245
DataTypeClass
Enum of Fundamental data classes.
Definition: H5DataType.hpp:23