Wrapping STL container return types using Pybind11
By : 9r6i7kr6
Date : March 29 2020, 07:55 AM
I hope this helps . The problem might be with the PYBIND11_MAKE_OPAQUE treatment of the ArrayComplex4 data structure, I couldn't get this to work, although I'll look at it some more when I get time. The following code, which I did get to work, is my closest approximation so far to your design. I used an additional user-defined data structure to wrap the std:: elements: code :
#include <pybind11/pybind11.h>
#include <pybind11/complex.h>
#include <pybind11/stl.h>
#include <memory>
#include <complex>
#include <array>
#include <cmath>
namespace py = pybind11;
typedef std::array<std::complex<double>, 4> ArrayComplex4;
struct ArrayComplex4Holder
{
ArrayComplex4 data;
ArrayComplex4 getData() { return data; }
};
class MyClass {
public:
MyClass() { }
std::unique_ptr<ArrayComplex4Holder> my_function(double x)
{
std::unique_ptr<ArrayComplex4Holder> ph( new ArrayComplex4Holder());
ph->data[0] = std::complex<double>(x);
return ph;
}
std::unique_ptr<ArrayComplex4Holder> my_function(double x, double y)
{
std::unique_ptr<ArrayComplex4Holder> ph( new ArrayComplex4Holder());
ph->data[0] = std::complex<double>(x);
ph->data[1] = std::complex<double>(y);
return ph;
}
};
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example"; // optional module docstring
py::class_<ArrayComplex4Holder>(m, "ArrayComplex4Holder")
.def(py::init<>())
.def("getData", &ArrayComplex4Holder::getData);
py::class_<MyClass>(m, "MyClass")
.def(py::init<>())
.def("my_function", (std::unique_ptr<ArrayComplex4Holder> (MyClass::*)(double)) &MyClass::my_function)
.def("my_function", (std::unique_ptr<ArrayComplex4Holder> (MyClass::*)(double, double)) &MyClass::my_function);
}
import sys
sys.path.append('/Volumes/RAID 1/Projects/workspace/Project CPP 1')
import example
p = example.MyClass()
print (p.my_function(1.2345).getData())
print (p.my_function(1.2345, 6.7890).getData())
[(1.2345+0j), 0j, 0j, 0j]
[(1.2345+0j), (6.789+0j), 0j, 0j]
|
Why only C++(and D language) provides variadic templates feature? Are variadic templates that good?
By : masi
Date : March 29 2020, 07:55 AM
hope this fix your issue Common Lisp provides much better than variadic templates thru its macro system (beware, Lisp macros are much more powerful than C++ templates or macros for its preprocessor). Lisp predates C++ (and C) by decades! Try SBCL (a freely available excellent Common Lisp implementation). BTW, I think that D and Terra and Template Haskell also have something similar to variadic templates, and probably other languages too .... I cannot explain why Lisp ideas vanished, the reasons are more sociological, educational and economical than technical; read also SICP, Practical Common Lisp and Lisp In Small Pieces.
|
pybind11 wrapping existing code
By : mathaig
Date : March 29 2020, 07:55 AM
like below fixes the issue Despite serving the same purpose, SWIG and Pybind11 are different tools. As the name implies, SWIG (Simplified Wrapper and Interface Generator) is a generator tool that create Python binding for existing C++ code, using definitions written in a special language.
|
Wrapping C++ void functions with pybind11
By : josher GT
Date : March 29 2020, 07:55 AM
|
Wrapping a C++ allocated instance with pybind11
By : Allen Kao
Date : September 30 2020, 11:00 AM
To fix the issue you can do If you check the resulting py::object from the cast (e.g. by casting it to bool), you will see that the call failed. The reason is that the python does not know the class "Pet" (nor shared_ptr). You can either use the code as above and create a module from it the usual way, then import that in a main program. Or, use the EMBEDDED_MODULE feature, which is closer to what you appear to be going for. Adjusting your example: code :
#include <stdio.h>
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
using namespace std;
namespace py = pybind11;
class Pet
{
public:
Pet() {}
void bark(void) { printf("wow!\n"); }
};
PYBIND11_EMBEDDED_MODULE(Pets, m) {
py::class_<Pet, shared_ptr<Pet>>(m, "Pet")
.def("bark", &Pet::bark)
;
}
int main(int argc, char *argv[])
{
py::scoped_interpreter guard{};
shared_ptr<Pet> pet = make_shared<Pet>();
auto pets_mod = py::module::import("Pets");
py::globals()["pet"] = py::cast(pet);
py::exec("pet.bark()\n");
}
|