GDAL
cpl_auto_close.h
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 
31 #ifndef CPL_AUTO_CLOSE_H_INCLUDED
32 #define CPL_AUTO_CLOSE_H_INCLUDED
33 
34 #if defined(__cplusplus)
35 #include <type_traits>
36 
37 /************************************************************************/
38 /* CPLAutoClose */
39 /************************************************************************/
40 
50 template<typename _Ty,typename _Dx>
51 class CPLAutoClose {
52  static_assert( !std::is_const<_Ty>::value && std::is_pointer<_Ty>::value,
53  "_Ty must is pointer type,_Dx must is function type");
54  private:
55  _Ty& m_ResourcePtr;
56  _Dx m_CloseFunc;
57  private:
58  CPLAutoClose(const CPLAutoClose&) = delete;
59  void operator=(const CPLAutoClose&) = delete;
60  public:
66  explicit CPLAutoClose(_Ty& ptr,_Dx dt) :
67  m_ResourcePtr(ptr),
68  m_CloseFunc(dt)
69  {}
74  {
75  if(m_ResourcePtr && m_CloseFunc)
76  m_CloseFunc(m_ResourcePtr);
77  }
78 };
79 
80 #define CPL_AUTO_CLOSE_WARP(hObject,closeFunc) \
81  CPLAutoClose<decltype(hObject),decltype(closeFunc)*> tAutoClose##hObject(hObject,closeFunc)
82 
83 #endif /* __cplusplus */
84 
85 #endif /* CPL_AUTO_CLOSE_H_INCLUDED */
The class use the destructor to automatically close the resource.
Definition: cpl_auto_close.h:51
CPLAutoClose(_Ty &ptr, _Dx dt)
Constructor.
Definition: cpl_auto_close.h:66
~CPLAutoClose()
Destructor.
Definition: cpl_auto_close.h:73

Generated for GDAL by doxygen 1.8.13.