Re: [Vala] How to wrap select(2)'s fd_set?



On Fri, 2009-01-30 at 02:01 +0100, Michael 'Mickey' Lauer wrote:
Ok, here's what I have so far:

----------------
    [CCode (cname = "fd_set", cheader_filename = "sys/select.h", free_function 
= "")]
    [Compact]
    public class FdSet
    {
        [CCode (cname = "FD_CLR", instance_pos=1.1)]
        public void clear (int fd);
        [CCode (cname = "FD_ISSET", instance_pos=1.1)]
        public bool isSet (int fd);
        [CCode (cname = "FD_SET", instance_pos=1.1)]
        public void set (int fd);
        [CCode (cname = "FD_ZERO")]
        public void zero ();
    }

FdSet should be a struct, not a class, we do not need reference type
semantics here. This eliminates unnecessary heap allocation. FD_ZERO
should be bound as initialization method as it just zeroes the streuct.

[CCode (cname = "fd_set", cheader_filename = "sys/select.h"]
public struct FdSet {
    [CCode (cname = "FD_CLEAR")]
    public FdSet ();
    ...
}

I'm trying to use this like that:
----------------
        var readfds = new Posix.FdSet();
        var writefds = new Posix.FdSet();
        var exceptfds = new Posix.FdSet();
        Posix.TimeVal t = { 0, 0 };
        int maxfd = 10;
        int result = Posix.select( maxfd, readfds, writefds, exceptfds, t );
----------------

When changed to a struct, it should be used as follows (untested):

    var readfds = Posix.FdSet ();
    var writefds = Posix.FdSet ();
    var exceptfds = Posix.FdSet ();
    Posix.TimeVal t = { 0, 0 };
    int maxfd = 10;
    int result = Posix.select (maxfd, ref readfds, ref writefds, ref exceptfds, ref t);


The code compiles, but emits two warnings:

src/multiplexer.vala:162.13-162.75: warning: result variable type incompatible 
with return type

This warning is currently completely harmless. Vala prints this warning
if you declare a local variable called `result' whose type is different
from the return type of the method where the variable has been declared.
The reason is that I am considering introducing an implicit `result'
variable for the return value. This might be useful in some cases, for
example, in connection with method postconditions.

Jürg




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