Saving dataset (instances of C ++ class) in hdf

I have an application that should store data in an hdf file.

Is it possible to store a dataset in hdf, which is actually a C ++ object? For example, I want to save the data contained in object a below to an hdf file (hdf4 or hdf5). I can do it? If so, I would appreciate it if someone could show this. Thanks.

 class A(){ public: A(int i, double j):i(i), j(j){}; ~A(); int i; double j; int* ai; vector<int> b; setValues(int i, double j){}; } void main(){ A *a; a = new A(10, 10.2); // Store this data to hdf4 or hdf5 // A *a2; // now read in the data stored in hdf file and assign the value here } 
+4
source share
2 answers

One option is to use composite types and explicitly map structural elements down to the basic types supported by HDF.

The only complication I found with the approach was with lists and sequences. One option is to store list items in a different dataset, and then reference the start and end index.

In cases where the data is not contiguous or more complex, one approach is to use the construction of a linked list type. So for the following:

 class A { int i; vector<int> b; } 

This maps to:

 ADataset => { i, b_index }; BDataset => { value, next_index }; 

Entries in 'ADataset' store 'b_index'. Each entry in "BDataset" contains a value for that entry, followed by the optional next_index. For "next_index" you can find out the value of the checksum to find out when to stop.

+1
source

If your class attributes were simple types ( int , float , char ... even arrays of such types), you could store them in a composite data type.

In the example you are showing, there is an STL vector . If you have STL containers, you'd better use Boost serialization . The result will be ASCII text, which can be saved in the hdf5 file.

If storage efficiency is important, then you should take care to write vector in an array of variable sizes, and, more generally, do the conversion between STL containers (and / or custom objects) and what hdf5 you want to save when export and import your objects to and from hdf5 file.

0
source

Source: https://habr.com/ru/post/1383015/


All Articles