|
| 1 | + |
| 2 | +**************** |
| 3 | +(CMake) Examples |
| 4 | +**************** |
| 5 | + |
| 6 | +Basic example (from readme) |
| 7 | +=========================== |
| 8 | + |
| 9 | +Consider the following C++ code: |
| 10 | + |
| 11 | +:download:`main.cpp <examples/readme_example_1/main.cpp>` |
| 12 | + |
| 13 | +.. literalinclude:: examples/readme_example_1/main.cpp |
| 14 | + :language: cpp |
| 15 | + |
| 16 | +There are several options to build the module, |
| 17 | +whereby we will CMake here with the following ``CMakeLists.txt``: |
| 18 | + |
| 19 | +:download:`CMakeLists.txt <examples/readme_example_1/CMakeLists.txt>` |
| 20 | + |
| 21 | +.. literalinclude:: examples/readme_example_1/CMakeLists.txt |
| 22 | + :language: cmake |
| 23 | + |
| 24 | +Then we can test the module: |
| 25 | + |
| 26 | +:download:`example.py <examples/readme_example_1/example.py>` |
| 27 | + |
| 28 | +.. literalinclude:: examples/readme_example_1/example.py |
| 29 | + :language: cmake |
| 30 | + |
| 31 | +.. note:: |
| 32 | + |
| 33 | + Since we did not install the module, |
| 34 | + we should compile and run the example from the same folder. |
| 35 | + To install, please consult |
| 36 | + `this pybind11 / CMake example <https://github.com/pybind/cmake_example>`_. |
| 37 | + |
| 38 | + |
| 39 | +Type restriction with SFINAE |
| 40 | +============================ |
| 41 | + |
| 42 | +.. seealso:: |
| 43 | + |
| 44 | + `Medium post by Johan Mabille <https://medium.com/@johan.mabille/designing-language-bindings-with-xtensor-f32aa0f20db>`__ |
| 45 | + This example covers "Option 4". |
| 46 | + |
| 47 | +In this example we will design a module with a function that accepts an ``xt::xtensor`` as argument, |
| 48 | +but in such a way that an ``xt::pyxtensor`` can be accepted in the Python module. |
| 49 | +This is done by having a templated function |
| 50 | + |
| 51 | +.. code-block:: cpp |
| 52 | +
|
| 53 | + template <class T> |
| 54 | + void times_dimension(T& t); |
| 55 | +
|
| 56 | +As this might be a bit too permissive for your liking, we will show you how to limit the |
| 57 | +scope to *xtensor* types, and allow other overloads using the principle of SFINAE |
| 58 | +(Substitution Failure Is Not An Error). |
| 59 | +In particular: |
| 60 | + |
| 61 | +:download:`mymodule.hpp <examples/sfinae/mymodule.hpp>` |
| 62 | + |
| 63 | +.. literalinclude:: examples/sfinae/mymodule.hpp |
| 64 | + :language: cpp |
| 65 | + |
| 66 | +Consequently from C++, the interaction with the module's function is trivial |
| 67 | + |
| 68 | +:download:`main.cpp <examples/sfinae/main.cpp>` |
| 69 | + |
| 70 | +.. literalinclude:: examples/sfinae/main.cpp |
| 71 | + :language: cpp |
| 72 | + |
| 73 | +For the Python module we just have to specify the template to be |
| 74 | +``xt::pyarray`` or ``xt::pytensor``. E.g. |
| 75 | + |
| 76 | +:download:`src/python.cpp <examples/sfinae/python.cpp>` |
| 77 | + |
| 78 | +.. literalinclude:: examples/sfinae/python.cpp |
| 79 | + :language: cpp |
| 80 | + |
| 81 | +We will again use CMake to compile, with the following ``CMakeLists.txt``: |
| 82 | + |
| 83 | +:download:`CMakeLists.txt <examples/sfinae/CMakeLists.txt>` |
| 84 | + |
| 85 | +.. literalinclude:: examples/sfinae/CMakeLists.txt |
| 86 | + :language: cmake |
| 87 | + |
| 88 | +Then we can test the module: |
| 89 | + |
| 90 | +:download:`example.py <examples/readme_example_1/example.py>` |
| 91 | + |
| 92 | +.. literalinclude:: examples/readme_example_1/example.py |
| 93 | + :language: cmake |
| 94 | + |
| 95 | +.. note:: |
| 96 | + |
| 97 | + Since we did not install the module, |
| 98 | + we should compile and run the example from the same folder. |
| 99 | + To install, please consult |
| 100 | + `this pybind11 / CMake example <https://github.com/pybind/cmake_example>`_. |
0 commit comments