CMS 3D CMS Logo

h5_Group.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondCore/CondHDF5ESSource
4 // Class : Group
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Christopher Jones
10 // Created: Fri, 30 Jun 2023 15:26:23 GMT
11 //
12 
13 // system include files
14 #include <cassert>
15 
16 // user include files
17 #include "h5_Group.h"
18 #include "h5_DataSet.h"
19 #include "h5_Attribute.h"
21 
22 namespace cms::h5 {
23  //
24  // constants, enums and typedefs
25  //
26 
27  //
28  // static data member definitions
29  //
30 
31  //
32  // constructors and destructor
33  //
34  Group::Group(hid_t iParentID, std::string const& iName) : id_(H5Gopen2(iParentID, iName.c_str(), H5P_DEFAULT)) {
35  if (id_ < 0) {
36  throw cms::Exception("UnknownH5Group") << "unable to find group " << iName;
37  }
38  }
39 
40  Group::Group(hid_t iParentID, const void* iRef) : id_(H5Rdereference2(iParentID, H5P_DEFAULT, H5R_OBJECT, iRef)) {
41  if (id_ < 0) {
42  throw cms::Exception("BadH5GroupRef") << "unable to dereference Group from parent " << iParentID;
43  }
44  }
45 
46  // Group::Group(const Group& rhs)
47  // {
48  // // do actual copying here;
49  // }
50 
51  Group::~Group() { H5Gclose(id_); }
52 
53  //
54  // assignment operators
55  //
56  // const Group& Group::operator=(const Group& rhs)
57  // {
58  // //An exception safe implementation is
59  // Group temp(rhs);
60  // swap(rhs);
61  //
62  // return *this;
63  // }
64 
65  //
66  // member functions
67  //
68 
69  //
70  // const member functions
71  //
72  std::shared_ptr<Group> Group::findGroup(std::string const& iName) const {
73  return std::make_shared<Group>(id_, iName);
74  }
75  std::shared_ptr<DataSet> Group::findDataSet(std::string const& iName) const {
76  return std::make_shared<DataSet>(id_, iName);
77  }
78 
79  std::shared_ptr<Attribute> Group::findAttribute(std::string const& iName) const {
80  return std::make_shared<Attribute>(id_, iName);
81  }
82 
83  std::shared_ptr<Group> Group::derefGroup(hobj_ref_t iRef) const { return std::make_shared<Group>(id_, &iRef); }
84 
85  std::shared_ptr<DataSet> Group::derefDataSet(hobj_ref_t iRef) const { return std::make_shared<DataSet>(id_, &iRef); }
86 
88  ssize_t name_size = H5Iget_name(id_, nullptr, 0);
89 
90  size_t actual_name_size = static_cast<size_t>(name_size) + 1;
91  std::unique_ptr<char[]> buffer(new char[actual_name_size]);
92  H5Iget_name(id_, buffer.get(), actual_name_size);
93 
94  return std::string(buffer.get());
95  }
96 
97  std::size_t Group::getNumObjs() const {
98  H5G_info_t ginfo; // Group information
99 
100  herr_t ret_value = H5Gget_info(id_, &ginfo);
101  assert(ret_value >= 0);
102  return (ginfo.nlinks);
103  }
104 
106  // call H5Lget_name_by_idx with name as NULL to get its length
107  ssize_t name_len = H5Lget_name_by_idx(id_, ".", H5_INDEX_NAME, H5_ITER_INC, idx, nullptr, 0, H5P_DEFAULT);
108  assert(name_len >= 0);
109 
110  // The actual size is the cast value + 1 for the terminal ASCII NUL
111  // (unfortunate in/out type sign mismatch)
112  size_t actual_name_len = static_cast<size_t>(name_len) + 1;
113 
114  std::unique_ptr<char[]> buffer(new char[actual_name_len]);
115 
116  (void)H5Lget_name_by_idx(id_, ".", H5_INDEX_NAME, H5_ITER_INC, idx, buffer.get(), actual_name_len, H5P_DEFAULT);
117 
118  return std::string(buffer.get());
119  }
120 
121  //
122  // static member functions
123  //
124 } // namespace cms::h5
std::shared_ptr< Group > findGroup(std::string const &iName) const
Definition: h5_Group.cc:72
std::string getObjnameByIdx(std::size_t) const
Definition: h5_Group.cc:105
std::size_t getNumObjs() const
Definition: h5_Group.cc:97
Group(hid_t, std::string const &)
Definition: h5_Group.cc:34
assert(be >=bs)
TEMPL(T2) struct Divides void
Definition: Factorize.h:24
std::shared_ptr< DataSet > findDataSet(std::string const &iName) const
Definition: h5_Group.cc:75
std::shared_ptr< Group > derefGroup(hobj_ref_t iRef) const
Definition: h5_Group.cc:83
std::string name() const
Definition: h5_Group.cc:87
std::shared_ptr< Attribute > findAttribute(std::string const &iName) const
Definition: h5_Group.cc:79
std::shared_ptr< DataSet > derefDataSet(hobj_ref_t iRef) const
Definition: h5_Group.cc:85