forked from tleonhardt/Python_Interface_Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci.cpp
42 lines (34 loc) · 1.18 KB
/
fibonacci.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* File : fibonacci.cpp
*
* Implementation of the algorithm in C++.
*
* For simplicity, we put both this function and the binding code into a single file.
*
* In practice, implementation and binding code will generally be located in separate files.
*/
#include <pybind11/pybind11.h>
int compute_fibonacci(int n)
{
int temp;
int a = 1;
int b = 1;
for (int x=0; x<n; x++)
{
temp = a;
a += b;
b = temp;
}
return a;
}
namespace py = pybind11;
// The PYBIND11_PLUGIN() macro creates a function that will be called when an import statement is issued within Python
PYBIND11_PLUGIN(fibonacci)
{
// creates a module named fibonacci (with the supplied docstring)
py::module m("fibonacci", "pybind11 fibonacci plugin");
// generates binding code that exposes the compute_fibonacci() function to Python
m.def("compute_fibonacci", &compute_fibonacci, "A function which computes the Nth Fibonacci number");
// Notice how little code was needed to expose our function to Python: all details regarding the function’s
// parameters and return value were automatically inferred using template metaprogramming
return m.ptr();
}