Click here to Skip to main content
15,891,431 members
Articles / Programming Languages / C++

Writing an exception safe code in generic way.

Rate me:
Please Sign up or sign in to vote.
4.88/5 (11 votes)
12 Dec 20069 min read 40.9K   326   36  
An article on how to change the way of writing exception-safe 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>am::mate: am::mate&lt; R &gt; Class Template Reference</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.1 -->
<div class="tabs">
  <ul>
    <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
    <li><a href="namespaces.html"><span>Namespaces</span></a></li>
    <li id="current"><a href="annotated.html"><span>Classes</span></a></li>
    <li><a href="files.html"><span>Files</span></a></li>
    <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
  </ul></div>
<div class="tabs">
  <ul>
    <li><a href="annotated.html"><span>Class&nbsp;List</span></a></li>
    <li><a href="hierarchy.html"><span>Class&nbsp;Hierarchy</span></a></li>
    <li><a href="functions.html"><span>Class&nbsp;Members</span></a></li>
  </ul></div>
<div class="nav">
<a class="el" href="namespaceam.html">am</a>::<a class="el" href="classam_1_1mate.html">mate</a></div>
<h1>am::mate&lt; R &gt; Class Template Reference</h1><!-- doxytag: class="am::mate" --><!-- doxytag: inherits="am::detail::mate_base" -->Mates a host function with the mate function object.  
<a href="#_details">More...</a>
<p>
<code>#include &lt;mate.hpp&gt;</code>
<p>
Inherits <a class="el" href="classam_1_1detail_1_1mate__base.html">am::detail::mate_base</a>.
<p>
Inheritance diagram for am::mate&lt; R &gt;:<p><center><img src="classam_1_1mate__inherit__graph.png" border="0" usemap="#am_1_1mate_3_01_r_01_4__inherit__map" alt="Inheritance graph"></center>
<map name="am_1_1mate_3_01_r_01_4__inherit__map">
<area href="classam_1_1detail_1_1mate__base.html" shape="rect" coords="6,5,158,32" alt="">
</map>
<center><font size="2">[<a href="graph_legend.html">legend</a>]</font></center>Collaboration diagram for am::mate&lt; R &gt;:<p><center><img src="classam_1_1mate__coll__graph.png" border="0" usemap="#am_1_1mate_3_01_r_01_4__coll__map" alt="Collaboration graph"></center>
<map name="am_1_1mate_3_01_r_01_4__coll__map">
<area href="classam_1_1detail_1_1mate__base.html" shape="rect" coords="6,5,158,32" alt="">
<area href="classam_1_1detail_1_1simple__ptr__holder.html" shape="rect" coords="182,5,374,32" alt="">
</map>
<center><font size="2">[<a href="graph_legend.html">legend</a>]</font></center><a href="classam_1_1mate-members.html">List of all members.</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="memTemplParams" nowrap colspan="2">template&lt;class T&gt; </td></tr>
<tr><td class="memTemplItemLeft" nowrap align="right" valign="top">&nbsp;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="classam_1_1mate.html#52e89c5bf178aff995128a241fc6c3cc">mate</a> (R ret, T mate_functor)</td></tr>

<tr><td class="memTemplParams" nowrap colspan="2">template&lt;class T, class C&gt; </td></tr>
<tr><td class="memTemplItemLeft" nowrap align="right" valign="top">&nbsp;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="classam_1_1mate.html#0992eeb490ba954833e27e0935861b5a">mate</a> (R ret, T mate_functor, C mate_if)</td></tr>

<tr><td class="memItemLeft" nowrap align="right" valign="top">&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classam_1_1mate.html#29cac99d41b0fb1cbd0be3e2c841444c">~mate</a> ()</td></tr>

<tr><td class="memItemLeft" nowrap align="right" valign="top">void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classam_1_1mate.html#93fe5fc5abbe71780e7638bde72e1845">run_mate</a> (bool delete_mate_now=false)</td></tr>

<tr><td class="memItemLeft" nowrap align="right" valign="top">void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classam_1_1mate.html#2ecbeb01954567c013e9322aa5e8e0da">dismiss_mate</a> (bool delete_mate_now=false)</td></tr>

<tr><td class="memItemLeft" nowrap align="right" valign="top">&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classam_1_1mate.html#5bd50a39a4ed2ee277652d394de0acab">operator R</a> () const</td></tr>

<tr><td colspan="2"><br><h2>Private Types</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="ee9347452cd87f7f0fc174ee8eece9de"></a><!-- doxytag: member="am::mate::typeless_mate_stub" ref="ee9347452cd87f7f0fc174ee8eece9de" args="(void *, R)" -->
typedef void(*)&nbsp;</td><td class="memItemRight" valign="bottom"><b>typeless_mate_stub</b> (void *, R)</td></tr>

