1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Any Hooks: A single hook for any Intrusive container</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../intrusive.html" title="Chapter 19. Boost.Intrusive"> 10<link rel="prev" href="obtaining_iterators_from_values.html" title="Obtaining iterators from values"> 11<link rel="next" href="concepts.html" title="Concepts explained"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="obtaining_iterators_from_values.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../intrusive.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="concepts.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="intrusive.any_hooks"></a><a class="link" href="any_hooks.html" title="Any Hooks: A single hook for any Intrusive container">Any Hooks: A single hook for any Intrusive 29 container</a> 30</h2></div></div></div> 31<p> 32 Sometimes, a class programmer wants to place a class in several intrusive containers 33 but no at the same time. In this case, the programmer might decide to insert 34 two hooks in the same class. 35 </p> 36<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">MyClass</span> 37 <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">list_base_hook</span><span class="special"><>,</span> <span class="keyword">public</span> <span class="identifier">slist_base_hook</span><span class="special"><></span> <span class="comment">//...</span> 38<span class="special">{};</span> 39</pre> 40<p> 41 However, there is a more size-efficient alternative in <span class="bold"><strong>Boost.Intrusive</strong></span>: 42 "any" hooks (<code class="computeroutput"><a class="link" href="../boost/intrusive/any_base_hook.html" title="Class template any_base_hook">any_base_hook</a></code> 43 and <code class="computeroutput"><a class="link" href="../boost/intrusive/any_member_hook.html" title="Class template any_member_hook">any_member_hook</a></code>). 44 These hooks can be used to store a type in several containers offered by <span class="bold"><strong>Boost.Intrusive</strong></span> minimizing the size of the class. 45 </p> 46<p> 47 These hooks support these options: 48 </p> 49<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 50<li class="listitem"> 51 <span class="bold"><strong><code class="computeroutput"><span class="identifier">tag</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">Tag</span><span class="special">></span></code></strong></span> 52 (for base hooks only): This argument serves as a tag, so you can derive 53 from more than one slist hook. Default: <code class="computeroutput"><span class="identifier">tag</span><span class="special"><</span><span class="identifier">default_tag</span><span class="special">></span></code>. 54 </li> 55<li class="listitem"> 56 <span class="bold"><strong><code class="computeroutput"><span class="identifier">link_mode</span><span class="special"><</span><span class="identifier">link_mode_type</span> 57 <span class="identifier">LinkMode</span><span class="special">></span></code></strong></span>: 58 The linking policy. <code class="computeroutput"><span class="identifier">link_mode</span><span class="special"><</span><span class="identifier">auto_unlink</span><span class="special">></span></code> is <span class="bold"><strong>not</strong></span> 59 supported and <code class="computeroutput"><span class="identifier">link_mode</span><span class="special"><</span><span class="identifier">safe_mode</span><span class="special">></span></code> might offer weaker error detection 60 in any hooks than in other hooks. Default: <code class="computeroutput"><span class="identifier">link_mode</span><span class="special"><</span><span class="identifier">safe_link</span><span class="special">></span></code>. 61 </li> 62<li class="listitem"> 63 <span class="bold"><strong><code class="computeroutput"><span class="identifier">void_pointer</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">VoidPointer</span><span class="special">></span></code></strong></span>: 64 The pointer type to be used internally in the hook and propagated to the 65 container. Default: <code class="computeroutput"><span class="identifier">void_pointer</span><span class="special"><</span><span class="keyword">void</span><span class="special">*></span></code>. 66 </li> 67</ul></div> 68<p> 69 <code class="computeroutput"><span class="identifier">auto_unlink</span></code> can't be supported 70 because the hook does not know in which type of container might be currently 71 inserted. Additionally, these hooks don't support <code class="computeroutput"><span class="identifier">unlink</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">swap_nodes</span><span class="special">()</span></code> operations for the same reason. 72 </p> 73<p> 74 Here is an example that creates a class with two any hooks, and uses one to 75 insert the class in a <code class="computeroutput">slist</code> and the other 76 one in a <code class="computeroutput">list</code>. 77 </p> 78<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">vector</span><span class="special">></span> 79<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">intrusive</span><span class="special">/</span><span class="identifier">any_hook</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 80<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">intrusive</span><span class="special">/</span><span class="identifier">slist</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 81<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">intrusive</span><span class="special">/</span><span class="identifier">list</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 82 83<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">intrusive</span><span class="special">;</span> 84 85<span class="keyword">class</span> <span class="identifier">MyClass</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">any_base_hook</span><span class="special"><></span> <span class="comment">//Base hook</span> 86<span class="special">{</span> 87 <span class="keyword">int</span> <span class="identifier">int_</span><span class="special">;</span> 88 89 <span class="keyword">public</span><span class="special">:</span> 90 <span class="identifier">any_member_hook</span><span class="special"><></span> <span class="identifier">member_hook_</span><span class="special">;</span> <span class="comment">//Member hook</span> 91 92 <span class="identifier">MyClass</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">int_</span><span class="special">(</span><span class="identifier">i</span><span class="special">)</span> 93 <span class="special">{}</span> 94<span class="special">};</span> 95 96<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 97<span class="special">{</span> 98 <span class="comment">//Define a base hook option that converts any_base_hook to a slist hook</span> 99 <span class="keyword">typedef</span> <span class="identifier">any_to_slist_hook</span> <span class="special"><</span> <span class="identifier">base_hook</span><span class="special"><</span> <span class="identifier">any_base_hook</span><span class="special"><></span> <span class="special">></span> <span class="special">></span> <span class="identifier">BaseSlistOption</span><span class="special">;</span> 100 <span class="keyword">typedef</span> <span class="identifier">slist</span><span class="special"><</span><span class="identifier">MyClass</span><span class="special">,</span> <span class="identifier">BaseSlistOption</span><span class="special">></span> <span class="identifier">BaseSList</span><span class="special">;</span> 101 102 <span class="comment">//Define a member hook option that converts any_member_hook to a list hook</span> 103 <span class="keyword">typedef</span> <span class="identifier">any_to_list_hook</span><span class="special"><</span> <span class="identifier">member_hook</span> 104 <span class="special"><</span> <span class="identifier">MyClass</span><span class="special">,</span> <span class="identifier">any_member_hook</span><span class="special"><>,</span> <span class="special">&</span><span class="identifier">MyClass</span><span class="special">::</span><span class="identifier">member_hook_</span><span class="special">></span> <span class="special">></span> <span class="identifier">MemberListOption</span><span class="special">;</span> 105 <span class="keyword">typedef</span> <span class="identifier">list</span><span class="special"><</span><span class="identifier">MyClass</span><span class="special">,</span> <span class="identifier">MemberListOption</span><span class="special">></span> <span class="identifier">MemberList</span><span class="special">;</span> 106 107 <span class="comment">//Create several MyClass objects, each one with a different value</span> 108 <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">MyClass</span><span class="special">></span> <span class="identifier">values</span><span class="special">;</span> 109 <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="number">100</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">){</span> <span class="identifier">values</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">MyClass</span><span class="special">(</span><span class="identifier">i</span><span class="special">));</span> <span class="special">}</span> 110 111 <span class="identifier">BaseSList</span> <span class="identifier">base_slist</span><span class="special">;</span> <span class="identifier">MemberList</span> <span class="identifier">member_list</span><span class="special">;</span> 112 113 <span class="comment">//Now insert them in reverse order in the slist and in order in the list</span> 114 <span class="keyword">for</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">MyClass</span><span class="special">>::</span><span class="identifier">iterator</span> <span class="identifier">it</span><span class="special">(</span><span class="identifier">values</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()),</span> <span class="identifier">itend</span><span class="special">(</span><span class="identifier">values</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">itend</span><span class="special">;</span> <span class="special">++</span><span class="identifier">it</span><span class="special">)</span> 115 <span class="identifier">base_slist</span><span class="special">.</span><span class="identifier">push_front</span><span class="special">(*</span><span class="identifier">it</span><span class="special">),</span> <span class="identifier">member_list</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(*</span><span class="identifier">it</span><span class="special">);</span> 116 117 <span class="comment">//Now test lists</span> 118 <span class="identifier">BaseSList</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">bit</span><span class="special">(</span><span class="identifier">base_slist</span><span class="special">.</span><span class="identifier">begin</span><span class="special">());</span> 119 <span class="identifier">MemberList</span><span class="special">::</span><span class="identifier">reverse_iterator</span> <span class="identifier">mrit</span><span class="special">(</span><span class="identifier">member_list</span><span class="special">.</span><span class="identifier">rbegin</span><span class="special">());</span> 120 <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">MyClass</span><span class="special">>::</span><span class="identifier">reverse_iterator</span> <span class="identifier">rit</span><span class="special">(</span><span class="identifier">values</span><span class="special">.</span><span class="identifier">rbegin</span><span class="special">()),</span> <span class="identifier">ritend</span><span class="special">(</span><span class="identifier">values</span><span class="special">.</span><span class="identifier">rend</span><span class="special">());</span> 121 122 <span class="comment">//Test the objects inserted in the base hook list</span> 123 <span class="keyword">for</span><span class="special">(;</span> <span class="identifier">rit</span> <span class="special">!=</span> <span class="identifier">ritend</span><span class="special">;</span> <span class="special">++</span><span class="identifier">rit</span><span class="special">,</span> <span class="special">++</span><span class="identifier">bit</span><span class="special">,</span> <span class="special">++</span><span class="identifier">mrit</span><span class="special">)</span> 124 <span class="keyword">if</span><span class="special">(&*</span><span class="identifier">bit</span> <span class="special">!=</span> <span class="special">&*</span><span class="identifier">rit</span> <span class="special">||</span> <span class="special">&*</span><span class="identifier">mrit</span> <span class="special">!=</span> <span class="special">&*</span><span class="identifier">rit</span><span class="special">)</span> <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> 125 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 126<span class="special">}</span> 127</pre> 128</div> 129<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 130<td align="left"></td> 131<td align="right"><div class="copyright-footer">Copyright © 2005 Olaf Krzikalla<br>Copyright © 2006-2015 Ion Gaztanaga<p> 132 Distributed under the Boost Software License, Version 1.0. (See accompanying 133 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 134 </p> 135</div></td> 136</tr></table> 137<hr> 138<div class="spirit-nav"> 139<a accesskey="p" href="obtaining_iterators_from_values.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../intrusive.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="concepts.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 140</div> 141</body> 142</html> 143