CMS 3D CMS Logo

h5_Attribute.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondCore/CondHDF5ESSource
4 // Class : Attribute
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Christopher Jones
10 // Created: Fri, 30 Jun 2023 15:26:38 GMT
11 //
12 
13 // system include files
14 
15 // user include files
16 #include "h5_Attribute.h"
18 
19 //
20 // constants, enums and typedefs
21 //
22 
23 namespace cms::h5 {
24  //
25  // static data member definitions
26  //
27 
28  //
29  // constructors and destructor
30  //
31  Attribute::Attribute(hid_t iParentID, std::string const& iName)
32  : id_(H5Aopen(iParentID, iName.c_str(), H5P_DEFAULT)) {
33  if (id_ < 1) {
34  throw cms::Exception("UnknownH5Attribute") << "unknown attribute " << iName;
35  }
36  }
37 
38  // Attribute::Attribute(const Attribute& rhs)
39  // {
40  // // do actual copying here;
41  // }
42 
43  Attribute::~Attribute() { H5Aclose(id_); }
44 
45  //
46  // assignment operators
47  //
48  // const Attribute& Attribute::operator=(const Attribute& rhs)
49  // {
50  // //An exception safe implementation is
51  // Attribute temp(rhs);
52  // swap(rhs);
53  //
54  // return *this;
55  // }
56 
57  //
58  // member functions
59  //
60 
61  //
62  // const member functions
63  //
65  // Prepare and call C API to read attribute.
66  char* strg_C;
67 
68  hid_t attr_type = H5Tcopy(H5T_C_S1);
69  (void)H5Tset_size(attr_type, H5T_VARIABLE);
70 
71  // Read attribute, no allocation for variable-len string; C library will
72  herr_t ret_value = H5Aread(id_, attr_type, &strg_C);
73  H5Tclose(attr_type);
74 
75  if (ret_value < 0) {
76  throw cms::Exception("H5AttributeReadStrinFailed") << " failed to read string from attribute";
77  }
78 
79  // Get string from the C char* and release resource allocated by C API
80  std::string strg = strg_C;
81  free(strg_C);
82 
83  return strg;
84  }
85 
86  uint32_t Attribute::readUInt32() const {
87  unsigned int ret;
88  // Read attribute, no allocation for variable-len string; C library will
89  herr_t ret_value = H5Aread(id_, H5T_NATIVE_UINT, &ret);
90 
91  if (ret_value < 0) {
92  throw cms::Exception("H5AttributeReadStrinFailed") << " failed to read unsigned int from attribute";
93  }
94  return ret;
95  }
96 
97  //
98  // static member functions
99  //
100 } // namespace cms::h5
std::string readString() const
Definition: h5_Attribute.cc:64
ret
prodAgent to be discontinued
TEMPL(T2) struct Divides void
Definition: Factorize.h:24
uint32_t readUInt32() const
Definition: h5_Attribute.cc:86
void free(void *ptr) noexcept
Attribute(hid_t, std::string const &)
Definition: h5_Attribute.cc:31