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 <class IntPtr> |
---|
31 | class FloatPointerHolder |
---|
32 | { |
---|
33 | <em>// Let's define a pointer to a float</em> |
---|
34 | typedef typename boost::pointer_to_other |
---|
35 | <IntPtr, float>::type float_ptr_t; |
---|
36 | float_ptr_t float_ptr; |
---|
37 | };</pre> |
---|
38 | <h2><a name="synopsis">Synopsis</a></h2> |
---|
39 | <pre> |
---|
40 | namespace boost { |
---|
41 | |
---|
42 | template<class T, class U> |
---|
43 | struct pointer_to_other; |
---|
44 | |
---|
45 | template<class T, class U, template <class> class Sp> |
---|
46 | struct pointer_to_other< Sp<T>, U > |
---|
47 | { |
---|
48 | typedef Sp<U> type; |
---|
49 | }; |
---|
50 | |
---|
51 | template<class T, class T2, class U, |
---|
52 | template <class, class> class Sp> |
---|
53 | struct pointer_to_other< Sp<T, T2>, U > |
---|
54 | { |
---|
55 | typedef Sp<U, T2> type; |
---|
56 | }; |
---|
57 | |
---|
58 | template<class T, class T2, class T3, class U, |
---|
59 | template <class, class, class> class Sp> |
---|
60 | struct pointer_to_other< Sp<T, T2, T3>, U > |
---|
61 | { |
---|
62 | typedef Sp<U, T2, T3> type; |
---|
63 | }; |
---|
64 | |
---|
65 | template<class T, class U> |
---|
66 | struct pointer_to_other< T*, U > |
---|
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 <boost/pointer_to_other.hpp> |
---|
79 | |
---|
80 | template <class VoidPtr> |
---|
81 | class 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<void>, block_ptr_t is smart_ptr<block></em> |
---|
89 | typedef typename boost::pointer_to_other |
---|
90 | <VoidPtr, block>::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 < <a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>>.)</small></p> |
---|
107 | </body> |
---|
108 | </html> |
---|