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>Using smart pointers with 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="recursive.html" title="Recursive Boost.Intrusive containers"> 11<link rel="next" href="obtaining_iterators_from_values.html" title="Obtaining iterators from values"> 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="recursive.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="obtaining_iterators_from_values.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.using_smart_pointers"></a><a class="link" href="using_smart_pointers.html" title="Using smart pointers with Boost.Intrusive containers">Using smart pointers with 29 Boost.Intrusive containers</a> 30</h2></div></div></div> 31<div class="toc"><dl class="toc"><dt><span class="section"><a href="using_smart_pointers.html#intrusive.using_smart_pointers.smart_pointers_requirements">Requirements 32 for smart pointers compatible with Boost.Intrusive</a></span></dt></dl></div> 33<p> 34 <span class="bold"><strong>Boost.Intrusive</strong></span> hooks can be configured to 35 use other pointers than raw pointers. When a <span class="bold"><strong>Boost.Intrusive</strong></span> 36 hook is configured with a smart pointer as an argument, this pointer configuration 37 is passed to the containers. For example, if the following hook is configured 38 with a smart pointer (for example, an offset pointer from <span class="bold"><strong>Boost.Interprocess</strong></span>): 39 </p> 40<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> 41<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">offset_ptr</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 42 43<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> 44<span class="keyword">namespace</span> <span class="identifier">ip</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span> 45 46<span class="keyword">class</span> <span class="identifier">shared_memory_data</span> 47 <span class="comment">//Declare the hook with an offset_ptr from Boost.Interprocess</span> 48 <span class="comment">//to make this class compatible with shared memory</span> 49 <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">list_base_hook</span><span class="special"><</span> <span class="identifier">void_pointer</span><span class="special"><</span> <span class="identifier">ip</span><span class="special">::</span><span class="identifier">offset_ptr</span><span class="special"><</span><span class="keyword">void</span><span class="special">></span> <span class="special">></span> <span class="special">></span> 50<span class="special">{</span> 51 <span class="keyword">int</span> <span class="identifier">data_id_</span><span class="special">;</span> 52 <span class="keyword">public</span><span class="special">:</span> 53 54 <span class="keyword">int</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">data_id_</span><span class="special">;</span> <span class="special">}</span> 55 <span class="keyword">void</span> <span class="identifier">set</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">id</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">data_id_</span> <span class="special">=</span> <span class="identifier">id</span><span class="special">;</span> <span class="special">}</span> 56<span class="special">};</span> 57</pre> 58<p> 59 Any intrusive list constructed using this hook will be ready for shared memory, 60 because the intrusive list will also use offset pointers internally. For example, 61 we can create an intrusive list in shared memory combining <span class="bold"><strong>Boost.Interprocess</strong></span> 62 and <span class="bold"><strong>Boost.Intrusive</strong></span>: 63 </p> 64<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 65<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">vector</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 66<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 67 68<span class="comment">//Definition of the shared memory friendly intrusive list</span> 69<span class="keyword">typedef</span> <span class="identifier">list</span><span class="special"><</span><span class="identifier">shared_memory_data</span><span class="special">></span> <span class="identifier">intrusive_list_t</span><span class="special">;</span> 70 71<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 72<span class="special">{</span> 73 <span class="comment">//Now create an intrusive list in shared memory:</span> 74 <span class="comment">//nodes and the container itself must be created in shared memory</span> 75 <span class="keyword">const</span> <span class="keyword">int</span> <span class="identifier">MaxElem</span> <span class="special">=</span> <span class="number">100</span><span class="special">;</span> 76 <span class="keyword">const</span> <span class="keyword">int</span> <span class="identifier">ShmSize</span> <span class="special">=</span> <span class="number">50000</span><span class="special">;</span> 77 <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">ShmName</span> <span class="special">=</span> <span class="identifier">get_shared_memory_name</span><span class="special">();</span> 78 <span class="special">{</span> 79 <span class="comment">//Erase all old shared memory</span> 80 <span class="identifier">ip</span><span class="special">::</span><span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="identifier">ShmName</span><span class="special">);</span> 81 <span class="identifier">ip</span><span class="special">::</span><span class="identifier">managed_shared_memory</span> <span class="identifier">shm</span><span class="special">(</span><span class="identifier">ip</span><span class="special">::</span><span class="identifier">create_only</span><span class="special">,</span> <span class="identifier">ShmName</span><span class="special">,</span> <span class="identifier">ShmSize</span><span class="special">);</span> 82 83 <span class="comment">//Create all nodes in shared memory using a shared memory vector</span> 84 <span class="comment">//See Boost.Interprocess documentation for more information on this</span> 85 <span class="keyword">typedef</span> <span class="identifier">ip</span><span class="special">::</span><span class="identifier">allocator</span> 86 <span class="special"><</span> <span class="identifier">shared_memory_data</span><span class="special">,</span> <span class="identifier">ip</span><span class="special">::</span><span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">></span> 87 <span class="identifier">shm_allocator_t</span><span class="special">;</span> 88 <span class="keyword">typedef</span> <span class="identifier">ip</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">shared_memory_data</span><span class="special">,</span> <span class="identifier">shm_allocator_t</span><span class="special">></span> <span class="identifier">shm_vector_t</span><span class="special">;</span> 89 <span class="identifier">shm_allocator_t</span> <span class="identifier">shm_alloc</span><span class="special">(</span><span class="identifier">shm</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span> 90 <span class="identifier">shm_vector_t</span> <span class="special">*</span><span class="identifier">pshm_vect</span> <span class="special">=</span> 91 <span class="identifier">shm</span><span class="special">.</span><span class="identifier">construct</span><span class="special"><</span><span class="identifier">shm_vector_t</span><span class="special">>(</span><span class="identifier">ip</span><span class="special">::</span><span class="identifier">anonymous_instance</span><span class="special">)(</span><span class="identifier">shm_alloc</span><span class="special">);</span> 92 <span class="identifier">pshm_vect</span><span class="special">-></span><span class="identifier">resize</span><span class="special">(</span><span class="identifier">MaxElem</span><span class="special">);</span> 93 94 <span class="comment">//Initialize all the nodes</span> 95 <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="identifier">MaxElem</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">(*</span><span class="identifier">pshm_vect</span><span class="special">)[</span><span class="identifier">i</span><span class="special">].</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">i</span><span class="special">);</span> 96 97 <span class="comment">//Now create the shared memory intrusive list</span> 98 <span class="identifier">intrusive_list_t</span> <span class="special">*</span><span class="identifier">plist</span> <span class="special">=</span> <span class="identifier">shm</span><span class="special">.</span><span class="identifier">construct</span><span class="special"><</span><span class="identifier">intrusive_list_t</span><span class="special">>(</span><span class="identifier">ip</span><span class="special">::</span><span class="identifier">anonymous_instance</span><span class="special">)();</span> 99 100 <span class="comment">//Insert objects stored in shared memory vector in the intrusive list</span> 101 <span class="identifier">plist</span><span class="special">-></span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">plist</span><span class="special">-></span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">pshm_vect</span><span class="special">-></span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">pshm_vect</span><span class="special">-></span><span class="identifier">end</span><span class="special">());</span> 102 103 <span class="comment">//Check all the inserted nodes</span> 104 <span class="keyword">int</span> <span class="identifier">checker</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> 105 <span class="keyword">for</span><span class="special">(</span> <span class="identifier">intrusive_list_t</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">plist</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">plist</span><span class="special">-></span><span class="identifier">end</span><span class="special">())</span> 106 <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> <span class="special">++</span><span class="identifier">checker</span><span class="special">){</span> 107 <span class="keyword">if</span><span class="special">(</span><span class="identifier">it</span><span class="special">-></span><span class="identifier">get</span><span class="special">()</span> <span class="special">!=</span> <span class="identifier">checker</span><span class="special">)</span> <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> 108 <span class="special">}</span> 109 110 <span class="comment">//Now delete the list and after that, the nodes</span> 111 <span class="identifier">shm</span><span class="special">.</span><span class="identifier">destroy_ptr</span><span class="special">(</span><span class="identifier">plist</span><span class="special">);</span> 112 <span class="identifier">shm</span><span class="special">.</span><span class="identifier">destroy_ptr</span><span class="special">(</span><span class="identifier">pshm_vect</span><span class="special">);</span> 113 <span class="special">}</span> 114 <span class="identifier">ip</span><span class="special">::</span><span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="identifier">ShmName</span><span class="special">);</span> 115 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 116<span class="special">}</span> 117</pre> 118<div class="section"> 119<div class="titlepage"><div><div><h3 class="title"> 120<a name="intrusive.using_smart_pointers.smart_pointers_requirements"></a><a class="link" href="using_smart_pointers.html#intrusive.using_smart_pointers.smart_pointers_requirements" title="Requirements for smart pointers compatible with Boost.Intrusive">Requirements 121 for smart pointers compatible with Boost.Intrusive</a> 122</h3></div></div></div> 123<p> 124 Not every smart pointer is compatible with <span class="bold"><strong>Boost.Intrusive</strong></span>: 125 </p> 126<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 127<li class="listitem"> 128 It must be compatible with C++11 <a href="http://en.cppreference.com/w/cpp/memory/pointer_traits" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pointer_traits</span></code></a> requirements. 129 <span class="bold"><strong>Boost.Intrusive</strong></span> uses its own <code class="computeroutput"><a class="link" href="../boost/intrusive/pointer_traits.html" title="Struct template pointer_traits">pointer_traits</a></code> class 130 to implement those features in both C++11 and C++03 compilers. 131 </li> 132<li class="listitem"> 133 It must have the same ownership semantics as a raw pointer. This means 134 that resource management smart pointers (like <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span></code>) 135 can't be used. 136 </li> 137</ul></div> 138<p> 139 The conversion from the smart pointer to a raw pointer will be implemented 140 as a recursive call to <code class="computeroutput"><span class="keyword">operator</span><span class="special">->()</span></code> until the function returns a raw pointer. 141 </p> 142</div> 143</div> 144<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 145<td align="left"></td> 146<td align="right"><div class="copyright-footer">Copyright © 2005 Olaf Krzikalla<br>Copyright © 2006-2015 Ion Gaztanaga<p> 147 Distributed under the Boost Software License, Version 1.0. (See accompanying 148 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>) 149 </p> 150</div></td> 151</tr></table> 152<hr> 153<div class="spirit-nav"> 154<a accesskey="p" href="recursive.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="obtaining_iterators_from_values.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 155</div> 156</body> 157</html> 158