| 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 |
|---|
| 15 | pointed to objects. Also, we have usually already defined how |
|---|
| 16 | to compare the objects. So to avoid some tedious boiler-plate code |
|---|
| 17 | this 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 |
|---|
| 19 | class <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"> |
|---|
| 23 | std::string* bar = new std::string("bar"); |
|---|
| 24 | std::string* foo = new std::string("foo"); |
|---|
| 25 | BOOST_ASSERT( indirect_fun< std::less<std::string> >()( bar, foo ) == true ); |
|---|
| 26 | BOOST_ASSERT( make_indirect_fun( std::less<std::string>() )( foo, bar ) == false ); |
|---|
| 27 | |
|---|
| 28 | void* vptr1 = ptr1; |
|---|
| 29 | void* vptr2 = ptr2; |
|---|
| 30 | void_ptr_indirect_fun< std::less<std::string>, std::string> cast_fun; |
|---|
| 31 | BOOST_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<T></a> |
|---|
| 47 | because of the type traits <tt class="docutils literal"><span class="pre">pointee<T>::type</span></tt> from the header <tt class="docutils literal"><span class="pre"><boost/pointee.hpp></span></tt>.</p> |
|---|
| 48 | <p><strong>Synopsis:</strong></p> |
|---|
| 49 | <p>Since the definition of the predicates is somewhat trivial, only the |
|---|
| 50 | first operation is expanded inline.</p> |
|---|
| 51 | <pre class="literal-block"> |
|---|
| 52 | namespace boost |
|---|
| 53 | { |
|---|
| 54 | |
|---|
| 55 | template< class Fun > |
|---|
| 56 | struct indirect_fun |
|---|
| 57 | { |
|---|
| 58 | indirect_fun() : fun(Fun()) |
|---|
| 59 | { } |
|---|
| 60 | |
|---|
| 61 | indirect_fun( Fun f ) : fun(f) |
|---|
| 62 | { } |
|---|
| 63 | |
|---|
| 64 | template< class T > |
|---|
| 65 | typename result_of< Fun( typename pointee<T>::type ) >::type |
|---|
| 66 | operator()( const T& r ) const |
|---|
| 67 | { |
|---|
| 68 | return fun( *r ); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | template< class T, class U > |
|---|
| 72 | typename result_of< Fun( typename pointee<T>::type, |
|---|
| 73 | typename pointee<U>::type ) >::type |
|---|
| 74 | operator()( const T& r, const U& r2 ) const |
|---|
| 75 | { |
|---|
| 76 | return fun( *r, *r2 ); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | private: |
|---|
| 80 | Fun fun; |
|---|
| 81 | }; |
|---|
| 82 | |
|---|
| 83 | template< class Fun > |
|---|
| 84 | inline indirect_fun<Fun> make_indirect_fun( Fun f ) |
|---|
| 85 | { |
|---|
| 86 | return indirect_fun<Fun>( f ); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | template< class Fun, class Arg1, class Arg2 = Arg1 > |
|---|
| 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< Fun( Arg1 ) >::type |
|---|
| 101 | operator()( const void* r ) const |
|---|
| 102 | { |
|---|
| 103 | return fun( * static_cast<const Arg1*>( r ) ); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | typename result_of< Fun( Arg1, Arg2 ) >::type |
|---|
| 107 | operator()( const void* l, const void* r ) const |
|---|
| 108 | { |
|---|
| 109 | return fun( * static_cast<const Arg1*>( l ), * static_cast<const Arg2*>( r ) ); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | private: |
|---|
| 113 | Fun fun; |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | template< class Fun, class Arg > |
|---|
| 117 | inline void_ptr_indirect_fun<Fun,Arg> |
|---|
| 118 | make_void_ptr_indirect_fun( Fun f ) |
|---|
| 119 | { |
|---|
| 120 | return void_ptr_indirect_fun<Fun,Arg>( 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> |
|---|