3

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 Answers2

3

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.

MrEricSir
  • 8,044
  • 4
  • 30
  • 35
  • Right now, I'm using vapigen to generate the vapi files, so the idea is that all the binding gets autogenerated everytime I build my project so I don't have to bother for most of the API changes. But if there's a way to put that code you proposed in a custom code file to be used by vapigen, maybe it could be a good way to go. – ATColombini Aug 20 '13 at 06:44
  • 1
    @ATColombini Yes you can use custom vala files when autogenerating bindings. For a bunch of examples, look into the vala tree itself under the vapi dir: https://git.gnome.org/browse/vala/tree/vapi . There are packages and metadata dirs, depending on whether the generated vapi is from gir or gidl. Some of the generated packages have custom vala code. Note that, if the property is a gobject property, it's using the getter/setter automatically. Vala is the correct tag. – lethalman Sep 16 '13 at 08:46
  • @lethalman thanks for the link to the examples. What I'm missing is a way to overwrite the behaviour of the current set and get methods with a direct call to my methods, for example. I've made a custom vala code, but as the GIR already has the definition of the C property, I can't redefine it there. Is there any GI annotation or Vala attribute to deal with this either on the C comments or in the Vala custom code? – ATColombini Sep 17 '13 at 11:33
  • @ATColombini Yes you can do that. You have to hide C properties using the metadata, then put your own definition using a custom vala file. Look deeper in the directories I've pointed you, there's plenty of examples. – lethalman Sep 18 '13 at 18:47
  • @lethalman thanks, I forgot metadata files can hide symbols. Now, when I apply the `hidden` argument to the symbol I want to hide, vapigen tells me "argument never used". In the examples I've seen a lot of cases of overwriting of symbols using custom vala files, but none of them are in the GIR format. Also, vapigen warns about the redefinition **before** the "never used" warning, It's obvious I'm doing something wrong, could it be the vapigen call? I used vapigen directly instead of the Autotools method, because I wrote it before that method was released and it's clearer. – ATColombini Sep 19 '13 at 12:29
  • 1
    @ATColombini please, as said there are two directories: metadata and packages. It's full of custom vala files under vapi/metadata for GIR. About the warning, use skip instead of hidden. Also try YourClass.yourproperty#property skip . Metadata syntax here: https://wiki.gnome.org/Vala/Manual/GIR%20metadata%20format – lethalman Sep 19 '13 at 19:39
  • @lethalman ok, I saw the directory before, I just didn't know I had to use `skip` instead of `hidden` in this case and I didn't find any use of `hidden` in the files under vapi/metadata directory. Thank you for your answers and patience. – ATColombini Sep 20 '13 at 09:11
0

(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).

Jussi Kukkonen
  • 13,857
  • 1
  • 37
  • 54
  • 1
    Thanks for your answer. The thing is that my methods are called from the `set`/`get_property` of the objects, I only want to know if I can translate them to avoid the work done by GObject and call my methods directly. I already use gobject-introspection to generate the gir files, and then the vapis are generated from these so (if there are any) I only need to add the annotations. – ATColombini Aug 19 '13 at 09:39