<!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>smart_ptr.hpp Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head><body> <div align="left"><a href="http://www.mrpt.org/">Main MRPT website</a> > <b>C++ reference</b> </div> <div align="right"> <a href="index.html"><img border="0" src="mrpt_logo.png" alt="MRPT logo"></a> </div> <!-- Generated by Doxygen 1.7.5 --> <script type="text/javascript"> var searchBox = new SearchBox("searchBox", "search",false,'Search'); </script> <div id="navrow1" class="tabs"> <ul class="tablist"> <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="modules.html"><span>Modules</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li> <div id="MSearchBox" class="MSearchBoxInactive"> <div class="left"> <form id="FSearchBox" action="search.php" method="get"> <img id="MSearchSelect" src="search/mag.png" alt=""/> <input type="text" id="MSearchField" name="query" value="Search" size="20" accesskey="S" onfocus="searchBox.OnSearchFieldFocus(true)" onblur="searchBox.OnSearchFieldFocus(false)"/> </form> </div><div class="right"></div> </div> </li> </ul> </div> <div id="navrow2" class="tabs2"> <ul class="tablist"> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="header"> <div class="headertitle"> <div class="title">smart_ptr.hpp</div> </div> </div> <div class="contents"> <a href="smart__ptr_8hpp.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"> The STL+ C++ Library Collection</span> <a name="l00003"></a>00003 <span class="comment"></span> <a name="l00004"></a>00004 <span class="comment"> Website <http://stlplus.sourceforge.net/> Collection <index.html></span> <a name="l00005"></a>00005 <span class="comment"></span> <a name="l00006"></a>00006 <span class="comment"></span> <a name="l00007"></a>00007 <span class="comment"> License Agreement</span> <a name="l00008"></a>00008 <span class="comment"></span> <a name="l00009"></a>00009 <span class="comment"> <http://www.opensource.org/></span> <a name="l00010"></a>00010 <span class="comment"></span> <a name="l00011"></a>00011 <span class="comment"> * License for using the STLplus Library Collection <#license></span> <a name="l00012"></a>00012 <span class="comment"> * The Intent of this License <#intent></span> <a name="l00013"></a>00013 <span class="comment"> * How to Comply with this License <#compliance></span> <a name="l00014"></a>00014 <span class="comment"> * Historical Note <#history></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="comment"> License for using the STLplus Library Collection</span> <a name="l00018"></a>00018 <span class="comment"></span> <a name="l00019"></a>00019 <span class="comment"> *© 1999-2008 Andy Rushton. All rights reserved.*</span> <a name="l00020"></a>00020 <span class="comment"></span> <a name="l00021"></a>00021 <span class="comment"> Redistribution and use in source and binary forms, with or without</span> <a name="l00022"></a>00022 <span class="comment"> modification, are permitted provided that the following conditions are met:</span> <a name="l00023"></a>00023 <span class="comment"></span> <a name="l00024"></a>00024 <span class="comment"> * Redistributions of source code must retain the above Copyright</span> <a name="l00025"></a>00025 <span class="comment"> notice, this list of conditions and the following disclaimer.</span> <a name="l00026"></a>00026 <span class="comment"> * Redistributions in binary form must reproduce the above Copyright</span> <a name="l00027"></a>00027 <span class="comment"> notice, this list of conditions and the following disclaimer in</span> <a name="l00028"></a>00028 <span class="comment"> the documentation and/or other materials provided with the</span> <a name="l00029"></a>00029 <span class="comment"> distribution.</span> <a name="l00030"></a>00030 <span class="comment"> * Neither the name of the STLplus library nor the names of its</span> <a name="l00031"></a>00031 <span class="comment"> contributors may be used to endorse or promote products derived</span> <a name="l00032"></a>00032 <span class="comment"> from this software without specific prior written permission.</span> <a name="l00033"></a>00033 <span class="comment"></span> <a name="l00034"></a>00034 <span class="comment"> This software is provided by the Copyright holders and contributors "as</span> <a name="l00035"></a>00035 <span class="comment"> is" and any express or implied warranties, including, but not limited</span> <a name="l00036"></a>00036 <span class="comment"> to, the implied warranties of merchantability and fitness for a</span> <a name="l00037"></a>00037 <span class="comment"> particular purpose are disclaimed. In no event shall the Copyright owner</span> <a name="l00038"></a>00038 <span class="comment"> or contributors be liable for any direct, indirect, incidental, special,</span> <a name="l00039"></a>00039 <span class="comment"> exemplary, or consequential damages (including, but not limited to,</span> <a name="l00040"></a>00040 <span class="comment"> procurement of substitute goods or services; loss of use, data, or</span> <a name="l00041"></a>00041 <span class="comment"> profits; or business interruption) however caused and on any theory of</span> <a name="l00042"></a>00042 <span class="comment"> liability, whether in contract, strict liability, or tort (including</span> <a name="l00043"></a>00043 <span class="comment"> negligence or otherwise) arising in any way out of the use of this</span> <a name="l00044"></a>00044 <span class="comment"> software, even if advised of the possibility of such damage.</span> <a name="l00045"></a>00045 <span class="comment">*/</span> <a name="l00046"></a>00046 <a name="l00047"></a>00047 <span class="comment">/*</span> <a name="l00048"></a>00048 <span class="comment"> Modified version of STL+ sources shipped with the Mobile Robot</span> <a name="l00049"></a>00049 <span class="comment"> Programming Toolkit (MRPT).</span> <a name="l00050"></a>00050 <span class="comment"></span> <a name="l00051"></a>00051 <span class="comment"> Sources have been modified to support thred-safe smart pointers</span> <a name="l00052"></a>00052 <span class="comment"> through atomic operations.</span> <a name="l00053"></a>00053 <span class="comment"></span> <a name="l00054"></a>00054 <span class="comment"> 2009, Jose Luis Blanco. University of Malaga.</span> <a name="l00055"></a>00055 <span class="comment">*/</span> <a name="l00056"></a>00056 <a name="l00057"></a>00057 <a name="l00058"></a>00058 <span class="preprocessor">#ifndef STLPLUS_SMART_PTR</span> <a name="l00059"></a>00059 <span class="preprocessor"></span><span class="preprocessor">#define STLPLUS_SMART_PTR</span> <a name="l00060"></a>00060 <span class="preprocessor"></span><span class="comment">////////////////////////////////////////////////////////////////////////////////</span> <a name="l00061"></a>00061 <span class="comment"></span> <a name="l00062"></a>00062 <span class="comment">// Author: Andy Rushton</span> <a name="l00063"></a>00063 <span class="comment">// Copyright: (c) Andy Rushton, 2007</span> <a name="l00064"></a>00064 <span class="comment">// License: BSD License, see ../docs/license.html</span> <a name="l00065"></a>00065 <a name="l00066"></a>00066 <span class="comment">// A smart pointer is a memory-managing pointer to an object. If you like, it</span> <a name="l00067"></a>00067 <span class="comment">// is a zero-dimensional container.</span> <a name="l00068"></a>00068 <a name="l00069"></a>00069 <span class="comment">// Assignment of smart pointers result in multiple aliases of the same object.</span> <a name="l00070"></a>00070 <span class="comment">// The term alias is used to differentiate from conventional pointers because</span> <a name="l00071"></a>00071 <span class="comment">// the semantics are different.</span> <a name="l00072"></a>00072 <a name="l00073"></a>00073 <span class="comment">// Aliases can be turned into copies if the pointed-to class supports copying.</span> <a name="l00074"></a>00074 <a name="l00075"></a>00075 <span class="comment">// The base class is smart_ptr_base which defines the common interface. Then</span> <a name="l00076"></a>00076 <span class="comment">// there are three subclasses which have the same interface but different copy</span> <a name="l00077"></a>00077 <span class="comment">// semantics:</span> <a name="l00078"></a>00078 <a name="l00079"></a>00079 <span class="comment">// - smart_ptr for simple types and classes which have copy constructors</span> <a name="l00080"></a>00080 <span class="comment">// - smart_ptr_clone for polymorphic class hierarchies which are copied using a clone method</span> <a name="l00081"></a>00081 <span class="comment">// - smart_ptr_nocopy for any class that cannot or should not be copied</span> <a name="l00082"></a>00082 <span class="comment"></span> <a name="l00083"></a>00083 <span class="comment">////////////////////////////////////////////////////////////////////////////////</span> <a name="l00084"></a>00084 <span class="comment"></span><span class="preprocessor">#include "<a class="code" href="containers__fixes_8hpp.html">containers_fixes.hpp</a>"</span> <a name="l00085"></a>00085 <span class="preprocessor">#include "<a class="code" href="exceptions_8hpp.html">exceptions.hpp</a>"</span> <a name="l00086"></a>00086 <span class="preprocessor">#include <map></span> <a name="l00087"></a>00087 <span class="preprocessor">#include <string></span> <a name="l00088"></a>00088 <a name="l00089"></a>00089 <span class="preprocessor">#include <<a class="code" href="atomic__incr_8h.html">mrpt/synch/atomic_incr.h</a>></span> <span class="comment">// JLB: For atomic_count. See smart_ptr.tpp</span> <a name="l00090"></a>00090 <a name="l00091"></a>00091 <span class="keyword">namespace </span>stlplus <a name="l00092"></a>00092 { <a name="l00093"></a>00093 <span class="comment"></span> <a name="l00094"></a>00094 <span class="comment"> ////////////////////////////////////////////////////////////////////////////////</span> <a name="l00095"></a>00095 <span class="comment"></span> <span class="comment">// internals</span> <a name="l00096"></a>00096 <a name="l00097"></a>00097 <span class="keyword">template</span><<span class="keyword">typename</span> T,<span class="keyword">typename</span> COUNTER> <span class="keyword">class </span>smart_ptr_holder; <a name="l00098"></a>00098 <span class="comment"></span> <a name="l00099"></a>00099 <span class="comment"> ////////////////////////////////////////////////////////////////////////////////</span> <a name="l00100"></a>00100 <span class="comment"></span> <span class="comment">// Base class</span><span class="comment"></span> <a name="l00101"></a>00101 <span class="comment"> ////////////////////////////////////////////////////////////////////////////////</span> <a name="l00102"></a>00102 <span class="comment"></span> <a name="l00103"></a>00103 <span class="keyword">template</span><<span class="keyword">typename</span> T, <span class="keyword">typename</span> C, <span class="keyword">typename</span> COUNTER> <span class="comment">// Typically: COUNTER = mrpt::synch::CAtomicCounter></span> <a name="l00104"></a>00104 <span class="keyword">class </span>smart_ptr_base <a name="l00105"></a>00105 { <a name="l00106"></a>00106 <span class="keyword">public</span>:<span class="comment"></span> <a name="l00107"></a>00107 <span class="comment"> //////////////////////////////////////////////////////////////////////////////</span> <a name="l00108"></a>00108 <span class="comment"></span> <span class="comment">// member type definitions</span> <a name="l00109"></a>00109 <a name="l00110"></a><a class="code" href="classstlplus_1_1smart__ptr__base.html#af9b8b42400c1a8daae7901e86b4f22cc">00110</a> <span class="keyword">typedef</span> T <a class="code" href="classstlplus_1_1smart__ptr__base.html#af9b8b42400c1a8daae7901e86b4f22cc">value_type</a>; <a name="l00111"></a><a class="code" href="classstlplus_1_1smart__ptr__base.html#a12bcf8041b948a587766b722051c1435">00111</a> <span class="keyword">typedef</span> T& <a class="code" href="classstlplus_1_1smart__ptr__base.html#a12bcf8041b948a587766b722051c1435">reference</a>; <a name="l00112"></a><a class="code" href="classstlplus_1_1smart__ptr__base.html#a9199c4c2e3e67d2c55dd34d7e434e2d8">00112</a> <span class="keyword">typedef</span> <span class="keyword">const</span> T& <a class="code" href="classstlplus_1_1smart__ptr__base.html#a9199c4c2e3e67d2c55dd34d7e434e2d8">const_reference</a>; <a name="l00113"></a><a class="code" href="classstlplus_1_1smart__ptr__base.html#a1c0db10bfc0a821c7a662ca06f8e5046">00113</a> <span class="keyword">typedef</span> C <a class="code" href="classstlplus_1_1smart__ptr__base.html#a1c0db10bfc0a821c7a662ca06f8e5046">value_copy</a>; <a name="l00114"></a>00114 <span class="comment"></span> <a name="l00115"></a>00115 <span class="comment"> //////////////////////////////////////////////////////////////////////////////</span> <a name="l00116"></a>00116 <span class="comment"></span> <span class="comment">// constructors and destructors</span> <a name="l00117"></a>00117 <a name="l00118"></a>00118 <span class="comment">// create a null pointer</span> <a name="l00119"></a>00119 <a class="code" href="classstlplus_1_1smart__ptr__base.html#abe74873bbbae84d779c6443caaef8653">smart_ptr_base</a>(<span class="keywordtype">void</span>); <a name="l00120"></a>00120 <a name="l00121"></a>00121 <span class="comment">// create a pointer containing a *copy* of the object using the template parameter C</span> <a name="l00122"></a>00122 <span class="comment">// this copy is taken because the pointer class maintains a dynamically allocated object</span> <a name="l00123"></a>00123 <span class="comment">// and the T& may not be (usually is not) dynamically allocated</span> <a name="l00124"></a>00124 <span class="keyword">explicit</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#abe74873bbbae84d779c6443caaef8653">smart_ptr_base</a>(<span class="keyword">const</span> T& data) <span class="keywordflow">throw</span>(<a class="code" href="classstlplus_1_1illegal__copy.html">illegal_copy</a>); <a name="l00125"></a>00125 <a name="l00126"></a>00126 <span class="comment">// create a pointer containing a dynamically created object</span> <a name="l00127"></a>00127 <span class="comment">// Note: the object must be allocated *by the user* with new</span> <a name="l00128"></a>00128 <span class="comment">// constructor form - must be called in the form smart_ptr_base<type> x(new type(args))</span> <a name="l00129"></a>00129 <span class="keyword">explicit</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#abe74873bbbae84d779c6443caaef8653">smart_ptr_base</a>(T* data); <a name="l00130"></a>00130 <a name="l00131"></a>00131 <span class="comment">// copy constructor implements aliasing so no copy is made</span> <a name="l00132"></a>00132 <span class="keyword">explicit</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#abe74873bbbae84d779c6443caaef8653">smart_ptr_base</a>(<span class="keyword">const</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html">smart_ptr_base<T,C,COUNTER></a>& r); <a name="l00133"></a>00133 <a name="l00134"></a>00134 <span class="comment">// destructor decrements the reference count and delete only when the last reference is destroyed</span> <a name="l00135"></a>00135 <a class="code" href="classstlplus_1_1smart__ptr__base.html#aba62fe4d641ed8cafe03da560e98a798">~smart_ptr_base</a>(<span class="keywordtype">void</span>); <a name="l00136"></a>00136 <span class="comment"></span> <a name="l00137"></a>00137 <span class="comment"> //////////////////////////////////////////////////////////////////////////////</span> <a name="l00138"></a>00138 <span class="comment"></span> <span class="comment">// logical tests to see if there is anything contained in the pointer since it can be null</span> <a name="l00139"></a>00139 <a name="l00140"></a>00140 <span class="comment">// there are two forms:explicit and implicit</span> <a name="l00141"></a>00141 <span class="comment">// implicit: if(!r) or if(r)</span> <a name="l00142"></a>00142 <span class="comment">// explicit: if(r.null()) or if(r.present())</span> <a name="l00143"></a>00143 <a class="code" href="classstlplus_1_1smart__ptr__base.html#acc379016149ac23a6572b188474b18d8">operator bool</a>(<span class="keywordtype">void</span>) <span class="keyword">const</span>; <a name="l00144"></a>00144 <span class="keywordtype">bool</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#a081578448de33f75eae2e013480cd385">operator!</a>(<span class="keywordtype">void</span>) <span class="keyword">const</span>; <a name="l00145"></a>00145 <span class="keywordtype">bool</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#ae6770e65491357f39d14c2e0faa35849">present</a>(<span class="keywordtype">void</span>) <span class="keyword">const</span>; <a name="l00146"></a>00146 <span class="keywordtype">bool</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#a728ff4f7aed60dbf897409cfb6bdcd41">null</a>(<span class="keywordtype">void</span>) <span class="keyword">const</span>; <a name="l00147"></a>00147 <span class="comment"></span> <a name="l00148"></a>00148 <span class="comment"> //////////////////////////////////////////////////////////////////////////////</span> <a name="l00149"></a>00149 <span class="comment"></span> <span class="comment">// dereference operators and functions</span> <a name="l00150"></a>00150 <a name="l00151"></a>00151 <span class="comment">// dereference the smart pointer to get the object - use in the form *p1</span> <a name="l00152"></a>00152 T& <a class="code" href="classstlplus_1_1smart__ptr__base.html#a792815c41b6f7bf9de665a318c4a5630">operator*</a>(<span class="keywordtype">void</span>) throw(<a class="code" href="classstlplus_1_1null__dereference.html">null_dereference</a>); <a name="l00153"></a>00153 const T& operator*(<span class="keywordtype">void</span>) const throw(null_dereference); <a name="l00154"></a>00154 <a name="l00155"></a>00155 <span class="comment">// used as a prefix to a member access to the contained object e.g. p1->print() calls T::print()</span> <a name="l00156"></a>00156 T* operator->(<span class="keywordtype">void</span>) throw(null_dereference); <a name="l00157"></a>00157 const T* operator->(<span class="keywordtype">void</span>) const throw(null_dereference); <a name="l00158"></a>00158 <span class="comment"></span> <a name="l00159"></a>00159 <span class="comment"> //////////////////////////////////////////////////////////////////////////////</span> <a name="l00160"></a>00160 <span class="comment"></span> <span class="comment">// explicit function forms of the above assignment and dereference operators</span> <a name="l00161"></a>00161 <a name="l00162"></a>00162 <span class="comment">// set the value - note that this does a copy using the C template parameter</span> <a name="l00163"></a>00163 <span class="keywordtype">void</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#ab48b95d77ce3071b6d3257cbc42af931">set_value</a>(const T& data) throw(<a class="code" href="classstlplus_1_1illegal__copy.html">illegal_copy</a>); <a name="l00164"></a>00164 <span class="comment">// get the value</span> <a name="l00165"></a>00165 T& <a class="code" href="classstlplus_1_1smart__ptr__base.html#a55de3c6999f4a5ad89b7c7aa59901188">value</a>(<span class="keywordtype">void</span>) throw(null_dereference); <a name="l00166"></a>00166 const T& <a class="code" href="classstlplus_1_1smart__ptr__base.html#a55de3c6999f4a5ad89b7c7aa59901188">value</a>(<span class="keywordtype">void</span>) const throw(null_dereference); <a name="l00167"></a>00167 <a name="l00168"></a>00168 <span class="comment">// set the pointer</span> <a name="l00169"></a>00169 <span class="comment">// deletes the previous pointer and adopts the passed pointer instead</span> <a name="l00170"></a>00170 <span class="comment">// Note: the object must be allocated *by the user* with new</span> <a name="l00171"></a>00171 <span class="comment">// Warning: it is very easy to break the memory management with this operation</span> <a name="l00172"></a>00172 <span class="keywordtype">void</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#a9f0f0048ac859448a2d9c484d9f4e940">set</a>(T* data = 0); <a name="l00173"></a>00173 <span class="comment">// get the pointer</span> <a name="l00174"></a>00174 T* <a class="code" href="classstlplus_1_1smart__ptr__base.html#a5867709e8f97a35d807a7c85d28692bd">pointer</a>(<span class="keywordtype">void</span>); <a name="l00175"></a>00175 const T* <a class="code" href="classstlplus_1_1smart__ptr__base.html#a5867709e8f97a35d807a7c85d28692bd">pointer</a>(<span class="keywordtype">void</span>) const; <a name="l00176"></a>00176 <span class="comment"></span> <a name="l00177"></a>00177 <span class="comment"> //////////////////////////////////////////////////////////////////////////////</span> <a name="l00178"></a>00178 <span class="comment"></span> <span class="comment">// functions to manage aliases</span> <a name="l00179"></a>00179 <a name="l00180"></a>00180 <span class="comment">// make this an alias of the passed object</span> <a name="l00181"></a>00181 inline <span class="keywordtype">void</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#a222d2ef7fe357157d64ba7aaf5558d73">alias</a>(const <a class="code" href="classstlplus_1_1smart__ptr__base.html">smart_ptr_base</a><T,C,COUNTER>&); <a name="l00182"></a>00182 <a name="l00183"></a>00183 <span class="comment">// test whether two pointers point to the same object(known as aliasing the object)</span> <a name="l00184"></a>00184 <span class="comment">// used in the form if(a.aliases(b))</span> <a name="l00185"></a>00185 inline <span class="keywordtype">bool</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#adb6dcfcefd7dd11f8fee69fea69d25a4">aliases</a>(const <a class="code" href="classstlplus_1_1smart__ptr__base.html">smart_ptr_base</a><T,C,COUNTER>&) const; <a name="l00186"></a>00186 <a name="l00187"></a>00187 <span class="comment">// find the number of aliases - used when you need to know whether an</span> <a name="l00188"></a>00188 <span class="comment">// object is still referred to from elsewhere (rare!)</span> <a name="l00189"></a>00189 inline <span class="keywordtype">unsigned</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#a428d4b9edd159f5570dac4c2826d0b9c">alias_count</a>(<span class="keywordtype">void</span>) const; <a name="l00190"></a>00190 <a name="l00191"></a>00191 <span class="comment">// delete the object and make the pointer null - does not make it unique</span> <a name="l00192"></a>00192 <span class="comment">// first, so all other pointers to this will be null too</span> <a name="l00193"></a>00193 inline <span class="keywordtype">void</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#a7a08a242d71f7a104e2a54d13f448a44">clear</a>(<span class="keywordtype">void</span>); <a name="l00194"></a>00194 <a name="l00195"></a>00195 <span class="comment">// make the pointer unique and null in one step - does not affect other</span> <a name="l00196"></a>00196 <span class="comment">// pointers that were pointing to the same object</span> <a name="l00197"></a>00197 inline <span class="keywordtype">void</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#a586945ba6008c8e3dbe24deb8bcb1344">clear_unique</a>(<span class="keywordtype">void</span>); <a name="l00198"></a>00198 <span class="comment"></span> <a name="l00199"></a>00199 <span class="comment"> //////////////////////////////////////////////////////////////////////////////</span> <a name="l00200"></a>00200 <span class="comment"></span> <span class="comment">// functions that involve copying</span> <a name="l00201"></a>00201 <a name="l00202"></a>00202 <span class="comment">// these functions use the copy functor passed as the template parameter C</span> <a name="l00203"></a>00203 <span class="comment">// to copy the object with the right copy semantics. If the copy functor</span> <a name="l00204"></a>00204 <span class="comment">// is no_copy, an exception will be thrown.</span> <a name="l00205"></a>00205 <a name="l00206"></a>00206 <span class="comment">// make this pointer unique with respect to any other references to the same object</span> <a name="l00207"></a>00207 <span class="comment">// if this pointer is already unique, it does nothing - otherwise it copies the object</span> <a name="l00208"></a>00208 inline <span class="keywordtype">void</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#a409515b3d55da1c3e468e6df5037128b">make_unique</a>(<span class="keywordtype">void</span>) throw(illegal_copy); <a name="l00209"></a>00209 <a name="l00210"></a>00210 <span class="comment">// make this pointer a unique copy of the parameter</span> <a name="l00211"></a>00211 <span class="comment">// useful for expressions like p1.copy(p2) which makes p1 a pointer to a unique copy of the contents of p2</span> <a name="l00212"></a>00212 <span class="keywordtype">void</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#a451642c0819ccaee311969af7c9bfc6b">copy</a>(const <a class="code" href="classstlplus_1_1smart__ptr__base.html">smart_ptr_base</a><T,C,COUNTER>&) throw(illegal_copy); <a name="l00213"></a>00213 <a name="l00214"></a>00214 protected: <a name="l00215"></a><a class="code" href="classstlplus_1_1smart__ptr__base.html#acd451bc76df1e6ef931cdb6ed625e90e">00215</a> <a class="code" href="classstlplus_1_1smart__ptr__holder.html">smart_ptr_holder</a><T,COUNTER>* <a class="code" href="classstlplus_1_1smart__ptr__base.html#acd451bc76df1e6ef931cdb6ed625e90e">m_holder</a>; <a name="l00216"></a>00216 <a name="l00217"></a>00217 public: <a name="l00218"></a>00218 <span class="comment">// internal use only - had to make them public because they need to be</span> <a name="l00219"></a>00219 <span class="comment">// accessed by routines that could not be made friends</span> <a name="l00220"></a>00220 <span class="keywordtype">void</span>* <a class="code" href="classstlplus_1_1smart__ptr__base.html#af943324feb2e32949c6a40042ae582c4">handle</a>(<span class="keywordtype">void</span>) const; <a name="l00221"></a>00221 <span class="keywordtype">void</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#a40f1c4e01f48973f243ce45cf0c6ebdc">make_alias</a>(<span class="keywordtype">void</span>* <a class="code" href="classstlplus_1_1smart__ptr__base.html#af943324feb2e32949c6a40042ae582c4">handle</a>); <a name="l00222"></a>00222 }; <a name="l00223"></a>00223 <span class="comment"></span> <a name="l00224"></a>00224 <span class="comment"> ////////////////////////////////////////////////////////////////////////////////</span> <a name="l00225"></a>00225 <span class="comment"></span> <span class="comment">// copy functors implementing the three possible copy semantics</span> <a name="l00226"></a>00226 <a name="l00227"></a>00227 <span class="comment">// constructor_copy uses the copy constructor of the object - used for simple types</span> <a name="l00228"></a>00228 <a name="l00229"></a>00229 template <typename T> <a name="l00230"></a>00230 class <a class="code" href="classstlplus_1_1constructor__copy.html">constructor_copy</a> <a name="l00231"></a>00231 { <a name="l00232"></a>00232 <span class="keyword">public</span>: <a name="l00233"></a><a class="code" href="classstlplus_1_1constructor__copy.html#afb02b0db633e4da478a67db4d761cd6e">00233</a> T* operator() (<span class="keyword">const</span> T& from) <span class="keywordflow">throw</span>() <a name="l00234"></a>00234 { <a name="l00235"></a>00235 <span class="keywordflow">return</span> <span class="keyword">new</span> T(from); <a name="l00236"></a>00236 } <a name="l00237"></a>00237 }; <a name="l00238"></a>00238 <a name="l00239"></a>00239 <span class="comment">// clone_copy uses the clone method of the object - used for polymorphic types</span> <a name="l00240"></a>00240 <a name="l00241"></a>00241 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00242"></a>00242 <span class="keyword">class </span>clone_copy <a name="l00243"></a>00243 { <a name="l00244"></a>00244 <span class="keyword">public</span>: <a name="l00245"></a><a class="code" href="classstlplus_1_1clone__copy.html#acf08599a4a4ea2a422f322d5d6131403">00245</a> T* operator() (<span class="keyword">const</span> T& from) <span class="keywordflow">throw</span>() <a name="l00246"></a>00246 { <a name="l00247"></a>00247 <span class="keywordflow">return</span> from.clone(); <a name="l00248"></a>00248 } <a name="l00249"></a>00249 }; <a name="l00250"></a>00250 <a name="l00251"></a>00251 <span class="comment">// no_copy throws an exception - used for types that cannot be copied</span> <a name="l00252"></a>00252 <a name="l00253"></a>00253 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00254"></a>00254 <span class="keyword">class </span>no_copy <a name="l00255"></a>00255 { <a name="l00256"></a>00256 <span class="keyword">public</span>: <a name="l00257"></a><a class="code" href="classstlplus_1_1no__copy.html#a43a81c0579e499d9791c2e4b6fb19c94">00257</a> T* operator() (<span class="keyword">const</span> T& from) <span class="keywordflow">throw</span>(illegal_copy) <a name="l00258"></a>00258 { <a name="l00259"></a>00259 <span class="keywordflow">throw</span> illegal_copy(<span class="stringliteral">"no_copy functor called"</span>); <a name="l00260"></a>00260 <span class="keywordflow">return</span> 0; <a name="l00261"></a>00261 } <a name="l00262"></a>00262 }; <a name="l00263"></a>00263 <span class="comment"></span> <a name="l00264"></a>00264 <span class="comment"> ////////////////////////////////////////////////////////////////////////////////</span> <a name="l00265"></a>00265 <span class="comment"></span> <span class="comment">// smart_ptr for simple types and classes which have copy constructors</span> <a name="l00266"></a>00266 <a name="l00267"></a>00267 <span class="keyword">template</span> <<span class="keyword">typename</span> T,<span class="keyword">typename</span> COUNTER = mrpt::synch::CAtomicCounter> <a name="l00268"></a>00268 <span class="keyword">class </span>smart_ptr : <span class="keyword">public</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#abe74873bbbae84d779c6443caaef8653">smart_ptr_base</a><T, constructor_copy<T>, COUNTER > <a name="l00269"></a>00269 { <a name="l00270"></a>00270 <span class="keyword">public</span>: <a name="l00271"></a><a class="code" href="classstlplus_1_1smart__ptr.html#a211996b5aaf19bcc096d2ab6b3940f2d">00271</a> <a class="code" href="classstlplus_1_1smart__ptr.html#a211996b5aaf19bcc096d2ab6b3940f2d">smart_ptr</a>(<span class="keywordtype">void</span>) {} <a name="l00272"></a><a class="code" href="classstlplus_1_1smart__ptr.html#a942bd56dff8e539f39f121678e4af9fa">00272</a> <span class="keyword">explicit</span> <a class="code" href="classstlplus_1_1smart__ptr.html#a942bd56dff8e539f39f121678e4af9fa">smart_ptr</a>(<span class="keyword">const</span> T& data) : <a class="code" href="classstlplus_1_1smart__ptr__base.html">smart_ptr_base</a><T, constructor_copy<T>,COUNTER >(data) {} <a name="l00273"></a><a class="code" href="classstlplus_1_1smart__ptr.html#ab6008354e9e8b0aac6864614d1270b1b">00273</a> <span class="keyword">explicit</span> <a class="code" href="classstlplus_1_1smart__ptr.html#ab6008354e9e8b0aac6864614d1270b1b">smart_ptr</a>(T* data) : <a class="code" href="classstlplus_1_1smart__ptr__base.html">smart_ptr_base</a><T, constructor_copy<T>,COUNTER >(data) {} <a name="l00274"></a><a class="code" href="classstlplus_1_1smart__ptr.html#a1ab6f485620451213a790923269021db">00274</a> <a class="code" href="classstlplus_1_1smart__ptr.html">smart_ptr<T></a>& <a class="code" href="classstlplus_1_1smart__ptr.html#a1ab6f485620451213a790923269021db">operator=</a>(<span class="keyword">const</span> T& data) {<a class="code" href="classstlplus_1_1smart__ptr__base.html#ab48b95d77ce3071b6d3257cbc42af931">set_value</a>(data); <span class="keywordflow">return</span> *<span class="keyword">this</span>;} <a name="l00275"></a><a class="code" href="classstlplus_1_1smart__ptr.html#a9c635213e6d6c742e0d39e48f2bb8c27">00275</a> <a class="code" href="classstlplus_1_1smart__ptr.html">smart_ptr<T></a>& <a class="code" href="classstlplus_1_1smart__ptr.html#a9c635213e6d6c742e0d39e48f2bb8c27">operator=</a>(<span class="keyword">const</span> <a class="code" href="classstlplus_1_1smart__ptr.html">smart_ptr<T></a>& r) {<a class="code" href="classstlplus_1_1smart__ptr__base.html#a222d2ef7fe357157d64ba7aaf5558d73">alias</a>(r); <span class="keywordflow">return</span> *<span class="keyword">this</span>;} <a name="l00276"></a><a class="code" href="classstlplus_1_1smart__ptr.html#a40f7b28d5f9253fdcace965bd561bae1">00276</a> <a class="code" href="classstlplus_1_1smart__ptr.html#a40f7b28d5f9253fdcace965bd561bae1">~smart_ptr</a>(<span class="keywordtype">void</span>) {} <a name="l00277"></a>00277 }; <a name="l00278"></a>00278 <span class="comment"></span> <a name="l00279"></a>00279 <span class="comment"> ////////////////////////////////////////////////////////////////////////////////</span> <a name="l00280"></a>00280 <span class="comment"></span> <span class="comment">// smart_ptr_clone for polymorphic class hierarchies which have a clone method</span> <a name="l00281"></a>00281 <a name="l00282"></a>00282 <span class="keyword">template</span> <<span class="keyword">typename</span> T,<span class="keyword">typename</span> COUNTER = mrpt::synch::CAtomicCounter> <a name="l00283"></a>00283 <span class="keyword">class </span>smart_ptr_clone : <span class="keyword">public</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#abe74873bbbae84d779c6443caaef8653">smart_ptr_base</a><T, clone_copy<T>, COUNTER > <a name="l00284"></a>00284 { <a name="l00285"></a>00285 <span class="keyword">public</span>: <a name="l00286"></a><a class="code" href="classstlplus_1_1smart__ptr__clone.html#aa21729f8b5b855f89b5200684e44b93b">00286</a> <a class="code" href="classstlplus_1_1smart__ptr__clone.html#aa21729f8b5b855f89b5200684e44b93b">smart_ptr_clone</a>(<span class="keywordtype">void</span>) {} <a name="l00287"></a><a class="code" href="classstlplus_1_1smart__ptr__clone.html#a7c63179ff18a1b37b62391771319dbee">00287</a> <span class="keyword">explicit</span> <a class="code" href="classstlplus_1_1smart__ptr__clone.html#a7c63179ff18a1b37b62391771319dbee">smart_ptr_clone</a>(<span class="keyword">const</span> T& data) : <a class="code" href="classstlplus_1_1smart__ptr__base.html">smart_ptr_base</a><T, <a class="code" href="classstlplus_1_1clone__copy.html">clone_copy</a><T>, COUNTER >(data) {} <a name="l00288"></a><a class="code" href="classstlplus_1_1smart__ptr__clone.html#a00475bcb3dd108294f34882730dd16ea">00288</a> <span class="keyword">explicit</span> <a class="code" href="classstlplus_1_1smart__ptr__clone.html#a00475bcb3dd108294f34882730dd16ea">smart_ptr_clone</a>(T* data) : <a class="code" href="classstlplus_1_1smart__ptr__base.html">smart_ptr_base</a><T, <a class="code" href="classstlplus_1_1clone__copy.html">clone_copy</a><T>, COUNTER >(data) {} <a name="l00289"></a><a class="code" href="classstlplus_1_1smart__ptr__clone.html#a23225136535db53085fb7e6cdea08085">00289</a> <a class="code" href="classstlplus_1_1smart__ptr__clone.html">smart_ptr_clone<T></a>& <a class="code" href="classstlplus_1_1smart__ptr__clone.html#a23225136535db53085fb7e6cdea08085">operator=</a>(<span class="keyword">const</span> T& data) {<a class="code" href="classstlplus_1_1smart__ptr__base.html#ab48b95d77ce3071b6d3257cbc42af931">set_value</a>(data); <span class="keywordflow">return</span> *<span class="keyword">this</span>;} <a name="l00290"></a><a class="code" href="classstlplus_1_1smart__ptr__clone.html#ae28cb723128cfdcd1397c8ddcab2fa3c">00290</a> <a class="code" href="classstlplus_1_1smart__ptr__clone.html">smart_ptr_clone<T></a>& <a class="code" href="classstlplus_1_1smart__ptr__clone.html#ae28cb723128cfdcd1397c8ddcab2fa3c">operator=</a>(<span class="keyword">const</span> <a class="code" href="classstlplus_1_1smart__ptr__clone.html">smart_ptr_clone<T></a>& r) {<a class="code" href="classstlplus_1_1smart__ptr__base.html#a222d2ef7fe357157d64ba7aaf5558d73">alias</a>(r); <span class="keywordflow">return</span> *<span class="keyword">this</span>;} <a name="l00291"></a><a class="code" href="classstlplus_1_1smart__ptr__clone.html#ab4d104fc2a71081730d382cec36e8f5b">00291</a> <a class="code" href="classstlplus_1_1smart__ptr__clone.html#ab4d104fc2a71081730d382cec36e8f5b">~smart_ptr_clone</a>(<span class="keywordtype">void</span>) {} <a name="l00292"></a>00292 }; <a name="l00293"></a>00293 <span class="comment"></span> <a name="l00294"></a>00294 <span class="comment"> ////////////////////////////////////////////////////////////////////////////////</span> <a name="l00295"></a>00295 <span class="comment"></span> <span class="comment">// smart_ptr_nocopy for any class that cannot or should not be copied</span> <a name="l00296"></a>00296 <a name="l00297"></a>00297 <span class="keyword">template</span> <<span class="keyword">typename</span> T,<span class="keyword">typename</span> COUNTER = mrpt::synch::CAtomicCounter> <a name="l00298"></a>00298 <span class="keyword">class </span>smart_ptr_nocopy : <span class="keyword">public</span> <a class="code" href="classstlplus_1_1smart__ptr__base.html#abe74873bbbae84d779c6443caaef8653">smart_ptr_base</a><T, no_copy<T>, COUNTER > <a name="l00299"></a>00299 { <a name="l00300"></a>00300 <span class="keyword">public</span>: <a name="l00301"></a><a class="code" href="classstlplus_1_1smart__ptr__nocopy.html#ab797bd416fd3871a9ac77c86200d0626">00301</a> <a class="code" href="classstlplus_1_1smart__ptr__nocopy.html#ab797bd416fd3871a9ac77c86200d0626">smart_ptr_nocopy</a>(<span class="keywordtype">void</span>) {} <a name="l00302"></a><a class="code" href="classstlplus_1_1smart__ptr__nocopy.html#ac0e6cb45bc0e24e117bc91a6b06aed10">00302</a> <span class="keyword">explicit</span> <a class="code" href="classstlplus_1_1smart__ptr__nocopy.html#ac0e6cb45bc0e24e117bc91a6b06aed10">smart_ptr_nocopy</a>(<span class="keyword">const</span> T& data) : <a class="code" href="classstlplus_1_1smart__ptr__base.html">smart_ptr_base</a><T, <a class="code" href="classstlplus_1_1no__copy.html">no_copy</a><T>, COUNTER >(data) {} <a name="l00303"></a><a class="code" href="classstlplus_1_1smart__ptr__nocopy.html#a8f79d6c7975d819c34a82ff62dcd4b5f">00303</a> <span class="keyword">explicit</span> <a class="code" href="classstlplus_1_1smart__ptr__nocopy.html#a8f79d6c7975d819c34a82ff62dcd4b5f">smart_ptr_nocopy</a>(T* data) : <a class="code" href="classstlplus_1_1smart__ptr__base.html">smart_ptr_base</a><T, <a class="code" href="classstlplus_1_1no__copy.html">no_copy</a><T>, COUNTER >(data) {} <a name="l00304"></a><a class="code" href="classstlplus_1_1smart__ptr__nocopy.html#ab28a27684cb21da36bc3ec915180c686">00304</a> <a class="code" href="classstlplus_1_1smart__ptr__nocopy.html">smart_ptr_nocopy<T></a>& <a class="code" href="classstlplus_1_1smart__ptr__nocopy.html#ab28a27684cb21da36bc3ec915180c686">operator=</a>(<span class="keyword">const</span> T& data) {<a class="code" href="classstlplus_1_1smart__ptr__base.html#ab48b95d77ce3071b6d3257cbc42af931">set_value</a>(data); <span class="keywordflow">return</span> *<span class="keyword">this</span>;} <a name="l00305"></a><a class="code" href="classstlplus_1_1smart__ptr__nocopy.html#a4ceb5e212739f31e64029548995f4221">00305</a> <a class="code" href="classstlplus_1_1smart__ptr__nocopy.html">smart_ptr_nocopy<T></a>& <a class="code" href="classstlplus_1_1smart__ptr__nocopy.html#a4ceb5e212739f31e64029548995f4221">operator=</a>(<span class="keyword">const</span> <a class="code" href="classstlplus_1_1smart__ptr__nocopy.html">smart_ptr_nocopy<T></a>& r) {<a class="code" href="classstlplus_1_1smart__ptr__base.html#a222d2ef7fe357157d64ba7aaf5558d73">alias</a>(r); <span class="keywordflow">return</span> *<span class="keyword">this</span>;} <a name="l00306"></a><a class="code" href="classstlplus_1_1smart__ptr__nocopy.html#a82eb6a576fcac22aeba0893aff65963a">00306</a> <a class="code" href="classstlplus_1_1smart__ptr__nocopy.html#a82eb6a576fcac22aeba0893aff65963a">~smart_ptr_nocopy</a>(<span class="keywordtype">void</span>) {} <a name="l00307"></a>00307 }; <a name="l00308"></a>00308 <span class="comment"></span> <a name="l00309"></a>00309 <span class="comment"> ////////////////////////////////////////////////////////////////////////////////</span> <a name="l00310"></a>00310 <span class="comment"></span> <a name="l00311"></a>00311 } <span class="comment">// end namespace stlplus</span> <a name="l00312"></a>00312 <a name="l00313"></a>00313 <span class="preprocessor">#include "<a class="code" href="smart__ptr_8tpp.html">smart_ptr.tpp</a>"</span> <a name="l00314"></a>00314 <span class="preprocessor">#endif</span> </pre></div></div> </div> <br><hr><br> <table border="0" width="100%"> <tr> <td> Page generated by <a href="http://www.doxygen.org" target="_blank">Doxygen 1.7.5</a> for MRPT 0.9.5 SVN: at Sun Sep 25 17:20:18 UTC 2011</td><td></td> <td width="100"> </td> <td width="150"> </td></tr> </table> </body></html>