Read EnumType using C++ API

Hi,

I would like to read in the possible values of an EnumType stored in a file. This is what I have so far:

H5::H5File f( filename, H5F_ACC_RDONLY );
H5::Group g = f.openGroup("/testGroup/analysis");
H5::Attribute attribute = g.openAttribute(“type”);
H5::DataType type = attribute.getDataType();

The attribute in the file looks like this:
ATTRIBUTE “type” {
DATATYPE H5T_ENUM {
H5T_STD_I32LE;
“bool” 3;
“double” 1;
“int” 0;
“string” 2;
“vector” 5;
}
DATASPACE SCALAR
DATA {
(0): analysis
}
}

How can I retrieve the pairs like “vector”,5 if I open a file with an attribute like above?

Hello,

Would using H5::CompType be reasonable for each of those pairs?

I’m not sure I understand. I just want to make sure, that if I read the “type” attribute, my program understands what some value like 5 means, that it is a vector. This seems to be stored in the H5T_ENUM, but I don’t know how I can access it. In the above example, I could do

int value=-1;
attribute.read(type,&value);

do read the actual value of the EnumType. Still I would not know that 5 corresponds to vector. I found that there is the function EnumType::nameOf (void *value, size_t size) const. But in the above example, I would not know how to convert the DataType to an EnumType to use that function.

Ah, I see. Can you use H5::EnumType type = attribute.getEnumType(); instead of H5::DataType type = attribute.getDataType();?

Yes, this is exactly what I was looking for. Here the code fragment which lists all the members:

H5::H5File f( “/home/user/test.hdf5”, H5F_ACC_RDONLY );
H5::Group g = f.openGroup("/testGroup/");
H5::Attribute attribute = g.openAttribute(“type”);
H5::EnumType type = attribute.getEnumType();
int nMembers = type.getNmembers();
for(int i=0;i<nMembers;i++) {
int v;
type.getMemberValue(i,&v);
string name = type.nameOf(&v,100);
cout << "HDF5 EnumType Members: " << name << " " << v << endl;
}

1 Like

I have a little additional question regarding memory consumption: Lets assume I have 10 attributes which have the same EnumType as type. Is then the whole EnumType (the whole map of strings and ints) stored for every attribute, or is the EnumType stored once per file, and an attribute only contains the value?

what is 100 in string name = type.nameOf(&v,100);? why hard coded it?