Re: [Vala] Generating bindings for non-GLib/GObject based API



On Thu, Apr 22, 2010 at 11:47:21 +0200, Michael Wild wrote:
1) Constructor function: Say I would like to use a C function as the
constructor for a class (as e.g. newwin is used in curses.vapi). However,
that C function does not return the allocated and initialized object in the
return value, but as the first argument. The return value is an error code
instead. How would I handle this?

I believe you'll have to declare it as a static function and wrap it as
constructor in the .vapi itself.

I suppose you have a function like:

int thing_init(Thing **self, int param);

(allocate+return by reference implies pointer-to-pointer, right?)

Than in the class you first declare the function itself, like

class Thing {
    private static init(out Thing self, int param);

Than you need to define the constructor, which is where I am a bit lost.
It does work for normal methods, but constructor is a bit special.
I would guess this might work:

    public Thing(int param) throws ThingError {
        int status = init(this, param);
        if(status != 0)
            throw new ThingError.FAILED(
                "Thing construction failed with status %d", status);
    }

But I am not sure whether you need to initialize this or create normal
variable and return it or employ some other trick. Maybe defining another
static function and than rebinding it as constructor. Something like

    public static Thing new(int param) throws ThingError {
        Thing self;
        int status = init(self, param);
        ...
        return self;
    }
    public Thing(int param) throws (ThingError);

You'll have to play with it a bit.

Note, that I made it throw an error. I assume if it returns an error code,
you'll want to get it somehow and since the allocated object is the return
value, an exception is the only option.

2) Delegates: The C API defines a number of callback functions, most of
which take some user-defined data as an argument one registers along with
the callback function. However, the user data is often neither the first
nor the last argument. How can I wrap this?

Declare a delegate type with the [CCode(instance_pos)] as you've been
already instructed. Vala will auto-generate appropriate wrapper function to
reorder the arguments.

-- 
                                                 Jan 'Bulb' Hudec <bulb ucw cz>



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