pythoncppyy

cppyy - trouble converting to MYMODEL*&


I am trying to use cppyy in python to invoke a C++ library. The header file provided is like:

typedef void MYMODEL;
namespace MY
{
    API MYMODEL* createModel(char *path);
    API int process(MYMODEL* model);
    API int destroyModel(MYMODEL* &model);
}

I have had success with the first two calls:

m = cppyy.gbl.MY.createModel(model_path)
cppyy.gbl.MY.process(m)
cppyy.gbl.MY.destroyModel(m)

but when I tried the last call (destroyModel), it is coming up with

TypeError: int MY::destroyModel(MYMODEL*& model) =>
    TypeError: could not convert argument 1

if I print m just before that, it is:

<cppyy.LowLevelView object at 0x000001E3F233F5B0>

I've tried a number of other things, like using cppyy.gbl.MY.destroyModel(cppyy.addressof(m)) with and without byref, or trying to cast m to "MYMODEL*" prior to passing it to destroyModel, or modifying the header ref from (MYMODEL* &model) to (MYMODEL** model) without avail.

How should I pass the model back in for destroyModel in cppyy?


Solution

  • Is an oversight, to be fixed soon (bound low-level C code needs special-cases everywhere and some, clearly, fell through the cracks).

    As a Q&D workaround, use any struct:

    cppyy.cppdef(r"""\
    namespace MY { struct FakeModel { }; }""")
    
    cppyy.gbl.MY.destroyModel(cppyy.bind_object(m, cppyy.gbl.MY.FakeModel))