<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta name="robots" content="noindex"> <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>ITK: itkAutoPointer.h Source File</title> <link href="DoxygenStyle.css" rel="stylesheet" type="text/css"> </head><body bgcolor="#ffffff"> <!-- Section customized for INSIGHT : Tue Jul 17 01:02:45 2001 --> <center> <a href="index.html" class="qindex">Main Page</a> <a href="modules.html" class="qindex">Groups</a> <a href="namespaces.html" class="qindex">Namespace List</a> <a href="hierarchy.html" class="qindex">Class Hierarchy</a> <a href="classes.html" class="qindex">Alphabetical List</a> <a href="annotated.html" class="qindex">Compound List</a> <a href="files.html" class="qindex">File List</a> <a href="namespacemembers.html" class="qindex">Namespace Members</a> <a href="functions.html" class="qindex">Compound Members</a> <a href="globals.html" class="qindex">File Members</a> <a href="pages.html" class="qindex">Concepts</a></center> <!-- Generated by Doxygen 1.5.9 --> <div class="navpath"><a class="el" href="dir_02e23d841c198393a6e961e9a0678fff.html">Code</a> » <a class="el" href="dir_ea7fc529ede9a38b0438cf13831ae873.html">Common</a> </div> <div class="contents"> <h1>itkAutoPointer.h</h1><a href="itkAutoPointer_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*=========================================================================</span> <a name="l00002"></a>00002 <span class="comment"></span> <a name="l00003"></a>00003 <span class="comment"> Program: Insight Segmentation & Registration Toolkit</span> <a name="l00004"></a>00004 <span class="comment"> Module: $RCSfile: itkAutoPointer.h,v $</span> <a name="l00005"></a>00005 <span class="comment"> Language: C++</span> <a name="l00006"></a>00006 <span class="comment"> Date: $Date: 2006-03-18 18:06:14 $</span> <a name="l00007"></a>00007 <span class="comment"> Version: $Revision: 1.11 $</span> <a name="l00008"></a>00008 <span class="comment"></span> <a name="l00009"></a>00009 <span class="comment"> Copyright (c) Insight Software Consortium. All rights reserved.</span> <a name="l00010"></a>00010 <span class="comment"> See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.</span> <a name="l00011"></a>00011 <span class="comment"></span> <a name="l00012"></a>00012 <span class="comment"> This software is distributed WITHOUT ANY WARRANTY; without even </span> <a name="l00013"></a>00013 <span class="comment"> the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR </span> <a name="l00014"></a>00014 <span class="comment"> PURPOSE. See the above copyright notices for more information.</span> <a name="l00015"></a>00015 <span class="comment"></span> <a name="l00016"></a>00016 <span class="comment">=========================================================================*/</span> <a name="l00017"></a>00017 <span class="preprocessor">#ifndef __itkAutoPointer_h</span> <a name="l00018"></a>00018 <span class="preprocessor"></span><span class="preprocessor">#define __itkAutoPointer_h</span> <a name="l00019"></a>00019 <span class="preprocessor"></span> <a name="l00020"></a>00020 <span class="preprocessor">#include "<a class="code" href="itkMacro_8h.html">itkMacro.h</a>"</span> <a name="l00021"></a>00021 <span class="preprocessor">#include <iostream></span> <a name="l00022"></a>00022 <a name="l00023"></a>00023 <span class="keyword">namespace </span>itk <a name="l00024"></a>00024 { <a name="l00025"></a>00025 <a name="l00044"></a>00044 <span class="keyword">template</span> <<span class="keyword">class</span> TObjectType> <a name="l00045"></a><a class="code" href="classitk_1_1AutoPointer.html">00045</a> <span class="keyword">class </span><a class="code" href="itkWin32Header_8h.html#7d4384322557205d75f0719c012dded6">ITK_EXPORT</a> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> <a name="l00046"></a>00046 { <a name="l00047"></a>00047 <span class="keyword">public</span>: <a name="l00049"></a><a class="code" href="classitk_1_1AutoPointer.html#d16b94ab30a7837f2715d458fe002c7e">00049</a> <span class="keyword">typedef</span> TObjectType ObjectType; <a name="l00050"></a><a class="code" href="classitk_1_1AutoPointer.html#4c96af52387f69fd244a8cc88185faa9">00050</a> <span class="keyword">typedef</span> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">Self</a>; <a name="l00051"></a>00051 <a name="l00053"></a><a class="code" href="classitk_1_1AutoPointer.html#7c68846c88b6c7ea1494d6c6d6f2613e">00053</a> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> (): m_Pointer(0),m_IsOwner(false) <a name="l00054"></a>00054 { } <a name="l00055"></a>00055 <a name="l00057"></a><a class="code" href="classitk_1_1AutoPointer.html#5be82f8667302425394397297d75cb53">00057</a> <span class="keyword">explicit</span> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> ( <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> & p ) <a name="l00058"></a>00058 { <a name="l00059"></a>00059 m_IsOwner = p.<a class="code" href="classitk_1_1AutoPointer.html#72342741883b4edf4db320a15864cec3">IsOwner</a>(); <span class="comment">// Ownership can only be taken from another owner</span> <a name="l00060"></a>00060 m_Pointer = p.<a class="code" href="classitk_1_1AutoPointer.html#3e7b3baf5a2d25b0fcc43df37ea8ef65">ReleaseOwnership</a>(); <span class="comment">// release ownership if appropriate</span> <a name="l00061"></a>00061 } <a name="l00063"></a>00063 <a name="l00064"></a>00064 <a name="l00066"></a><a class="code" href="classitk_1_1AutoPointer.html#9707dbc71bbedaf59733ca37426c7d22">00066</a> <span class="keyword">explicit</span> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> ( ObjectType * p, <span class="keywordtype">bool</span> takeOwnership ): <a name="l00067"></a>00067 m_Pointer(p), m_IsOwner(takeOwnership) <a name="l00068"></a>00068 { } <a name="l00069"></a>00069 <a name="l00071"></a><a class="code" href="classitk_1_1AutoPointer.html#e27cf0ffbe9ae95c48d57f7770747dab">00071</a> ~<a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> () <a name="l00072"></a>00072 { <a name="l00073"></a>00073 <span class="keywordflow">if</span>( m_IsOwner && m_Pointer ) <a name="l00074"></a>00074 { <a name="l00075"></a>00075 <span class="keyword">delete</span> m_Pointer; <a name="l00076"></a>00076 } <a name="l00077"></a>00077 m_Pointer = 0; <a name="l00078"></a>00078 m_IsOwner = <span class="keyword">false</span>; <a name="l00079"></a>00079 } <a name="l00081"></a>00081 <a name="l00083"></a><a class="code" href="classitk_1_1AutoPointer.html#676641411345887df41e53e5933a7755">00083</a> ObjectType *operator -> ()<span class="keyword"> const</span> <a name="l00084"></a>00084 <span class="keyword"> </span>{ <span class="keywordflow">return</span> m_Pointer; } <a name="l00085"></a>00085 <a name="l00088"></a><a class="code" href="classitk_1_1AutoPointer.html#05ddd48a7d31aba5271528ae9d3bcb6e">00088</a> <span class="keywordtype">void</span> Reset( <span class="keywordtype">void</span> ) <a name="l00089"></a>00089 { <a name="l00090"></a>00090 <span class="keywordflow">if</span>( m_IsOwner && m_Pointer ) <a name="l00091"></a>00091 { <a name="l00092"></a>00092 <span class="keyword">delete</span> m_Pointer; <a name="l00093"></a>00093 } <a name="l00094"></a>00094 m_Pointer = 0; <a name="l00095"></a>00095 m_IsOwner = <span class="keyword">false</span>; <a name="l00096"></a>00096 } <a name="l00098"></a>00098 <a name="l00099"></a>00099 <a name="l00101"></a><a class="code" href="classitk_1_1AutoPointer.html#3ee9a56b89657a080d02df2463e6fabe">00101</a> <span class="keywordtype">void</span> TakeOwnership(<span class="keywordtype">void</span>) <a name="l00102"></a>00102 { m_IsOwner = <span class="keyword">true</span>; } <a name="l00103"></a>00103 <a name="l00105"></a><a class="code" href="classitk_1_1AutoPointer.html#8f82e418cb71250dd9a0c585e08fe91f">00105</a> <span class="keywordtype">void</span> TakeOwnership(ObjectType * objectptr) <a name="l00106"></a>00106 { <a name="l00107"></a>00107 <span class="keywordflow">if</span>( m_IsOwner && m_Pointer ) <a name="l00108"></a>00108 { <a name="l00109"></a>00109 <span class="keyword">delete</span> m_Pointer; <span class="comment">// remove the current one</span> <a name="l00110"></a>00110 } <a name="l00111"></a>00111 m_Pointer = objectptr; <a name="l00112"></a>00112 m_IsOwner = <span class="keyword">true</span>; <a name="l00113"></a>00113 } <a name="l00115"></a>00115 <a name="l00117"></a><a class="code" href="classitk_1_1AutoPointer.html#fb06aed96953def4853779bbf0b81581">00117</a> <span class="keywordtype">void</span> TakeNoOwnership(ObjectType * objectptr) <a name="l00118"></a>00118 { <a name="l00119"></a>00119 <span class="keywordflow">if</span>( m_IsOwner && m_Pointer ) <a name="l00120"></a>00120 { <a name="l00121"></a>00121 <span class="keyword">delete</span> m_Pointer; <span class="comment">// remove the current one</span> <a name="l00122"></a>00122 } <a name="l00123"></a>00123 m_Pointer = objectptr; <a name="l00124"></a>00124 m_IsOwner = <span class="keyword">false</span>; <a name="l00125"></a>00125 } <a name="l00127"></a>00127 <a name="l00129"></a><a class="code" href="classitk_1_1AutoPointer.html#72342741883b4edf4db320a15864cec3">00129</a> <span class="keywordtype">bool</span> IsOwner(<span class="keywordtype">void</span>)<span class="keyword"> const</span> <a name="l00130"></a>00130 <span class="keyword"> </span>{ <span class="keywordflow">return</span> m_IsOwner; } <a name="l00131"></a>00131 <a name="l00144"></a><a class="code" href="classitk_1_1AutoPointer.html#3e7b3baf5a2d25b0fcc43df37ea8ef65">00144</a> ObjectType * ReleaseOwnership( <span class="keywordtype">void</span> ) <a name="l00145"></a>00145 { <a name="l00146"></a>00146 m_IsOwner = <span class="keyword">false</span>; <a name="l00147"></a>00147 <span class="keywordflow">return</span> m_Pointer; <a name="l00148"></a>00148 } <a name="l00150"></a>00150 <a name="l00152"></a><a class="code" href="classitk_1_1AutoPointer.html#dd3dbae3a95dc70963ffaf193452048f">00152</a> ObjectType *GetPointer ()<span class="keyword"> const </span> <a name="l00153"></a>00153 <span class="keyword"> </span>{ <span class="keywordflow">return</span> m_Pointer; } <a name="l00154"></a>00154 <a name="l00156"></a><a class="code" href="classitk_1_1AutoPointer.html#00399f0a57488d4750f4d77c1b98f590">00156</a> <span class="keywordtype">bool</span> <a class="code" href="namespaceitk.html#e1c1b02c301187419478f5126c789a01">operator == </a>(<span class="keyword">const</span> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> &r)<span class="keyword"> const</span> <a name="l00157"></a>00157 <span class="keyword"> </span>{ <span class="keywordflow">return</span> (<span class="keywordtype">void</span>*)m_Pointer == (<span class="keywordtype">void</span>*) r.m_Pointer; } <a name="l00158"></a>00158 <a name="l00160"></a><a class="code" href="classitk_1_1AutoPointer.html#4e42e5e383e6783fb57b497140bd8a33">00160</a> <span class="keywordtype">bool</span> <a class="code" href="namespaceitk_1_1ImageToImageFilterDetail.html#7bf30438b5d3bdc0eddbecd9b17baa3b">operator != </a>(<span class="keyword">const</span> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> &r)<span class="keyword"> const</span> <a name="l00161"></a>00161 <span class="keyword"> </span>{ <span class="keywordflow">return</span> (<span class="keywordtype">void</span>*)m_Pointer != (<span class="keywordtype">void</span>*) r.m_Pointer; } <a name="l00162"></a>00162 <a name="l00164"></a><a class="code" href="classitk_1_1AutoPointer.html#820b5919cf3329dc73d7b59625da276c">00164</a> <span class="keywordtype">bool</span> operator < (<span class="keyword">const</span> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> &r)<span class="keyword"> const</span> <a name="l00165"></a>00165 <span class="keyword"> </span>{ <span class="keywordflow">return</span> (<span class="keywordtype">void</span>*)m_Pointer < (<span class="keywordtype">void</span>*) r.m_Pointer; } <a name="l00166"></a>00166 <a name="l00168"></a><a class="code" href="classitk_1_1AutoPointer.html#e4dfa33506cbbe1032352448173acf65">00168</a> <span class="keywordtype">bool</span> operator > (<span class="keyword">const</span> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> &r)<span class="keyword"> const</span> <a name="l00169"></a>00169 <span class="keyword"> </span>{ <span class="keywordflow">return</span> (<span class="keywordtype">void</span>*)m_Pointer > (<span class="keywordtype">void</span>*) r.m_Pointer; } <a name="l00170"></a>00170 <a name="l00172"></a><a class="code" href="classitk_1_1AutoPointer.html#67bca2ea949f579a1127a4edd258b086">00172</a> <span class="keywordtype">bool</span> operator <= (<span class="keyword">const</span> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> &r)<span class="keyword"> const</span> <a name="l00173"></a>00173 <span class="keyword"> </span>{ <span class="keywordflow">return</span> (<span class="keywordtype">void</span>*)m_Pointer <= (<span class="keywordtype">void</span>*) r.m_Pointer; } <a name="l00174"></a>00174 <a name="l00176"></a><a class="code" href="classitk_1_1AutoPointer.html#77c32e53f6d41ad4c9ae77347d3dafab">00176</a> <span class="keywordtype">bool</span> operator >= (<span class="keyword">const</span> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> &r)<span class="keyword"> const</span> <a name="l00177"></a>00177 <span class="keyword"> </span>{ <span class="keywordflow">return</span> (<span class="keywordtype">void</span>*)m_Pointer >= (<span class="keywordtype">void</span>*) r.m_Pointer; } <a name="l00178"></a>00178 <a name="l00180"></a><a class="code" href="classitk_1_1AutoPointer.html#8e6af1a0ce95fe8a41f9bd7913f63ee1">00180</a> <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> &operator = (<a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> &r)<span class="keyword"> const</span> <a name="l00181"></a>00181 <span class="keyword"> </span>{ <a name="l00182"></a>00182 <a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a>( r ).Swap( *<span class="keyword">this</span> ); <a name="l00183"></a>00183 <span class="keywordflow">return</span> *<span class="keyword">this</span>; <a name="l00184"></a>00184 } <a name="l00186"></a>00186 <a name="l00189"></a><a class="code" href="classitk_1_1AutoPointer.html#ceeb28fbaad970f733af6b6cd2903032">00189</a> operator bool ()<span class="keyword"> const</span> <a name="l00190"></a>00190 <span class="keyword"> </span>{ <span class="keywordflow">return</span> (m_Pointer!=<a class="code" href="itkVoronoiDiagram2D_8h.html#070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>); } <a name="l00191"></a>00191 <a name="l00193"></a><a class="code" href="classitk_1_1AutoPointer.html#0742cf2423b8bb7695ed369c42686ef7">00193</a> ObjectType *Print (std::ostream& os)<span class="keyword"> const </span> <a name="l00194"></a>00194 <span class="keyword"> </span>{ <a name="l00195"></a>00195 <span class="comment">// This prints the object pointed to by the pointer </span> <a name="l00196"></a>00196 (*m_Pointer).Print(os); <a name="l00197"></a>00197 os << <span class="stringliteral">"Owner: "</span> << m_IsOwner << std::endl; <a name="l00198"></a>00198 <span class="keywordflow">return</span> m_Pointer; <a name="l00199"></a>00199 } <a name="l00201"></a>00201 <a name="l00202"></a>00202 <span class="keyword">private</span>: <a name="l00203"></a>00203 <a name="l00205"></a>00205 <span class="keywordtype">void</span> Swap(<a class="code" href="classitk_1_1AutoPointer.html" title="Implements an Automatic Pointer to an object.">AutoPointer</a> &r) <span class="keywordflow">throw</span>() <a name="l00206"></a>00206 { <a name="l00207"></a>00207 ObjectType * temp = m_Pointer; <a name="l00208"></a>00208 m_Pointer = r.m_Pointer; <a name="l00209"></a>00209 r.m_Pointer = temp; <a name="l00210"></a>00210 } <a name="l00211"></a>00211 <a name="l00212"></a>00212 <a name="l00214"></a>00214 ObjectType* m_Pointer; <a name="l00215"></a>00215 <span class="keywordtype">bool</span> m_IsOwner; <a name="l00216"></a>00216 }; <a name="l00217"></a>00217 <a name="l00218"></a>00218 <a name="l00219"></a>00219 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00220"></a><a class="code" href="namespaceitk.html#7c71ed8eb261e3d7a9cbbce0e270c02e">00220</a> std::ostream& operator<< (std::ostream& os, AutoPointer<T> p) <a name="l00221"></a>00221 { <a name="l00222"></a>00222 p.Print(os); <a name="l00223"></a>00223 os << <span class="stringliteral">"Owner: "</span> << p.IsOwner() << std::endl; <a name="l00224"></a>00224 <span class="keywordflow">return</span> os; <a name="l00225"></a>00225 } <a name="l00226"></a>00226 <a name="l00227"></a>00227 <a name="l00230"></a>00230 <span class="keyword">template</span> <<span class="keyword">typename</span> TAutoPo<span class="keywordtype">int</span>erBase, <span class="keyword">typename</span> TAutoPo<span class="keywordtype">int</span>erDerived> <a name="l00231"></a>00231 <span class="keywordtype">void</span> <a name="l00232"></a><a class="code" href="namespaceitk.html#1b0784457b10ddaf93e30e37d3554f58">00232</a> <a class="code" href="itkWin32Header_8h.html#7d4384322557205d75f0719c012dded6">ITK_EXPORT</a> <a class="code" href="namespaceitk.html#1b0784457b10ddaf93e30e37d3554f58">TransferAutoPointer</a>(TAutoPointerBase & pa, TAutoPointerDerived & pb) <a name="l00233"></a>00233 { <a name="l00234"></a>00234 <span class="comment">// give a chance to natural polymorphism</span> <a name="l00235"></a>00235 pa.TakeNoOwnership( pb.GetPointer() ); <a name="l00236"></a>00236 <span class="keywordflow">if</span>( pb.IsOwner() ) <a name="l00237"></a>00237 { <a name="l00238"></a>00238 pa.TakeOwnership(); <span class="comment">// pa Take Ownership</span> <a name="l00239"></a>00239 pb.ReleaseOwnership(); <span class="comment">// pb Release Ownership and clears</span> <a name="l00240"></a>00240 } <a name="l00241"></a>00241 } <a name="l00243"></a>00243 <a name="l00244"></a>00244 <a name="l00245"></a>00245 } <span class="comment">// end namespace itk</span> <a name="l00246"></a>00246 <a name="l00247"></a>00247 <span class="preprocessor">#endif</span> <a name="l00248"></a>00248 <span class="preprocessor"></span> </pre></div></div> <hr><address><small> Generated at Thu May 7 22:07:55 2009 for ITK by <a href="http://www.stack.nl/~dimitri/doxygen/index.html"> <img src="http://www.stack.nl/~dimitri/doxygen/doxygen.png" alt="doxygen" align="middle" border=0 width=110 height=53> </a> 1.5.9 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>, © 1997-2000</small></address> </body> </html>