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