Skip to content

Commit c25dfee

Browse files
committed
Update callback's documentation.
1 parent 20f0894 commit c25dfee

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

4.x/_sources/bindings/callbacks.rst.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ Remember that C style callbacks are simply function pointers, they do not have a
9191

9292
Simple Callback
9393
"""""""""""""""
94-
In the simplest case, a callback is only used once in a code base. Thus the ``Proc`` can be associated with the function pointer type. This is exactly what Rice does via the `NativeCallbackSimple <https://github.com/ruby-rice/rice/blob/master/rice/detail/NativeCallbackSimple.hpp>`_ template. Each callback signature creates a new C++ class from the NativeCallback template. The created class has a static member field that stores the ``Proc``.
94+
In the simplest case, a callback is only used once in a code base. Thus there is a one-to-one mapping between a callback and its associated Ruby ``Proc``.
95+
96+
This is easy to implement - Rice generates a new C++ class based on the callback's signature using the `NativeCallbackSimple <https://github.com/ruby-rice/rice/blob/master/rice/detail/NativeCallbackSimple.hpp>`_ class template. The generated class has a static member field that stores the ``Proc``. Thus every callback is associated with a single instantiation of the `NativeCallbackSimple` template.
9597

9698
LibFFI Callback
9799
"""""""""""""""
@@ -102,10 +104,10 @@ However, a library often times use a callback in multiple places. For example:
102104
void setMouseClickCallback(MouseCallback_T onSingleClick, MouseCallback_T onDoubleClick, );
103105
void setMouseEnterExitCallback(MouseCallback_T onEnterExit);
104106
105-
The above code uses the same callback type 3 different times, thus the one-to-one mapping between callback type and C++ class is broken. Intead, we want a callback type to map to 3 different Ruby ``Procs``.
107+
The above code uses the same callback type 3 different times, thus the one-to-one mapping between callback type and C++ class is broken. Therefore the simple solution of using a static member variable to store the Ruby proc no longer works. Instead, we need to store 3 different ``Procs`` and figure out which one to call when the callback is invoked.
106108

107-
In this case, Ruby can use libffi's `closure <https://github.com/libffi/libffi/blob/master/src/closures.c>`_ API. The closure API associates a piece of user data with a callback and then dynamically generates a new function that the callback invokes.
109+
In this case, Rice uses libffi's `closure <https://github.com/libffi/libffi/blob/master/src/closures.c>`_ API. The closure API associates a piece of user data, in this case the ``Proc``, with a callback and then dynamically generates a new function which is what is invoked by the callback function.
108110

109-
Since you are working with Ruby, it is highly likely that you have LibFFI installed since the `Fiddle <https://github.com/ruby/fiddle>`_ gem requires it. The default build script will check for LibFFI and if it is found compile it into your bindings.
111+
Since you are working with Ruby, it is highly likely that LibFFI is already installed since the `Fiddle <https://github.com/ruby/fiddle>`_ gem requires it. The default build script will check for LibFFI and if it is found compile it into your bindings.
110112

111-
If you are using CMake, you will need to define a C++ preprocessor define called ``HAVE_LIBFFI`` and link to libffi.
113+
If you are using CMake, you will need to add a C++ preprocessor define called ``HAVE_LIBFFI`` and link to libffi.

4.x/_sources/bindings/operators.rst.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ C++ classes that support the ``()`` operator are known as functors. Ruby support
126126

127127
Conversion Operators
128128
--------------------
129-
C++ allows uses to create both explicit and implicit converion operators or functions. These are used to convert a class to a different types. For example:
129+
C++ allows users to define explicit and implicit conversion operators or functions. These are used to convert a class to a different types. For example:
130130

131131
.. code-block:: cpp
132132

