Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:36:27

0001 // -*- C++ -*-
0002 //
0003 // Package:     CondCore/CondHDF5ESSource
0004 // Class  :     Group
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Christopher Jones
0010 //         Created:  Fri, 30 Jun 2023 15:26:23 GMT
0011 //
0012 
0013 // system include files
0014 #include <cassert>
0015 
0016 // user include files
0017 #include "h5_Group.h"
0018 #include "h5_DataSet.h"
0019 #include "h5_Attribute.h"
0020 #include "FWCore/Utilities/interface/Exception.h"
0021 
0022 namespace cms::h5 {
0023   //
0024   // constants, enums and typedefs
0025   //
0026 
0027   //
0028   // static data member definitions
0029   //
0030 
0031   //
0032   // constructors and destructor
0033   //
0034   Group::Group(hid_t iParentID, std::string const& iName) : id_(H5Gopen2(iParentID, iName.c_str(), H5P_DEFAULT)) {
0035     if (id_ < 0) {
0036       throw cms::Exception("UnknownH5Group") << "unable to find group " << iName;
0037     }
0038   }
0039 
0040   Group::Group(hid_t iParentID, const void* iRef) : id_(H5Rdereference2(iParentID, H5P_DEFAULT, H5R_OBJECT, iRef)) {
0041     if (id_ < 0) {
0042       throw cms::Exception("BadH5GroupRef") << "unable to dereference Group from parent " << iParentID;
0043     }
0044   }
0045 
0046   // Group::Group(const Group& rhs)
0047   // {
0048   //    // do actual copying here;
0049   // }
0050 
0051   Group::~Group() { H5Gclose(id_); }
0052 
0053   //
0054   // assignment operators
0055   //
0056   // const Group& Group::operator=(const Group& rhs)
0057   // {
0058   //   //An exception safe implementation is
0059   //   Group temp(rhs);
0060   //   swap(rhs);
0061   //
0062   //   return *this;
0063   // }
0064 
0065   //
0066   // member functions
0067   //
0068 
0069   //
0070   // const member functions
0071   //
0072   std::shared_ptr<Group> Group::findGroup(std::string const& iName) const {
0073     return std::make_shared<Group>(id_, iName);
0074   }
0075   std::shared_ptr<DataSet> Group::findDataSet(std::string const& iName) const {
0076     return std::make_shared<DataSet>(id_, iName);
0077   }
0078 
0079   std::shared_ptr<Attribute> Group::findAttribute(std::string const& iName) const {
0080     return std::make_shared<Attribute>(id_, iName);
0081   }
0082 
0083   std::shared_ptr<Group> Group::derefGroup(hobj_ref_t iRef) const { return std::make_shared<Group>(id_, &iRef); }
0084 
0085   std::shared_ptr<DataSet> Group::derefDataSet(hobj_ref_t iRef) const { return std::make_shared<DataSet>(id_, &iRef); }
0086 
0087   std::string Group::name() const {
0088     ssize_t name_size = H5Iget_name(id_, nullptr, 0);
0089 
0090     size_t actual_name_size = static_cast<size_t>(name_size) + 1;
0091     std::unique_ptr<char[]> buffer(new char[actual_name_size]);
0092     H5Iget_name(id_, buffer.get(), actual_name_size);
0093 
0094     return std::string(buffer.get());
0095   }
0096 
0097   std::size_t Group::getNumObjs() const {
0098     H5G_info_t ginfo;  // Group information
0099 
0100     herr_t ret_value = H5Gget_info(id_, &ginfo);
0101     assert(ret_value >= 0);
0102     return (ginfo.nlinks);
0103   }
0104 
0105   std::string Group::getObjnameByIdx(std::size_t idx) const {
0106     // call H5Lget_name_by_idx with name as NULL to get its length
0107     ssize_t name_len = H5Lget_name_by_idx(id_, ".", H5_INDEX_NAME, H5_ITER_INC, idx, nullptr, 0, H5P_DEFAULT);
0108     assert(name_len >= 0);
0109 
0110     // The actual size is the cast value + 1 for the terminal ASCII NUL
0111     // (unfortunate in/out type sign mismatch)
0112     size_t actual_name_len = static_cast<size_t>(name_len) + 1;
0113 
0114     std::unique_ptr<char[]> buffer(new char[actual_name_len]);
0115 
0116     (void)H5Lget_name_by_idx(id_, ".", H5_INDEX_NAME, H5_ITER_INC, idx, buffer.get(), actual_name_len, H5P_DEFAULT);
0117 
0118     return std::string(buffer.get());
0119   }
0120 
0121   //
0122   // static member functions
0123   //
0124 }  // namespace cms::h5