Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:30

0001 // -*- C++ -*-
0002 //
0003 // Package:     CondCore/CondHDF5ESSource
0004 // Class  :     Attribute
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Christopher Jones
0010 //         Created:  Fri, 30 Jun 2023 15:26:38 GMT
0011 //
0012 
0013 // system include files
0014 
0015 // user include files
0016 #include "h5_Attribute.h"
0017 #include "FWCore/Utilities/interface/Exception.h"
0018 
0019 //
0020 // constants, enums and typedefs
0021 //
0022 
0023 namespace cms::h5 {
0024   //
0025   // static data member definitions
0026   //
0027 
0028   //
0029   // constructors and destructor
0030   //
0031   Attribute::Attribute(hid_t iParentID, std::string const& iName)
0032       : id_(H5Aopen(iParentID, iName.c_str(), H5P_DEFAULT)) {
0033     if (id_ < 1) {
0034       throw cms::Exception("UnknownH5Attribute") << "unknown attribute " << iName;
0035     }
0036   }
0037 
0038   // Attribute::Attribute(const Attribute& rhs)
0039   // {
0040   //    // do actual copying here;
0041   // }
0042 
0043   Attribute::~Attribute() { H5Aclose(id_); }
0044 
0045   //
0046   // assignment operators
0047   //
0048   // const Attribute& Attribute::operator=(const Attribute& rhs)
0049   // {
0050   //   //An exception safe implementation is
0051   //   Attribute temp(rhs);
0052   //   swap(rhs);
0053   //
0054   //   return *this;
0055   // }
0056 
0057   //
0058   // member functions
0059   //
0060 
0061   //
0062   // const member functions
0063   //
0064   std::string Attribute::readString() const {
0065     // Prepare and call C API to read attribute.
0066     char* strg_C;
0067 
0068     hid_t attr_type = H5Tcopy(H5T_C_S1);
0069     (void)H5Tset_size(attr_type, H5T_VARIABLE);
0070 
0071     // Read attribute, no allocation for variable-len string; C library will
0072     herr_t ret_value = H5Aread(id_, attr_type, &strg_C);
0073     H5Tclose(attr_type);
0074 
0075     if (ret_value < 0) {
0076       throw cms::Exception("H5AttributeReadStrinFailed") << " failed to read string from attribute";
0077     }
0078 
0079     // Get string from the C char* and release resource allocated by C API
0080     std::string strg = strg_C;
0081     free(strg_C);
0082 
0083     return strg;
0084   }
0085 
0086   uint32_t Attribute::readUInt32() const {
0087     unsigned int ret;
0088     // Read attribute, no allocation for variable-len string; C library will
0089     herr_t ret_value = H5Aread(id_, H5T_NATIVE_UINT, &ret);
0090 
0091     if (ret_value < 0) {
0092       throw cms::Exception("H5AttributeReadStrinFailed") << " failed to read unsigned int from attribute";
0093     }
0094     return ret;
0095   }
0096 
0097   //
0098   // static member functions
0099   //
0100 }  // namespace cms::h5