<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>VTK: dox/Common/vtkSmartPointer.h Source File</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.6 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="classes.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <h1>dox/Common/vtkSmartPointer.h</h1><a href="a02127.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: Visualization Toolkit</span> <a name="l00004"></a>00004 <span class="comment"> Module: $RCSfile: vtkSmartPointer.h,v $</span> <a name="l00005"></a>00005 <span class="comment"></span> <a name="l00006"></a>00006 <span class="comment"> Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen</span> <a name="l00007"></a>00007 <span class="comment"> All rights reserved.</span> <a name="l00008"></a>00008 <span class="comment"> See Copyright.txt or http://www.kitware.com/Copyright.htm for details.</span> <a name="l00009"></a>00009 <span class="comment"></span> <a name="l00010"></a>00010 <span class="comment"> This software is distributed WITHOUT ANY WARRANTY; without even</span> <a name="l00011"></a>00011 <span class="comment"> the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR</span> <a name="l00012"></a>00012 <span class="comment"> PURPOSE. See the above copyright notice for more information.</span> <a name="l00013"></a>00013 <span class="comment"></span> <a name="l00014"></a>00014 <span class="comment">=========================================================================*/</span> <a name="l00025"></a>00025 <span class="preprocessor">#ifndef __vtkSmartPointer_h</span> <a name="l00026"></a>00026 <span class="preprocessor"></span><span class="preprocessor">#define __vtkSmartPointer_h</span> <a name="l00027"></a>00027 <span class="preprocessor"></span> <a name="l00028"></a>00028 <span class="preprocessor">#include "<a class="code" href="a02128.html">vtkSmartPointerBase.h</a>"</span> <a name="l00029"></a>00029 <span class="preprocessor">#include <vtksys/Configure.hxx></span> <span class="comment">// need check for member template support</span> <a name="l00030"></a>00030 <a name="l00031"></a>00031 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00032"></a><a class="code" href="a01562.html">00032</a> <span class="keyword">class </span><a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer</a>: <span class="keyword">public</span> <a class="code" href="a01563.html" title="Non-templated superclass for vtkSmartPointer.">vtkSmartPointerBase</a> <a name="l00033"></a>00033 { <a name="l00034"></a>00034 <span class="keyword">static</span> T* CheckType(T* t) { <span class="keywordflow">return</span> t; } <a name="l00035"></a>00035 <span class="keyword">public</span>: <a name="l00037"></a><a class="code" href="a01562.html#16a387bc7d47e3b1b5eb33272524b319">00037</a> <a class="code" href="a01562.html#16a387bc7d47e3b1b5eb33272524b319">vtkSmartPointer</a>() {} <a name="l00038"></a>00038 <a name="l00040"></a><a class="code" href="a01562.html#1d3f6c522727dfce8b137ef630adf3e0">00040</a> <a class="code" href="a01562.html#16a387bc7d47e3b1b5eb33272524b319">vtkSmartPointer</a>(T* r): <a class="code" href="a01563.html" title="Non-templated superclass for vtkSmartPointer.">vtkSmartPointerBase</a>(r) {} <a name="l00041"></a>00041 <a name="l00043"></a>00043 <a name="l00045"></a>00045 <span class="preprocessor">#ifdef vtksys_CXX_HAS_MEMBER_TEMPLATES</span> <a name="l00046"></a>00046 <span class="preprocessor"></span> <span class="keyword">template</span> <<span class="keyword">class</span> U> <a name="l00047"></a>00047 <a class="code" href="a01562.html#16a387bc7d47e3b1b5eb33272524b319">vtkSmartPointer</a>(<span class="keyword">const</span> <a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer<U></a>& r): <a name="l00048"></a>00048 <a class="code" href="a01563.html" title="Non-templated superclass for vtkSmartPointer.">vtkSmartPointerBase</a>(CheckType(r.<a class="code" href="a01562.html#864ded1a8d272f26b1f49c78f8348459">GetPointer</a>())) {} <a name="l00050"></a>00050 <span class="preprocessor">#else</span> <a name="l00051"></a><a class="code" href="a01562.html#c07baa13b1bb7d025253aa01541278ff">00051</a> <span class="preprocessor"></span> <a class="code" href="a01562.html#16a387bc7d47e3b1b5eb33272524b319">vtkSmartPointer</a>(<span class="keyword">const</span> <a class="code" href="a01563.html" title="Non-templated superclass for vtkSmartPointer.">vtkSmartPointerBase</a>& r): <a class="code" href="a01563.html" title="Non-templated superclass for vtkSmartPointer.">vtkSmartPointerBase</a>(r) {} <a name="l00052"></a>00052 <span class="preprocessor">#endif</span> <a name="l00053"></a>00053 <span class="preprocessor"></span> <a name="l00055"></a>00055 <a name="l00057"></a><a class="code" href="a01562.html#7a8b363f6f604735f5368d9c5577fda2">00057</a> <a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer</a>& <a class="code" href="a01562.html#7a8b363f6f604735f5368d9c5577fda2">operator=</a>(T* r) <a name="l00058"></a>00058 { <a name="l00059"></a>00059 this-><a class="code" href="a01563.html#985bd12c359582817630a86ce65dc710">vtkSmartPointerBase::operator=</a>(r); <a name="l00060"></a>00060 <span class="keywordflow">return</span> *<span class="keyword">this</span>; <a name="l00061"></a>00061 } <a name="l00063"></a>00063 <a name="l00065"></a>00065 <a name="l00067"></a>00067 <span class="preprocessor">#ifdef vtksys_CXX_HAS_MEMBER_TEMPLATES</span> <a name="l00068"></a>00068 <span class="preprocessor"></span> <span class="keyword">template</span> <<span class="keyword">class</span> U> <a name="l00069"></a>00069 <a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer</a>& <a class="code" href="a01562.html#7a8b363f6f604735f5368d9c5577fda2">operator=</a>(<span class="keyword">const</span> <a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer<U></a>& r) <a name="l00070"></a>00070 { <a name="l00071"></a>00071 this-><a class="code" href="a01563.html#985bd12c359582817630a86ce65dc710">vtkSmartPointerBase::operator=</a>(CheckType(r.<a class="code" href="a01562.html#864ded1a8d272f26b1f49c78f8348459">GetPointer</a>())); <a name="l00072"></a>00072 <span class="keywordflow">return</span> *<span class="keyword">this</span>; <a name="l00073"></a>00073 } <a name="l00075"></a>00075 <span class="preprocessor">#else</span> <a name="l00076"></a><a class="code" href="a01562.html#1bab291a880c4873c84f4b4c3381fe62">00076</a> <span class="preprocessor"></span> <a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer</a>& <a class="code" href="a01562.html#7a8b363f6f604735f5368d9c5577fda2">operator=</a>(<span class="keyword">const</span> <a class="code" href="a01563.html" title="Non-templated superclass for vtkSmartPointer.">vtkSmartPointerBase</a>& r) <a name="l00077"></a>00077 { <a name="l00078"></a>00078 this-><a class="code" href="a01563.html#985bd12c359582817630a86ce65dc710">vtkSmartPointerBase::operator=</a>(r); <a name="l00079"></a>00079 <span class="keywordflow">return</span> *<span class="keyword">this</span>; <a name="l00080"></a>00080 } <a name="l00081"></a>00081 <span class="preprocessor">#endif</span> <a name="l00082"></a>00082 <span class="preprocessor"></span> <a name="l00084"></a>00084 <a name="l00085"></a><a class="code" href="a01562.html#864ded1a8d272f26b1f49c78f8348459">00085</a> T* <a class="code" href="a01562.html#864ded1a8d272f26b1f49c78f8348459">GetPointer</a>()<span class="keyword"> const</span> <a name="l00086"></a>00086 <span class="keyword"> </span>{ <a name="l00087"></a>00087 <span class="keywordflow">return</span> <span class="keyword">static_cast<</span>T*<span class="keyword">></span>(this-><a class="code" href="a01563.html#c3dbd2e5f2b15677caa8a764c1fa8aa5">Object</a>); <a name="l00088"></a>00088 } <a name="l00090"></a>00090 <a name="l00092"></a>00092 <a name="l00093"></a><a class="code" href="a01562.html#d7d0447552c8af0d71d9e9a04b4071fb">00093</a> <a class="code" href="a01562.html#d7d0447552c8af0d71d9e9a04b4071fb">operator T* </a>()<span class="keyword"> const</span> <a name="l00094"></a>00094 <span class="keyword"> </span>{ <a name="l00095"></a>00095 <span class="keywordflow">return</span> <span class="keyword">static_cast<</span>T*<span class="keyword">></span>(this-><a class="code" href="a01563.html#c3dbd2e5f2b15677caa8a764c1fa8aa5">Object</a>); <a name="l00096"></a>00096 } <a name="l00098"></a>00098 <a name="l00100"></a>00100 <a name="l00102"></a><a class="code" href="a01562.html#eaa927cbc974a06387fc22b8ee42506d">00102</a> T& <a class="code" href="a01562.html#eaa927cbc974a06387fc22b8ee42506d">operator*</a>()<span class="keyword"> const</span> <a name="l00103"></a>00103 <span class="keyword"> </span>{ <a name="l00104"></a>00104 <span class="keywordflow">return</span> *<span class="keyword">static_cast<</span>T*<span class="keyword">></span>(this-><a class="code" href="a01563.html#c3dbd2e5f2b15677caa8a764c1fa8aa5">Object</a>); <a name="l00105"></a>00105 } <a name="l00107"></a>00107 <a name="l00109"></a>00109 <a name="l00110"></a><a class="code" href="a01562.html#b63c859a05247ebee3ba9f743df90013">00110</a> T* <a class="code" href="a01562.html#b63c859a05247ebee3ba9f743df90013">operator-></a>()<span class="keyword"> const</span> <a name="l00111"></a>00111 <span class="keyword"> </span>{ <a name="l00112"></a>00112 <span class="keywordflow">return</span> <span class="keyword">static_cast<</span>T*<span class="keyword">></span>(this-><a class="code" href="a01563.html#c3dbd2e5f2b15677caa8a764c1fa8aa5">Object</a>); <a name="l00113"></a>00113 } <a name="l00115"></a>00115 <a name="l00117"></a>00117 <a name="l00124"></a><a class="code" href="a01562.html#2d62985395d352803fe2c60173732eb7">00124</a> <span class="keywordtype">void</span> <a class="code" href="a01562.html#2d62985395d352803fe2c60173732eb7">TakeReference</a>(T* t) <a name="l00125"></a>00125 { <a name="l00126"></a>00126 *<span class="keyword">this</span> = <a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer<T></a>(t, <a class="code" href="a01564.html">NoReference</a>()); <a name="l00127"></a>00127 } <a name="l00129"></a>00129 <a name="l00131"></a>00131 <a name="l00132"></a><a class="code" href="a01562.html#4a83739df5e7ea185a855a55c085b79f">00132</a> <span class="keyword">static</span> <a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer<T></a> <a class="code" href="a01562.html#4a83739df5e7ea185a855a55c085b79f">New</a>() <a name="l00133"></a>00133 { <a name="l00134"></a>00134 <span class="keywordflow">return</span> <a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer<T></a>(T::New(), <a class="code" href="a01564.html">NoReference</a>()); <a name="l00135"></a>00135 } <a name="l00137"></a>00137 <a name="l00139"></a>00139 <a name="l00140"></a><a class="code" href="a01562.html#167a7c6d9ec9a70233628b6a1d30166f">00140</a> <span class="keyword">static</span> <a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer<T></a> <a class="code" href="a01562.html#167a7c6d9ec9a70233628b6a1d30166f">NewInstance</a>(T* t) <a name="l00141"></a>00141 { <a name="l00142"></a>00142 <span class="keywordflow">return</span> <a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer<T></a>(t->NewInstance(), <a class="code" href="a01564.html">NoReference</a>()); <a name="l00143"></a>00143 } <a name="l00145"></a>00145 <a name="l00147"></a>00147 <a name="l00155"></a><a class="code" href="a01562.html#d8fa0b8ab5e3b8ef486d4ef1d2ba9322">00155</a> <span class="keyword">static</span> <a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer<T></a> <a class="code" href="a01562.html#d8fa0b8ab5e3b8ef486d4ef1d2ba9322">Take</a>(T* t) <a name="l00156"></a>00156 { <a name="l00157"></a>00157 <span class="keywordflow">return</span> <a class="code" href="a01562.html" title="Hold a reference to a vtkObjectBase instance.">vtkSmartPointer<T></a>(t, <a class="code" href="a01564.html">NoReference</a>()); <a name="l00158"></a>00158 } <a name="l00160"></a>00160 <a name="l00161"></a>00161 <span class="comment">// Work-around for HP and IBM overload resolution bug. Since</span> <a name="l00162"></a>00162 <span class="comment">// NullPointerOnly is a private type the only pointer value that can</span> <a name="l00163"></a>00163 <span class="comment">// be passed by user code is a null pointer. This operator will be</span> <a name="l00164"></a>00164 <span class="comment">// chosen by the compiler when comparing against null explicitly and</span> <a name="l00165"></a>00165 <span class="comment">// avoid the bogus ambiguous overload error.</span> <a name="l00166"></a>00166 <span class="preprocessor">#if defined(__HP_aCC) || defined(__IBMCPP__)</span> <a name="l00167"></a>00167 <span class="preprocessor"></span><span class="preprocessor"># define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \</span> <a name="l00168"></a>00168 <span class="preprocessor"> vtkstd_bool operator op (NullPointerOnly*) const \</span> <a name="l00169"></a>00169 <span class="preprocessor"> { \</span> <a name="l00170"></a>00170 <span class="preprocessor"> return ::operator op (*this, 0); \</span> <a name="l00171"></a>00171 <span class="preprocessor"> }</span> <a name="l00172"></a>00172 <span class="preprocessor"></span><span class="keyword">private</span>: <a name="l00173"></a>00173 <span class="keyword">class </span>NullPointerOnly {}; <a name="l00174"></a>00174 <span class="keyword">public</span>: <a name="l00175"></a>00175 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==) <a name="l00176"></a>00176 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=) <a name="l00177"></a>00177 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<) <a name="l00178"></a>00178 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=) <a name="l00179"></a>00179 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>) <a name="l00180"></a>00180 VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=) <a name="l00181"></a>00181 <span class="preprocessor"># undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND</span> <a name="l00182"></a>00182 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00183"></a>00183 <span class="preprocessor"></span><span class="keyword">protected</span>: <a name="l00184"></a><a class="code" href="a01562.html#d65526881cde44760b1b3e4ae187fbf4">00184</a> <a class="code" href="a01562.html#16a387bc7d47e3b1b5eb33272524b319">vtkSmartPointer</a>(T* r, <span class="keyword">const</span> <a class="code" href="a01564.html">NoReference</a>& n): <a class="code" href="a01563.html" title="Non-templated superclass for vtkSmartPointer.">vtkSmartPointerBase</a>(r, n) {} <a name="l00185"></a>00185 <span class="keyword">private</span>: <a name="l00186"></a>00186 <span class="comment">// These are purposely not implemented to prevent callers from</span> <a name="l00187"></a>00187 <span class="comment">// trying to take references from other smart pointers.</span> <a name="l00188"></a>00188 <span class="keywordtype">void</span> <a class="code" href="a01562.html#2d62985395d352803fe2c60173732eb7">TakeReference</a>(<span class="keyword">const</span> <a class="code" href="a01563.html" title="Non-templated superclass for vtkSmartPointer.">vtkSmartPointerBase</a>&); <span class="comment">// Not implemented.</span> <a name="l00189"></a>00189 <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="a01562.html#d8fa0b8ab5e3b8ef486d4ef1d2ba9322">Take</a>(<span class="keyword">const</span> <a class="code" href="a01563.html" title="Non-templated superclass for vtkSmartPointer.">vtkSmartPointerBase</a>&); <span class="comment">// Not implemented.</span> <a name="l00190"></a>00190 }; <a name="l00191"></a>00191 <a name="l00192"></a><a class="code" href="a02127.html#a4c1a47a190f2f0b755dcf526b9b6025">00192</a> <span class="preprocessor">#define VTK_SMART_POINTER_DEFINE_OPERATOR(op) \</span> <a name="l00193"></a>00193 <span class="preprocessor"> template <class T> \</span> <a name="l00194"></a>00194 <span class="preprocessor"> inline vtkstd_bool \</span> <a name="l00195"></a>00195 <span class="preprocessor"> operator op (const vtkSmartPointer<T>& l, const vtkSmartPointer<T>& r) \</span> <a name="l00196"></a>00196 <span class="preprocessor"> { \</span> <a name="l00197"></a>00197 <span class="preprocessor"> return (l.GetPointer() op r.GetPointer()); \</span> <a name="l00198"></a>00198 <span class="preprocessor"> } \</span> <a name="l00199"></a>00199 <span class="preprocessor"> template <class T> \</span> <a name="l00200"></a>00200 <span class="preprocessor"> inline vtkstd_bool operator op (T* l, const vtkSmartPointer<T>& r) \</span> <a name="l00201"></a>00201 <span class="preprocessor"> { \</span> <a name="l00202"></a>00202 <span class="preprocessor"> return (l op r.GetPointer()); \</span> <a name="l00203"></a>00203 <span class="preprocessor"> } \</span> <a name="l00204"></a>00204 <span class="preprocessor"> template <class T> \</span> <a name="l00205"></a>00205 <span class="preprocessor"> inline vtkstd_bool operator op (const vtkSmartPointer<T>& l, T* r) \</span> <a name="l00206"></a>00206 <span class="preprocessor"> { \</span> <a name="l00207"></a>00207 <span class="preprocessor"> return (l.GetPointer() op r); \</span> <a name="l00208"></a>00208 <span class="preprocessor"> }</span> <a name="l00209"></a>00209 <span class="preprocessor"></span> <a name="l00210"></a>00210 <a class="code" href="a02127.html#a4c1a47a190f2f0b755dcf526b9b6025">VTK_SMART_POINTER_DEFINE_OPERATOR</a>(==) <a name="l00211"></a>00211 <a class="code" href="a02127.html#a4c1a47a190f2f0b755dcf526b9b6025">VTK_SMART_POINTER_DEFINE_OPERATOR</a>(!=) <a name="l00212"></a>00212 <a class="code" href="a02127.html#a4c1a47a190f2f0b755dcf526b9b6025">VTK_SMART_POINTER_DEFINE_OPERATOR</a>(<) <a name="l00213"></a>00213 <a class="code" href="a02127.html#a4c1a47a190f2f0b755dcf526b9b6025">VTK_SMART_POINTER_DEFINE_OPERATOR</a>(<=) <a name="l00214"></a>00214 <a class="code" href="a02127.html#a4c1a47a190f2f0b755dcf526b9b6025">VTK_SMART_POINTER_DEFINE_OPERATOR</a>(>) <a name="l00215"></a>00215 <a class="code" href="a02127.html#a4c1a47a190f2f0b755dcf526b9b6025">VTK_SMART_POINTER_DEFINE_OPERATOR</a>(>=) <a name="l00216"></a>00216 <a name="l00217"></a>00217 <span class="preprocessor">#undef VTK_SMART_POINTER_DEFINE_OPERATOR</span> <a name="l00218"></a>00218 <span class="preprocessor"></span> <a name="l00220"></a>00220 <a name="l00221"></a>00221 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00222"></a><a class="code" href="a02127.html#bf8f84843979741090cee412c9a4c33a">00222</a> <span class="keyword">inline</span> ostream& operator << (ostream& os, const vtkSmartPointer<T>& p) <a name="l00224"></a>00224 { <a name="l00225"></a>00225 <span class="keywordflow">return</span> os << static_cast<const vtkSmartPointerBase&>(p); <a name="l00226"></a>00226 } <a name="l00227"></a>00227 <a name="l00228"></a>00228 <span class="preprocessor">#endif</span> </pre></div></div> <hr size="1"><address style="text-align: right;"><small>Generated on Wed Jun 3 18:35:12 2009 for VTK by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address> </body> </html>