I'm working on an app that has a C++ and Python API, and am implementing an API call that needs to be used in both languages. Part of the data this API sends back is an array of structs representing color information:
struct {
float r;
float g;
float b;
float a;
} Foo_Pixel;
In my interop code, I marshal the data thusly:
Foo_Pixel color;
PyObject* tileValues = PyList_New( numValues );
for (int pixelIndex = 0; pixelIndex < numValues; pixelIndex++)
{
color = value[pixelIndex];
PyObject* newColourItem = Py_BuildValue("ffff", color.r, color.g, color.b, color.a);
PyList_SetItem(tileValues, pixelIndex, newColourItem);
}
Essentially, I'm looping over the provided data and am building a list of tuples for the user to consume. However, this seems a little inefficient to me. Is there a faster way to create a PyObject out of an array of float structs?