technicalities.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Technicalities - Boost.GIL documentation</title>
  7. <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  8. <link rel="stylesheet" href="../_static/style.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '../',
  12. VERSION: '',
  13. COLLAPSE_MODINDEX: false,
  14. FILE_SUFFIX: '.html'
  15. };
  16. </script>
  17. <script type="text/javascript" src="../_static/jquery.js"></script>
  18. <script type="text/javascript" src="../_static/underscore.js"></script>
  19. <script type="text/javascript" src="../_static/doctools.js"></script>
  20. <link rel="index" title="Index" href="../genindex.html" />
  21. <link rel="search" title="Search" href="../search.html" />
  22. <link rel="top" title="Boost.GIL documentation" href="../index.html" />
  23. <link rel="up" title="Design Guide" href="index.html" />
  24. <link rel="next" title="Extending" href="extending.html" />
  25. <link rel="prev" title="Examples" href="examples.html" />
  26. </head>
  27. <body>
  28. <div class="header">
  29. <table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
  30. "header">
  31. <tr>
  32. <td valign="top" width="300">
  33. <h3><a href="../index.html"><img
  34. alt="C++ Boost" src="../_static/gil.png" border="0"></a></h3>
  35. </td>
  36. <td >
  37. <h1 align="center"><a href="../index.html"></a></h1>
  38. </td>
  39. <td>
  40. <div id="searchbox" style="display: none">
  41. <form class="search" action="../search.html" method="get">
  42. <input type="text" name="q" size="18" />
  43. <input type="submit" value="Search" />
  44. <input type="hidden" name="check_keywords" value="yes" />
  45. <input type="hidden" name="area" value="default" />
  46. </form>
  47. </div>
  48. <script type="text/javascript">$('#searchbox').show(0);</script>
  49. </td>
  50. </tr>
  51. </table>
  52. </div>
  53. <hr/>
  54. <div class="content">
  55. <div class="navbar" style="text-align:right;">
  56. <a class="prev" title="Examples" href="examples.html"><img src="../_static/prev.png" alt="prev"/></a>
  57. <a class="up" title="Design Guide" href="index.html"><img src="../_static/up.png" alt="up"/></a>
  58. <a class="next" title="Extending" href="extending.html"><img src="../_static/next.png" alt="next"/></a>
  59. </div>
  60. <div class="section" id="technicalities">
  61. <h1>Technicalities</h1>
  62. <div class="contents local topic" id="contents">
  63. <ul class="simple">
  64. <li><a class="reference internal" href="#creating-a-reference-proxy" id="id1">Creating a reference proxy</a></li>
  65. </ul>
  66. </div>
  67. <div class="section" id="creating-a-reference-proxy">
  68. <h2><a class="toc-backref" href="#id1">Creating a reference proxy</a></h2>
  69. <p>Sometimes it is necessary to create a proxy class that represents a
  70. reference to a given object. Examples of these are GIL&#8217;s reference to
  71. a planar pixel (<code class="docutils literal"><span class="pre">planar_pixel_reference</span></code>) and GIL&#8217;s sub-byte channel
  72. references. Writing a reference proxy class can be tricky. One problem
  73. is that the proxy reference is constructed as a temporary object and
  74. returned by value upon dereferencing the iterator:</p>
  75. <div class="highlight-cpp"><div class="highlight"><pre><span class="k">struct</span> <span class="n">rgb_planar_pixel_iterator</span>
  76. <span class="p">{</span>
  77. <span class="k">typedef</span> <span class="n">my_reference_proxy</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="n">reference</span><span class="p">;</span>
  78. <span class="n">reference</span> <span class="k">operator</span><span class="o">*</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">reference</span><span class="p">(</span><span class="n">red</span><span class="p">,</span><span class="n">green</span><span class="p">,</span><span class="n">blue</span><span class="p">);</span> <span class="p">}</span>
  79. <span class="p">};</span>
  80. </pre></div>
  81. </div>
  82. <p>The problem arises when an iterator is dereferenced directly into a
  83. function that takes a mutable pixel:</p>
  84. <div class="highlight-cpp"><div class="highlight"><pre><span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">Pixel</span><span class="o">&gt;</span> <span class="c1">// Models MutablePixelConcept</span>
  85. <span class="kt">void</span> <span class="n">invert_pixel</span><span class="p">(</span><span class="n">Pixel</span><span class="o">&amp;</span> <span class="n">p</span><span class="p">);</span>
  86. <span class="n">rgb_planar_pixel_iterator</span> <span class="n">myIt</span><span class="p">;</span>
  87. <span class="n">invert_pixel</span><span class="p">(</span><span class="o">*</span><span class="n">myIt</span><span class="p">);</span> <span class="c1">// compile error!</span>
  88. </pre></div>
  89. </div>
  90. <p>C++ does not allow for matching a temporary object against a non-constant
  91. reference. The solution is to:</p>
  92. <ul class="simple">
  93. <li>Use const qualifier on all members of the reference proxy object:</li>
  94. </ul>
  95. <div class="highlight-cpp"><div class="highlight"><pre><span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">T</span><span class="o">&gt;</span>
  96. <span class="k">struct</span> <span class="n">my_reference_proxy</span>
  97. <span class="p">{</span>
  98. <span class="k">const</span> <span class="n">my_reference_proxy</span><span class="o">&amp;</span> <span class="k">operator</span><span class="o">=</span><span class="p">(</span><span class="k">const</span> <span class="n">my_reference_proxy</span><span class="o">&amp;</span> <span class="n">p</span><span class="p">)</span> <span class="k">const</span><span class="p">;</span>
  99. <span class="k">const</span> <span class="n">my_reference_proxy</span><span class="o">*</span> <span class="k">operator</span><span class="o">-&gt;</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="k">this</span><span class="p">;</span> <span class="p">}</span>
  100. <span class="p">...</span>
  101. <span class="p">};</span>
  102. </pre></div>
  103. </div>
  104. <ul class="simple">
  105. <li>Use different classes to denote mutable and constant reference
  106. (maybe based on the constness of the template parameter)</li>
  107. <li>Define the reference type of your iterator with const qualifier:</li>
  108. </ul>
  109. <div class="highlight-cpp"><div class="highlight"><pre><span class="k">struct</span> <span class="n">iterator_traits</span><span class="o">&lt;</span><span class="n">rgb_planar_pixel_iterator</span><span class="o">&gt;</span>
  110. <span class="p">{</span>
  111. <span class="k">typedef</span> <span class="k">const</span> <span class="n">my_reference_proxy</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="n">reference</span><span class="p">;</span>
  112. <span class="p">};</span>
  113. </pre></div>
  114. </div>
  115. <p>A second important issue is providing an overload for <code class="docutils literal"><span class="pre">swap</span></code> for
  116. your reference class. The default <code class="docutils literal"><span class="pre">std::swap</span></code> will not work
  117. correctly. You must use a real value type as the temporary. A further
  118. complication is that in some implementations of the STL the <code class="docutils literal"><span class="pre">swap</span></code>
  119. function is incorrectly called qualified, as <code class="docutils literal"><span class="pre">std::swap</span></code>. The only
  120. way for these STL algorithms to use your overload is if you define it
  121. in the <code class="docutils literal"><span class="pre">std</span></code> namespace:</p>
  122. <div class="highlight-cpp"><div class="highlight"><pre><span class="k">namespace</span> <span class="n">std</span>
  123. <span class="p">{</span>
  124. <span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">T</span><span class="o">&gt;</span>
  125. <span class="kt">void</span> <span class="n">swap</span><span class="p">(</span><span class="n">my_reference_proxy</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;&amp;</span> <span class="n">x</span><span class="p">,</span> <span class="n">my_reference_proxy</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;&amp;</span> <span class="n">y</span><span class="p">)</span>
  126. <span class="p">{</span>
  127. <span class="n">my_value</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="n">tmp</span><span class="o">=</span><span class="n">x</span><span class="p">;</span>
  128. <span class="n">x</span><span class="o">=</span><span class="n">y</span><span class="p">;</span>
  129. <span class="n">y</span><span class="o">=</span><span class="n">tmp</span><span class="p">;</span>
  130. <span class="p">}</span>
  131. <span class="p">}</span>
  132. </pre></div>
  133. </div>
  134. <p>Lastly, remember that constructors and copy-constructors of proxy
  135. references are always shallow and assignment operators are deep.</p>
  136. <p>We are grateful to Dave Abrahams, Sean Parent and Alex Stepanov for
  137. suggesting the above solution.</p>
  138. </div>
  139. </div>
  140. <div class="navbar" style="text-align:right;">
  141. <a class="prev" title="Examples" href="examples.html"><img src="../_static/prev.png" alt="prev"/></a>
  142. <a class="up" title="Design Guide" href="index.html"><img src="../_static/up.png" alt="up"/></a>
  143. <a class="next" title="Extending" href="extending.html"><img src="../_static/next.png" alt="next"/></a>
  144. </div>
  145. </div>
  146. <div class="footer" role="contentinfo">
  147. Last updated on 2019-12-10 00:12:10.
  148. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.6.
  149. </div>
  150. </body>
  151. </html>