if you have a C++ function foo like that and you need to pass 2 C++ pointers bar and baz like that, the simplest way to do it is probably to do it in C++. In PyROOT you can do e.g.:
ROOT.gInterpreter.ProcessLine("foo(&bar, &baz)")
Similarly you can declare any helper function that might be useful with ROOT.gInterpreter.Declare and the function will be accessible as ROOT.functionname.
Feel free to share a minimal reproducer in case you need something more specific.
Cheers,
Enrico
I can provide something, but what the best translation is really depends on the actual context in which this code is used: “who” creates Bar and Baz, what’s their lifetime? Do you care about the “pointer-ness” of bar and baz when using them from Python or do you just need access to the values they point to? Are the Bar and Baz types expensive to copy? Etc. etc.
One way to do the same thing as that C++ snippet in Python is:
import ROOT
# Given this C++ setup...
ROOT.gInterpreter.Declare("""
struct Bar {
Bar() { std::cout << "Bar constructed\\n"; }
void bar_method() { std::cout << "some_method called\\n"; }
};
Bar* bar = nullptr;
void foo(Bar **x) { std::cout << "foo called\\n"; *x = new Bar(); }
""")
ROOT.foo(ROOT.bar)
ROOT.bar.bar_method()
A patched version of cppyy comes with ROOT. import cppyy should work if import ROOT works.
This works (thanks!) but is it correct w.r.t. ownership? The outputs of this function (bar_in_py and baz_in_py) should be managed by python (e.g., the pointed objects should be deleted when bar_in_py and baz_in_py are garbage collected).
How would I do instead if foo was a method? Is it possible to attach the adapter function to the same class with a pythonization?