LCOV - code coverage report
Current view: top level - port - cpl_auto_close.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 7 7 100.0 %
Date: 2024-11-21 22:18:42 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /**********************************************************************
       2             :  * $Id$
       3             :  *
       4             :  * Name:     cpl_auto_close.h
       5             :  * Project:  CPL - Common Portability Library
       6             :  * Purpose:  CPL Auto Close handling
       7             :  * Author:   Liu Yimin, ymwh@foxmail.com
       8             :  *
       9             :  **********************************************************************
      10             :  * Copyright (c) 2018, Liu Yimin
      11             :  *
      12             :  * SPDX-License-Identifier: MIT
      13             :  ****************************************************************************/
      14             : 
      15             : #ifndef CPL_AUTO_CLOSE_H_INCLUDED
      16             : #define CPL_AUTO_CLOSE_H_INCLUDED
      17             : 
      18             : #if defined(__cplusplus)
      19             : #include <type_traits>
      20             : 
      21             : /************************************************************************/
      22             : /*                           CPLAutoClose                               */
      23             : /************************************************************************/
      24             : 
      25             : /**
      26             :  * The class use the destructor to automatically close the resource.
      27             :  * Example:
      28             :  *     GDALDatasetH hDset = GDALOpen(path,GA_ReadOnly);
      29             :  *     CPLAutoClose<GDALDatasetH,void(*)(void*)>
      30             :  * autoclosehDset(hDset,GDALClose); Or: GDALDatasetH hDset =
      31             :  * GDALOpen(path,GA_ReadOnly); CPL_AUTO_CLOSE_WARP(hDset,GDALClose);
      32             :  */
      33             : template <typename _Ty, typename _Dx> class CPLAutoClose
      34             : {
      35             :     static_assert(!std::is_const<_Ty>::value && std::is_pointer<_Ty>::value,
      36             :                   "_Ty must is pointer type,_Dx must is function type");
      37             : 
      38             :   private:
      39             :     _Ty &m_ResourcePtr;
      40             :     _Dx m_CloseFunc;
      41             : 
      42             :   private:
      43             :     CPLAutoClose(const CPLAutoClose &) = delete;
      44             :     void operator=(const CPLAutoClose &) = delete;
      45             : 
      46             :   public:
      47             :     /**
      48             :      * @brief Constructor.
      49             :      * @param ptr Pointer to the resource object.
      50             :      * @param dt  Resource release(close) function.
      51             :      */
      52           2 :     explicit CPLAutoClose(_Ty &ptr, _Dx dt)
      53           2 :         : m_ResourcePtr(ptr), m_CloseFunc(dt)
      54             :     {
      55           2 :     }
      56             : 
      57             :     /**
      58             :      * @brief Destructor.
      59             :      */
      60           2 :     ~CPLAutoClose()
      61             :     {
      62           2 :         if (m_ResourcePtr && m_CloseFunc)
      63           2 :             m_CloseFunc(m_ResourcePtr);
      64           2 :     }
      65             : };
      66             : 
      67             : #define CPL_AUTO_CLOSE_WARP(hObject, closeFunc)                                \
      68             :     CPLAutoClose<decltype(hObject), decltype(closeFunc) *>                     \
      69             :         tAutoClose##hObject(hObject, closeFunc)
      70             : 
      71             : #endif /* __cplusplus */
      72             : 
      73             : #endif /* CPL_AUTO_CLOSE_H_INCLUDED */

Generated by: LCOV version 1.14