Hi All,
I am wondering if anyone else in C++ coding of HDF5 has run into the following problem…
I have a templatized function that writes an HDF5 dataset…
template <class T> static void WriteVecToHDF5(hid_t fid, char const *name, std::vector<T> const &vec, int d2size)
{
hsize_t siz2d[2] = {(hsize_t) vec.size() / d2size, d2size};
hid_t spid = H5Screate_simple(d2size>1?2:1, siz2d, 0);
hid_t dsid = H5Dcreate(fid, name, HDF5Type<T>().Type(), spid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dsid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &vec[0]);
H5Dclose(dsid);
H5Sclose(spid);
}
I would like the template instantiation to then return the correct H5T_NATIVE_XXX type so WriteVecToHDF5<int>(…) produces a call to H5Dcreae, there in bold red with H5T_NATIVE_INT, etc.
There does not appear to be any support in the HDF5 implementation for this. Am I missing something?
Below is what I did (for int, float and double but can easily be expanded to cover all primitive types) and am wondering if others find this useful enough (or something like it) that it ought to be included in HDF5 C++ interface?
// base class
template <class T> class HDF5TypeBase {
public:
HDF5TypeBase() {} ;
virtual ~HDF5TypeBase() {};
};
// base class for type-specific template specializations to follow
template <class T> class HDF5Type : public HDF5TypeBase<T> {
public:
HDF5Type() : HDF5TypeBase<T>() {};
virtual ~HDF5Type() {};
virtual hid_t Type() const { return H5T_NATIVE_HERR; };
};
// int, H5T_NATIVE_INT specialization
template <> class HDF5Type<int> : public HDF5TypeBase<int> {
public:
HDF5Type() : HDF5TypeBase<int>() {};
virtual ~HDF5Type() {};
virtual hid_t Type() const { return H5T_NATIVE_INT; };
};
// float, H5T_NATIVE_FLOAT specialization
template <> class HDF5Type<float> : public HDF5TypeBase<float> {
public:
HDF5Type() : HDF5TypeBase<float>() {};
virtual ~HDF5Type() {};
virtual hid_t Type() const { return H5T_NATIVE_FLOAT; };
};
// double, H5T_NATIVE_DOUBLE specialization
template <> class HDF5Type<double> : public HDF5TypeBase<double> {
public:
HDF5Type() : HDF5TypeBase<double>() {};
virtual ~HDF5Type() {};
virtual hid_t Type() const { return H5T_NATIVE_DOUBLE; };
};
This permits in the HDF5Type<T>().Type(), bold red, in the snipit above, to actually work
···
--
Mark C. Miller, LLNL
"In the end, we will remember not the words of
our enemies but the silence of our friends" - MLK