Skip to content

How to call c++ in-class functions (whose arguments are float pointers) in python with pybind11 #4599

Closed Answered by Shin-ichi-Takayama
Shin-ichi-Takayama asked this question in Q&A
Discussion options

You must be logged in to vote

The solution was to define a class to WRAP as follows.

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>

namespace py = pybind11;

class MyClass {
public:
  void myFunc(float *arr, int size) {
    for (int i = 0; i < size; i++) {
      arr[i] += 1.0f;
    }
  }
};

class MyClassWrapper {
private:
    MyClass my_py_class;
public:
    void myPyFunc(py::array_t<float> input_array, int size) {
        // Get a pointer to the underlying data
        float *data = static_cast<float *>(input_array.request().ptr);
        
        my_py_class.myFunc(data, size);
    }
};

PYBIND11_MODULE(example, m) {
    py::class_<MyClassWrapper>(m, "MyClassWrapper")
      .def

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Shin-ichi-Takayama
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant