pybind11 and blitz array

  c++, pybind11, python

I have a C++ module that I use with pybind11 to expose functionality in python.
It all works nice, for example I am exposing classes, functions and enumerations from the C++ code, for example:

   py::class_<MapErrorMsg>(m, "MapErrorMsg")
       .def(py::init())
       .def("SetFilename", &MapErrorMsg::SetFilename)
       .def("GetFilename", &MapErrorMsg::GetFilename)
       .def("SetWarnLevel", &MapErrorMsg::SetWarnLevel)
       .def("Warn", (void (MapErrorMsg::*)(MapErrorState, std::string, std::string)) &MapErrorMsg::Warn, "Warn with a level and two string messages")
       .def("Warn", (void (MapErrorMsg::*)(MapErrorState, std::string, int)) &MapErrorMsg::Warn, "Warn with a level, a string message and an integer message")
       .def("WarnAddInfo", (void (MapErrorMsg::*)(MapErrorState a_level, std::string, std::string)) &MapErrorMsg::WarnAddInfo, "Desc ToDo")
       .def("WarnAddInfo", (void (MapErrorMsg::*)(MapErrorState a_level, std::string, double)) &MapErrorMsg::WarnAddInfo, "Desc ToDo")
       .def("__repr__", [](MapErrorMsg& a) { return "<almass_py.MapErrorMsg mapped to file '"+ a.m_warnfile + "' >";} );```

``` m.def("CreateErrorMsg", CreateErrorMsg, "Instantiate the g_msg MapErrorMsg object"); ```


   ``` py::enum_<MapErrorState>(m, "MapErrorState")
       .value("WARN_BUG", WARN_BUG)
       .value("WARN_FATAL", WARN_FATAL)
       .value("WARN_UNDEF", WARN_UNDEF)
       .value("WARN_FILE", WARN_FILE)
       .value("WARN_MSG", WARN_MSG)
       .value("WARN_TRIVIAL", WARN_TRIVIAL)
       .value("WARN_ALL", WARN_ALL)
       .export_values(); ```

However, I now need to expose a function that returns a  blitz::Array<int,2> , and this is not recognized by pybind11, it would be nice if it was recognized as a numpy array for example. 

Do I need to write a custom type caster, and how would this be integrated ? 
https://pybind11.readthedocs.io/en/latest/advanced/cast/custom.html

Source: Windows Questions C++

LEAVE A COMMENT