HDF5 Union/Variant Support

Hello! I have defined a compound datatype in HDF5 that is closely tied to a struct. To save memory, I was considering adding a union or variant to the struct. However, I anticipate that this might cause issues with the compound datatype.

Does HDF5 provide support for unions or variants, or has anyone encountered a similar situation in the past?

1 Like

Hi @zarahtaufique,

The use-case you described could be solved through an opaque data type (as HDF5 does not support C/C++ union data type natively).

Using HDFql, a high-level (declarative) programming language to manage HDF5 data, your use-case could be solved as follows in C++:

// include necessary C++ header files
#include <cstdlib>
#include <iostream>
#include "HDFql.hpp"


// define an union named "Data"
union Data
{
	int i;
	double d;
	char c[12];
};


// implement program
int main(int argc, char *argv[])
{

	// declare variables used in the program
	union Data data;
	std::ostringstream script;


	// create an HDF5 file named 'test.h5' and use (i.e. open) it
	HDFql::execute("CREATE AND USE FILE test.h5");


	// create a dataset named 'dset' of type opaque with a size equal to the size of variable 'data'
	script << "CREATE DATASET dset AS OPAQUE(" << sizeof(data) << ")";
	HDFql::execute(script);


	// register variable 'data' for subsequent usage (by HDFql)
	HDFql::variableRegister(&data);


	// write 'data' as an integer into dataset 'dset'
	data.i = 100;
	HDFql::execute("INSERT INTO dset VALUES FROM MEMORY 0");


	// populate 'data' (from dataset 'dset') and print it as an integer
	memset((void *) &data, 0, sizeof(data));
	HDFql::execute("SELECT FROM dset INTO MEMORY 0");
	std::cout << data.i << std::endl;


	// write 'data' as a double into dataset 'dset'
	data.d = 250.7;
	HDFql::execute("INSERT INTO dset VALUES FROM MEMORY 0");


	// populate 'data' (from dataset 'dset') and print it as a double
	memset((void *) &data, 0, sizeof(data));
	HDFql::execute("SELECT FROM dset INTO MEMORY 0");
	std::cout << data.d << std::endl;


	// write 'data' as a char into dataset 'dset'
	strcpy(data.c, "Hello world!");
	HDFql::execute("INSERT INTO dset VALUES FROM MEMORY 0");


	// populate 'data' (from dataset 'dset') and print it as a char
	memset((void *) &data, 0, sizeof(data));
	HDFql::execute("SELECT FROM dset INTO MEMORY 0");
	std::cout << data.c << std::endl;


	return EXIT_SUCCESS;

}

Hope it helps!