<tr><td colspan="2"><br><h2>Private Member Functions</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="a2cffaf7bd7a21ba6671e3eb7b7290be"></a><!-- doxytag: member="am::mate::operator=" ref="a2cffaf7bd7a21ba6671e3eb7b7290be" args="(mate&lt; R &gt; const &amp;)" -->
<a class="el" href="classam_1_1mate.html">mate</a>&lt; R &gt; &amp;&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classam_1_1mate.html#a2cffaf7bd7a21ba6671e3eb7b7290be">operator=</a> (<a class="el" href="classam_1_1mate.html">mate</a>&lt; R &gt; const &amp;)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Restrict assignment. <br></td></tr>
<tr><td colspan="2"><br><h2>Private Attributes</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="96d8f086acedaaba49fa8dda704c6e68"></a><!-- doxytag: member="am::mate::ret_" ref="96d8f086acedaaba49fa8dda704c6e68" args="" -->
R&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classam_1_1mate.html#96d8f086acedaaba49fa8dda704c6e68">ret_</a></td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Return of the host function. <br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="dac8b8d494662bc0c5cf0a9f0586a465"></a><!-- doxytag: member="am::mate::mate_functor_ptr_" ref="dac8b8d494662bc0c5cf0a9f0586a465" args="" -->
<a class="el" href="classam_1_1detail_1_1simple__ptr__holder.html">detail::simple_ptr_holder</a>&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classam_1_1mate.html#dac8b8d494662bc0c5cf0a9f0586a465">mate_functor_ptr_</a></td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Mate functoion object. <br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="72f5837ec74e2e14cdb93f69031b95be"></a><!-- doxytag: member="am::mate::mate_stub_" ref="72f5837ec74e2e14cdb93f69031b95be" args="" -->
typeless_mate_stub&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classam_1_1mate.html#72f5837ec74e2e14cdb93f69031b95be">mate_stub_</a></td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Typeless mate function object invoker. <br></td></tr>
<tr><td colspan="2"><br><h2>Classes</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">struct &nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="structam_1_1mate_1_1typed__.html">typed_</a></td></tr>

</table>
<hr><a name="_details"></a><h2>Detailed Description</h2>
<h3>template&lt;class R&gt;<br>
 class am::mate&lt; R &gt;</h3>

