Click here to Skip to main content
15,886,258 members
Articles / Multimedia / GDI

boost 2: shared_ptr wraps resource handles

Rate me:
Please Sign up or sign in to vote.
4.96/5 (41 votes)
16 Nov 200415 min read 228.8K   1.3K   56  
Using boost, we can write "almost perfect" wrappers for GDI and other resource handles, in a few lines of code.
<!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>
00043 <span class="comment"></span>
00044 <span class="preprocessor">#ifndef FILE_PH_HANDLEREF_H_200427719_INCLUDED_</span>
00045 <span class="preprocessor"></span><span class="preprocessor">#define FILE_PH_HANDLEREF_H_200427719_INCLUDED_</span>
00046 <span class="preprocessor"></span>
00047 <span class="preprocessor">#if _MSC_VER &gt; 1000</span>
00048 <span class="preprocessor"></span><span class="preprocessor">#pragma once</span>
00049 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
00050 <span class="preprocessor"></span>
00051 <span class="comment">// boost::shared_ptr is required for this file, see boost.org for details</span>
00052 <span class="preprocessor">#include &lt;boost/shared_ptr.hpp&gt;</span> 
00053 
00054 <span class="comment">// ==================================================================</span>
00055 <span class="comment">//  namespace nsHandleRefDetail</span>
00056 <span class="comment">// ------------------------------------------------------------------</span>
<a name="l00065"></a><a class="code" href="namespacensHandleRefDetail.html">00065</a> <span class="comment"></span><span class="keyword">namespace </span>nsHandleRefDetail
00066 {
00067 
<a name="l00069"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterVoid.html">00069</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterVoid.html">CDeleterVoid</a>
00070 {
<a name="l00071"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterVoid.html#a0">00071</a>     <span class="keywordtype">void</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterVoid.html#a0">operator()</a>(<span class="keywordtype">void</span> * p) { }
00072 };
00073 
<a name="l00075"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterGDI.html">00075</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterGDI.html">CDeleterGDI</a>
00076 {
<a name="l00077"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterGDI.html#a0">00077</a>     <span class="keywordtype">void</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterGDI.html#a0">operator()</a>(<span class="keywordtype">void</span> * p) { DeleteObject(p); }
00078 };
00079 
<a name="l00081"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterImageList.html">00081</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterImageList.html">CDeleterImageList</a>
00082 {
<a name="l00083"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterImageList.html#a0">00083</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); }
00084 };
00085 
<a name="l00087"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterHMENU.html">00087</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterHMENU.html">CDeleterHMENU</a>
00088 {
<a name="l00089"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterHMENU.html#a0">00089</a>     <span class="keywordtype">void</span> <a class="code" href="structnsHandleRefDetail_1_1CDeleterHMENU.html#a0">operator()</a>(<span class="keywordtype">void</span> * p) { DeleteMenu((HMENU) p); }
00090 };
00091 
00092 
<a name="l00094"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterHANDLE.html">00094</a> <span class="keyword">struct </span><a class="code" href="structnsHandleRefDetail_1_1CDeleterHANDLE.html">CDeleterHANDLE</a>
00095 {
<a name="l00096"></a><a class="code" href="structnsHandleRefDetail_1_1CDeleterHANDLE.html#a0">00096</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); }
00097 };
00098 
00099 
00100 } <span class="comment">// namespace nsHandleRef_Detail</span>
00101 
00102 
00103 
00104 <span class="comment">// ==================================================================</span>
00105 <span class="comment">//  CHandleRefT::</span>
00106 <span class="comment">// ------------------------------------------------------------------</span>
00137 <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="l00138"></a><a class="code" href="classCHandleRefT.html">00138</a> <span class="keyword">class </span><a class="code" href="classCHandleRefT.html">CHandleRefT</a>
00139 {
00140 <span class="keyword">protected</span>:
<a name="l00141"></a><a class="code" href="classCHandleRefT.html#x0">00141</a>     <span class="keyword">typedef</span> boost::shared_ptr&lt;void&gt; <a class="code" href="classCHandleRefT.html#x0">tBoostSP</a>;
<a name="l00142"></a><a class="code" href="classCHandleRefT.html#p0">00142</a>     <a class="code" href="classCHandleRefT.html#x0">tBoostSP</a> <a class="code" href="classCHandleRefT.html#p0">m_ptr</a>;
00143 
00144 <span class="keyword">public</span>:
00145 
<a name="l00147"></a><a class="code" href="classCHandleRefT.html#a0">00147</a>     <a class="code" href="classCHandleRefT.html#a0">CHandleRefT</a>() {}
00148 
<a name="l00153"></a><a class="code" href="classCHandleRefT.html#a1">00153</a>     <span class="keyword">explicit</span> <a class="code" href="classCHandleRefT.html#a0">CHandleRefT</a>(HDL h, <span class="keywordtype">bool</span> deleteOnRelease)
00154     {
00155         <span class="keywordflow">if</span> (deleteOnRelease)
00156             <a class="code" href="classCHandleRefT.html#p0">m_ptr</a> = <a class="code" href="classCHandleRefT.html#x0">tBoostSP</a>(h, DELETER());
00157         <span class="keywordflow">else</span>
00158             <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>());
00159     }
00160 
<a name="l00162"></a><a class="code" href="classCHandleRefT.html#a2">00162</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(); }
00163 };
00164 
00165 
00166 
<a name="l00167"></a><a class="code" href="handleref_8h.html#a0">00167</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="l00168"></a><a class="code" href="handleref_8h.html#a1">00168</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>;
00169 <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="l00170"></a><a class="code" href="handleref_8h.html#a2">00170</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="l00171"></a><a class="code" href="handleref_8h.html#a3">00171</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="l00172"></a><a class="code" href="handleref_8h.html#a4">00172</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="l00173"></a><a class="code" href="handleref_8h.html#a5">00173</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>;
00174 
00175 
00224 <span class="preprocessor">#endif // FILE_PH_HANDLEREF_H_200427719_INCLUDED_</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Oct 5 01:58:09 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


Written By
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

Comments and Discussions