"Hdf-forum on behalf of Ger van Diepen" wrote:
I don’t use the HDF5 C++ interface, but quite some time ago I’ve made my own (more limited) interface which works well with our Casacore library. Yeah, I am really not an experienced c++ programmer myself 😉 Your solution will also work fine when writing a dataset with a compound data type. The compiler will catch the cases where no Type() function exists. Why do you make the functions virtual? Isn’t that necessary if some derived class might wanna override? That might preclude the compiler from inlining. Yeah, the inlining function-only templates another user shared were *much* better 😉 I am using that approach now. I guess I just think something like this ought to be part of the THG-supported C++ interface to HDF5, for the pre-defined primitive types at least. Furthermore, shouldn’t Type() return a copy of the type? Not for the simple case I needed here; *writing* datasets of just primitive types. Those or HDF5 lib-wide constants. It does not know what the caller will be doing with it. Otherwise it must be made very clear that the caller should not close it. That’s why I don’t like using a bare hid_t. In my own interface I’ve encapsulated it which automatically makes copies and closes them, not only for datatypes but for all types of hid_t. Yeah, I agree. I the general case, you’d wanna pay attention to type ids, make copies and close them. BTW. Isn’t using H5T_NATIVE_INT, etc. bad in case the data can be read on a machine with a different byte ordering or on a machine with a different sizeof(int)? I am in favour of having a MemType function and a FileType function. I wasn’t interested in reading it back into this particular data producer. Cheers, Ger On 5 Feb 2018, at 18:03, Miller, Mark C. <mille...@llnl.gov<mailto:mille...@llnl.gov>> wrote: 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 _______________________________________________ Hdf-forum is for HDF software users discussion. Hdf-forum@lists.hdfgroup.org<mailto:Hdf-forum@lists.hdfgroup.org> http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org Twitter: https://twitter.com/hdf5
_______________________________________________ Hdf-forum is for HDF software users discussion. Hdf-forum@lists.hdfgroup.org http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org Twitter: https://twitter.com/hdf5