LCOV - code coverage report
Current view: top level - frmts/hdf5 - gh5_convenience.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 45 46 97.8 %
Date: 2025-12-29 15:50:47 Functions: 47 47 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  Hierarchical Data Format Release 5 (HDF5)
       4             :  * Purpose:  HDF5 convenience functions.
       5             :  * Author:   Frank Warmerdam <warmerdam@pobox.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2009, Frank Warmerdam <warmerdam@pobox.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #ifndef GH5_CONVENIENCE_H_INCLUDED_
      14             : #define GH5_CONVENIENCE_H_INCLUDED_
      15             : 
      16             : #include "hdf5_api.h"
      17             : 
      18             : #include "cpl_string.h"
      19             : #include "gdal.h"
      20             : 
      21             : #include <stdint.h>
      22             : 
      23             : /* release 1.6.3 or 1.6.4 changed the type of count in some api functions */
      24             : 
      25             : #if H5_VERS_MAJOR == 1 && H5_VERS_MINOR <= 6 &&                                \
      26             :     (H5_VERS_MINOR < 6 || H5_VERS_RELEASE < 3)
      27             : #define H5OFFSET_TYPE hssize_t
      28             : #else
      29             : #define H5OFFSET_TYPE hsize_t
      30             : #endif
      31             : 
      32             : bool GH5_FetchAttribute(hid_t loc_id, const char *pszName, CPLString &osResult,
      33             :                         bool bReportError = false);
      34             : bool GH5_FetchAttribute(hid_t loc_id, const char *pszName, double &dfResult,
      35             :                         bool bReportError = false);
      36             : GDALDataType GH5_GetDataType(hid_t TypeID);
      37             : constexpr unsigned VARIABLE_LENGTH = UINT32_MAX;
      38             : bool GH5_CreateAttribute(hid_t loc_id, const char *pszAttrName, hid_t TypeID,
      39             :                          unsigned nMaxLen = 0);
      40             : bool GH5_WriteAttribute(hid_t loc_id, const char *pszAttrName,
      41             :                         const char *pszValue);
      42             : bool GH5_WriteAttribute(hid_t loc_id, const char *pszAttrName, double dfValue);
      43             : bool GH5_WriteAttribute(hid_t loc_id, const char *pszAttrName, int nValue);
      44             : bool GH5_WriteAttribute(hid_t loc_id, const char *pszAttrName, unsigned nValue);
      45             : 
      46             : /************************************************************************/
      47             : /*                                h5check()                             */
      48             : /************************************************************************/
      49             : 
      50             : #ifdef DEBUG
      51       14380 : template <class T> static T h5check(T ret, const char *filename, int line)
      52             : {
      53       14380 :     if (ret < 0)
      54             :     {
      55           0 :         CPLError(CE_Failure, CPLE_AppDefined, "HDF5 API failed at %s:%d",
      56             :                  filename, line);
      57             :     }
      58       14380 :     return ret;
      59             : }
      60             : 
      61             : #define H5_CHECK(x) h5check(x, __FILE__, __LINE__)
      62             : #else
      63             : #define H5_CHECK(x) (x)
      64             : #endif
      65             : 
      66             : /************************************************************************/
      67             : /*                              GH5_HIDBaseHolder                       */
      68             : /************************************************************************/
      69             : 
      70             : template <herr_t (*closeFunc)(hid_t)> struct GH5_HIDBaseHolder /* non final */
      71             : {
      72         118 :     inline hid_t get() const
      73             :     {
      74         118 :         return m_hid;
      75             :     }
      76             : 
      77        4353 :     inline operator bool() const
      78             :     {
      79        4353 :         return m_hid >= 0;
      80             :     }
      81             : 
      82       15414 :     inline operator hid_t() const
      83             :     {
      84       15414 :         return m_hid;
      85             :     }
      86             : 
      87        3921 :     inline ~GH5_HIDBaseHolder()
      88             :     {
      89        3921 :         clear();
      90        3921 :     }
      91             : 
      92        1012 :     inline void reset(hid_t hid)
      93             :     {
      94        1012 :         clear();
      95        1012 :         m_hid = hid;
      96        1012 :     }
      97             : 
      98        6353 :     inline bool clear()
      99             :     {
     100        6353 :         const bool ret = m_hid < 0 || H5_CHECK(closeFunc(m_hid)) >= 0;
     101        6353 :         m_hid = -1;
     102        6353 :         return ret;
     103             :     }
     104             : 
     105             :   protected:
     106        3921 :     inline explicit GH5_HIDBaseHolder(hid_t hid) : m_hid(hid)
     107             :     {
     108        3921 :     }
     109             : 
     110             :     hid_t m_hid = -1;
     111             : 
     112             :   private:
     113             :     CPL_DISALLOW_COPY_ASSIGN(GH5_HIDBaseHolder)
     114             : };
     115             : 
     116             : struct GH5_HIDFileHolder : public GH5_HIDBaseHolder<H5Fclose>
     117             : {
     118         200 :     inline explicit GH5_HIDFileHolder(hid_t hid = -1) : GH5_HIDBaseHolder(hid)
     119             :     {
     120         200 :     }
     121             : };
     122             : 
     123             : struct GH5_HIDGroupHolder : public GH5_HIDBaseHolder<H5Gclose>
     124             : {
     125         800 :     inline explicit GH5_HIDGroupHolder(hid_t hid = -1) : GH5_HIDBaseHolder(hid)
     126             :     {
     127         800 :     }
     128             : };
     129             : 
     130             : struct GH5_HIDTypeHolder : public GH5_HIDBaseHolder<H5Tclose>
     131             : {
     132        1205 :     inline explicit GH5_HIDTypeHolder(hid_t hid = -1) : GH5_HIDBaseHolder(hid)
     133             :     {
     134        1205 :     }
     135             : };
     136             : 
     137             : struct GH5_HIDSpaceHolder : public GH5_HIDBaseHolder<H5Sclose>
     138             : {
     139        1202 :     inline explicit GH5_HIDSpaceHolder(hid_t hid = -1) : GH5_HIDBaseHolder(hid)
     140             :     {
     141        1202 :     }
     142             : };
     143             : 
     144             : struct GH5_HIDDatasetHolder : public GH5_HIDBaseHolder<H5Dclose>
     145             : {
     146         373 :     inline explicit GH5_HIDDatasetHolder(hid_t hid = -1)
     147         373 :         : GH5_HIDBaseHolder(hid)
     148             :     {
     149         373 :     }
     150             : };
     151             : 
     152             : struct GH5_HIDParametersHolder : public GH5_HIDBaseHolder<H5Pclose>
     153             : {
     154         141 :     inline explicit GH5_HIDParametersHolder(hid_t hid = -1)
     155         141 :         : GH5_HIDBaseHolder(hid)
     156             :     {
     157         141 :     }
     158             : };
     159             : 
     160             : // Silence "HDF5-DIAG: Error detected in HDF5" messages coming from libhdf4
     161             : struct GH5_libhdf5_error_silencer
     162             : {
     163             :     H5E_auto2_t old_func = nullptr;
     164             :     void *old_data = nullptr;
     165             : 
     166          90 :     GH5_libhdf5_error_silencer()
     167          90 :     {
     168          90 :         H5Eget_auto2(H5E_DEFAULT, &old_func, &old_data);
     169          90 :         H5Eset_auto2(H5E_DEFAULT, nullptr, nullptr);
     170          90 :     }
     171             : 
     172          90 :     ~GH5_libhdf5_error_silencer()
     173          90 :     {
     174          90 :         H5Eset_auto2(H5E_DEFAULT, old_func, old_data);
     175          90 :     }
     176             : 
     177             :     CPL_DISALLOW_COPY_ASSIGN(GH5_libhdf5_error_silencer)
     178             : };
     179             : 
     180             : #endif /* ndef GH5_CONVENIENCE_H_INCLUDED_ */

Generated by: LCOV version 1.14