Click here to Skip to main content
15,880,392 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 227.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:  TemplateCHandleRefT&lt; HDL, DELETER &gt; class Reference</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>CHandleRefT&lt; HDL, DELETER &gt; Class Template Reference</h1><code>#include &lt;<a class="el" href="handleref_8h-source.html">handleref.h</a>&gt;</code>
<p>
<hr><a name="_details"></a><h2>Detailed Description</h2>
<h3>template&lt;class HDL, class DELETER&gt;<br>
 class CHandleRefT&lt; HDL, DELETER &gt;</h3>

Template class implementing a counted reference to a windows resource handle. 
<p>
Usage rules are similar to a reference counted smart pointer.<p>
<dl compact><dt><b>template parameters:</b></dt><dd><ul>
<li><code>HDL</code> : type of the resource handle (e.g. HFONT)</li><li><code>DELETER</code> : a functor releasing the resources of Handle of type HDL (e.g. a functor calling DeleteObject). The handle is passed as void *.</li></ul>
</dd></dl>
<dl compact><dt><b>Automatic vs. manual handles</b></dt><dd>When constructing a HandleRef from a raw handle, you pass a <code>bDeleteOnRelease</code> flag indicating if the handle should be released automatically when the last reference to it goes out of scope. <br>
<br>
 For an automatic handle, assign the raw handle to a HandleRef immediately after construction, specifying bDeleteOnRelease=true. Then then pass it around only as HandleRef. This guarantees the handle is deleted when it is no longer used. <br>
<br>
 For a manual handle, that you will delete yourself manually, or that must not be deleted, specify <code>bDeleteOnRelease=false</code>. <br>
<br>
 The idea here is that you can pass around, copy, store, or return a HandleRef, it remembers it's deletion policy.</dd></dl>
<dl compact><dt><b>Guidelines</b></dt><dd>Utility functions that merely receive and use a resource handle (without storing it) may use the raw handle as argument. However, when the Handle is stored (e.g. as a class member) or used as a return value from a function, a HandleRef is recommended. </dd></dl>

<p>

<p>
Definition at line <a class="el" href="handleref_8h-source.html#l00138">138</a> of file <a class="el" href="handleref_8h-source.html">handleref.h</a>.<table border=0 cellpadding=0 cellspacing=0>
<tr><td></td></tr>
<tr><td colspan=2><br><h2>Public Member Functions</h2></td></tr>
<tr><td class="memItemLeft" nowrap align=right valign=top>&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="classCHandleRefT.html#a0">CHandleRefT</a> ()</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">creates a null handle  <a href="#a0"></a><br><br></td></tr>
<tr><td class="memItemLeft" nowrap align=right valign=top>&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="classCHandleRefT.html#a1">CHandleRefT</a> (HDL h, bool deleteOnRelease)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Initialize the Handle Reference with a handle.  <a href="#a1"></a><br><br></td></tr>
<tr><td class="memItemLeft" nowrap align=right valign=top>&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="classCHandleRefT.html#a2">operator HDL</a> () const </td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">automatic cast to the handle type  <a href="#a2"></a><br><br></td></tr>
<tr><td colspan=2><br><h2>Protected Types</h2></td></tr>
<tr><td class="memItemLeft" nowrap align=right valign=top>typedef boost::shared_ptr&lt;<br>
 void &gt;&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="classCHandleRefT.html#x0">tBoostSP</a></td></tr>

<tr><td colspan=2><br><h2>Protected Attributes</h2></td></tr>
<tr><td class="memItemLeft" nowrap align=right valign=top><a class="el" href="classCHandleRefT.html#x0">tBoostSP</a>&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="classCHandleRefT.html#p0">m_ptr</a></td></tr>

</table>
<hr><h2>Member Typedef Documentation</h2>
<a class="anchor" name="x0" doxytag="CHandleRefT::tBoostSP" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" colspan="4">
template&lt;class HDL, class DELETER&gt; </td>
        </tr>
        <tr>
          <td class="md" nowrap valign="top"> typedef boost::shared_ptr&lt;void&gt; <a class="el" href="classCHandleRefT.html">CHandleRefT</a>&lt; HDL, DELETER &gt;::<a class="el" href="classCHandleRefT.html#x0">tBoostSP</a><code> [protected]</code>
      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>

