<!-- This comment will put IE 6, 7 and 8 in quirks mode --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>ICU 4.4.1: localpointer.h Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="search/search.css" rel="stylesheet" type="text/css"/> <script type="text/javaScript" src="search/search.js"></script> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body onload='searchBox.OnSelectItem(0);'> <!-- Generated by Doxygen 1.6.2-20100208 --> <script type="text/javascript"><!-- var searchBox = new SearchBox("searchBox", "search",false,'Search'); --></script> <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="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li> <div id="MSearchBox" class="MSearchBoxInactive"> <img id="MSearchSelect" src="search/search.png" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" alt=""/> <input type="text" id="MSearchField" value="Search" accesskey="S" onfocus="searchBox.OnSearchFieldFocus(true)" onblur="searchBox.OnSearchFieldFocus(false)" onkeyup="searchBox.OnSearchFieldChange(event)"/> <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a> </div> </li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>Globals</span></a></li> </ul> </div> <h1>localpointer.h</h1><a href="localpointer_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">*</span> <a name="l00004"></a>00004 <span class="comment">* Copyright (C) 2009, International Business Machines</span> <a name="l00005"></a>00005 <span class="comment">* Corporation and others. All Rights Reserved.</span> <a name="l00006"></a>00006 <span class="comment">*</span> <a name="l00007"></a>00007 <span class="comment">*******************************************************************************</span> <a name="l00008"></a>00008 <span class="comment">* file name: localpointer.h</span> <a name="l00009"></a>00009 <span class="comment">* encoding: US-ASCII</span> <a name="l00010"></a>00010 <span class="comment">* tab size: 8 (not used)</span> <a name="l00011"></a>00011 <span class="comment">* indentation:4</span> <a name="l00012"></a>00012 <span class="comment">*</span> <a name="l00013"></a>00013 <span class="comment">* created on: 2009nov13</span> <a name="l00014"></a>00014 <span class="comment">* created by: Markus W. Scherer</span> <a name="l00015"></a>00015 <span class="comment">*/</span> <a name="l00016"></a>00016 <a name="l00017"></a>00017 <span class="preprocessor">#ifndef __LOCALPOINTER_H__</span> <a name="l00018"></a>00018 <span class="preprocessor"></span><span class="preprocessor">#define __LOCALPOINTER_H__</span> <a name="l00019"></a>00019 <span class="preprocessor"></span> <a name="l00039"></a>00039 <span class="preprocessor">#include "<a class="code" href="utypes_8h.html" title="Basic definitions for ICU, for both C and C++ APIs.">unicode/utypes.h</a>"</span> <a name="l00040"></a>00040 <a name="l00041"></a>00041 <span class="preprocessor">#if U_SHOW_CPLUSPLUS_API</span> <a name="l00042"></a>00042 <span class="preprocessor"></span> <a name="l00043"></a>00043 <a class="code" href="uversion_8h.html#a7d9374e037299886d4192b763b4c5e8b" title="This is used to begin a declaration of a public ICU C++ API.">U_NAMESPACE_BEGIN</a> <a name="l00044"></a>00044 <a name="l00063"></a>00063 <span class="keyword">template</span><<span class="keyword">typename</span> T> <a name="l00064"></a>00064 <span class="keyword">class </span>LocalPointerBase { <a name="l00065"></a>00065 <span class="keyword">public</span>: <a name="l00071"></a>00071 <span class="keyword">explicit</span> LocalPointerBase(T *p=<a class="code" href="utypes_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4" title="Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.">NULL</a>) : ptr(p) {} <a name="l00077"></a>00077 ~LocalPointerBase() { <span class="comment">/* delete ptr; */</span> } <a name="l00083"></a>00083 <a class="code" href="umachine_8h.html#a349ef00011f20ccd1d3b424445681aa5" title="The ICU boolean type.">UBool</a> isNull()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> ptr==<a class="code" href="utypes_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4" title="Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.">NULL</a>; } <a name="l00089"></a>00089 <a class="code" href="umachine_8h.html#a349ef00011f20ccd1d3b424445681aa5" title="The ICU boolean type.">UBool</a> isValid()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> ptr!=<a class="code" href="utypes_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4" title="Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.">NULL</a>; } <a name="l00097"></a>00097 <span class="keywordtype">bool</span> <a class="code" href="stringpiece_8h.html#a7652049cbac0bfeae2cd2aa4514a3352" title="Global operator == for StringPiece.">operator==</a>(<span class="keyword">const</span> T *other)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> ptr==other; } <a name="l00105"></a>00105 <span class="keywordtype">bool</span> <a class="code" href="stringpiece_8h.html#abf0ccab9536bab551afc07de65eb5d95" title="Global operator != for StringPiece.">operator!=</a>(<span class="keyword">const</span> T *other)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> ptr!=other; } <a name="l00111"></a>00111 T *getAlias()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> ptr; } <a name="l00117"></a>00117 T &operator*()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> *ptr; } <a name="l00123"></a>00123 T *operator->()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> ptr; } <a name="l00130"></a>00130 T *orphan() { <a name="l00131"></a>00131 T *p=ptr; <a name="l00132"></a>00132 ptr=<a class="code" href="utypes_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4" title="Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.">NULL</a>; <a name="l00133"></a>00133 <span class="keywordflow">return</span> p; <a name="l00134"></a>00134 } <a name="l00142"></a>00142 <span class="keywordtype">void</span> adoptInstead(T *p) { <a name="l00143"></a>00143 <span class="comment">// delete ptr;</span> <a name="l00144"></a>00144 ptr=p; <a name="l00145"></a>00145 } <a name="l00146"></a>00146 <span class="keyword">protected</span>: <a name="l00147"></a>00147 T *ptr; <a name="l00148"></a>00148 <span class="keyword">private</span>: <a name="l00149"></a>00149 <span class="comment">// No comparison operators with other LocalPointerBases.</span> <a name="l00150"></a>00150 <span class="keywordtype">bool</span> <a class="code" href="stringpiece_8h.html#a7652049cbac0bfeae2cd2aa4514a3352" title="Global operator == for StringPiece.">operator==</a>(<span class="keyword">const</span> LocalPointerBase &other); <a name="l00151"></a>00151 <span class="keywordtype">bool</span> <a class="code" href="stringpiece_8h.html#abf0ccab9536bab551afc07de65eb5d95" title="Global operator != for StringPiece.">operator!=</a>(<span class="keyword">const</span> LocalPointerBase &other); <a name="l00152"></a>00152 <span class="comment">// No ownership transfer: No copy constructor, no assignment operator.</span> <a name="l00153"></a>00153 LocalPointerBase(<span class="keyword">const</span> LocalPointerBase &other); <a name="l00154"></a>00154 <span class="keywordtype">void</span> operator=(<span class="keyword">const</span> LocalPointerBase &other); <a name="l00155"></a>00155 <span class="comment">// No heap allocation. Use only on the stack.</span> <a name="l00156"></a>00156 <span class="keyword">static</span> <span class="keywordtype">void</span> * <a class="code" href="platform_8h.html#a72bf6d8ec0640a37b1915e5d853b2016">U_EXPORT2</a> operator new(<span class="keywordtype">size_t</span> size); <a name="l00157"></a>00157 <span class="keyword">static</span> <span class="keywordtype">void</span> * <a class="code" href="platform_8h.html#a72bf6d8ec0640a37b1915e5d853b2016">U_EXPORT2</a> operator new[](<span class="keywordtype">size_t</span> size); <a name="l00158"></a>00158 <span class="preprocessor">#if U_HAVE_PLACEMENT_NEW</span> <a name="l00159"></a>00159 <span class="preprocessor"></span> <span class="keyword">static</span> <span class="keywordtype">void</span> * <a class="code" href="platform_8h.html#a72bf6d8ec0640a37b1915e5d853b2016">U_EXPORT2</a> operator new(<span class="keywordtype">size_t</span>, <span class="keywordtype">void</span> *ptr); <a name="l00160"></a>00160 <span class="preprocessor">#endif</span> <a name="l00161"></a>00161 <span class="preprocessor"></span>}; <a name="l00162"></a>00162 <a name="l00181"></a>00181 <span class="keyword">template</span><<span class="keyword">typename</span> T> <a name="l00182"></a>00182 <span class="keyword">class </span>LocalPointer : <span class="keyword">public</span> LocalPointerBase<T> { <a name="l00183"></a>00183 <span class="keyword">public</span>: <a name="l00189"></a>00189 <span class="keyword">explicit</span> LocalPointer(T *p=<a class="code" href="utypes_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4" title="Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.">NULL</a>) : LocalPointerBase<T>(p) {} <a name="l00194"></a>00194 ~LocalPointer() { <a name="l00195"></a>00195 <span class="keyword">delete</span> LocalPointerBase<T>::ptr; <a name="l00196"></a>00196 } <a name="l00203"></a>00203 <span class="keywordtype">void</span> adoptInstead(T *p) { <a name="l00204"></a>00204 <span class="keyword">delete</span> LocalPointerBase<T>::ptr; <a name="l00205"></a>00205 LocalPointerBase<T>::ptr=p; <a name="l00206"></a>00206 } <a name="l00207"></a>00207 }; <a name="l00208"></a>00208 <a name="l00227"></a>00227 <span class="keyword">template</span><<span class="keyword">typename</span> T> <a name="l00228"></a>00228 <span class="keyword">class </span>LocalArray : <span class="keyword">public</span> LocalPointerBase<T> { <a name="l00229"></a>00229 <span class="keyword">public</span>: <a name="l00235"></a>00235 <span class="keyword">explicit</span> LocalArray(T *p=<a class="code" href="utypes_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4" title="Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.">NULL</a>) : LocalPointerBase<T>(p) {} <a name="l00240"></a>00240 ~LocalArray() { <a name="l00241"></a>00241 <span class="keyword">delete</span>[] LocalPointerBase<T>::ptr; <a name="l00242"></a>00242 } <a name="l00249"></a>00249 <span class="keywordtype">void</span> adoptInstead(T *p) { <a name="l00250"></a>00250 <span class="keyword">delete</span>[] LocalPointerBase<T>::ptr; <a name="l00251"></a>00251 LocalPointerBase<T>::ptr=p; <a name="l00252"></a>00252 } <a name="l00260"></a>00260 T &operator[](ptrdiff_t i)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> LocalPointerBase<T>::ptr[i]; } <a name="l00261"></a>00261 }; <a name="l00262"></a>00262 <a name="l00286"></a>00286 <span class="preprocessor">#define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \</span> <a name="l00287"></a>00287 <span class="preprocessor"> class LocalPointerClassName : public LocalPointerBase<Type> { \</span> <a name="l00288"></a>00288 <span class="preprocessor"> public: \</span> <a name="l00289"></a>00289 <span class="preprocessor"> explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \</span> <a name="l00290"></a>00290 <span class="preprocessor"> ~LocalPointerClassName() { closeFunction(ptr); } \</span> <a name="l00291"></a>00291 <span class="preprocessor"> void adoptInstead(Type *p) { \</span> <a name="l00292"></a>00292 <span class="preprocessor"> closeFunction(ptr); \</span> <a name="l00293"></a>00293 <span class="preprocessor"> ptr=p; \</span> <a name="l00294"></a>00294 <span class="preprocessor"> } \</span> <a name="l00295"></a>00295 <span class="preprocessor"> }</span> <a name="l00296"></a>00296 <span class="preprocessor"></span> <a name="l00297"></a>00297 <a class="code" href="uversion_8h.html#a3da1f00f3b02976ac53c014a3e4c0933" title="This is used to end a declaration of a public ICU C++ API.">U_NAMESPACE_END</a> <a name="l00298"></a>00298 <a name="l00299"></a>00299 <span class="preprocessor">#endif </span><span class="comment">/* U_SHOW_CPLUSPLUS_API */</span> <a name="l00300"></a>00300 <span class="preprocessor">#endif </span><span class="comment">/* __LOCALPOINTER_H__ */</span> </pre></div></div> <!--- window showing the filter options --> <div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)"> <a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </span>Defines</a></div> <!-- iframe showing the search results (closed by default) --> <div id="MSearchResultsWindow"> <iframe src="" frameborder="0" name="MSearchResults" id="MSearchResults"> </iframe> </div> <hr class="footer"/><address style="text-align: right;"><small>Generated by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2-20100208 </small></address> </body> </html>