Re: [Vala] Properly wrapping callbacks



Michael 'Mickey' Lauer wrote:
Thanks for your mails, I think the VAPI part is clear to me now. Unfortunately 
I still can't do the actual setting of the callback. I have:


=========== gsm0710_p.h ===========

typedef    int     (*gsm0710_context_at_command_callback)(struct 
gsm0710_context *ctx, const char *cmd);
...
struct gsm0710_context
{
    /* variables */
...
    gsm0710_context_at_command_callback at_command;
...
}

========= gsm0710.vapi ===========
        [CCode (instance_pos = 0)]
        public delegate int AtCommandCallback( string cmd );

========= multiplexer.vala ==========
public class Multiplexer : Object
{
    Context ctx;

    public static int at_command_cb( Context ctx, char* command )
    {
        Multiplexer m = (Multiplexer) ctx.user_data;
        debug( "CB: atCommand %s", (string)command );
        return 1;
    }

    public Multiplexer( bool advanced, int framesize, string device, int 
portspeed )
    {
        debug( "Multiplexer created for mode %s, framesize %d, device %s 
@ %d", advanced? "advanced":"basic", framesize, device, portspeed );
        ctx = new Context();
        ctx.initialize();

        ctx.server = 1;
        ctx.mode = advanced? 1 : 0;
        ctx.frame_size = framesize;
        ctx.port_speed = portspeed;

        ctx.at_command = at_command_cb;
    }
}

I now get the following error:

make[1]: Leaving directory `/local/pkg/fso/fso-gsm0710muxd'
/usr/local/bin/valac -c --vapidir gsm0710 --pkg dbus-glib-1 --pkg 
gsm0710 --save-temps --basedir . src/consts.vala src/multiplexer.vala 
src/server.vala src/main.vala
src/multiplexer.vala:53.9-53.38: error: Assignment: Cannot convert from 
`Multiplexer.at_command_cb' to `Gsm0710.Context.AtCommandCallback'
        ctx.at_command = at_command_cb;
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)

What am I missing?

Cheers,

Mickey.
_______________________________________________
Vala-list mailing list
Vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list


If you give it a different name ('AtCommandCallback' instead of
'at_command_callback') you should specify a 'cname'-attribute with the
original C name. And make the delegate static, as Jürg wrote, and pass
the context explicitly, since it is not intended as instance data
('this') but as callback source object.

-----------

[CCode (cname="gsm0710_context_at_command_callback")]
public static delegate int AtCommandCallback( Context ctx, string cmd );

-----------

public static int at_command_cb( Context ctx, string command )
{
    Multiplexer m = (Multiplexer) ctx.user_data;
    debug( "CB: atCommand %s", command );
    return 1;
}

// ...


Regards,

Frederik



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