1 The py2cpp library was created in order to make easier the call of a python
2 function within c++ sources. It provides conversion functions to and from a
3 python object for some basic c++ types (int, double, std:: string and
4 collections for these types: std::vector, std::list, std::map). It is possible
5 to add your own conversion functions for your own types.
10 Consider you have the following python file "mymodule.py":
12 ________________________________________________________________________________
14 return "The result is", a/b
15 ________________________________________________________________________________
17 You can call this function from c++ this way:
19 ________________________________________________________________________________
20 #include "TypeConversions.hxx"
22 #include "PyFunction.hxx"
28 py2cpp::PyFunction fn;
29 fn.load("mymodule", "myfunction");
30 py2cpp::pyResult(s,d) = fn(1,2);
32 std::cout << "String parameter from the python function:" << s << std::endl;
33 std::cout << "Double parameter from the python function:" << d << std::endl;
36 ________________________________________________________________________________
38 The full example which also deals with possible errors, can be this:
40 ________________________________________________________________________________
41 #include "TypeConversions.hxx"
43 #include "PyFunction.hxx"
53 py2cpp::PyFunction fn;
54 fn.load("mymodule", "myfunction");
57 std::cerr << "Impossible to load myfunction from the module mymodule!";
58 std::cerr << std::endl;
59 std::cerr << py2cpp::getLastPyError();
65 py2cpp::pyResult(s,d) = fn(1, 2);
66 std::cout << "String parameter from the python function:" << s << std::endl;
67 std::cout << "Double parameter from the python function:" << d << std::endl;
69 catch(const py2cpp::Exception& err)
71 std::cerr << err.what();
78 ________________________________________________________________________________