From e8af74a030058fc661fc8eb4e9d49f65347b2523 Mon Sep 17 00:00:00 2001 From: Olivier Parcollet Date: Sat, 28 Sep 2013 16:09:47 +0200 Subject: [PATCH] h5: add a parent in group - add a parent in the group, to allow iteration on group elements - normally in h5, one needs to have the parent group and the name of the group to iterate on its elements. - added a parent (possibly empty) to get a simply syntax to get the element of a group... --- triqs/h5/group.hpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/triqs/h5/group.hpp b/triqs/h5/group.hpp index e8537d23..0e03654a 100644 --- a/triqs/h5/group.hpp +++ b/triqs/h5/group.hpp @@ -27,11 +27,13 @@ namespace triqs { namespace h5 { * Rational : use ADL for h5_read/h5_write, catch and rethrow exception, add some policy for opening/creating */ class group { - H5::Group _g; + H5::Group _g, _parent; + std::string _name_in_parent; public: group() = default; - group( group const & ) = default; - group (H5::Group g) : _g(g) {} + group(group const &) = default; + group(H5::Group g) : _g(g) {} + group(H5::Group g, H5::Group parent, std::string name_in_parent) : _g(g), _parent(parent), _name_in_parent(name_in_parent) {} /// Takes the "/" group at the top of the file. group (H5::H5File f) : _g(f.openGroup("/")) {} // can not fail, right ? @@ -42,6 +44,9 @@ namespace triqs { namespace h5 { if (is_group) { _g.setId(id_); } else { H5::H5File f; f.setId(id_); *this = group(f); } } + + bool has_parent() const { return _name_in_parent != "";} + /// Write the triqs tag of the group if it is an object. template void write_triqs_hdf5_data_scheme (T const & obj) { h5::write_string_attribute( &_g, "TRIQS_HDF5_data_scheme" , get_triqs_hdf5_data_scheme(obj).c_str() ) ; @@ -56,7 +61,7 @@ namespace triqs { namespace h5 { group open_group(std::string const & key) const { if (!has_key(key)) TRIQS_RUNTIME_ERROR << "no subgroup "< get_all_subgroup_names(std::string const & key) const; + std::vector get_all_subgroup_names() const { + if (!has_parent()) TRIQS_RUNTIME_ERROR << "Group hdf5 : parent not found"; + return group(_parent).get_all_subgroup_names(_name_in_parent); + } + /// Returns all names of dataset of key in G std::vector get_all_dataset_names(std::string const & key) const; + std::vector get_all_dataset_names() const { + if (!has_parent()) TRIQS_RUNTIME_ERROR << "Group hdf5 : parent not found"; + return group(_parent).get_all_dataset_names(_name_in_parent); + } + void write_string_attribute (std::string const & obj_name, std::string const & attr_name, std::string const & value){ herr_t err = H5LTset_attribute_string(_g.getId(),obj_name.c_str(),attr_name.c_str(), value.c_str() ) ; if (err<0) TRIQS_RUNTIME_ERROR << "Error in setting attribute of "<< obj_name<<" named "<< attr_name << " to " << value;