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>Recursive Boost.Intrusive containers</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="function_hooks.html" title="Using function hooks"> 11<link rel="next" href="using_smart_pointers.html" title="Using smart pointers with Boost.Intrusive containers"> 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="function_hooks.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="using_smart_pointers.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.recursive"></a><a class="link" href="recursive.html" title="Recursive Boost.Intrusive containers">Recursive Boost.Intrusive containers</a> 29</h2></div></div></div> 30<p> 31 <span class="bold"><strong>Boost.Intrusive</strong></span> containers can be used to 32 define recursive structures very easily, allowing complex data structures with 33 very low overhead. Let's see an example: 34 </p> 35<pre class="programlisting"><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> 36<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cassert</span><span class="special">></span> 37 38<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> 39 40<span class="keyword">typedef</span> <span class="identifier">list_base_hook</span><span class="special"><></span> <span class="identifier">BaseHook</span><span class="special">;</span> 41 42<span class="comment">//A recursive class</span> 43<span class="keyword">class</span> <span class="identifier">Recursive</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">BaseHook</span> 44<span class="special">{</span> 45 <span class="keyword">private</span><span class="special">:</span> 46 <span class="identifier">Recursive</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">Recursive</span><span class="special">&);</span> 47 <span class="identifier">Recursive</span> <span class="special">&</span> <span class="keyword">operator</span><span class="special">=(</span><span class="keyword">const</span> <span class="identifier">Recursive</span><span class="special">&);</span> 48 49 <span class="keyword">public</span><span class="special">:</span> 50 <span class="identifier">Recursive</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">BaseHook</span><span class="special">(),</span> <span class="identifier">children</span><span class="special">(){}</span> 51 <span class="identifier">list</span><span class="special"><</span> <span class="identifier">Recursive</span><span class="special">,</span> <span class="identifier">base_hook</span><span class="special"><</span><span class="identifier">BaseHook</span><span class="special">></span> <span class="special">></span> <span class="identifier">children</span><span class="special">;</span> 52<span class="special">};</span> 53 54<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 55<span class="special">{</span> 56 <span class="identifier">Recursive</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">f2</span><span class="special">;</span> 57 <span class="comment">//A recursive list of Recursive</span> 58 <span class="identifier">list</span><span class="special"><</span> <span class="identifier">Recursive</span><span class="special">,</span> <span class="identifier">base_hook</span><span class="special"><</span><span class="identifier">BaseHook</span><span class="special">></span> <span class="special">></span> <span class="identifier">l</span><span class="special">;</span> 59 60 <span class="comment">//Insert a node in parent list</span> 61 <span class="identifier">l</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">f</span><span class="special">);</span> 62 63 <span class="comment">//Insert a node in child list</span> 64 <span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()-></span><span class="identifier">children</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()-></span><span class="identifier">children</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">f2</span><span class="special">);</span> 65 66 <span class="comment">//Objects properly inserted</span> 67 <span class="identifier">assert</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()-></span><span class="identifier">children</span><span class="special">.</span><span class="identifier">size</span><span class="special">());</span> 68 <span class="identifier">assert</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span> 69 70 <span class="comment">//Clear both lists</span> 71 <span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()-></span><span class="identifier">children</span><span class="special">.</span><span class="identifier">clear</span><span class="special">();</span> 72 <span class="identifier">l</span><span class="special">.</span><span class="identifier">clear</span><span class="special">();</span> 73 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 74<span class="special">}</span> 75</pre> 76<p> 77 Recursive data structures using <span class="bold"><strong>Boost.Intrusive</strong></span> 78 containers must avoid using hook deduction to avoid early type instantiation: 79 </p> 80<pre class="programlisting"><span class="comment">//This leads to compilation error (Recursive is instantiated by</span> 81<span class="comment">//'list' to deduce hook properties (pointer type, tag, safe-mode...)</span> 82<span class="keyword">class</span> <span class="identifier">Recursive</span> 83<span class="special">{</span> <span class="comment">//...</span> 84 85 <span class="identifier">list</span><span class="special"><</span> <span class="identifier">Recursive</span> <span class="special">></span> <span class="identifier">l</span><span class="special">;</span> 86 <span class="comment">//...</span> 87<span class="special">};</span> 88 89<span class="comment">//Ok, programmer must specify the hook type to avoid early Recursive instantiation</span> 90<span class="keyword">class</span> <span class="identifier">Recursive</span> 91<span class="special">{</span> <span class="comment">//...</span> 92 <span class="identifier">list</span><span class="special"><</span> <span class="identifier">Recursive</span><span class="special">,</span> <span class="identifier">base_hook</span><span class="special"><</span><span class="identifier">BaseHook</span><span class="special">></span> <span class="special">></span> <span class="identifier">l</span><span class="special">;</span> 93 <span class="comment">//...</span> 94<span class="special">};</span> 95</pre> 96<p> 97 Member hooks are not suitable for recursive structures: 98 </p> 99<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">Recursive</span> 100<span class="special">{</span> 101 <span class="keyword">private</span><span class="special">:</span> 102 <span class="identifier">Recursive</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">Recursive</span><span class="special">&);</span> 103 <span class="identifier">Recursive</span> <span class="special">&</span> <span class="keyword">operator</span><span class="special">=(</span><span class="keyword">const</span> <span class="identifier">Recursive</span><span class="special">&);</span> 104 105 <span class="keyword">public</span><span class="special">:</span> 106 <span class="identifier">list_member_hook</span><span class="special"><></span> <span class="identifier">memhook</span><span class="special">;</span> 107 <span class="identifier">list</span><span class="special"><</span> <span class="identifier">Recursive</span><span class="special">,</span> <span class="identifier">member_hook</span><span class="special"><</span><span class="identifier">Recursive</span><span class="special">,</span> <span class="identifier">list_member_hook</span><span class="special"><>,</span> <span class="special">&</span><span class="identifier">Recursive</span><span class="special">::</span><span class="identifier">memhook</span><span class="special">></span> <span class="special">></span> <span class="identifier">children</span><span class="special">;</span> 108<span class="special">};</span> 109</pre> 110<p> 111 Specifying <code class="computeroutput"><span class="special">&</span><span class="identifier">Recursive</span><span class="special">::</span><span class="identifier">memhook</span></code> 112 (that is, the offset between memhook and Recursive) provokes an early instantiation 113 of <code class="computeroutput"><span class="identifier">Recursive</span></code>. To define recursive 114 structures using member hooks, a programmer should use <code class="computeroutput">function_hook</code>: 115 </p> 116<pre class="programlisting"><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> 117<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">parent_from_member</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 118 119<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> 120 121<span class="keyword">class</span> <span class="identifier">Recursive</span><span class="special">;</span> 122 123<span class="comment">//Declaration of the functor that converts betwen the Recursive</span> 124<span class="comment">//class and the hook</span> 125<span class="keyword">struct</span> <span class="identifier">Functor</span> 126<span class="special">{</span> 127 <span class="comment">//Required types</span> 128 <span class="keyword">typedef</span> <span class="identifier">list_member_hook</span><span class="special"><></span> <span class="identifier">hook_type</span><span class="special">;</span> 129 <span class="keyword">typedef</span> <span class="identifier">hook_type</span><span class="special">*</span> <span class="identifier">hook_ptr</span><span class="special">;</span> 130 <span class="keyword">typedef</span> <span class="keyword">const</span> <span class="identifier">hook_type</span><span class="special">*</span> <span class="identifier">const_hook_ptr</span><span class="special">;</span> 131 <span class="keyword">typedef</span> <span class="identifier">Recursive</span> <span class="identifier">value_type</span><span class="special">;</span> 132 <span class="keyword">typedef</span> <span class="identifier">value_type</span><span class="special">*</span> <span class="identifier">pointer</span><span class="special">;</span> 133 <span class="keyword">typedef</span> <span class="keyword">const</span> <span class="identifier">value_type</span><span class="special">*</span> <span class="identifier">const_pointer</span><span class="special">;</span> 134 135 <span class="comment">//Required static functions</span> 136 <span class="keyword">static</span> <span class="identifier">hook_ptr</span> <span class="identifier">to_hook_ptr</span> <span class="special">(</span><span class="identifier">value_type</span> <span class="special">&</span><span class="identifier">value</span><span class="special">);</span> 137 <span class="keyword">static</span> <span class="identifier">const_hook_ptr</span> <span class="identifier">to_hook_ptr</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">value_type</span> <span class="special">&</span><span class="identifier">value</span><span class="special">);</span> 138 <span class="keyword">static</span> <span class="identifier">pointer</span> <span class="identifier">to_value_ptr</span><span class="special">(</span><span class="identifier">hook_ptr</span> <span class="identifier">n</span><span class="special">);</span> 139 <span class="keyword">static</span> <span class="identifier">const_pointer</span> <span class="identifier">to_value_ptr</span><span class="special">(</span><span class="identifier">const_hook_ptr</span> <span class="identifier">n</span><span class="special">);</span> 140<span class="special">};</span> 141 142<span class="comment">//Define the recursive class</span> 143<span class="keyword">class</span> <span class="identifier">Recursive</span> 144<span class="special">{</span> 145 <span class="keyword">private</span><span class="special">:</span> 146 <span class="identifier">Recursive</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">Recursive</span><span class="special">&);</span> 147 <span class="identifier">Recursive</span> <span class="special">&</span> <span class="keyword">operator</span><span class="special">=(</span><span class="keyword">const</span> <span class="identifier">Recursive</span><span class="special">&);</span> 148 149 <span class="keyword">public</span><span class="special">:</span> 150 <span class="identifier">Recursive</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">hook</span><span class="special">(),</span> <span class="identifier">children</span><span class="special">()</span> <span class="special">{}</span> 151 <span class="identifier">list_member_hook</span><span class="special"><></span> <span class="identifier">hook</span><span class="special">;</span> 152 <span class="identifier">list</span><span class="special"><</span> <span class="identifier">Recursive</span><span class="special">,</span> <span class="identifier">function_hook</span><span class="special"><</span> <span class="identifier">Functor</span><span class="special">></span> <span class="special">></span> <span class="identifier">children</span><span class="special">;</span> 153<span class="special">};</span> 154 155<span class="comment">//Definition of Functor functions</span> 156<span class="keyword">inline</span> <span class="identifier">Functor</span><span class="special">::</span><span class="identifier">hook_ptr</span> <span class="identifier">Functor</span><span class="special">::</span><span class="identifier">to_hook_ptr</span> <span class="special">(</span><span class="identifier">Functor</span><span class="special">::</span><span class="identifier">value_type</span> <span class="special">&</span><span class="identifier">value</span><span class="special">)</span> 157 <span class="special">{</span> <span class="keyword">return</span> <span class="special">&</span><span class="identifier">value</span><span class="special">.</span><span class="identifier">hook</span><span class="special">;</span> <span class="special">}</span> 158<span class="keyword">inline</span> <span class="identifier">Functor</span><span class="special">::</span><span class="identifier">const_hook_ptr</span> <span class="identifier">Functor</span><span class="special">::</span><span class="identifier">to_hook_ptr</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">Functor</span><span class="special">::</span><span class="identifier">value_type</span> <span class="special">&</span><span class="identifier">value</span><span class="special">)</span> 159 <span class="special">{</span> <span class="keyword">return</span> <span class="special">&</span><span class="identifier">value</span><span class="special">.</span><span class="identifier">hook</span><span class="special">;</span> <span class="special">}</span> 160<span class="keyword">inline</span> <span class="identifier">Functor</span><span class="special">::</span><span class="identifier">pointer</span> <span class="identifier">Functor</span><span class="special">::</span><span class="identifier">to_value_ptr</span><span class="special">(</span><span class="identifier">Functor</span><span class="special">::</span><span class="identifier">hook_ptr</span> <span class="identifier">n</span><span class="special">)</span> 161 <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">get_parent_from_member</span><span class="special"><</span><span class="identifier">Recursive</span><span class="special">>(</span><span class="identifier">n</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Recursive</span><span class="special">::</span><span class="identifier">hook</span><span class="special">);</span> <span class="special">}</span> 162<span class="keyword">inline</span> <span class="identifier">Functor</span><span class="special">::</span><span class="identifier">const_pointer</span> <span class="identifier">Functor</span><span class="special">::</span><span class="identifier">to_value_ptr</span><span class="special">(</span><span class="identifier">Functor</span><span class="special">::</span><span class="identifier">const_hook_ptr</span> <span class="identifier">n</span><span class="special">)</span> 163 <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">get_parent_from_member</span><span class="special"><</span><span class="identifier">Recursive</span><span class="special">>(</span><span class="identifier">n</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Recursive</span><span class="special">::</span><span class="identifier">hook</span><span class="special">);</span> <span class="special">}</span> 164 165<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 166<span class="special">{</span> 167 <span class="identifier">Recursive</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">f2</span><span class="special">;</span> 168 <span class="comment">//A recursive list of Recursive</span> 169 <span class="identifier">list</span><span class="special"><</span> <span class="identifier">Recursive</span><span class="special">,</span> <span class="identifier">function_hook</span><span class="special"><</span> <span class="identifier">Functor</span><span class="special">></span> <span class="special">></span> <span class="identifier">l</span><span class="special">;</span> 170 171 <span class="comment">//Insert a node in parent list</span> 172 <span class="identifier">l</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">f</span><span class="special">);</span> 173 174 <span class="comment">//Insert a node in child list</span> 175 <span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()-></span><span class="identifier">children</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()-></span><span class="identifier">children</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">f2</span><span class="special">);</span> 176 177 <span class="comment">//Objects properly inserted</span> 178 <span class="identifier">assert</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()-></span><span class="identifier">children</span><span class="special">.</span><span class="identifier">size</span><span class="special">());</span> 179 <span class="identifier">assert</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span> 180 181 <span class="comment">//Clear both lists</span> 182 <span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()-></span><span class="identifier">children</span><span class="special">.</span><span class="identifier">clear</span><span class="special">();</span> 183 <span class="identifier">l</span><span class="special">.</span><span class="identifier">clear</span><span class="special">();</span> 184 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 185<span class="special">}</span> 186</pre> 187</div> 188<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 189<td align="left"></td> 190<td align="right"><div class="copyright-footer">Copyright © 2005 Olaf Krzikalla<br>Copyright © 2006-2015 Ion Gaztanaga<p> 191 Distributed under the Boost Software License, Version 1.0. (See accompanying 192 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>) 193 </p> 194</div></td> 195</tr></table> 196<hr> 197<div class="spirit-nav"> 198<a accesskey="p" href="function_hooks.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="using_smart_pointers.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 199</div> 200</body> 201</html> 202