deepan muthusamy
deepan muthusamy

Reputation: 371

How to convert c++ std::map<enum, enum> to python type using pythonBinding

I have a C++ library that needs to be used with a Python application. I have created a pythonBinding for that library.

header.h

enum class enumA {
        app1,
        app2,
        app3};

 enum class enumB {
        kInit,
        kRunning,
        kFailed};

 void registerApp(std::map<enumA, enumB>& appMap, std::function<void(std::string)> callback);

pythonBinding.hpp

class PyAppIface : public AppIface {
  void registerApp(std::map<enumA, enumB>& appMap, std::function<void(std::string)> callback) 
    override {
          PYBIND11_OVERRIDE_PURE(void, AppIface, registerApp, appMap, callback);
  }
};

pythonBinding.cpp

static py::object pyCallback;

void callbackFunction(std::string str)
{
   PyEval_InitThreads();
   PyGILState_STATE state = PyGILState_Ensure();
   pyCallback(nodeMap);
   PyGILState_Release(state);
}


void registerApp(AppIface& appIface, std::map<enumA, enumB>& appMap, py::object object){
 pyCallback = object;
 appIface.registerApp(appMap, callbackFunction);
}

PYBIND11_MODULE(AppBinding, bindModule) {
  bindModule.doc() = "AppBinding";

  py::enum_<enumA>(bindModule, "enumA")
      .value("app1", enumA::app1)
      .value("app2", enumA::app2)
      .value("app3", enumA::app3)
      .export_values();

  py::enum_<enumB>(bindModule, "enumB")
      .value("kInit", enumB::kInit)
      .value("kRunning", enumB::kRunning)
      .value("kFailed", enumB::kFailed)
      .export_values();


  py::class_<AppIface, PyAppIface, std::shared_ptr<AppIface>>(bindModule,
                                                                 "AppIface")
      .def(py::init<>())
      .def("registerApp", &registerApp);
    
    }

The registerApp function will be called by python application. How will std::map<enumA, enumB>& appMap be converted to a C++ type and vice versa? What am I missing here?

Upvotes: 1

Views: 79

Answers (0)

Related Questions