<p>
Definition at line <a class="el" href="handleref_8h-source.html#l00141">141</a> of file <a class="el" href="handleref_8h-source.html">handleref.h</a>.    </td>
  </tr>
</table>
<hr><h2>Constructor &amp; Destructor Documentation</h2>
<a class="anchor" name="a0" doxytag="CHandleRefT::CHandleRefT" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" colspan="4">
template&lt;class HDL, class DELETER&gt; </td>
        </tr>
        <tr>
          <td class="md" nowrap valign="top"> <a class="el" href="classCHandleRefT.html">CHandleRefT</a>&lt; HDL, DELETER &gt;::<a class="el" href="classCHandleRefT.html">CHandleRefT</a> </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap><code> [inline]</code></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
creates a null handle 
<p>

<p>
Definition at line <a class="el" href="handleref_8h-source.html#l00147">147</a> of file <a class="el" href="handleref_8h-source.html">handleref.h</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a1" doxytag="CHandleRefT::CHandleRefT" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" colspan="4">
template&lt;class HDL, class DELETER&gt; </td>
        </tr>
        <tr>
          <td class="md" nowrap valign="top"> <a class="el" href="classCHandleRefT.html">CHandleRefT</a>&lt; HDL, DELETER &gt;::<a class="el" href="classCHandleRefT.html">CHandleRefT</a> </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top">HDL&nbsp;</td>
          <td class="mdname" nowrap> <em>h</em>, </td>
        </tr>
        <tr>
          <td></td>
          <td></td>
          <td class="md" nowrap>bool&nbsp;</td>
          <td class="mdname" nowrap> <em>deleteOnRelease</em></td>
        </tr>
        <tr>
          <td></td>
          <td class="md">)&nbsp;</td>
          <td class="md" colspan="2"><code> [inline, explicit]</code></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Initialize the Handle Reference with a handle. 
<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign=top><em>h</em>&nbsp;</td><td>: Handle to the resource to (of type <code>HDL</code>) </td></tr>
    <tr><td valign=top><em>deleteOnRelease</em>&nbsp;</td><td>[bool]: if true, the handle is deleted automatically (using the specified <code>DELETER</code>) when the last HandleRef goes put of scope. </td></tr>
  </table>
</dl>

<p>
Definition at line <a class="el" href="handleref_8h-source.html#l00153">153</a> of file <a class="el" href="handleref_8h-source.html">handleref.h</a>.    </td>
  </tr>
</table>
<hr><h2>Member Function Documentation</h2>
<a class="anchor" name="a2" doxytag="CHandleRefT::operator HDL" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" colspan="4">
template&lt;class HDL, class DELETER&gt; </td>
        </tr>
        <tr>
          <td class="md" nowrap valign="top"> <a class="el" href="classCHandleRefT.html">CHandleRefT</a>&lt; HDL, DELETER &gt;::operator HDL </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap> const<code> [inline]</code></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
automatic cast to the handle type 
<p>

<p>
Definition at line <a class="el" href="handleref_8h-source.html#l00162">162</a> of file <a class="el" href="handleref_8h-source.html">handleref.h</a>.    </td>
  </tr>
</table>
<hr><h2>Field Documentation</h2>
<a class="anchor" name="p0" doxytag="CHandleRefT::m_ptr" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" colspan="4">
template&lt;class HDL, class DELETER&gt; </td>
        </tr>
        <tr>
          <td class="md" nowrap valign="top"> <a class="el" href="classCHandleRefT.html#x0">tBoostSP</a> <a class="el" href="classCHandleRefT.html">CHandleRefT</a>&lt; HDL, DELETER &gt;::<a class="el" href="classCHandleRefT.html#p0">m_ptr</a><code> [protected]</code>
      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>

<p>
Definition at line <a class="el" href="handleref_8h-source.html#l00142">142</a> of file <a class="el" href="handleref_8h-source.html">handleref.h</a>.    </td>
  </tr>
</table>
<hr>The documentation for this class was generated from the following file:<ul>
<li><a class="el" href="handleref_8h-source.html">handleref.h</a></ul>
<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