Assistance with structuring addition of HDF5 writer

I am working on the codebase of a simulation program. At the moment output of simulation timeseries data is in text form. This is unwieldy, and I would like to write the data to HDF5 instead so that I can read and process the data using Python.

So far, my experience with C++ has been restricted to understanding existing code and making revisions, so I would like some feedback on how I might implement my HDF5 writer.

My idea is to wrap the C API of HDF5 in a class so that the main executable code doesn't need to have knowledge of the HDF5 datatypes and details. I have some idea of what functions the class would need, like create_group, write_attribute_str, write_attribute_int, write_buffered, close_group, close_file, etc.

What I'm unsure about is that there are already three output classes for three different styles of text output, differing in how much data is generated. The three classes all print data to a std::ostream. Would I have to create three additional output classes that interface with the HDF5 writer class?

Within the executable, things currently look like

1
2
3
4
5
6
7
8
9
10
11
12
13
// build output class
switch (simOut)
{
case OUT_E:
    output = new OutEnergy(...);
    break;
case OUT_ALL:
    output = new OutAll(...);
    break;
case OUT_S:
    output = new OutStruct(...);
    break;
}


And I'm thinking to change things to...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bool outHDF; // whether to output in HDF
if (outHDF)
    HDF5Writer writer(...);
...
// build output class:
switch (simOut)
{
case OUT_E:
    if (outHDF)
        output = new OutEnergyHDF(writer, ...);
    else
        output = new OutEnergy(...);
    break;
case OUT_ALL:
    if (outHDF)
        output = new OutAllHDF(writer, ...);
    else
        output = new OutAll(...);
    break;
case OUT_S:
    if (outHDF)
        output = new OutStructHDF(writer, ...);
    else
        output = new OutStruct(...);
    break;
}


It just feels inelegant with all these if statements and now three pairs of output classes. Am I going in the right direction?
Sorry, I didn't understand if you want to write your wrapper as an exercise or because you really need it.
As far as I can see, there's already some C++ wrapper provided by no less a team than the HDF5 developers:
https://support.hdfgroup.org/HDF5/doc/cpplus_RM/

I've read that HDF5 specification spans about 150 pages, so I think writing a new wrapper would take quite a long time - but I didn't mean to discourage you, just to emphasize that you'll be more likely to find help on specific topic rather than on such wide-ranging questions.

Anyway, good luck with your coding!

My intention is not to rewrite that C++ wrapper. Rather, I think I need an interface to it. I mentioned C API instead of C++ since it seems easier for me to understand. I think I only need a few basic functions.

I agree my question is more open-ended. Perhaps specifically, is it a good idea to have an if statement based structure as described above? Is duplicating the three different output classes that exist to enable HDF5 output a good idea?
My idea is to wrap the C API of HDF5 in a class

Is there a public accessible description of those C APIs?

there are already three output classes

Are there classes in C?

is it a good idea to have an if statement based structure...?

It looks an acceptable idea for a single function, not for an entire class, IMHO.

My intention is not to rewrite that C++ wrapper. Rather, I think I need an interface to it. I mentioned C API instead of C++ since it seems easier for me to understand.

I'm confused. You want to write a C++ wrapper around C APIs because C is easer to understand for you?

Ok, I see I'm not able to help you, but let me venture an opinion: if there are C++ classes already done and downloadable for free and you only need to compile against them, it really seems the best option.
If managing many classes is disturbing you, or you prefer different methods, just inherit a new class from one or more of them.
If the problem is the documentation, well I don't think the C++ interface could be so distant from the Python one, which you already know.
But that's just my point of you.

Hope somebody else may give you a more informed advice.
Is there a public accessible description of those C APIs?


For HDF5? Yes, C and C++ API are described, although documentation for the C API is more thorough.

Are there classes in C?


When I said "there are already three output classes," I was referring to the output classes of my simulation program.

I am programming in C++, but it's possible to use either the C++ or the C API for HDF5. I got the idea for just using the C API based on the more available documentation and forum threads like this one: http://hdf-forum.184993.n3.nabble.com/HDF5-C-API-and-programming-model-td757443.html

thanks for your advice.
Topic archived. No new replies allowed.