Re: [Vala] posix: FILE binding



Abderrahim Kitouni <a kitouni gmail com> writes:

2009/11/20, Michael 'Mickey' Lauer <mickey vanille-media de>:
While trying to add popen(3) to posix.vapi, I stumbled
over the current binding of FILE, which looks incorrect
to me. All the functions that operate on FILE seem to
take a FILE*, so binding it as [SimpleType] defeats this
purpose.
I think it's wrong to define it as a struct in the first place (let
alone as a simple type) :  it's an opaque struct in C, so it should be
a compact class (and it is in glib-2.0.vapi)

Thanks to all involved in adding popen.

The next problem is that FILE and FileStream are exactly the same on a C
level, but completely unrelated on the Vala level, so passing popened
file to a function expecting FileStream (cat_line in the example below)
currently requires explicit casting.

But if we treat FILE as FileStream but with posix specific additions
(thus in that sense a subclass) than it may be reasonable to define FILE
as a subclass of FileStream in the GOBJECT profile then at least one way
conversion is possible without explicit casting (last line in main()
below).  Attached patch does that, it could be modify to inherit
identical methods in GOBJECT and only declare then in FILE in other
profiles.

My immediate use case is to to call FileStream.read_line on popened
file.

Is there a way to make FILE and FileStream two way convertible?

example:

void cat_line(GLib.FileStream f)
{
    GLib.stdout.printf("%s\n", f.read_line());
}

void main()
{
    cat_line((GLib.FileStream)Posix.FILE.popen("echo hello", "r"));

    // fails as now FILE and FileStream are unrelated
    // atached patch makes this possible in GOBJECT profile
    cat_line(Posix.FILE.popen("echo hello", "r"));
}


Abderrahim
From 84ffa1328ed99274ab7b78522de301f02ca67121 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Pankowski?= <lukpank o2 pl>
Date: Fri, 20 Nov 2009 23:31:05 +0100
Subject: [PATCH] subclass Posix.FILE from FileStream in GOBJECT profile

---
 vapi/posix.vapi |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/vapi/posix.vapi b/vapi/posix.vapi
index 651ba38..25e510b 100644
--- a/vapi/posix.vapi
+++ b/vapi/posix.vapi
@@ -1995,7 +1995,11 @@ namespace Posix {
 
        [Compact]
        [CCode (cname = "FILE", free_function = "fclose", cheader_filename = "stdio.h")]
+#if GOBJECT
+       public class FILE : GLib.FileStream {
+#else
        public class FILE {
+#endif
                [CCode (cname = "EOF", cheader_filename = "stdio.h")]
                public const int EOF;
                [CCode (cname = "SEEK_SET", cheader_filename = "stdio.h")]
-- 
1.6.5.3



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