Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

boost 2: shared_ptr wraps resource handles

, 16 Nov 2004
Using boost, we can write "almost perfect" wrappers for GDI and other resource handles, in a few lines of code.
handleref.zip
handleref_2.zip
handleref_project.zip
HandleRef
default.doxygen
doxydoc
doxygen.png
ftv2blank.png
ftv2doc.png
ftv2folderclosed.png
ftv2folderopen.png
ftv2lastnode.png
ftv2link.png
ftv2mlastnode.png
ftv2mnode.png
ftv2node.png
ftv2plastnode.png
ftv2pnode.png
ftv2vertline.png
ResHandle.dsp
ResHandle.dsw
handleref_project_2.zip
default.doxygen
doxygen.png
ftv2blank.png
ftv2doc.png
ftv2folderclosed.png
ftv2folderopen.png
ftv2lastnode.png
ftv2link.png
ftv2mlastnode.png
ftv2mnode.png
ftv2node.png
ftv2plastnode.png
ftv2pnode.png
ftv2vertline.png
ResHandle.dsp
ResHandle.dsw
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>HandleRef: handleref.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.5 -->
<div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
<h1>handleref.h</h1><a href="handleref_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">// ==================================================================</span>
00002 <span class="comment">// </span>
00003 <span class="comment">//  handleref.h</span>
00004 <span class="comment">//  </span>
00005 <span class="comment">//  Created:       03.10.2004</span>
00006 <span class="comment">//</span>
00007 <span class="comment">//  Copyright (C) Peter Hauptmann 2004</span>
00008 <span class="comment">//              </span>
00009 <span class="comment">// ------------------------------------------------------------------</span>
00045 <span class="comment"></span>
00046 <span class="preprocessor">#ifndef FILE_PH_HANDLEREF_H_200427719_INCLUDED_</span>
00047 <span class="preprocessor"></span><span class="preprocessor">#define FILE_PH_HANDLEREF_H_200427719_INCLUDED_</span>
00048 <span class="preprocessor"></span>
00049 <span class="preprocessor">#if _MSC_VER &gt; 1000</span>
00050 <span class="preprocessor"></span><span class="preprocessor">#pragma once</span>
00051 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
00052 <span class="preprocessor"></span>
00053 <span class="comment">// boost::shared_ptr is required for this file, see boost.org for details</span>
00054 <span class="preprocessor">#include &lt;boost/shared_ptr.hpp&gt;</span> 
00055 
00056 <span class="comment">// ==================================================================</span>
00057 <span class="comment">//  namespace nsHandleRefDetail</span>
00058 <span class="comment">// ------------------------------------------------------------------</span>
<a name="l00067"></a><a class="code" href="namespacensHandleRefDetail.html">00067</a> <span class="comment"></span><span class="keyword">namespace </span>nsHandleRefDetail
00068 {
00069 
<a name="l00071"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterVoid.html">00071</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterVoid.html">CDeleterVoid</a>
00072 {
<a name="l00073"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterVoid.html#a0">00073</a>     <span class="keywordtype">void</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterVoid.html#a0">operator()</a>(<span class="keywordtype">void</span> * p) { }
00074 };
00075 
<a name="l00077"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterGDI.html">00077</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterGDI.html">CDeleterGDI</a>
00078 {
<a name="l00079"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterGDI.html#a0">00079</a>     <span class="keywordtype">void</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterGDI.html#a0">operator()</a>(<span class="keywordtype">void</span> * p) { DeleteObject( (HGDIOBJ) p); }
00080 };
00081 
<a name="l00083"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterImageList.html">00083</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterImageList.html">CDeleterImageList</a>
00084 {
<a name="l00085"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterImageList.html#a0">00085</a>     <span class="keywordtype">void</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterImageList.html#a0">operator()</a>(<span class="keywordtype">void</span> * p) { ImageList_Destroy( (HIMAGELIST) p); }
00086 };
00087 
<a name="l00089"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterHMENU.html">00089</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterHMENU.html">CDeleterHMENU</a>
00090 {
<a name="l00091"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterHMENU.html#a0">00091</a>     <span class="keywordtype">void</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterHMENU.html#a0">operator()</a>(<span class="keywordtype">void</span> * p) { DestroyMenu((HMENU) p); }
00092 };
00093 
00094 
<a name="l00096"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterHANDLE.html">00096</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterHANDLE.html">CDeleterHANDLE</a>
00097 {
<a name="l00098"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterHANDLE.html#a0">00098</a>     <span class="keywordtype">void</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterHANDLE.html#a0">operator()</a>(<span class="keywordtype">void</span> * p) { CloseHandle( (HANDLE) p); }
00099 };
00100 
<a name="l00102"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterHKEY.html">00102</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterHKEY.html">CDeleterHKEY</a>
00103 {
<a name="l00104"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterHKEY.html#a0">00104</a>     <span class="keywordtype">void</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterHKEY.html#a0">operator()</a>(<span class="keywordtype">void</span> * p) { RegCloseKey( (HKEY) p); }
00105 };
00106 
<a name="l00107"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterReleaseDC.html">00107</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterReleaseDC.html">CDeleterReleaseDC</a>
00108 {
<a name="l00109"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterReleaseDC.html#o0">00109</a>     HWND <a class="code" href="structnsHandleRefDetail_1_1CDeleterReleaseDC.html#o0">wnd</a>;
<a name="l00110"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterReleaseDC.html#a0">00110</a>     <a class="code" href="structnsHandleRefDetail_1_1CDeleterReleaseDC.html#a0">CDeleterReleaseDC</a>(HWND _wnd) : <a class="code" href="structnsHandleRefDetail_1_1CDeleterReleaseDC.html#o0">wnd</a>(_wnd) {}
00111 
<a name="l00112"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterReleaseDC.html#a1">00112</a>     <span class="keywordtype">void</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterReleaseDC.html#a1">operator()</a>(<span class="keywordtype">void</span> * p) { ReleaseDC(<a class="code" href="structnsHandleRefDetail_1_1CDeleterReleaseDC.html#o0">wnd</a>, (HDC) p); }
00113 };
00114 
<a name="l00115"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterDeleteDC.html">00115</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterDeleteDC.html">CDeleterDeleteDC</a>
00116 {
<a name="l00117"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterDeleteDC.html#a0">00117</a>     <span class="keywordtype">void</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterDeleteDC.html#a0">operator()</a>(<span class="keywordtype">void</span> * p) { DeleteDC( (HDC) p); }
00118 };
00119 
00120 
00121 
00122 } <span class="comment">// namespace nsHandleRef_Detail</span>
00123 
00124 
00125 
00126 <span class="comment">// ==================================================================</span>
00127 <span class="comment">//  CHandleRefT::</span>
00128 <span class="comment">// ------------------------------------------------------------------</span>
00159 <span class="comment"></span><span class="keyword">template</span> &lt;<span class="keyword">class</span> HDL, <span class="keyword">class</span> DELETER&gt;
<a name="l00160"></a><a class="code" href="classCHandleRefT.html">00160</a> <span class="keyword">class </span><a class="code" href="classCHandleRefT.html">CHandleRefT</a>
00161 {
00162 <span class="keyword">protected</span>:
<a name="l00163"></a><a class="code" href="classCHandleRefT.html#x0">00163</a>     <span class="keyword">typedef</span> boost::shared_ptr&lt;void&gt; <a class="code" href="classCHandleRefT.html#x0">tBoostSP</a>;
<a name="l00164"></a><a class="code" href="classCHandleRefT.html#p0">00164</a>     <a class="code" href="classCHandleRefT.html#x0">tBoostSP</a> <a class="code" href="classCHandleRefT.html#p0">m_ptr</a>;
00165 
00166 <span class="keyword">public</span>:
00167 
<a name="l00169"></a><a class="code" href="classCHandleRefT.html#a0">00169</a>     <a class="code" href="classCHandleRefT.html#a0">CHandleRefT</a>() {}
00170 
<a name="l00175"></a><a class="code" href="classCHandleRefT.html#a1">00175</a>     <span class="keyword">explicit</span> <a class="code" href="classCHandleRefT.html#a0">CHandleRefT</a>(HDL h, <span class="keywordtype">bool</span> deleteOnRelease)
00176     {
00177         <span class="keywordflow">if</span> (deleteOnRelease)
00178             <a class="code" href="classCHandleRefT.html#p0">m_ptr</a> = <a class="code" href="classCHandleRefT.html#x0">tBoostSP</a>(h, DELETER());
00179         <span class="keywordflow">else</span>
00180             <a class="code" href="classCHandleRefT.html#p0">m_ptr</a> = <a class="code" href="classCHandleRefT.html#x0">tBoostSP</a>(h, <a class="code" href="structnsHandleRefDetail_1_1CDeleterVoid.html">nsHandleRefDetail::CDeleterVoid</a>());
00181     }
00182 
<a name="l00184"></a><a class="code" href="classCHandleRefT.html#a2">00184</a>     <a class="code" href="classCHandleRefT.html#a2">operator HDL</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> (HDL) <a class="code" href="classCHandleRefT.html#p0">m_ptr</a>.get(); }
00185 
<a name="l00186"></a><a class="code" href="classCHandleRefT.html#a3">00186</a>     <span class="keywordtype">void</span> <a class="code" href="classCHandleRefT.html#a3">reset</a>() { <a class="code" href="classCHandleRefT.html#p0">m_ptr</a>.reset(); }
00187 };
00188 
00189 
00190 <span class="comment">// ==================================================================</span>
00191 <span class="comment">//  CDCRef::CDCRef</span>
00192 <span class="comment">// ------------------------------------------------------------------</span>
<a name="l00198"></a><a class="code" href="classCDCRef.html">00198</a> <span class="comment"></span><span class="keyword">class </span><a class="code" href="classCDCRef.html">CDCRef</a> 
00199 {
00200 <span class="keyword">protected</span>:
<a name="l00201"></a><a class="code" href="classCDCRef.html#x0">00201</a>     <span class="keyword">typedef</span> boost::shared_ptr&lt;void&gt; <a class="code" href="classCDCRef.html#x0">tBoostSP</a>;
<a name="l00202"></a><a class="code" href="classCDCRef.html#p0">00202</a>     <a class="code" href="classCDCRef.html#x0">tBoostSP</a> <a class="code" href="classCDCRef.html#p0">m_ptr</a>;
00203 
<a name="l00204"></a><a class="code" href="classCDCRef.html#x1">00204</a>     <span class="keyword">typedef</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterVoid.html">nsHandleRefDetail::CDeleterVoid</a>         <a class="code" href="structnsHandleRefDetail_1_1CDeleterVoid.html">Null</a>;
<a name="l00205"></a><a class="code" href="classCDCRef.html#x2">00205</a>     <span class="keyword">typedef</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterDeleteDC.html">nsHandleRefDetail::CDeleterDeleteDC</a>     <a class="code" href="structnsHandleRefDetail_1_1CDeleterDeleteDC.html">Delete</a>;
<a name="l00206"></a><a class="code" href="classCDCRef.html#x3">00206</a>     <span class="keyword">typedef</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterReleaseDC.html">nsHandleRefDetail::CDeleterReleaseDC</a>    <a class="code" href="structnsHandleRefDetail_1_1CDeleterReleaseDC.html">Release</a>;
00207 
00208 <span class="keyword">public</span>:
00209 
<a name="l00211"></a><a class="code" href="classCDCRef.html#a0">00211</a>     <a class="code" href="classCDCRef.html#a0">CDCRef</a>() {}
00212 
<a name="l00214"></a><a class="code" href="classCDCRef.html#a1">00214</a>     <a class="code" href="classCDCRef.html#a1">operator HDC</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> (HDC) <a class="code" href="classCDCRef.html#p0">m_ptr</a>.get(); }
00215 
00216 
00222 
<a name="l00223"></a><a class="code" href="classCDCRef.html#a2">00223</a>     <a class="code" href="classCDCRef.html#a0">CDCRef</a>(HDC dc, <span class="keywordtype">bool</span> deleteOnRelease) 
00224     { 
00225         <a class="code" href="classCDCRef.html#p0">m_ptr</a> = deleteOnRelease ? <a class="code" href="classCDCRef.html#x0">tBoostSP</a>(dc, <a class="code" href="classCDCRef.html#x2">Delete</a>()) :
00226                                   <a class="code" href="classCDCRef.html#x0">tBoostSP</a>(dc, <a class="code" href="classCDCRef.html#x1">Null</a>());
00227     }
00228 
<a name="l00229"></a><a class="code" href="classCDCRef.html#a3">00229</a>     <a class="code" href="classCDCRef.html#a0">CDCRef</a>(HWND wnd, HDC dc, <span class="keywordtype">bool</span> releaseOnRelease)
00230     {
00231         <a class="code" href="classCDCRef.html#p0">m_ptr</a> = releaseOnRelease ? <a class="code" href="classCDCRef.html#x0">tBoostSP</a>(dc, <a class="code" href="classCDCRef.html#x3">Release</a>(wnd)) :
00232                                    <a class="code" href="classCDCRef.html#x0">tBoostSP</a>(dc, <a class="code" href="classCDCRef.html#x1">Null</a>());
00233     }
00234 
<a name="l00237"></a><a class="code" href="classCDCRef.html#e0">00237</a>     <span class="keyword">static</span> <a class="code" href="classCDCRef.html">CDCRef</a> <a class="code" href="classCDCRef.html#e0">GetDC</a>(HWND wnd) 
00238     { 
00239         <span class="keywordflow">return</span> <a class="code" href="classCDCRef.html#a0">CDCRef</a>(wnd, ::<a class="code" href="classCDCRef.html#e0">GetDC</a>(wnd), <span class="keyword">true</span>); 
00240     }
00241 
<a name="l00244"></a><a class="code" href="classCDCRef.html#e1">00244</a>     <span class="keyword">static</span> <a class="code" href="classCDCRef.html">CDCRef</a> <a class="code" href="classCDCRef.html#e1">GetDCEx</a>(HWND wnd, HRGN rgnClip, DWORD flags) 
00245     { 
00246         <span class="keywordflow">return</span> <a class="code" href="classCDCRef.html#a0">CDCRef</a>(wnd, ::<a class="code" href="classCDCRef.html#e1">GetDCEx</a>(wnd, rgnClip, flags), <span class="keyword">true</span>); 
00247     }
00248 
<a name="l00249"></a><a class="code" href="classCDCRef.html#e2">00249</a>     <span class="keyword">static</span> <a class="code" href="classCDCRef.html">CDCRef</a> <a class="code" href="classCDCRef.html#e2">GetWindowDC</a>(HWND wnd)
00250     {
00251         <span class="keywordflow">return</span> <a class="code" href="classCDCRef.html#a0">CDCRef</a>(wnd, ::<a class="code" href="classCDCRef.html#e2">GetWindowDC</a>(wnd), <span class="keyword">true</span> );
00252     }
00253 
00254 
<a name="l00259"></a><a class="code" href="classCDCRef.html#e3">00259</a>     <span class="keyword">static</span> <a class="code" href="classCDCRef.html">CDCRef</a> <a class="code" href="classCDCRef.html#e3">CreateDC</a>(LPCTSTR lpszDriver, 
00260                            LPCTSTR lpszDevice, 
00261                            LPCTSTR  lpszOutput, 
00262                            CONST DEVMODE * lpInitData, <span class="keywordtype">bool</span> infoDCOnly = <span class="keyword">false</span>)
00263     {
00264         HDC dc = infoDCOnly ? ::CreateIC(lpszDriver, lpszDevice, lpszOutput, lpInitData) :
00265                               ::CreateDC(lpszDriver, lpszDevice, lpszOutput, lpInitData);
00266         <span class="keywordflow">return</span> <a class="code" href="classCDCRef.html#a0">CDCRef</a>(dc, <span class="keyword">true</span>);
00267     }
00268 
<a name="l00272"></a><a class="code" href="classCDCRef.html#e4">00272</a>     <span class="keyword">static</span> <a class="code" href="classCDCRef.html">CDCRef</a> <a class="code" href="classCDCRef.html#e4">CreateCompatibleDC</a>(HDC dc)
00273     {
00274         <span class="keywordflow">return</span> <a class="code" href="classCDCRef.html#a0">CDCRef</a>( ::<a class="code" href="classCDCRef.html#e4">CreateCompatibleDC</a>(dc), <span class="keyword">true</span>);
00275     }
00276 };
00277 
00278 
00279 
00280 
<a name="l00281"></a><a class="code" href="handleref_8h.html#a0">00281</a> <span class="keyword">typedef</span> <a class="code" href="classCHandleRefT.html">CHandleRefT&lt;HIMAGELIST, nsHandleRefDetail::CDeleterImageList&gt;</a> <a class="code" href="classCHandleRefT.html">CImageListRef</a>;
<a name="l00282"></a><a class="code" href="handleref_8h.html#a1">00282</a> <span class="keyword">typedef</span> <a class="code" href="classCHandleRefT.html">CHandleRefT&lt;HBITMAP, nsHandleRefDetail::CDeleterGDI&gt;</a> <a class="code" href="classCHandleRefT.html">CBitmapRef</a>;
00283 <span class="keyword">typedef</span> <a class="code" href="classCHandleRefT.html">CHandleRefT&lt;HBITMAP, nsHandleRefDetail::CDeleterGDI&gt;</a> <a class="code" href="handleref_8h.html#a1">CBitmapRef</a>;
<a name="l00284"></a><a class="code" href="handleref_8h.html#a2">00284</a> <span class="keyword">typedef</span> <a class="code" href="classCHandleRefT.html">CHandleRefT&lt;HBRUSH, nsHandleRefDetail::CDeleterGDI&gt;</a> <a class="code" href="classCHandleRefT.html">CBrushRef</a>;
<a name="l00285"></a><a class="code" href="handleref_8h.html#a3">00285</a> <span class="keyword">typedef</span> <a class="code" href="classCHandleRefT.html">CHandleRefT&lt;HPEN, nsHandleRefDetail::CDeleterGDI&gt;</a> <a class="code" href="classCHandleRefT.html">CPenRef</a>;
<a name="l00286"></a><a class="code" href="handleref_8h.html#a4">00286</a> <span class="keyword">typedef</span> <a class="code" href="classCHandleRefT.html">CHandleRefT&lt;HANDLE, nsHandleRefDetail::CDeleterHANDLE&gt;</a> <a class="code" href="classCHandleRefT.html">CHandleRef</a>;
<a name="l00287"></a><a class="code" href="handleref_8h.html#a5">00287</a> <span class="keyword">typedef</span> <a class="code" href="classCHandleRefT.html">CHandleRefT&lt;HMENU, nsHandleRefDetail::CDeleterHMENU&gt;</a> <a class="code" href="classCHandleRefT.html">CMenuRef</a>;
<a name="l00288"></a><a class="code" href="handleref_8h.html#a6">00288</a> <span class="keyword">typedef</span> <a class="code" href="classCHandleRefT.html">CHandleRefT&lt;HKEY, nsHandleRefDetail::CDeleterHKEY&gt;</a> <a class="code" href="classCHandleRefT.html">CHKeyRef</a>;
00289 <span class="comment">// CDCRef - special class</span>
00290 
00291 
00340 <span class="preprocessor">#endif // FILE_PH_HANDLEREF_H_200427719_INCLUDED_</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Nov 16 22:21:51 2004 for HandleRef by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 > 
</a>1.3.5 </small></address>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

peterchen
Klippel
Germany Germany
Peter is tired of being called "Mr. Chen", even so certain individuals insist on it. No, he's not chinese.
 
Peter has seen lots of boxes you youngsters wouldn't even accept as calculators. He is proud of having visited the insides of a 16 Bit Machine.
 
In his spare time he ponders new ways of turning groceries into biohazards, or tries to coax South American officials to add some stamps to his passport.
 
Beyond these trivialities Peter works for Klippel[^], a small german company that wants to make mankind happier by selling them novel loudspeaker measurement equipment.
 

Where are you from?[^]



Please, if you are using one of my articles for anything, just leave me a comment. Seeing that this stuff is actually useful to someone is what keeps me posting and updating them.
Should you happen to not like it, tell me, too

| Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 16 Nov 2004
Article Copyright 2004 by peterchen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid