Oh, sheesh. Yes, that looks *a lot* better. Thanks so much!

Mark


"Hdf-forum on behalf of Kevin B. McCarty" wrote:

I had a similar need, but I think you are overthinking.  No need to involve any 
classes at all.
Try this:
inline hid_t hid_from_type(const char &) { return H5T_NATIVE_CHAR; }
inline hid_t hid_from_type(const signed char &) { return H5T_NATIVE_SCHAR; }
inline hid_t hid_from_type(const unsigned char &) { return H5T_NATIVE_UCHAR; }

inline hid_t hid_from_type(const short &) { return H5T_NATIVE_SHORT; }
inline hid_t hid_from_type(const unsigned short &) { return H5T_NATIVE_USHORT; }

inline hid_t hid_from_type(const int &) { return H5T_NATIVE_INT; }
inline hid_t hid_from_type(const unsigned &) { return H5T_NATIVE_UINT; }

inline hid_t hid_from_type(const long &) { return H5T_NATIVE_LONG; }
inline hid_t hid_from_type(const unsigned long &) { return H5T_NATIVE_ULONG; }

inline hid_t hid_from_type(const long long &) { return H5T_NATIVE_LLONG; }
inline hid_t hid_from_type(const unsigned long long &) { return 
H5T_NATIVE_ULLONG; }

inline hid_t hid_from_type(const float &) { return H5T_NATIVE_FLOAT; }
inline hid_t hid_from_type(const double &) { return H5T_NATIVE_DOUBLE; }
// And the coup de grace:
template <typename T>
inline hid_t hid_from_type() { return hid_from_type(T()); }
// Usage for your example:

hid_t dsid = H5Dcreate(fid, name, hid_from_type<T>(), spid, H5P_DEFAULT, 
H5P_DEFAULT, H5P_DEFAULT);

Kevin



On Mon, Feb 5, 2018 at 9:03 AM, Miller, Mark C. 
<[email protected]<mailto:[email protected]>> 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.
[email protected]<mailto:[email protected]>
http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org
Twitter: https://twitter.com/hdf5



--
Kevin B. McCarty
<[email protected]<mailto:[email protected]>>
_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org
Twitter: https://twitter.com/hdf5

Reply via email to