Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/smart_ptr/pointer_to_other.html @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.8 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html>
3        <head>
4                <title>pointer_to_other.hpp</title>
5                <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6        </head>
7        <body bgcolor="#ffffff" text="#000000">
8                <h1><img src="../../boost.png" alt="boost.png (6897 bytes)" align="middle" WIDTH="277" HEIGHT="86">Header
9                        <a href="../../boost/pointer_to_other.hpp">boost/pointer_to_other.hpp</a></h1>
10                <p>
11                        The pointer to other utility provides a way, given a source pointer type,
12                        to obtain a pointer of the same type to another pointee type. The utility is
13                        defined in <cite><a href="../../boost/pointer_to_other.hpp">boost/pointer_to_other.hpp</a>.</cite></p>
14                <p>There is test/example code in <cite><a href="test/pointer_to_other_test.cpp">pointer_to_other_test.cpp</a></cite>.</p>
15                <h2><a name="contents">Contents</a></h2>
16                <ul>
17                        <li>
18                                <a href="#rationale">Rationale</a>
19                        <li>
20                                <a href="#synopsis">Synopsis</a>
21                        <li>
22                                <a href="#example">Example</a></li>
23                </ul>
24                <h2><a name="rationale">Rationale</a></h2>
25                <p>When building pointer independent classes, like memory managers, allocators, or
26                        containers, there is often a need to define pointers generically, so that if a
27                        template parameter represents a pointer (for example, a raw or smart pointer to
28                        an int), we can define another pointer of the same type to another pointee (a
29                        raw or smart pointer to a float.)</p>
30                <pre>template &lt;class IntPtr&gt;
31class FloatPointerHolder
32{   
33   <em>// Let's define a pointer to a float</em>
34   typedef typename boost::pointer_to_other
35      &lt;IntPtr, float&gt;::type float_ptr_t;
36   float_ptr_t float_ptr;
37};</pre>
38                <h2><a name="synopsis">Synopsis</a></h2>
39                <pre>
40namespace boost {
41
42template&lt;class T, class U&gt;
43   struct pointer_to_other;
44
45template&lt;class T, class U, template &lt;class&gt; class Sp&gt;
46   struct pointer_to_other&lt; Sp&lt;T&gt;, U &gt;
47{
48   typedef Sp&lt;U&gt; type;
49};
50
51template&lt;class T, class T2, class U,
52        template &lt;class, class&gt; class Sp&gt;
53   struct pointer_to_other&lt; Sp&lt;T, T2&gt;, U &gt;
54{
55   typedef Sp&lt;U, T2&gt; type;
56};
57
58template&lt;class T, class T2, class T3, class U,
59        template &lt;class, class, class&gt; class Sp&gt;
60struct pointer_to_other&lt; Sp&lt;T, T2, T3&gt;, U &gt;
61{
62   typedef Sp&lt;U, T2, T3&gt; type;
63};
64
65template&lt;class T, class U&gt;
66struct pointer_to_other&lt; T*, U &gt; 
67{
68   typedef U* type;
69};
70
71} <em>// namespace boost</em></pre>
72                <p>If these definitions are not correct for a specific smart pointer, we can define
73                        a specialization of pointer_to_other.</p>
74                <h2><a name="example">Example</a></h2>
75                <pre><em>// Let's define a memory allocator that can
76// work with raw and smart pointers</em>
77
78#include &lt;boost/pointer_to_other.hpp&gt;
79
80template &lt;class VoidPtr&gt;
81class memory_allocator
82{<em>
83   // Predefine a memory_block </em>
84   struct block;<em>
85
86   // Define a pointer to a memory_block from a void pointer
87   // If VoidPtr is void *, block_ptr_t is block*
88   // If VoidPtr is smart_ptr&lt;void&gt;, block_ptr_t is smart_ptr&lt;block&gt;</em>
89   typedef typename boost::pointer_to_other     
90            &lt;VoidPtr, block&gt;::type block_ptr_t;
91           
92   struct block
93   {
94      std::size_t size;
95      block_ptr_t next_block;
96   };
97
98   block_ptr_t free_blocks;
99};</pre>
100                <p>As we can see, using pointer_to_other we can create pointer independent code.</p>
101                <hr>
102                <p>Last revised: $Date: 2006/03/18 14:58:20 $</p>
103                <p><small>Copyright 2005, 2006 Ion Gaztañaga and Peter Dimov. Use, modification,
104                                and distribution are subject to the Boost Software License, Version 1.0.<br>
105                                (See accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or a
106                                copy at &lt; <a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>&gt;.)</small></p>
107        </body>
108</html>
Note: See TracBrowser for help on using the repository browser.