4.x/bindings/callbacks.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ <h3>Associating Callbacks with Procs<a class="headerlink" href="#associating-cal
360360
<p>Remember that C style callbacks are simply function pointers, they do not have a place to store any state. Thus when a C style callback is invoked, Rice needs to determine what Ruby <code class="docutils literal notranslate"><span class="pre">Proc</span></code> to call. This is a tricky problem to solve.</p>
361361
<section id="simple-callback">
362362
<h4>Simple Callback<a class="headerlink" href="#simple-callback" title="Link to this heading"></a></h4>
363-
<p>In the simplest case, a callback is only used once in a code base. Thus the <code class="docutils literal notranslate"><span class="pre">Proc</span></code> can be associated with the function pointer type. This is exactly what Rice does via the <a class="reference external" href="https://github.com/ruby-rice/rice/blob/master/rice/detail/NativeCallbackSimple.hpp">NativeCallbackSimple</a> template. Each callback signature creates a new C++ class from the NativeCallback template. The created class has a static member field that stores the <code class="docutils literal notranslate"><span class="pre">Proc</span></code>.</p>
363+
<p>In the simplest case, a callback is only used once in a code base. Thus there is a one-to-one mapping between a callback and its associated Ruby <code class="docutils literal notranslate"><span class="pre">Proc</span></code>.</p>
364+
<p>This is easy to implement - Rice generates a new C++ class based on the callback’s signature using the <a class="reference external" href="https://github.com/ruby-rice/rice/blob/master/rice/detail/NativeCallbackSimple.hpp">NativeCallbackSimple</a> class template. The generated class has a static member field that stores the <code class="docutils literal notranslate"><span class="pre">Proc</span></code>. Thus every callback is associated with a single instantiation of the <cite>NativeCallbackSimple</cite> template.</p>
364365
</section>
365366
<section id="libffi-callback">
366367
<h4>LibFFI Callback<a class="headerlink" href="#libffi-callback" title="Link to this heading"></a></h4>
@@ -369,10 +370,10 @@ <h4>LibFFI Callback<a class="headerlink" href="#libffi-callback" title="Link to
369370
<span class="kt">void</span><span class="w"> </span><span class="nf">setMouseEnterExitCallback</span><span class="p">(</span><span class="n">MouseCallback_T</span><span class="w"> </span><span class="n">onEnterExit</span><span class="p">);</span>
370371
</pre></div>
371372
</div>
372-
<p>The above code uses the same callback type 3 different times, thus the one-to-one mapping between callback type and C++ class is broken. Intead, we want a callback type to map to 3 different Ruby <code class="docutils literal notranslate"><span class="pre">Procs</span></code>.</p>
373-
<p>In this case, Ruby can use libffi’s <a class="reference external" href="https://github.com/libffi/libffi/blob/master/src/closures.c">closure</a> API. The closure API associates a piece of user data with a callback and then dynamically generates a new function that the callback invokes.</p>
374-
<p>Since you are working with Ruby, it is highly likely that you have LibFFI installed since the <a class="reference external" href="https://github.com/ruby/fiddle">Fiddle</a> gem requires it. The default build script will check for LibFFI and if it is found compile it into your bindings.</p>
375-
<p>If you are using CMake, you will need to define a C++ preprocessor define called <code class="docutils literal notranslate"><span class="pre">HAVE_LIBFFI</span></code> and link to libffi.</p>
373+
<p>The above code uses the same callback type 3 different times, thus the one-to-one mapping between callback type and C++ class is broken. Therefore the simple solution of using a static member variable to store the Ruby proc no longer works. Instead, we need to store 3 different <code class="docutils literal notranslate"><span class="pre">Procs</span></code> and figure out which one to call when the callback is invoked.</p>
374+
<p>In this case, Rice uses libffi’s <a class="reference external" href="https://github.com/libffi/libffi/blob/master/src/closures.c">closure</a> API. The closure API associates a piece of user data, in this case the <code class="docutils literal notranslate"><span class="pre">Proc</span></code>, with a callback and then dynamically generates a new function which is what is invoked by the callback function.</p>
375+
<p>Since you are working with Ruby, it is highly likely that LibFFI is already installed since the <a class="reference external" href="https://github.com/ruby/fiddle">Fiddle</a> gem requires it. The default build script will check for LibFFI and if it is found compile it into your bindings.</p>
376+
<p>If you are using CMake, you will need to add a C++ preprocessor define called <code class="docutils literal notranslate"><span class="pre">HAVE_LIBFFI</span></code> and link to libffi.</p>
376377
</section>
377378
</section>
378379
</section>

4.x/bindings/operators.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ <h2>Other Operators<a class="headerlink" href="#other-operators" title="Link to
506506
</section>
507507
<section id="conversion-operators">
508508
<h2>Conversion Operators<a class="headerlink" href="#conversion-operators" title="Link to this heading"></a></h2>
509-
<p>C++ allows uses to create both explicit and implicit converion operators or functions. These are used to convert a class to a different types. For example:</p>
509+
<p>C++ allows users to define explicit and implicit conversion operators or functions. These are used to convert a class to a different types. For example:</p>
510510
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">Money</span>
511511
<span class="p">{</span>
512512
<span class="k">public</span><span class="o">:</span>

4.x/searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)