Re: binding of a C struct


On Wed, 2016-04-20 at 08:34 +0000, PICCA Frederic-Emmanuel wrote:

here my C struct

typedef struct _HklQuaternion HklQuaternion;

struct _HklQuaternion
      double data[4];

I want to annotate a method which return an HklQuaternion

HklQuaternion hkl_geometry_sample_rotation_get(const HklGeometry
*self, const HklSample *sample);


You cannot return structs by value in gobject-introspection: if you
return a struct, it's expected to be a pointer to a heap allocated

You have three options:

If heap allocation is not a problem:

 * @self:
 * @sample:
 * Returns: (transfer full)
HklQuaternion *hkl_geometry_sample_rotation_get(const HklGeometry
*self, const HklSample *sample) {
  return hkl_quaternion_dup(&darray_item(self->holders, 0)->q);

If you're ok with tying lifetimes to the container:
(for C/Vala only - in Python or gjs a copy is always made for memory

 * @self:
 * @sample:
 * Returns: (transfer none)
*hkl_geometry_sample_rotation_get(const HklGeometry *self, const
HklSample *sample) {
  return &darray_item(self->holders, 0)->q;

If you're ok with a slightly more awkward API from C (with no
difference from introspection):

 * @self:
 * @sample:
 * @quat: (out caller-allocates)
hkl_geometry_sample_rotation_get(const HklGeometry *self, const
HklSample *sample, HklQuaternion *quat) {
  *quat = darray_item(self-
holders, 0)->q;

Hope this helps,



gir-devel-list mailing list
gir-devel-list gnome org

Attachment: signature.asc
Description: This is a digitally signed message part

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]