Utopia  2
Framework for studying models of complex & adaptive systems.
hdfgroup.hh
Go to the documentation of this file.
1 
8 #ifndef UTOPIA_DATAIO_HDFGROUP_HH
9 #define UTOPIA_DATAIO_HDFGROUP_HH
10 #include <string>
11 #include <vector>
12 
13 #include <hdf5.h>
14 #include <hdf5_hl.h>
15 
16 #include "hdfattribute.hh"
17 #include "hdfdataset.hh"
18 #include "hdfobject.hh"
19 #include "hdfutilities.hh"
20 
21 namespace Utopia
22 {
23 namespace DataIO
24 {
40 class HDFGroup final : public HDFObject< HDFCategory::group >
41 {
42  public:
52  template < typename Attrdata >
53  void
54  add_attribute(std::string name, Attrdata attribute_data)
55  {
56  HDFAttribute(*this, name).write(attribute_data);
57  }
58 
66  template < HDFCategory cat >
67  void
68  open(HDFObject< cat >& parent, std::string path)
69  {
70 
71  this->_log->debug(
72  "Opening group with path {} at parent {}", path, parent.get_path());
73  // check if given path exists in parent
74  if (path_is_valid(parent.get_C_id(), path))
75  {
76  // manage new group using his class
77  bind_to(H5Gopen(parent.get_C_id(), path.c_str(), H5P_DEFAULT),
78  &H5Gclose,
79  path);
80 
81  if (get_C_id() < 0)
82  {
83  throw std::runtime_error("Group opening for path '" + path +
84  "' failed!");
85  }
86  }
87  else
88  { // group does not exist yet
89  // create the group and intermediates
90  hid_t group_plist = H5Pcreate(H5P_LINK_CREATE);
91  H5Pset_create_intermediate_group(group_plist, 1);
92 
93  // manage new group using his class
94  bind_to(H5Gcreate(parent.get_C_id(),
95  path.c_str(),
96  group_plist,
97  H5P_DEFAULT,
98  H5P_DEFAULT),
99  &H5Gclose,
100  path);
101 
102  H5Pclose(group_plist);
103 
104  if (get_C_id() < 0)
105  {
106  throw std::runtime_error("Group creation for path '" + path +
107  "' failed!");
108  }
109  }
110  }
111 
119  std::shared_ptr< HDFGroup >
120  open_group(std::string path)
121  {
122  return std::make_shared< HDFGroup >(*this, path);
123  }
124 
133  std::shared_ptr< HDFDataset >
134  open_dataset(std::string path,
135  std::vector< hsize_t > capacity = {},
136  std::vector< hsize_t > chunksizes = {},
137  std::size_t compresslevel = 0)
138  {
139  return std::make_shared< HDFDataset >(
140  *this, path, capacity, chunksizes, compresslevel);
141  }
142 
148  void
149  delete_group(std::string path)
150  {
151  this->_log->debug("Deleting group {} in {}", path, _path);
152 
153  // check if group exists in file. If it does, delete the link
154  herr_t status = 1;
155  if (is_valid())
156  {
157  // group exists, can be deleted
158  status = H5Ldelete(get_C_id(), path.c_str(), H5P_DEFAULT);
159  if (status < 0)
160  {
161  throw std::runtime_error("Deletion of group at path '" + path +
162  "' failed! Wrong path?");
163  }
164  }
165  // group does not exist, do nothing
166  }
167 
171  HDFGroup() = default;
172 
178  HDFGroup(const HDFGroup& other) = default;
179 
185  HDFGroup(HDFGroup&& other) = default;
193  HDFGroup&
194  operator=(HDFGroup&& other) = default;
201  HDFGroup&
202  operator=(const HDFGroup& other) = default;
211  template < HDFCategory cat >
212  HDFGroup(HDFObject< cat >& parent, std::string path)
213  {
214  open(parent, path);
215  }
216 
221  virtual ~HDFGroup() = default;
222 }; // namespace Utopia
223 
230 void
231 swap(HDFGroup& lhs, HDFGroup& rhs)
232 {
233  lhs.swap(rhs);
234 } // end of group HDF5 // end of group DataIO
237 
238 } // namespace DataIO
239 } // namespace Utopia
240 
241 #endif
Class for hdf5 attribute, which can be attached to groups and datasets.
Definition: hdfattribute.hh:46
void write(Type attribute_data, std::vector< hsize_t > shape={})
Function for writing data to the attribute.
Definition: hdfattribute.hh:780
Class represting a HDFGroup, an object analogous to a folder for HDFFiles.
Definition: hdfgroup.hh:41
HDFGroup(const HDFGroup &other)=default
Construct a new HDFGroup object.
void add_attribute(std::string name, Attrdata attribute_data)
write attribute
Definition: hdfgroup.hh:54
HDFGroup()=default
Default constructor.
void open(HDFObject< cat > &parent, std::string path)
Bind the object to a new HDF5 Group, either opening existing or creating a new one at path 'path' in ...
Definition: hdfgroup.hh:68
std::shared_ptr< HDFDataset > open_dataset(std::string path, std::vector< hsize_t > capacity={}, std::vector< hsize_t > chunksizes={}, std::size_t compresslevel=0)
open a HDFDataset
Definition: hdfgroup.hh:134
HDFGroup & operator=(const HDFGroup &other)=default
Copy assignment operator.
void delete_group(std::string path)
Delete the group at the given relative path.
Definition: hdfgroup.hh:149
virtual ~HDFGroup()=default
Destroy the HDFGroup object.
HDFGroup & operator=(HDFGroup &&other)=default
assignment operator
std::shared_ptr< HDFGroup > open_group(std::string path)
Opens a group.
Definition: hdfgroup.hh:120
HDFGroup(HDFGroup &&other)=default
Construct a new HDFGroup object.
HDFGroup(HDFObject< cat > &parent, std::string path)
Construct a new HDFGroup object.
Definition: hdfgroup.hh:212
Common base class for all HDF5 classes in the DATAIO Module i.e., for all classes that wrap HDF5-C-Li...
Definition: hdfobject.hh:37
std::string _path
Name of the object.
Definition: hdfobject.hh:50
std::string get_path() const
Get the name or path object.
Definition: hdfobject.hh:88
virtual bool is_valid() const
Check if the object is still valid.
Definition: hdfobject.hh:143
void swap(HDFObject &other)
swap the state of the caller with the state of the argument
Definition: hdfobject.hh:71
std::shared_ptr< spdlog::logger > _log
pointer to the logger for dataio
Definition: hdfobject.hh:56
hid_t get_C_id() const
Get the C id object.
Definition: hdfobject.hh:120
void bind_to(hid_t id, std::function< herr_t(hid_t) > closing_func, std::string path={})
Open the object and bind it to a HDF5 object identified by 'id' with name 'path'. Object should be cr...
Definition: hdfobject.hh:186
void swap(WriteTask< BGB, DW, DB, AWG, AWD > &lhs, WriteTask< BGB, DW, DB, AWG, AWD > &rhs)
Swaps the state of lhs and rhs.
Definition: write_task.hh:240
auto path_is_valid(hid_t id, std::string path)
Check if the path given relative to the object identified by 'id' exists and points to a valid hdf5 o...
Definition: hdfutilities.hh:150
This file implements a C++ class which wraps a C HDF5 attribute to a HDF5-object (group or dataset),...
This is the central file of the HDF5 dataIO module of Utopia and provides a class for writing to,...
This file provides metafunctions for automatically determining the nature of a C/C++ types at compile...
Definition: agent.hh:11