Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/ptr_container/doc/indirect_fun.html @ 20

Last change on this file since 20 was 12, checked in by landauf, 17 years ago

added boost

File size: 5.2 KB
Line 
1<?xml version="1.0" encoding="utf-8" ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<meta name="generator" content="Docutils 0.3.9: http://docutils.sourceforge.net/" />
7<title>Boost Pointer Container Library</title>
8<link rel="stylesheet" href="default.css" type="text/css" />
9</head>
10<body>
11<div class="document" id="boost-pointer-container-library">
12<h1 class="title"><img alt="Boost" src="boost.png" /> Pointer Container Library</h1>
13<h2 class="subtitle" id="indirected-functions">Indirected functions</h2>
14<p>It is quite common that we have two pointers and what to compare the
15pointed to objects. Also, we have usually already defined how
16to compare the objects. So to avoid some tedious boiler-plate code
17this library defines predicates that apply an indirection before comparing.</p>
18<p>When the container uses <tt class="docutils literal"><span class="pre">void*</span></tt> internally, we can use the
19class <tt class="docutils literal"><span class="pre">void_ptr_indirect_fun</span></tt>; otherwise we use the class
20<tt class="docutils literal"><span class="pre">indirect_fun</span></tt>.</p>
21<p><strong>Example:</strong></p>
22<pre class="literal-block">
23std::string* bar = new std::string(&quot;bar&quot;);
24std::string* foo = new std::string(&quot;foo&quot;);
25BOOST_ASSERT( indirect_fun&lt; std::less&lt;std::string&gt; &gt;()( bar, foo ) == true );
26BOOST_ASSERT( make_indirect_fun( std::less&lt;std::string&gt;() )( foo, bar ) == false );   
27
28void*       vptr1  = ptr1;
29void*       vptr2  = ptr2;
30void_ptr_indirect_fun&lt; std::less&lt;std::string&gt;, std::string&gt; cast_fun;
31BOOST_CHECK( cast_fun( vptr1, vptr2 ) == true );
32</pre>
33<p><strong>See also:</strong></p>
34<ul class="simple">
35<li><a class="reference" href="http://www.boost.org/libs/utility/utility.htm#result_of">result_of</a></li>
36<li><a class="reference" href="http://www.boost.org/libs/iterator/doc/pointee.html">pointee</a></li>
37<li><a class="reference" href="ptr_set.html">ptr_set</a></li>
38<li><a class="reference" href="ptr_multiset.html">ptr_multiset</a></li>
39</ul>
40<p><strong>Navigate</strong></p>
41<ul class="simple">
42<li><a class="reference" href="ptr_container.html">home</a></li>
43<li><a class="reference" href="reference.html">reference</a></li>
44</ul>
45<p><strong>Remarks:</strong></p>
46<p>The class <tt class="docutils literal"><span class="pre">indirect_fun</span></tt> will work with smart pointers such as <a class="reference" href="http://www.boost.org/libs/smart_ptr/shared_ptr.htm">boost::shared_ptr&lt;T&gt;</a>
47because of the type traits <tt class="docutils literal"><span class="pre">pointee&lt;T&gt;::type</span></tt> from the header <tt class="docutils literal"><span class="pre">&lt;boost/pointee.hpp&gt;</span></tt>.</p>
48<p><strong>Synopsis:</strong></p>
49<p>Since the definition of the predicates is somewhat trivial, only the
50first operation is expanded inline.</p>
51<pre class="literal-block">
52namespace boost
53{     
54
55    template&lt; class Fun &gt;
56    struct indirect_fun
57    {
58        indirect_fun() : fun(Fun())
59        { }
60       
61        indirect_fun( Fun f ) : fun(f)
62        { }
63   
64        template&lt; class T &gt;
65        typename result_of&lt; Fun( typename pointee&lt;T&gt;::type ) &gt;::type
66        operator()( const T&amp; r ) const
67        {
68            return fun( *r );
69        }
70   
71        template&lt; class T, class U &gt;
72        typename result_of&lt; Fun( typename pointee&lt;T&gt;::type,
73                                 typename pointee&lt;U&gt;::type ) &gt;::type
74        operator()( const T&amp; r, const U&amp; r2 ) const
75        {
76            return fun( *r, *r2 );
77        }
78   
79    private:
80        Fun fun;
81    };
82
83    template&lt; class Fun &gt;
84    inline indirect_fun&lt;Fun&gt; make_indirect_fun( Fun f )
85    {
86        return indirect_fun&lt;Fun&gt;( f );
87    }       
88
89
90
91    template&lt; class Fun, class Arg1, class Arg2 = Arg1 &gt;
92    struct void_ptr_indirect_fun
93    {
94        void_ptr_indirect_fun() : fun(Fun())
95        { }
96
97        void_ptr_indirect_fun( Fun f ) : fun(f)
98        { }
99
100        typename result_of&lt; Fun( Arg1 ) &gt;::type
101        operator()( const void* r ) const
102        {
103            return fun( * static_cast&lt;const Arg1*&gt;( r ) );
104        }
105
106        typename result_of&lt; Fun( Arg1, Arg2 ) &gt;::type
107        operator()( const void* l, const void* r ) const
108        {
109            return fun( * static_cast&lt;const Arg1*&gt;( l ), * static_cast&lt;const Arg2*&gt;( r ) );
110        }
111       
112    private:
113        Fun fun;   
114    };
115
116    template&lt; class Fun, class Arg &gt;
117    inline void_ptr_indirect_fun&lt;Fun,Arg&gt; 
118    make_void_ptr_indirect_fun( Fun f )
119    {
120        return void_ptr_indirect_fun&lt;Fun,Arg&gt;( f );
121    }
122         
123} // namespace 'boost' 
124</pre>
125<table class="docutils field-list" frame="void" rules="none">
126<col class="field-name" />
127<col class="field-body" />
128<tbody valign="top">
129<tr class="field"><th class="field-name">copyright:</th><td class="field-body">Thorsten Ottosen 2004-2005.</td>
130</tr>
131</tbody>
132</table>
133</div>
134</body>
135</html>
Note: See TracBrowser for help on using the repository browser.