1 ++++++++++++++++++++++++++++++++++
2 |Boost| Pointer Container Library
3 ++++++++++++++++++++++++++++++++++
5 .. |Boost| image:: boost.png
10 It is quite common that we have two pointers and what to compare the
11 pointed to objects. Also, we have usually already defined how
12 to compare the objects. So to avoid some tedious boiler-plate code
13 this library defines predicates that apply an indirection before comparing.
15 When the container uses ``void*`` internally, we can use the
16 class ``void_ptr_indirect_fun``; otherwise we use the class
21 std::string* bar = new std::string("bar");
22 std::string* foo = new std::string("foo");
23 BOOST_ASSERT( indirect_fun< std::less<std::string> >()( bar, foo ) == true );
24 BOOST_ASSERT( make_indirect_fun( std::less<std::string>() )( foo, bar ) == false );
28 void_ptr_indirect_fun< std::less<std::string>, std::string> cast_fun;
29 BOOST_CHECK( cast_fun( vptr1, vptr2 ) == true );
33 - `result_of <http://www.boost.org/libs/utility/utility.htm#result_of>`_
34 - `pointee <http://www.boost.org/libs/iterator/doc/pointee.html>`_
35 - `ptr_set <ptr_set.html>`_
36 - `ptr_multiset <ptr_multiset.html>`_
40 - `home <ptr_container.html>`_
41 - `reference <reference.html>`_
45 The class ``indirect_fun`` will work with smart pointers such as `boost::shared_ptr<T> <http://www.boost.org/libs/smart_ptr/shared_ptr.htm>`_
46 because of the type traits ``pointee<T>::type`` from the header ``<boost/pointee.hpp>``.
50 Since the definition of the predicates is somewhat trivial, only the
51 first operation is expanded inline.
61 indirect_fun() : fun(Fun())
64 indirect_fun( Fun f ) : fun(f)
68 typename result_of< Fun( typename pointee<T>::type ) >::type
69 operator()( const T& r ) const
74 template< class T, class U >
75 typename result_of< Fun( typename pointee<T>::type,
76 typename pointee<U>::type ) >::type
77 operator()( const T& r, const U& r2 ) const
79 return fun( *r, *r2 );
87 inline indirect_fun<Fun> make_indirect_fun( Fun f )
89 return indirect_fun<Fun>( f );
94 template< class Fun, class Arg1, class Arg2 = Arg1 >
95 struct void_ptr_indirect_fun
97 void_ptr_indirect_fun() : fun(Fun())
100 void_ptr_indirect_fun( Fun f ) : fun(f)
103 typename result_of< Fun( Arg1 ) >::type
104 operator()( const void* r ) const
106 return fun( * static_cast<const Arg1*>( r ) );
109 typename result_of< Fun( Arg1, Arg2 ) >::type
110 operator()( const void* l, const void* r ) const
112 return fun( * static_cast<const Arg1*>( l ), * static_cast<const Arg2*>( r ) );
119 template< class Fun, class Arg >
120 inline void_ptr_indirect_fun<Fun,Arg>
121 make_void_ptr_indirect_fun( Fun f )
123 return void_ptr_indirect_fun<Fun,Arg>( f );
126 } // namespace 'boost'
132 :Copyright: Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see LICENSE_1_0.txt__).
134 __ http://www.boost.org/LICENSE_1_0.txt