How to tell vala which getters and setters to use using GObjectIntrospection and vapigen?

1.3k views Asked by At

I've written a C library and used vapigen to create the vapis for it. In the generated files there are some properties defined like:

public int index { get; set; }

And the accessor methods (which I use from C and only return the value of the property) duplicating this 'get' and 'set' functionality:

public int get_index ();
public void request_index (int index);

What I want to do is to tell Vala to call my methods when the Vala code gets or sets the properties using the notation:

i = object.index;
object.index = 42;

instead of translating it to a g_object_get/g_object_set call as it does right now.

Is there any way to do this?

I've posted this both in vala-devel and vala mailing lists, but no one answered.

Edit: I'm using gobject-introspection and vapigen with autotools to generate the vapi files automatically without worrying about API changes, so redefining the classes in Vala to do this is not an option for me, but I can use gobject-introspection annotations and metadata files.

Edit with solution: The comments in the selected answer contain the solution to my problem, but basically what I did is to use a custom Vala file and skip the used property using the metadata file.

Relevant contents in the metadata file:

MyObject.index skip

And in the custom Vala file:

namespace MyNamespace
{
    public class MyObject : GLib.Object
    {
        public int index
        {
            owned get;
            [CCode (cname = "db_model_request_index")]
            set;
        }
    }
}
2

There are 2 answers

7
MrEricSir On BEST ANSWER

Have you tried something like this?

public int index {
        get { return get_index(); }
        set { set_index(value); }
    }

There's more info about Vala properties in the official Vala Tutorial.

1
Jussi Kukkonen On

(Sorry for being the guy whose answer doesn't strictly speaking answer the question)

I think you should fix the library instead as someone using the C api might legitimately do the same thing (use g_object_set() instead of your property-specific setter function): if your_object_set_property and your_object_get_property functions call the specific setter and getter functions, then this problem wouldn't exist at all, right?

If you really want to modify the vapi, then the right way to go about it would probably be to use gobject-introspection and it's annotations, although I'm not sure about the exact syntax (and there's a bit of setup involved if you don't use gobject-introspection yet).