QMCPACK
h5data_proxy< std::vector< std::string > > Struct Template Reference

Specialization for vector of strings. More...

+ Collaboration diagram for h5data_proxy< std::vector< std::string > >:

Public Types

using data_type = std::vector< std::string >
 

Public Member Functions

 h5data_proxy (const data_type &a)
 
bool read (data_type &ref, hid_t grp, const std::string &aname, hid_t xfer_plist=H5P_DEFAULT)
 
bool write (const data_type &ref, hid_t grp, const std::string &aname, hid_t xfer_plist=H5P_DEFAULT) const
 

Detailed Description

template<>
struct qmcplusplus::h5data_proxy< std::vector< std::string > >

Specialization for vector of strings.

Definition at line 195 of file hdf_stl.h.

Member Typedef Documentation

◆ data_type

using data_type = std::vector<std::string>

Definition at line 197 of file hdf_stl.h.

Constructor & Destructor Documentation

◆ h5data_proxy()

h5data_proxy ( const data_type a)
inline

Definition at line 199 of file hdf_stl.h.

199 {}

Member Function Documentation

◆ read()

bool read ( data_type ref,
hid_t  grp,
const std::string &  aname,
hid_t  xfer_plist = H5P_DEFAULT 
)
inline

Definition at line 201 of file hdf_stl.h.

202  {
203  hid_t datatype = H5Tcopy(H5T_C_S1);
204  H5Tset_size(datatype, H5T_VARIABLE);
205  hid_t dataset = H5Dopen(grp, aname.c_str(), H5P_DEFAULT);
206  std::vector<char*> char_list;
207  herr_t ret = -1;
208  if (dataset > -1)
209  {
210  hsize_t dim_out;
211  hid_t dataspace = H5Dget_space(dataset);
212  hid_t status = H5Sget_simple_extent_dims(dataspace, &dim_out, NULL);
213 
214  char_list.resize(dim_out);
215  ret = H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, xfer_plist, char_list.data());
216 
217  for (int i = 0; i < dim_out; i++)
218  ref.push_back(char_list[i]);
219 
220  H5Dvlen_reclaim(datatype, dataspace, xfer_plist, char_list.data());
221 
222  H5Sclose(dataspace);
223  H5Dclose(dataset);
224  }
225  H5Tclose(datatype);
226 
227  return ret >= 0;
228  }

◆ write()

bool write ( const data_type ref,
hid_t  grp,
const std::string &  aname,
hid_t  xfer_plist = H5P_DEFAULT 
) const
inline

Definition at line 230 of file hdf_stl.h.

231  {
232  // See the section in the HDF user's manual on datatypes,
233  // particularly the subsection on strings.
234  // (e.g. http://davis.lbl.gov/Manuals/HDF5-1.8.7/UG/11_Datatypes.html)
235  // and stackoverflow
236  // https://stackoverflow.com/questions/6184817/hdf5-inserting-a-set-of-strings-in-a-dataset
237  hid_t datatype = H5Tcopy(H5T_C_S1);
238  H5Tset_size(datatype, H5T_VARIABLE);
239  hsize_t dim = ref.size();
240 
241  // Create vector of pointers to the actual string data
242  std::vector<const char*> char_list;
243  for (int i = 0; i < ref.size(); i++)
244  char_list.push_back(ref[i].data());
245 
246  hid_t h1 = H5Dopen(grp, aname.c_str(), H5P_DEFAULT);
247  herr_t ret = -1;
248  if (h1 < 0) // missing create one
249  {
250  hid_t dataspace = H5Screate_simple(1, &dim, NULL);
251  hid_t dataset = H5Dcreate(grp, aname.c_str(), datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
252  ret = H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, xfer_plist, char_list.data());
253  H5Sclose(dataspace);
254  H5Dclose(dataset);
255  }
256  else
257  ret = H5Dwrite(h1, datatype, H5S_ALL, H5S_ALL, xfer_plist, char_list.data());
258 
259  H5Dclose(h1);
260  return ret >= 0;
261  }

The documentation for this struct was generated from the following file: