samedi 20 novembre 2021

Pybind compilation problems with pointer argument in embedded python function call

I am getting a compilation error with the following simple pybind11 code (looks like something is trying to copy a unique pointer, if I change all unique pointers to shared pointers, code compiles)

class Instrument
{
public:
   // ...

private:
  std::map<std::string, std::unique_ptr<Module>> mModules;
};

class Module
{
public:
  Module(/* some ctor args */) {}

protected:
  static Instrument* mInstrument;

private:
  using ModuleFactory = std::function<std::unique_ptr<Module>(const std::string&)>;
  using ProcedureFactory = std::function<std::unique_ptr<Procedure>()>;
  using ModuleProcedureFactory = std::pair<std::string, ProcedureFactory>;
  using ModuleProcedureFactories = std::vector<ModuleProcedureFactory>;

  static std::map<std::string, ModuleFactory> mModuleFactories;
  static std::map<std::string, ModuleProcedureFactories> mModuleProcedureFactories;
  std::map<std::string, std::unique_ptr<Procedure>> mProcedures;
};

My pybind code

PYBIND11_MODULE(pyICSW, m)
{
  py::class_<Instrument>(m, "Instrument");
  py::class_<Module>(m, "Module");
}

I get compilation error with this embedded python function call (if I remove this call, project compiles with no problems)

void Run(Instrument* instrument, Module* module)
{
  py::module::import("module").attr("fun")(instrument, module);
}

The unique_ptr copy compilation error I get

error: use of deleted function ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const std::__cxx11::basic_string<char>; _T2 = std::unique_ptr<Procedure>]’

Now I deliberately did not provide any constructor bindings - I just want python to modify C++ data. Also, all the std::maps in Instrument and Module are private and I do not want to expose them to the python side. I am explicitly calling the python function with pointers (Instrument* and Module*), so no copies should be happening. Do I need to give special attention to static members, even though I am not binding them? How do I fix this issue?

Aucun commentaire:

Enregistrer un commentaire