indirect_fun.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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.10: http://docutils.sourceforge.net/" />
  7. <title>Boost Pointer Container Library</title>
  8. <style type="text/css">
  9. /*
  10. :Author: David Goodger
  11. :Contact: goodger@users.sourceforge.net
  12. :Date: $Date$
  13. :Revision: $Revision$
  14. :Copyright: This stylesheet has been placed in the public domain.
  15. Default cascading style sheet for the HTML output of Docutils.
  16. See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
  17. customize this style sheet.
  18. */
  19. /* "! important" is used here to override other ``margin-top`` and
  20. ``margin-bottom`` styles that are later in the stylesheet or
  21. more specific. See http://www.w3.org/TR/CSS1#the-cascade */
  22. .first {
  23. margin-top: 0 ! important }
  24. .last, .with-subtitle {
  25. margin-bottom: 0 ! important }
  26. .hidden {
  27. display: none }
  28. a.toc-backref {
  29. text-decoration: none ;
  30. color: black }
  31. blockquote.epigraph {
  32. margin: 2em 5em ; }
  33. dl.docutils dd {
  34. margin-bottom: 0.5em }
  35. /* Uncomment (and remove this text!) to get bold-faced definition list terms
  36. dl.docutils dt {
  37. font-weight: bold }
  38. */
  39. div.abstract {
  40. margin: 2em 5em }
  41. div.abstract p.topic-title {
  42. font-weight: bold ;
  43. text-align: center }
  44. div.admonition, div.attention, div.caution, div.danger, div.error,
  45. div.hint, div.important, div.note, div.tip, div.warning {
  46. margin: 2em ;
  47. border: medium outset ;
  48. padding: 1em }
  49. div.admonition p.admonition-title, div.hint p.admonition-title,
  50. div.important p.admonition-title, div.note p.admonition-title,
  51. div.tip p.admonition-title {
  52. font-weight: bold ;
  53. font-family: sans-serif }
  54. div.attention p.admonition-title, div.caution p.admonition-title,
  55. div.danger p.admonition-title, div.error p.admonition-title,
  56. div.warning p.admonition-title {
  57. color: red ;
  58. font-weight: bold ;
  59. font-family: sans-serif }
  60. /* Uncomment (and remove this text!) to get reduced vertical space in
  61. compound paragraphs.
  62. div.compound .compound-first, div.compound .compound-middle {
  63. margin-bottom: 0.5em }
  64. div.compound .compound-last, div.compound .compound-middle {
  65. margin-top: 0.5em }
  66. */
  67. div.dedication {
  68. margin: 2em 5em ;
  69. text-align: center ;
  70. font-style: italic }
  71. div.dedication p.topic-title {
  72. font-weight: bold ;
  73. font-style: normal }
  74. div.figure {
  75. margin-left: 2em }
  76. div.footer, div.header {
  77. clear: both;
  78. font-size: smaller }
  79. div.line-block {
  80. display: block ;
  81. margin-top: 1em ;
  82. margin-bottom: 1em }
  83. div.line-block div.line-block {
  84. margin-top: 0 ;
  85. margin-bottom: 0 ;
  86. margin-left: 1.5em }
  87. div.sidebar {
  88. margin-left: 1em ;
  89. border: medium outset ;
  90. padding: 1em ;
  91. background-color: #ffffee ;
  92. width: 40% ;
  93. float: right ;
  94. clear: right }
  95. div.sidebar p.rubric {
  96. font-family: sans-serif ;
  97. font-size: medium }
  98. div.system-messages {
  99. margin: 5em }
  100. div.system-messages h1 {
  101. color: red }
  102. div.system-message {
  103. border: medium outset ;
  104. padding: 1em }
  105. div.system-message p.system-message-title {
  106. color: red ;
  107. font-weight: bold }
  108. div.topic {
  109. margin: 2em }
  110. h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
  111. h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
  112. margin-top: 0.4em }
  113. h1.title {
  114. text-align: center }
  115. h2.subtitle {
  116. text-align: center }
  117. hr.docutils {
  118. width: 75% }
  119. img.align-left {
  120. clear: left }
  121. img.align-right {
  122. clear: right }
  123. img.borderless {
  124. border: 0 }
  125. ol.simple, ul.simple {
  126. margin-bottom: 1em }
  127. ol.arabic {
  128. list-style: decimal }
  129. ol.loweralpha {
  130. list-style: lower-alpha }
  131. ol.upperalpha {
  132. list-style: upper-alpha }
  133. ol.lowerroman {
  134. list-style: lower-roman }
  135. ol.upperroman {
  136. list-style: upper-roman }
  137. p.attribution {
  138. text-align: right ;
  139. margin-left: 50% }
  140. p.caption {
  141. font-style: italic }
  142. p.credits {
  143. font-style: italic ;
  144. font-size: smaller }
  145. p.label {
  146. white-space: nowrap }
  147. p.rubric {
  148. font-weight: bold ;
  149. font-size: larger ;
  150. color: maroon ;
  151. text-align: center }
  152. p.sidebar-title {
  153. font-family: sans-serif ;
  154. font-weight: bold ;
  155. font-size: larger }
  156. p.sidebar-subtitle {
  157. font-family: sans-serif ;
  158. font-weight: bold }
  159. p.topic-title {
  160. font-weight: bold }
  161. pre.address {
  162. margin-bottom: 0 ;
  163. margin-top: 0 ;
  164. font-family: serif ;
  165. font-size: 100% }
  166. pre.line-block {
  167. font-family: serif ;
  168. font-size: 100% }
  169. pre.literal-block, pre.doctest-block {
  170. margin-left: 2em ;
  171. margin-right: 2em ;
  172. background-color: #eeeeee }
  173. span.classifier {
  174. font-family: sans-serif ;
  175. font-style: oblique }
  176. span.classifier-delimiter {
  177. font-family: sans-serif ;
  178. font-weight: bold }
  179. span.interpreted {
  180. font-family: sans-serif }
  181. span.option {
  182. white-space: nowrap }
  183. span.pre {
  184. white-space: pre }
  185. span.problematic {
  186. color: red }
  187. span.section-subtitle {
  188. /* font-size relative to parent (h1..h6 element) */
  189. font-size: 80% }
  190. table.citation {
  191. border-left: solid thin gray }
  192. table.docinfo {
  193. margin: 2em 4em }
  194. table.docutils {
  195. margin-top: 0.5em ;
  196. margin-bottom: 0.5em }
  197. table.footnote {
  198. border-left: solid thin black }
  199. table.docutils td, table.docutils th,
  200. table.docinfo td, table.docinfo th {
  201. padding-left: 0.5em ;
  202. padding-right: 0.5em ;
  203. vertical-align: top }
  204. table.docutils th.field-name, table.docinfo th.docinfo-name {
  205. font-weight: bold ;
  206. text-align: left ;
  207. white-space: nowrap ;
  208. padding-left: 0 }
  209. h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
  210. h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
  211. font-size: 100% }
  212. tt.docutils {
  213. background-color: #eeeeee }
  214. ul.auto-toc {
  215. list-style-type: none }
  216. </style>
  217. </head>
  218. <body>
  219. <div class="document" id="boost-pointer-container-library">
  220. <h1 class="title"><img alt="Boost" src="boost.png" /> Pointer Container Library</h1>
  221. <h2 class="subtitle" id="indirected-functions">Indirected functions</h2>
  222. <p>It is quite common that we have two pointers and what to compare the
  223. pointed to objects. Also, we have usually already defined how
  224. to compare the objects. So to avoid some tedious boiler-plate code
  225. this library defines predicates that apply an indirection before comparing.</p>
  226. <p>When the container uses <tt class="docutils literal"><span class="pre">void*</span></tt> internally, we can use the
  227. class <tt class="docutils literal"><span class="pre">void_ptr_indirect_fun</span></tt>; otherwise we use the class
  228. <tt class="docutils literal"><span class="pre">indirect_fun</span></tt>.</p>
  229. <p><strong>Example:</strong></p>
  230. <pre class="literal-block">
  231. std::string* bar = new std::string(&quot;bar&quot;);
  232. std::string* foo = new std::string(&quot;foo&quot;);
  233. BOOST_ASSERT( indirect_fun&lt; std::less&lt;std::string&gt; &gt;()( bar, foo ) == true );
  234. BOOST_ASSERT( make_indirect_fun( std::less&lt;std::string&gt;() )( foo, bar ) == false );
  235. void* vptr1 = ptr1;
  236. void* vptr2 = ptr2;
  237. void_ptr_indirect_fun&lt; std::less&lt;std::string&gt;, std::string&gt; cast_fun;
  238. BOOST_CHECK( cast_fun( vptr1, vptr2 ) == true );
  239. </pre>
  240. <p><strong>See also:</strong></p>
  241. <ul class="simple">
  242. <li><a class="reference" href="http://www.boost.org/libs/utility/utility.htm#result_of">result_of</a></li>
  243. <li><a class="reference" href="http://www.boost.org/libs/iterator/doc/pointee.html">pointee</a></li>
  244. <li><a class="reference" href="ptr_set.html">ptr_set</a></li>
  245. <li><a class="reference" href="ptr_multiset.html">ptr_multiset</a></li>
  246. </ul>
  247. <p><strong>Navigate</strong></p>
  248. <ul class="simple">
  249. <li><a class="reference" href="ptr_container.html">home</a></li>
  250. <li><a class="reference" href="reference.html">reference</a></li>
  251. </ul>
  252. <p><strong>Remarks:</strong></p>
  253. <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>
  254. because 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>
  255. <p><strong>Synopsis:</strong></p>
  256. <p>Since the definition of the predicates is somewhat trivial, only the
  257. first operation is expanded inline.</p>
  258. <pre class="literal-block">
  259. namespace boost
  260. {
  261. template&lt; class Fun &gt;
  262. struct indirect_fun
  263. {
  264. indirect_fun() : fun(Fun())
  265. { }
  266. indirect_fun( Fun f ) : fun(f)
  267. { }
  268. template&lt; class T &gt;
  269. typename result_of&lt; Fun( typename pointee&lt;T&gt;::type ) &gt;::type
  270. operator()( const T&amp; r ) const
  271. {
  272. return fun( *r );
  273. }
  274. template&lt; class T, class U &gt;
  275. typename result_of&lt; Fun( typename pointee&lt;T&gt;::type,
  276. typename pointee&lt;U&gt;::type ) &gt;::type
  277. operator()( const T&amp; r, const U&amp; r2 ) const
  278. {
  279. return fun( *r, *r2 );
  280. }
  281. private:
  282. Fun fun;
  283. };
  284. template&lt; class Fun &gt;
  285. inline indirect_fun&lt;Fun&gt; make_indirect_fun( Fun f )
  286. {
  287. return indirect_fun&lt;Fun&gt;( f );
  288. }
  289. template&lt; class Fun, class Arg1, class Arg2 = Arg1 &gt;
  290. struct void_ptr_indirect_fun
  291. {
  292. void_ptr_indirect_fun() : fun(Fun())
  293. { }
  294. void_ptr_indirect_fun( Fun f ) : fun(f)
  295. { }
  296. typename result_of&lt; Fun( Arg1 ) &gt;::type
  297. operator()( const void* r ) const
  298. {
  299. return fun( * static_cast&lt;const Arg1*&gt;( r ) );
  300. }
  301. typename result_of&lt; Fun( Arg1, Arg2 ) &gt;::type
  302. operator()( const void* l, const void* r ) const
  303. {
  304. return fun( * static_cast&lt;const Arg1*&gt;( l ), * static_cast&lt;const Arg2*&gt;( r ) );
  305. }
  306. private:
  307. Fun fun;
  308. };
  309. template&lt; class Fun, class Arg &gt;
  310. inline void_ptr_indirect_fun&lt;Fun,Arg&gt;
  311. make_void_ptr_indirect_fun( Fun f )
  312. {
  313. return void_ptr_indirect_fun&lt;Fun,Arg&gt;( f );
  314. }
  315. } // namespace 'boost'
  316. </pre>
  317. <hr><table class="docutils field-list" frame="void" rules="none">
  318. <col class="field-name" />
  319. <col class="field-body" />
  320. <tbody valign="top">
  321. <tr class="field"><th class="field-name">Copyright:</th><td class="field-body">Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see <a class="reference" href="http://www.boost.org/LICENSE_1_0.txt">LICENSE_1_0.txt</a>).</td>
  322. </tr>
  323. </tbody>
  324. </table>
  325. </div>
  326. </body>
  327. </html>