Re: [Vala] using a non-glib C library in vala



On Wed, Sep 16, 2009 at 10:33:22 -0400, Michael B. Trausch wrote:
Mr. Maxwell, sorry for the dupe.  Forgot to cc the list.

On Tue, 2009-09-15 at 17:14 -0500, Mr. Maxwell . wrote:
What do I do with the typedef struct declarations? This is what I have
but I don't think it's right.

c code
  typedef struct { char _dummy; } ID3Tag;

vala code
    [CCode(cname = "typedef struct")]
    public struct ID3Tag { private char _dummy; } 

The type name for a non-typedef'd struct would be:

struct struct_name

Whereas the name for a typedef'd one is simply the name of the type.

That's correct. Except the bigger problem is, that it's not a struct.

Why? Well, because the real content of the struct is not defined, so it
cannot be passed by value -- which is a meaning of 'struct' in vala. It must
be passed by reference, which is signified by using 'class'.

So, for example, let's say that we have a structure that contains a IP
network address and could hold either an IPv4 or IPv6 address in C.
This could look like:

typedef enum _ip_address_type_t {
  IP_ADDRESS_V4,
  IP_ADDRESS_V6
} ip_address_type_t;

typedef struct _ip_address_t {
  ip_address_type_t type;
  unsigned char address[16];
} ip_address_t;

In Vala, this would be bound as (assuming this is in
ip_address_type.h): 

[CCode(cheader_filename = "ip_address_type.h",
       cprefix = "",
       c_lowercase_prefix = "")]
namespace Native {
  [CCode(cprefix = "IP_ADDRESS_")]
  public enum IpAddressType {
    V4,
    V6
  }

  [CCode(cname = "ip_address_t")]
  public struct IpAddress {
    public IpAddressType type;
    public uchar address[16];
  }
}
[...]
However, there is one small problem:  You can *not* use the structure as
a [SimpleType], due to a bug in Vala (see bug 588280).  If you attempt

Why do you think it is a [SimpleType]. SimpleTypes are only the built-in
non-structured types (int, char, float...) or anything typedefed to them
(size_t, ssize_t, time_t...). Anything defined using the struct keyword is
not one.

to do so, Vala will generate an incorrect .c code file to feed to gcc,
and gcc will (correctly) error out, stating that memset was not passed
enough parameters.  This was deemed to be not a problem, though in my
personal opinion, Vala should either throw an error or generate correct
C code, not generate incorrect C code to be fed to the compiler.

However, I don't know how to fix the issue in valac, so I can't really
reopen the bug.  Just keep in mind that using a SimpleType-decorated
struct is impossible in Vala, even if that is what you would do in C.

Vala should not generate invalid code, but this bug is indeed invalid as
described.

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



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