Mates a host function with the mate function object. 
<p>
Mates (associates) the result of the host function with the mate function object so that the associated mate function object is called int its destructor automatically when it goes out of scope. 
<p>
<hr><h2>Constructor &amp; Destructor Documentation</h2>
<a class="anchor" name="52e89c5bf178aff995128a241fc6c3cc"></a><!-- doxytag: member="am::mate::mate" ref="52e89c5bf178aff995128a241fc6c3cc" args="(R ret, T mate_functor)" -->
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;class R&gt; </div>
<div class="memtemplate">
template&lt;class T&gt; </div>
      <table class="memname">
        <tr>
          <td class="memname"><a class="el" href="classam_1_1mate.html">am::mate</a>&lt; R &gt;::<a class="el" href="classam_1_1mate.html">mate</a>           </td>
          <td>(</td>
          <td class="paramtype">R&nbsp;</td>
          <td class="paramname"> <em>ret</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">T&nbsp;</td>
          <td class="paramname"> <em>mate_functor</em></td><td>&nbsp;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td><td width="100%"><code> [inline]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Constructor.<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"><tt>[in]</tt>&nbsp;</td><td valign="top"><em>ret</em>&nbsp;</td><td>Specifies the return of the host function. </td></tr>
    <tr><td valign="top"><tt>[in]</tt>&nbsp;</td><td valign="top"><em>mate_functor</em>&nbsp;</td><td>Specifies the mate function object which will be called in the destructor. It should be a unary function object which accept <em>ret</em> as its argument.</td></tr>
  </table>
</dl>
<dl class="pre" compact><dt><b>Precondition:</b></dt><dd>Host function should not throw an exception. </dd></dl>

</div>
</div><p>
<a class="anchor" name="0992eeb490ba954833e27e0935861b5a"></a><!-- doxytag: member="am::mate::mate" ref="0992eeb490ba954833e27e0935861b5a" args="(R ret, T mate_functor, C mate_if)" -->
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;class R&gt; </div>
<div class="memtemplate">
template&lt;class T, class C&gt; </div>
      <table class="memname">
        <tr>
          <td class="memname"><a class="el" href="classam_1_1mate.html">am::mate</a>&lt; R &gt;::<a class="el" href="classam_1_1mate.html">mate</a>           </td>
          <td>(</td>
          <td class="paramtype">R&nbsp;</td>
          <td class="paramname"> <em>ret</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">T&nbsp;</td>
          <td class="paramname"> <em>mate_functor</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">C&nbsp;</td>
          <td class="paramname"> <em>mate_if</em></td><td>&nbsp;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td><td width="100%"><code> [inline]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Constructor.<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"><tt>[in]</tt>&nbsp;</td><td valign="top"><em>ret</em>&nbsp;</td><td>Specifies the return of the host function. </td></tr>
    <tr><td valign="top"><tt>[in]</tt>&nbsp;</td><td valign="top"><em>mate_functor</em>&nbsp;</td><td>Specifies the mate function object which will be called in the destructor. It should be an unary function object which accept <em>ret</em> as its argument. </td></tr>
    <tr><td valign="top"><tt>[in]</tt>&nbsp;</td><td valign="top"><em>mate_if</em>&nbsp;</td><td>Specifies the predicate which determines whether or not the specified mate function object will be called in the destructor. It should be an unary predicate which accept <em>ret</em> as its argument. If it return false, no heap memory allocation is even occurred to store the mate function object.</td></tr>
  </table>
</dl>
<dl class="pre" compact><dt><b>Precondition:</b></dt><dd>Host function should not throw an exception. </dd></dl>

</div>
</div><p>
<a class="anchor" name="29cac99d41b0fb1cbd0be3e2c841444c"></a><!-- doxytag: member="am::mate::~mate" ref="29cac99d41b0fb1cbd0be3e2c841444c" args="()" -->
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;class R&gt; </div>
      <table class="memname">
        <tr>
          <td class="memname"><a class="el" href="classam_1_1mate.html">am::mate</a>&lt; R &gt;::~<a class="el" href="classam_1_1mate.html">mate</a>           </td>
          <td>(</td>
          <td class="paramname">          </td>
          <td>&nbsp;)&nbsp;</td>
          <td width="100%"><code> [inline]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Destructor. Calls mate function object. 
</div>
</div><p>
<hr><h2>Member Function Documentation</h2>
<a class="anchor" name="93fe5fc5abbe71780e7638bde72e1845"></a><!-- doxytag: member="am::mate::run_mate" ref="93fe5fc5abbe71780e7638bde72e1845" args="(bool delete_mate_now=false)" -->
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;class R&gt; </div>
      <table class="memname">
        <tr>
          <td class="memname">void <a class="el" href="classam_1_1mate.html">am::mate</a>&lt; R &gt;::run_mate           </td>
          <td>(</td>
          <td class="paramtype">bool&nbsp;</td>
          <td class="paramname"> <em>delete_mate_now</em> = <code>false</code>          </td>
          <td>&nbsp;)&nbsp;</td>
          <td width="100%"><code> [inline]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Run mate function object now.<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"><tt>[in]</tt>&nbsp;</td><td valign="top"><em>delete_mate_now</em>&nbsp;</td><td>Specifies whether or not the mate function object will be deallocated after calling the mate function object. If false, the mate function object will be deallocated in the destructor when it goes out of scope. </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>void. </dd></dl>

</div>
</div><p>
<a class="anchor" name="2ecbeb01954567c013e9322aa5e8e0da"></a><!-- doxytag: member="am::mate::dismiss_mate" ref="2ecbeb01954567c013e9322aa5e8e0da" args="(bool delete_mate_now=false)" -->
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;class R&gt; </div>
      <table class="memname">
        <tr>
          <td class="memname">void <a class="el" href="classam_1_1mate.html">am::mate</a>&lt; R &gt;::dismiss_mate           </td>
          <td>(</td>
          <td class="paramtype">bool&nbsp;</td>
          <td class="paramname"> <em>delete_mate_now</em> = <code>false</code>          </td>
          <td>&nbsp;)&nbsp;</td>
          <td width="100%"><code> [inline]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Dismiss the mate functor.<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"><tt>[in]</tt>&nbsp;</td><td valign="top"><em>delete_mate_now</em>&nbsp;</td><td>Specifies whether or not the mate function object will be deallocated. If false, the mate function object will be deallocated in the destructor when it goes out of scope. </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>void. </dd></dl>

</div>
</div><p>
<a class="anchor" name="5bd50a39a4ed2ee277652d394de0acab"></a><!-- doxytag: member="am::mate::operator R" ref="5bd50a39a4ed2ee277652d394de0acab" args="() const" -->
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;class R&gt; </div>
      <table class="memname">
        <tr>
          <td class="memname"><a class="el" href="classam_1_1mate.html">am::mate</a>&lt; R &gt;::operator R           </td>
          <td>(</td>
          <td class="paramname">          </td>
          <td>&nbsp;)&nbsp;</td>
          <td width="100%"> const<code> [inline]</code></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Implicit conversion. Cast an illusion to make it possible to use a mate instance as if it is a raw variable stored, which is the return of the host function.<p>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>Copy of the stored raw variable, which is the return of the host function. </dd></dl>

</div>
</div><p>
<hr>The documentation for this class was generated from the following file:<ul>
<li><a class="el" href="mate_8hpp.html">mate.hpp</a></ul>
<hr size="1"><address style="align: right;"><small>Generated on Sat Dec 16 22:27:43 2006 for am::mate by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.1 </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
Other
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions