Skip to content

Commit 8fdfdce

Browse files
committed
Update docs for 4.5 release.
1 parent c25dfee commit 8fdfdce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+280
-206
lines changed

4.x/_sources/bindings.rst.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Bindings
1414
bindings/enums
1515
bindings/constants
1616
bindings/iterators
17+
bindings/arrays
1718
bindings/class_templates
1819
bindings/operators
1920
bindings/memory_management

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ To wrap this code in Rice, first expose the register function to Ruby:
3333
rb_mCv.define_module_function("set_mouse_callback", &cv::setMouseCallback,
3434
Arg("winname"), Arg("on_mouse"), Arg("userdata") = static_cast<void *>(0));
3535
36-
Next, in Ruby, define a Proc to handle the callback and then call the resgister exposed register function:
36+
Next, in Ruby, define a Proc to handle the callback and then call the register exposed register function:
3737

3838
.. code-block:: ruby
3939
@@ -46,18 +46,18 @@ Next, in Ruby, define a Proc to handle the callback and then call the resgister
4646
4747
# Register the proc
4848
Cv::set_mouse_callback("Starry", on_mouse_event)
49-
Module rb_mCv = define_module("Cv");
5049
5150
You can also use ``lambdas`` in addition to ``Procs``
5251

5352
User Data
5453
^^^^^^^^^
55-
Notice that the ``MouseCallback`` callback defines a parameter called ``userdata`` which has a type of ``void*``. This is a common pattern in C style callbacks and allows clients to pass information into the callback - its a way of handling state.
54+
Notice that the ``MouseCallback`` callback defines a parameter called ``userdata`` which has a type of ``void*``. This is a common pattern in C style callbacks and allows clients to pass information into the callback - it's a way of handling state.
5655

5756
Rice enables Ruby code to pass Ruby objects from the client to the callback. To do this, you must tell Rice that it should not try to convert the Ruby object to C++ or from C++ to Ruby. This is done by using the ``setOpaque`` method:
5857

5958
.. code-block:: cpp
6059
60+
Module rb_mCv = define_module("Cv");
6161
rb_mCv.define_module_function("set_mouse_callback", &cv::setMouseCallback,
6262
Arg("winname"), Arg("on_mouse"), Arg("userdata").setOpaque() = static_cast<void *>(0));
6363
@@ -67,9 +67,9 @@ Notice the addition of ``Arg("userdata").setOpaque()``. Ruby code can now call t
6767
6868
Cv::set_mouse_callback("Starry", on_mouse_event, self)
6969
70-
This allows the current Ruby object, self, to pass a reference to itself to the callback method.
70+
This allows the current Ruby object, ``self``, to pass a reference to itself to the callback method.
7171

72-
However, this only solves 1/2 the problem - passing a Ruby object unchanged to C++. When C++ later invokes the callback, Rice will try to tranlsate it from C++ to Ruby. Of course, that does not make sense for the self reference, so we need to tell Rice not to do it. This is done by using the ``define_callback`` function.
72+
However, this only solves 1/2 the problem - passing a Ruby object unchanged to C++. When C++ later invokes the callback, Rice will try to translate it from C++ to Ruby. Of course, that does not make sense for the self reference, so we need to tell Rice not to do it. This is done by using the ``define_callback`` function.
7373

7474
.. code-block:: cpp
7575
@@ -93,21 +93,27 @@ Simple Callback
9393
"""""""""""""""
9494
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``.
9595

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.
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.
9797

9898
LibFFI Callback
9999
"""""""""""""""
100100
However, a library often times use a callback in multiple places. For example:
101101

