gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

86 行
3.3KB

  1. /**********************************************************************
  2. * $Id: cpl_auto_close.h 928c6bcbd3901094d5680a73a623acc194f55afc 2018-10-07 18:11:27 +0800 小旋风 $
  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. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included
  20. * in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  28. * DEALINGS IN THE SOFTWARE.
  29. ****************************************************************************/
  30. #ifndef CPL_AUTO_CLOSE_H_INCLUDED
  31. #define CPL_AUTO_CLOSE_H_INCLUDED
  32. #if defined(__cplusplus)
  33. #include <type_traits>
  34. /************************************************************************/
  35. /* CPLAutoClose */
  36. /************************************************************************/
  37. /**
  38. * The class use the destructor to automatically close the resource.
  39. * Example:
  40. * GDALDatasetH hDset = GDALOpen(path,GA_ReadOnly);
  41. * CPLAutoClose<GDALDatasetH,void(*)(void*)> autoclosehDset(hDset,GDALClose);
  42. * Or:
  43. * GDALDatasetH hDset = GDALOpen(path,GA_ReadOnly);
  44. * CPL_AUTO_CLOSE_WARP(hDset,GDALClose);
  45. */
  46. template<typename _Ty,typename _Dx>
  47. class CPLAutoClose {
  48. static_assert( !std::is_const<_Ty>::value && std::is_pointer<_Ty>::value,
  49. "_Ty must is pointer type,_Dx must is function type");
  50. private:
  51. _Ty& m_ResourcePtr;
  52. _Dx m_CloseFunc;
  53. private:
  54. CPLAutoClose(const CPLAutoClose&) = delete;
  55. void operator=(const CPLAutoClose&) = delete;
  56. public:
  57. /**
  58. * @brief Constructor.
  59. * @param ptr Pointer to the resource object.
  60. * @param dt Resource release(close) function.
  61. */
  62. explicit CPLAutoClose(_Ty& ptr,_Dx dt) :
  63. m_ResourcePtr(ptr),
  64. m_CloseFunc(dt)
  65. {}
  66. /**
  67. * @brief Destructor.
  68. */
  69. ~CPLAutoClose()
  70. {
  71. if(m_ResourcePtr && m_CloseFunc)
  72. m_CloseFunc(m_ResourcePtr);
  73. }
  74. };
  75. #define CPL_AUTO_CLOSE_WARP(hObject,closeFunc) \
  76. CPLAutoClose<decltype(hObject),decltype(closeFunc)*> tAutoClose##hObject(hObject,closeFunc)
  77. #endif /* __cplusplus */
  78. #endif /* CPL_AUTO_CLOSE_H_INCLUDED */
上海开阖软件有限公司 沪ICP备12045867号-1