• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1++++++++++++++++++++++++++++++++++
2 |Boost| Pointer Container Library
3++++++++++++++++++++++++++++++++++
4
5.. |Boost| image:: boost.png
6
7Indirected functions
8--------------------
9
10It is quite common that we have two pointers and what to compare the
11pointed to objects. Also, we have usually already defined how
12to compare the objects. So to avoid some tedious boiler-plate code
13this library defines predicates that apply an indirection before comparing.
14
15When the container uses ``void*`` internally, we can use the
16class ``void_ptr_indirect_fun``; otherwise we use the class
17``indirect_fun``.
18
19**Example:** ::
20
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 );
25
26    void*       vptr1  = ptr1;
27    void*       vptr2  = ptr2;
28    void_ptr_indirect_fun< std::less<std::string>, std::string> cast_fun;
29    BOOST_CHECK( cast_fun( vptr1, vptr2 ) == true );
30
31**See also:**
32
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>`_
37
38**Navigate**
39
40- `home <ptr_container.html>`_
41- `reference <reference.html>`_
42
43**Remarks:**
44
45The class ``indirect_fun`` will work with smart pointers such as `boost::shared_ptr<T> <http://www.boost.org/libs/smart_ptr/shared_ptr.htm>`_
46because of the type traits ``pointee<T>::type`` from the header ``<boost/pointee.hpp>``.
47
48**Synopsis:**
49
50Since the definition of the predicates is somewhat trivial, only the
51first operation is expanded inline.
52
53::
54
55        namespace boost
56        {
57
58            template< class Fun >
59            struct indirect_fun
60            {
61                indirect_fun() : fun(Fun())
62                { }
63
64                indirect_fun( Fun f ) : fun(f)
65                { }
66
67                template< class T >
68                typename result_of< Fun( typename pointee<T>::type ) >::type
69                operator()( const T& r ) const
70                {
71                    return fun( *r );
72                }
73
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
78                {
79                    return fun( *r, *r2 );
80                }
81
82            private:
83                Fun fun;
84            };
85
86            template< class Fun >
87            inline indirect_fun<Fun> make_indirect_fun( Fun f )
88            {
89                return indirect_fun<Fun>( f );
90            }
91
92
93
94            template< class Fun, class Arg1, class Arg2 = Arg1 >
95            struct void_ptr_indirect_fun
96            {
97                void_ptr_indirect_fun() : fun(Fun())
98                { }
99
100                void_ptr_indirect_fun( Fun f ) : fun(f)
101                { }
102
103                typename result_of< Fun( Arg1 ) >::type
104                operator()( const void* r ) const
105                {
106                    return fun( * static_cast<const Arg1*>( r ) );
107                }
108
109                typename result_of< Fun( Arg1, Arg2 ) >::type
110                operator()( const void* l, const void* r ) const
111                {
112                    return fun( * static_cast<const Arg1*>( l ), * static_cast<const Arg2*>( r ) );
113                }
114
115            private:
116                Fun fun;
117            };
118
119            template< class Fun, class Arg >
120            inline void_ptr_indirect_fun<Fun,Arg>
121            make_void_ptr_indirect_fun( Fun f )
122            {
123                return void_ptr_indirect_fun<Fun,Arg>( f );
124            }
125
126        } // namespace 'boost'
127
128.. raw:: html
129
130        <hr>
131
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__).
133
134__ http://www.boost.org/LICENSE_1_0.txt
135
136
137