PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jni/boost/libs/smart_ptr/weak_ptr.htm

https://github.com/LeifAndersen/Android-Supertux
HTML | 244 lines | 227 code | 17 blank | 0 comment | 0 complexity | 09e711e1e4b7e8ecd1f6f22e0936097c MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>weak_ptr</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. </head>
  7. <body text="#000000" bgColor="#ffffff">
  8. <h1><IMG height="86" alt="boost.png (6897 bytes)" src="../../boost.png" width="277" align="middle">weak_ptr
  9. class template</h1>
  10. <p><A href="#Introduction">Introduction</A><br>
  11. <A href="#Synopsis">Synopsis</A><br>
  12. <A href="#Members">Members</A><br>
  13. <A href="#functions">Free Functions</A><br>
  14. <A href="#FAQ">Frequently Asked Questions</A>
  15. </p>
  16. <h2><a name="Introduction">Introduction</a></h2>
  17. <p>The <b>weak_ptr</b> class template stores a "weak reference" to an object that's
  18. already managed by a <b>shared_ptr</b>. To access the object, a <STRONG>weak_ptr</STRONG>
  19. can be converted to a <STRONG>shared_ptr</STRONG> using <A href="shared_ptr.htm#constructors">
  20. the <STRONG>shared_ptr</STRONG> constructor</A> or the member function <STRONG><A href="#lock">
  21. lock</A></STRONG>. When the last <b>shared_ptr</b> to the object goes
  22. away and the object is deleted, the attempt to obtain a <STRONG>shared_ptr</STRONG>
  23. from the <b>weak_ptr</b> instances that refer to the deleted object will fail:
  24. the constructor will throw an exception of type <STRONG>boost::bad_weak_ptr</STRONG>,
  25. and <STRONG>weak_ptr::lock</STRONG> will return an <EM>empty</EM> <STRONG>shared_ptr</STRONG>.</p>
  26. <p>Every <b>weak_ptr</b> meets the <b>CopyConstructible</b> and <b>Assignable</b> requirements
  27. of the C++ Standard Library, and so can be used in standard library containers.
  28. Comparison operators are supplied so that <b>weak_ptr</b> works with the
  29. standard library's associative containers.</p>
  30. <P><STRONG>weak_ptr</STRONG> operations never throw&nbsp;exceptions.</P>
  31. <p>The class template is parameterized on <b>T</b>, the type of the object pointed
  32. to.</p>
  33. <P>Compared to <STRONG>shared_ptr</STRONG>, <STRONG>weak_ptr</STRONG> provides a
  34. very limited subset of operations since accessing its stored pointer is often
  35. dangerous in multithreaded programs, and sometimes unsafe even within a single
  36. thread (that is, it may invoke undefined behavior.) Pretend for a moment that <b>weak_ptr</b>
  37. has a <b>get</b> member function that returns a raw pointer, and consider this
  38. innocent piece of code:</P>
  39. <pre>shared_ptr&lt;int&gt; p(new int(5));
  40. weak_ptr&lt;int&gt; q(p);
  41. // some time later
  42. if(int * r = q.get())
  43. {
  44. // use *r
  45. }
  46. </pre>
  47. <P>Imagine that after the <STRONG>if</STRONG>, but immediately before <STRONG>r</STRONG>
  48. is used, another thread executes the statement <code>p.reset()</code>. Now <STRONG>r</STRONG>
  49. is a dangling pointer.</P>
  50. <P>The solution to this problem is to create a temporary <STRONG>shared_ptr</STRONG>
  51. from <STRONG>q</STRONG>:</P>
  52. <pre>shared_ptr&lt;int&gt; p(new int(5));
  53. weak_ptr&lt;int&gt; q(p);
  54. // some time later
  55. if(shared_ptr&lt;int&gt; r = q.<A href="#lock" >lock</A>())
  56. {
  57. // use *r
  58. }
  59. </pre>
  60. <p>Now <STRONG>r</STRONG> holds a reference to the object that was pointed by <STRONG>q</STRONG>.
  61. Even if <code>p.reset()</code> is executed in another thread, the object will
  62. stay alive until <STRONG>r</STRONG> goes out of scope or is reset. By obtaining
  63. a <STRONG>shared_ptr</STRONG> to the object, we have effectively locked it
  64. against destruction.</p>
  65. <h2><a name="Synopsis">Synopsis</a></h2>
  66. <pre>namespace boost {
  67. template&lt;class T&gt; class weak_ptr {
  68. public:
  69. typedef T <A href="#element_type" >element_type</A>;
  70. <A href="#default-constructor" >weak_ptr</A>();
  71. template&lt;class Y&gt; <A href="#constructors" >weak_ptr</A>(shared_ptr&lt;Y&gt; const &amp; r);
  72. <A href="#constructors" >weak_ptr</A>(weak_ptr const &amp; r);
  73. template&lt;class Y&gt; <A href="#constructors" >weak_ptr</A>(weak_ptr&lt;Y&gt; const &amp; r);
  74. <A href="#destructor" >~weak_ptr</A>();
  75. weak_ptr &amp; <A href="#assignment" >operator=</A>(weak_ptr const &amp; r);
  76. template&lt;class Y&gt; weak_ptr &amp; <A href="#assignment" >operator=</A>(weak_ptr&lt;Y&gt; const &amp; r);
  77. template&lt;class Y&gt; weak_ptr &amp; <A href="#assignment" >operator=</A>(shared_ptr&lt;Y&gt; const &amp; r);
  78. long <A href="#use_count" >use_count</A>() const;
  79. bool <A href="#expired" >expired</A>() const;
  80. shared_ptr&lt;T&gt; <A href="#lock" >lock</A>() const;
  81. void <A href="#reset" >reset</A>();
  82. void <A href="#swap" >swap</A>(weak_ptr&lt;T&gt; &amp; b);
  83. };
  84. template&lt;class T, class U&gt;
  85. bool <A href="#comparison" >operator&lt;</A>(weak_ptr&lt;T&gt; const &amp; a, weak_ptr&lt;U&gt; const &amp; b);
  86. template&lt;class T&gt;
  87. void <A href="#free-swap" >swap</A>(weak_ptr&lt;T&gt; &amp; a, weak_ptr&lt;T&gt; &amp; b);
  88. }
  89. </pre>
  90. <h2><a name="Members">Members</a></h2>
  91. <h3><a name="element_type">element_type</a></h3>
  92. <pre>typedef T element_type;</pre>
  93. <blockquote>
  94. <p>Provides the type of the template parameter T.</p>
  95. </blockquote>
  96. <h3><a name="default-constructor">constructors</a></h3>
  97. <pre>weak_ptr();</pre>
  98. <blockquote>
  99. <p><b>Effects:</b> Constructs an <EM>empty</EM> <b>weak_ptr</b>.</p>
  100. <p><b>Postconditions:</b> <code>use_count() == 0</code>.</p>
  101. <p><b>Throws:</b> nothing.</p>
  102. </blockquote><a name="constructors"></a>
  103. <pre>template&lt;class Y&gt; weak_ptr</A>(shared_ptr&lt;Y&gt; const &amp; r);
  104. weak_ptr(weak_ptr const &amp; r);
  105. template&lt;class Y&gt; weak_ptr(weak_ptr&lt;Y&gt; const &amp; r);</pre>
  106. <blockquote>
  107. <p><b>Effects:</b> If <STRONG>r</STRONG> is <EM>empty</EM>, constructs an <EM>empty</EM>
  108. <STRONG>weak_ptr</STRONG>; otherwise, constructs a <b>weak_ptr</b> that <EM>shares
  109. ownership</EM> with <STRONG>r</STRONG> as if by storing a copy of the
  110. pointer stored in <b>r</b>.</p>
  111. <p><b>Postconditions:</b> <code>use_count() == r.use_count()</code>.</p>
  112. <p><b>Throws:</b> nothing.</p>
  113. </blockquote>
  114. <h3><a name="destructor">destructor</a></h3>
  115. <pre>~weak_ptr();</pre>
  116. <BLOCKQUOTE>
  117. <P><B>Effects:</B> Destroys this <b>weak_ptr</b> but has no effect on the object
  118. its stored pointer points to.</P>
  119. <P><B>Throws:</B> nothing.</P>
  120. </BLOCKQUOTE>
  121. <h3><a name="assignment">assignment</a></h3>
  122. <pre>weak_ptr &amp; operator=(weak_ptr const &amp; r);
  123. template&lt;class Y&gt; weak_ptr &amp; operator=(weak_ptr&lt;Y&gt; const &amp; r);
  124. template&lt;class Y&gt; weak_ptr &amp; operator=(shared_ptr&lt;Y&gt; const &amp; r);</pre>
  125. <BLOCKQUOTE>
  126. <P><B>Effects:</B> Equivalent to <code>weak_ptr(r).swap(*this)</code>.</P>
  127. <P><B>Throws:</B> nothing.</P>
  128. <P><B>Notes:</B> The implementation is free to meet the effects (and the implied
  129. guarantees) via different means, without creating a temporary.</P>
  130. </BLOCKQUOTE>
  131. <h3><a name="use_count">use_count</a></h3>
  132. <pre>long use_count() const;</pre>
  133. <blockquote>
  134. <p><b>Returns:</b> 0 if <STRONG>*this</STRONG> is <EM>empty</EM>; otherwise, the
  135. number of <b>shared_ptr</b> objects that <EM>share ownership</EM> with <STRONG>*this</STRONG>.</p>
  136. <p><b>Throws:</b> nothing.</p>
  137. <P><B>Notes:</B> <code>use_count()</code> is not necessarily efficient. Use only
  138. for debugging and testing purposes, not for production code.</P>
  139. </blockquote>
  140. <h3><a name="expired">expired</a></h3>
  141. <pre>bool expired() const;</pre>
  142. <blockquote>
  143. <p><b>Returns:</b> <code>use_count() == 0</code>.</p>
  144. <p><b>Throws:</b> nothing.</p>
  145. <P><B>Notes:</B> <code>expired()</code> may be faster than <code>use_count()</code>.</P>
  146. </blockquote>
  147. <h3><a name="lock">lock</a></h3>
  148. <pre>shared_ptr&lt;T&gt; lock() const;</pre>
  149. <BLOCKQUOTE>
  150. <P><B>Returns:</B> <code>expired()? shared_ptr&lt;T&gt;(): shared_ptr&lt;T&gt;(*this)</code>.</P>
  151. <P><B>Throws:</B> nothing.</P>
  152. </BLOCKQUOTE>
  153. <h3><a name="reset">reset</a></h3>
  154. <pre>void reset();</pre>
  155. <BLOCKQUOTE>
  156. <P><B>Effects:</B> Equivalent to <code>weak_ptr().swap(*this)</code>.</P>
  157. </BLOCKQUOTE>
  158. <h3><a name="swap">swap</a></h3>
  159. <pre>void swap(weak_ptr &amp; b);</pre>
  160. <blockquote>
  161. <p><b>Effects:</b> Exchanges the contents of the two smart pointers.</p>
  162. <p><b>Throws:</b> nothing.</p>
  163. </blockquote>
  164. <h2><a name="functions">Free Functions</a></h2>
  165. <h3><a name="comparison">comparison</a></h3>
  166. <pre>template&lt;class T, class U&gt;
  167. bool operator&lt;(weak_ptr&lt;T&gt; const &amp; a, weak_ptr&lt;U&gt; const &amp; b);</pre>
  168. <blockquote>
  169. <p><b>Returns:</b> an unspecified value such that</p>
  170. <UL>
  171. <LI>
  172. <b>operator&lt;</b> is a strict weak ordering as described in section 25.3 <code>[lib.alg.sorting]</code>
  173. of the C++ standard;
  174. <LI>
  175. under the equivalence relation defined by <STRONG>operator&lt;</STRONG>, <code>!(a
  176. &lt; b) &amp;&amp; !(b &lt; a)</code>, two <STRONG>weak_ptr</STRONG> instances
  177. are equivalent if and only if they <EM>share ownership</EM> or are both <EM>empty</EM>.</LI></UL>
  178. <p><b>Throws:</b> nothing.</p>
  179. <P><B>Notes:</B> Allows <STRONG>weak_ptr</STRONG> objects to be used as keys in
  180. associative containers.</P>
  181. </blockquote>
  182. <h3><a name="free-swap">swap</a></h3>
  183. <pre>template&lt;class T&gt;
  184. void swap(weak_ptr&lt;T&gt; &amp; a, weak_ptr&lt;T&gt; &amp; b)</pre>
  185. <BLOCKQUOTE>
  186. <P><B>Effects:</B> Equivalent to <code>a.swap(b)</code>.</P>
  187. <P><B>Throws:</B> nothing.</P>
  188. <P><B>Notes:</B> Matches the interface of <B>std::swap</B>. Provided as an aid to
  189. generic programming.</P>
  190. </BLOCKQUOTE>
  191. <h2><a name="FAQ">Frequently Asked Questions</a></h2>
  192. <P><B>Q.</B> Can an object create a <STRONG>weak_ptr</STRONG> to itself in its
  193. constructor?</P>
  194. <P><b>A.</b> No. A <STRONG>weak_ptr</STRONG> can only be created from a <STRONG>shared_ptr</STRONG>,
  195. and at object construction time no <STRONG>shared_ptr</STRONG> to the object
  196. exists yet. Even if you could create a temporary <STRONG>shared_ptr</STRONG> to <STRONG>
  197. this</STRONG>, it would go out of scope at the end of the constructor, and
  198. all <STRONG>weak_ptr</STRONG> instances would instantly expire.</P>
  199. <P>The solution is to make the constructor private, and supply a factory function
  200. that returns a <STRONG>shared_ptr</STRONG>:<BR>
  201. </P>
  202. <pre>
  203. class X
  204. {
  205. private:
  206. X();
  207. public:
  208. static shared_ptr&lt;X&gt; create()
  209. {
  210. shared_ptr&lt;X&gt; px(new X);
  211. // create weak pointers from px here
  212. return px;
  213. }
  214. };
  215. </pre>
  216. <p><br>
  217. </p>
  218. <hr>
  219. <p>$Date: 2005/07/18 20:33:59 $</p>
  220. <p><small>Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler.
  221. Copyright 2002-2005 Peter Dimov. Permission to copy, use, modify, sell and
  222. distribute this document is granted provided this copyright notice appears in
  223. all copies. This document is provided "as is" without express or implied
  224. warranty, and with no claim as to its suitability for any purpose.</small></p>
  225. </A>
  226. </body>
  227. </html>