indirect_fun.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. ++++++++++++++++++++++++++++++++++
  2. |Boost| Pointer Container Library
  3. ++++++++++++++++++++++++++++++++++
  4. .. |Boost| image:: boost.png
  5. Indirected functions
  6. --------------------
  7. It is quite common that we have two pointers and what to compare the
  8. pointed to objects. Also, we have usually already defined how
  9. to compare the objects. So to avoid some tedious boiler-plate code
  10. this library defines predicates that apply an indirection before comparing.
  11. When the container uses ``void*`` internally, we can use the
  12. class ``void_ptr_indirect_fun``; otherwise we use the class
  13. ``indirect_fun``.
  14. **Example:** ::
  15. std::string* bar = new std::string("bar");
  16. std::string* foo = new std::string("foo");
  17. BOOST_ASSERT( indirect_fun< std::less<std::string> >()( bar, foo ) == true );
  18. BOOST_ASSERT( make_indirect_fun( std::less<std::string>() )( foo, bar ) == false );
  19. void* vptr1 = ptr1;
  20. void* vptr2 = ptr2;
  21. void_ptr_indirect_fun< std::less<std::string>, std::string> cast_fun;
  22. BOOST_CHECK( cast_fun( vptr1, vptr2 ) == true );
  23. **See also:**
  24. - `result_of <http://www.boost.org/libs/utility/utility.htm#result_of>`_
  25. - `pointee <http://www.boost.org/libs/iterator/doc/pointee.html>`_
  26. - `ptr_set <ptr_set.html>`_
  27. - `ptr_multiset <ptr_multiset.html>`_
  28. **Navigate**
  29. - `home <ptr_container.html>`_
  30. - `reference <reference.html>`_
  31. **Remarks:**
  32. 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>`_
  33. because of the type traits ``pointee<T>::type`` from the header ``<boost/pointee.hpp>``.
  34. **Synopsis:**
  35. Since the definition of the predicates is somewhat trivial, only the
  36. first operation is expanded inline.
  37. ::
  38. namespace boost
  39. {
  40. template< class Fun >
  41. struct indirect_fun
  42. {
  43. indirect_fun() : fun(Fun())
  44. { }
  45. indirect_fun( Fun f ) : fun(f)
  46. { }
  47. template< class T >
  48. typename result_of< Fun( typename pointee<T>::type ) >::type
  49. operator()( const T& r ) const
  50. {
  51. return fun( *r );
  52. }
  53. template< class T, class U >
  54. typename result_of< Fun( typename pointee<T>::type,
  55. typename pointee<U>::type ) >::type
  56. operator()( const T& r, const U& r2 ) const
  57. {
  58. return fun( *r, *r2 );
  59. }
  60. private:
  61. Fun fun;
  62. };
  63. template< class Fun >
  64. inline indirect_fun<Fun> make_indirect_fun( Fun f )
  65. {
  66. return indirect_fun<Fun>( f );
  67. }
  68. template< class Fun, class Arg1, class Arg2 = Arg1 >
  69. struct void_ptr_indirect_fun
  70. {
  71. void_ptr_indirect_fun() : fun(Fun())
  72. { }
  73. void_ptr_indirect_fun( Fun f ) : fun(f)
  74. { }
  75. typename result_of< Fun( Arg1 ) >::type
  76. operator()( const void* r ) const
  77. {
  78. return fun( * static_cast<const Arg1*>( r ) );
  79. }
  80. typename result_of< Fun( Arg1, Arg2 ) >::type
  81. operator()( const void* l, const void* r ) const
  82. {
  83. return fun( * static_cast<const Arg1*>( l ), * static_cast<const Arg2*>( r ) );
  84. }
  85. private:
  86. Fun fun;
  87. };
  88. template< class Fun, class Arg >
  89. inline void_ptr_indirect_fun<Fun,Arg>
  90. make_void_ptr_indirect_fun( Fun f )
  91. {
  92. return void_ptr_indirect_fun<Fun,Arg>( f );
  93. }
  94. } // namespace 'boost'
  95. .. raw:: html
  96. <hr>
  97. :Copyright: Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see LICENSE_1_0.txt__).
  98. __ http://www.boost.org/LICENSE_1_0.txt