Skip to content

Commit 2dea4bc

Browse files
committed
Added sections for SWIG and boost.python
1 parent 44ca0b1 commit 2dea4bc

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

docs/scenarios/clibs.rst

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,67 @@ Struct Equivalents
4040
class my_struct(ctypes.Structure):
4141
_fields_ = [("a", c_int),
4242
("b", c_int)]
43+
44+
SWIG
45+
----
46+
47+
`SWIG <http://www.swig.org>`_, though not strictly Python focused (it supports a
48+
large number of scripting languages), is a tool for generating bindings for
49+
interpreted languages from C/C++ header files. It is extremely simple to use:
50+
the consumer simply needs to define an interface file (detailed in the
51+
tutorial and documentations), include the requisite C/C++ headers, and run
52+
the build tool against them. While it does have some limits, (it currently
53+
seems to have issues with a small subset of newer C++ features, and getting
54+
template-heavy code to work can be a bit verbose), it provides a great deal
55+
of power and exposes lots of features to Python with little effort.
56+
Additionally, you can easily extend the bindings SWIG creates (in the
57+
interface file) to overload operators and built-in methods, effectively re-
58+
cast C++ exceptions to be catchable by Python, etc.
59+
60+
Example: Overloading __repr__
61+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62+
63+
:file:`MyClass.h`
64+
65+
.. code-block:: c++
66+
:linenos:
67+
68+
#include <string>
69+
class MyClass {
70+
private:
71+
std::string name;
72+
public:
73+
std::string getName();
74+
};
75+
76+
:file:`myclass.i`
77+
78+
.. code-block:: c++
79+
:linenos:
80+
81+
%include "string.i"
82+
83+
%module myclass
84+
%{
85+
#include <string>
86+
#include "MyClass.h"
87+
%}
88+
89+
%extend MyClass {
90+
std::string __repr__()
91+
{
92+
return $self->getName();
93+
}
94+
}
95+
96+
%include "MyClass.h"
97+
98+
99+
Boost.Python
100+
------------
101+
102+
`Boost.Python <http://www.boost.org/doc/libs/1_59_0/libs/python/doc/>`_
103+
requires a bit more manual work to expose C++ object functionality, but
104+
it is capable of providing all the same features SWIG does, and then some,
105+
to include providing wrappers to access PyObjects in C++, extracting SWIG-
106+
wrapper objects, and even embedding bits of Python into your C++ code.

0 commit comments

Comments
 (0)