102102
.. code-block:: cpp
103103
104-
void setMouseClickCallback(MouseCallback_T onSingleClick, MouseCallback_T onDoubleClick, );
104+
void setMouseClickCallback(MouseCallback_T onSingleClick, MouseCallback_T onDoubleClick);
105105
void setMouseEnterExitCallback(MouseCallback_T onEnterExit);
106106
107107
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.
108108

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.
109+
In this case, Rice 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, in this case the ``Proc``, with a callback and then dynamically generates a new function which is what is invoked by the callback function.
110+
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.
112+
113+
However, you must opt into using libffi. To do this update your ``extconf.rb`` file like this:
114+
115+
.. code-block:: ruby
110116
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.
117+
abort "libffi not found" unless have_libffi
112118
113-
If you are using CMake, you will need to add a C++ preprocessor define called ``HAVE_LIBFFI`` and link to libffi.
119+
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/class_templates.rst.txt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ Instead, write a function to create wrappers. A simplified version looks like th
5656
inline void Mat__builder(Data_Type_T& klass)
5757
{
5858
klass.define_constructor(Constructor<cv::Mat_::Mat_<_Tp>>()).
59-
define_constructor(Constructor<cv::Mat_::Mat_<_Tp>, int, int>(),
60-
Arg("_rows"), Arg("_cols")).
59+
define_constructor(Constructor<cv::Mat_::Mat_<_Tp>, int, int>(), Arg("_rows"), Arg("_cols")).
60+
6161
template define_iterator<cv::Mat_<_Tp>::iterator(cv::Mat_<_Tp>::*)()>(&cv::Mat_<_Tp>::begin, &cv::Mat_<_Tp>::end, "each").
62-
template define_method<_Tp&(cv::Mat_<_Tp>::*)(int, int)>("[]", &cv::Mat_<_Tp>::operator(),
63-
Arg("row"), Arg("col")).
62+
template define_method<_Tp&(cv::Mat_<_Tp>::*)(int, int)>("[]", &cv::Mat_<_Tp>::operator(), Arg("row"), Arg("col")).
63+
6464
define_method("[]=", [](cv::Mat_<_Tp>& self, int row, int column, _Tp& value)
6565
{
6666
self(row, column) = value;
@@ -89,7 +89,7 @@ Second, the ``template`` keyword needs to be used in front of methods:
8989
9090
template define_iterator<cv::Mat_<_Tp>::iterator(cv::Mat_<_Tp>::*)()>(&cv::Mat_<_Tp>::begin, &cv::Mat_<_Tp>::end, "each").
9191
92-
template define_method<_Tp&(cv::Mat_<_Tp>::*)(int, int)>("[]", &cv::Mat_<_Tp>::operator(),
92+
template define_method<_Tp&(cv::Mat_<_Tp>::*)(int, int)>("[]", &cv::Mat_<_Tp>::operator(), Arg("row"), Arg("col")).
9393
9494
Third, the array constructor cannot be wrapped because it uses a template parameter that is not defined:
9595

@@ -102,11 +102,10 @@ Fourth, the ``operator()`` is mapped to two Ruby methods, ``[]`` and ``[]=``.
102102

103103
.. code-block:: cpp
104104
105-
template define_method<_Tp&(cv::Mat_<_Tp>::*)(int, int)>("[]", &cv::Mat_<_Tp>::operator(),
106-
Arg("row"), Arg("col")).
105+
template define_method<_Tp&(cv::Mat_<_Tp>::*)(int, int)>("[]", &cv::Mat_<_Tp>::operator(), Arg("row"), Arg("col")).
107106
define_method("[]=", [](cv::Mat_<_Tp>& self, int row, int column, _Tp& value)
108107
{
109108
self(row, column) = value;
110109
});
111110
112-
Once you have created a class builder function it then becomes easy to create new C++ classes from class templates and wrap them in Ruby.
111+
Once you have created a class builder function it then becomes easy to create new C++ classes from class templates and wrap them in Ruby.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ These constants can be wrapped like this:
3232
3333
Enums as Constants
3434
------------------
35-
Older C++ code sometimes uses anonymous C style enums as a hack for defining class constants. For more information see :ref:anonymous_enums.
35+
Older C++ code sometimes uses anonymous C style enums as a hack for defining class constants. For more information see :ref:`anonymous_enums`.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ This defines a constructor that takes no arguments. It can be invoked from Ruby
7272
7373
Mat.new
7474
75-
Under the hood, the ``define_constructor`` call creates a new ``initialize`` method on the Ruby Mat class. The ``initialize`` method is responsible for creating a new C++ ``Mat`` instance and associating it with the wrapper Ruby object. Thus if you override the ``initialize`` method you MUST call super:
75+
Under the hood, the ``define_constructor`` call creates a new ``initialize`` method on the Ruby ``Mat`` class. The ``initialize`` method is responsible for creating a new C++ ``Mat`` instance and associating it with the wrapper Ruby object. Thus if you override the ``initialize`` method you MUST call ``super``:
7676

7777
.. code-block:: ruby
7878
@@ -113,7 +113,7 @@ Most C++ classes include a copy constructor that takes one argument. These are m
113113

114114
.. code-block:: cpp
115115
116-
define_constructor(Constructor<const Mat&())
116+
define_constructor(Constructor<const Mat&>())
117117
118118
Rice maps copy constructors to Ruby's ``clone`` and ``dup`` methods:
119119

@@ -123,7 +123,7 @@ Rice maps copy constructors to Ruby's ``clone`` and ``dup`` methods:
123123
mat2 = mat1.dup
124124
mat3 = mat1.clone
125125
126-
Under the hood, the ``define_constructor`` call creates a new ``initialize_copy`` method on the Ruby Mat class. The ``initialize_copy`` method is responsible for calling the C++ copy constructor and assigning the new C++ instance to the wrapper Ruby object. Thus if you override the ``initialize_copy`` method you MUST call super:
126+
Under the hood, the ``define_constructor`` call creates a new ``initialize_copy`` method on the Ruby Mat class. The ``initialize_copy`` method is responsible for calling the C++ copy constructor and assigning the new C++ instance to the wrapper Ruby object. Thus if you override the ``initialize_copy`` method you MUST call ``super``:
127127

128128
.. code-block:: ruby
129129

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ To expose an enum to Ruby, use ``define_enum`` like this:
2626
.define_value("BLACK", BLACK)
2727
.define_value("GREEN", GREEN);
2828
29-
``define_enum<Color>("Color")`` creates a new Ruby class called ``Color``. Each call to ``define_value`` defines a new instance of Color that is stored as a constant on the Color class. Thus from the Ruby side of things, the mapping looks like:
29+
``define_enum<Color>("Color")`` creates a new Ruby class called ``Color``. Each call to ``define_value`` defines a new instance of Color that is stored as a constant on the Color class. Thus from the Ruby side of things, the mapping looks like:
3030

3131
.. code-block:: ruby
3232
@@ -88,7 +88,7 @@ From the Ruby side, this creates:
8888
class MyClass
8989
SOME_CONSTANT = 42
9090
HACKED_CLASS_CONSTANT_1 = MyClass::HACKED_CLASS_CONSTANT_1
91-
HACKED_CLASS_CONSTANT_2", MyClass::HACKED_CLASS_CONSTANT_2
91+
HACKED_CLASS_CONSTANT_2 = MyClass::HACKED_CLASS_CONSTANT_2
9292
9393
class Season
9494
Spring = Color.new(Season::Spring)
@@ -102,29 +102,35 @@ Ruby API
102102
--------
103103
Generated enum classes have the following Ruby API.
104104

105-
Enum.from_int
105+
.. code-block::
106106
107-
Enum#<=>
108-
Enum#eql?
109-
Enum#hash
110-
Enum#each
111-
Enum#inspect
112-
Enum#to_int
113-
Enum#to_s
107+
Enum.from_int
114108
115-
Enum#&
116-
Enum#|
117-
Enum#^
118-
Enum#~
119-
Enum#<<
120-
Enum#>>
109+
Enum#<=>
110+
Enum#eql?
111+
Enum#hash
112+
Enum#each
113+
Enum#inspect
114+
Enum#to_int
115+
Enum#to_s
116+
117+
Enum#&
118+
Enum#|
119+
Enum#^
120+
Enum#~
121+
Enum#<<
122+
Enum#>>
121123
122124
In addition, they have the following aliases:
123125

124-
Enum#=== Enum#eql?
125-
Enum#to_i Enum#to_int
126+
.. code-block::
127+
128+
Enum#===
129+
Enum#eql?
130+
Enum#to_i
131+
Enum#to_int
126132
127133
And mixin the following modules:
128134

129-
* Comparable
130-
* Enumerable
135+
* `Comparable <https://ruby-doc.org/core-3.0.3/Comparable.html>`_
136+
* `Enumerable <https://ruby-doc.org/core-3.0.3/Enumerable.html>`_

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ If your C++ code calls a Ruby API which then in turns calls C++ code, you will n
128128
std::map<T, U>* result = (std::map<T, U>*)(user_data);
129129
130130
// This method is being called from Ruby so we cannot let any C++
131-
// exceptions propogate back to Ruby
131+
// exceptions propagate back to Ruby
132132
return cpp_protect([&]
133133
{
134134
result->operator[](From_Ruby<T>().convert(key)) = From_Ruby<U>().convert(value);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ Directors
3838
Inheritance becomes much more complex if you want to create Ruby classes that inherit from wrapped C++ classes. This introduces several problems:
3939

4040
* Ruby classes should be able to override C++ virtual methods
41-
* Overriden virtual methods should be able to call ``super`` and invoke the overriden C++ method
42-
* C++ code calling the virtual methods should invoke the overriden version in Ruby
41+
* Overridden virtual methods should be able to call ``super`` and invoke the overridden C++ method
42+
* C++ code calling the virtual methods should invoke the overridden version in Ruby
4343

4444
Rice supports these use cases through the use of ``Director`` classes. ``Directors`` are proxies that can correctly dispatch method invocations up or down a Class hierarchy.
4545

@@ -126,7 +126,7 @@ Next we implement ``doWork``. The director class overrides its by forwarding the
126126
return VirtualBase::doWork();
127127
}
128128
129-
It director also implements ``default_doWork`` which enables Ruby to call the overriden virtual C++ method. The ``default_`` prefix is a naming convention to help keep straight which methods perform which functions.
129+
It director also implements ``default_doWork`` which enables Ruby to call the overridden virtual C++ method. The ``default_`` prefix is a naming convention to help keep straight which methods perform which functions.
130130

131131
If Ruby should never call the C++ method then the ``default_`` implementation should call ``raisePureVirtual()``:
132132

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Iterators
44
=========
55

6-
C++ iterators are used to traverse through elements stored in a container. C++ iterators are external iterators that work in pairs, with a beginning iterator and an ending iterator. For example, std::vector has begin/end, cbegin/cend, rbegin/rend, etc.
6+
C++ iterators are used to traverse through elements stored in a container. C++ iterators are external iterators that work in pairs, with a beginning iterator and an ending iterator. For example, ``std::vector`` has begin/end, cbegin/cend, rbegin/rend, etc.
77

88
Enumerable Support (Internal Iterators)
99
---------------------------------------
@@ -32,7 +32,7 @@ For example let's create a simple wrapper around std::vector (for full support p
3232
3333
Notice that we have to tell Rice which overloaded version of ``push_back``, ``begin`` and ``end`` we want to expose For more information please see :ref:`overloaded_methods`.
3434

35-
Once the interator is defined you can write standard Ruby code such as:
35+
Once the iterator is defined you can write standard Ruby code such as:
3636

3737
.. code-block:: ruby
3838
@@ -47,7 +47,7 @@ Once the interator is defined you can write standard Ruby code such as:
4747
4848
Where result will be ``[2, 4, 6]``.
4949

50-
Let's say you also want to expose std::vector's reverse iterator to Ruby using the method name ``reach``. This is done by adding a third parameter to the ``define_iterator`` call, in this case it is set to reach":
50+
Let's say you also want to expose std::vector's reverse iterator to Ruby using the method name ``reach``. This is done by adding a third parameter to the ``define_iterator`` call, in this case it is set to ``"reach"``:
5151

5252
.. code-block:: cpp
5353
@@ -80,7 +80,7 @@ Enumerator Support (External Iterators)
8080
---------------------------------------
8181
Ruby supports external iterators via the `Enumerator <https://ruby-doc.org/3.2.2/Enumerator.html>`_ class. The ``define_iterator`` method automatically adds support for Enumerators.
8282

83-
Enumerators can be created by calling an iterator method without a block, in the same way you can call Array#each or other methods without a block. For example:
83+
Enumerators can be created by calling an iterator method without a block, in the same way you can call ``Array#each`` or other methods without a block. For example:
8484

8585
.. code-block:: ruby
8686

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ Obviously this code could be rewritten to make sure the database object remains
198198
define_class<Database>("Database")
199199
.define_method("get_column", &Database::getColumn, Return().keepAlive())
200200
201-
Note that Return().keepAlive() will work with external types only. An attempt to use it with builtin type will result in runtime exception.
201+
Note that ``Return().keepAlive()`` will work with external types only. An attempt to use it with builtin type will result in runtime exception.
202202

203203
C++ Referencing Ruby Objects
204204
----------------------------

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,22 @@ For example, reusing the example above:
112112
113113
The ``hello`` function can be called from Ruby like this:
114114

115-
.. code-block:: cpp
115+
.. code-block:: ruby
116116
117117
test = Test.new
118118
test.hello(first: "Hello", second: "World")
119119
test.hello(first: "Hello") # This is ok because the second parameter has a default value
120120
121+
And it can also be called in the traditional manner like this:
122+
123+
.. code-block:: ruby
124+
125+
test = Test.new
126+
test.hello("Hello", "World")
127+
test.hello("Hello")
128+
129+
The ability to call the function in two different ways (position and keyword) could cause problems in the future. Imagine that you decide to move the code to Ruby - you will need to chose one of the two forms. That could result in breaking someone else's code. This risk seems low though, so for the moment Rice only supports defining arguments using the ``Arg`` class. In the future Rice may introduce a ``KeyArg`` class to avoid this issue.
130+
121131
.. _return_self:
122132

123133
Return Self
@@ -136,7 +146,7 @@ The above code works because the ``<<`` method returns the Array ``a``. You can
136146
.. code-block:: cpp
137147
138148
define_vector<std::vector<int32_t>>().
139-
define_method("<<", [](std::vector<int32_t>& self, int32_t value) -> std::vector<int32_t>& // <----- DONT MISS THIS
149+
define_method("<<", [](std::vector<int32_t>& self, int32_t value) -> std::vector<int32_t>& // <----- DON'T MISS THIS
140150
{
141151
self.push_back(value);
142152
return self; // <------ Allows chaining on calls
@@ -155,8 +165,7 @@ Ruby classes are expected to define a ``to_s`` method that provides a string rep
155165
define_method("to_s", [](Test& self)
156166
{
157167
return "<Test>";
158-
})
159-
);
168+
});
160169
161170
We define the ``to_s`` method to take a single parameter, self, which is an C++ instance of ``Test``. Note that ``self`` is passed by reference - we do not want to create a copy of the Test object!
162171

0 commit comments

Comments
 (0)