Re: [Vala] canberra.vapi

On Sat, 11 Jul 2009, Michael 'Mickey' Lauer wrote:

Thanks, that's slightly better, still I'd love to have a classical constructor
way of creating a context, like:

var c = new Canberra.Context();

Is this impossible without changes in the library itself?

You can (with a bit of work) create APIs that are more object-oriented. You have to do a bit of extra work, though, and the function has to return a pointer to a structure (at least, AFAIK). For example, the xlib function:

 Display *XOpenDisplay(char *d);

is a candidate for a constructor call to a Display class:

[CCode(cprefix = "", lower_case_cprefix = "",
       cheader_filename = "X11/Xlib.h")]
namespace XLib {
 public struct Window {}

 [CCode(cprefix = "", lower_case_cprefix = "",
        free_function = "XCloseDisplay", )]
 public class Display {
   [CCode(cname = "XOpenDisplay")]
   public Display(string? name = null);

   [CCode(cname = "XDefaultRootWindow")]
   public Window get_default_root_window();

That creates a binding that permits you to do:

 XLib.Display d = new XLib.Display();
 XLib.Window w = d.get_default_root_window();

In an object-oriented fashion.

Essentially, you can treat any C code in an object-oriented way if it has a function that initializes a structure and returns a pointer to it, where that structure's pointer is passed to the functions which are not "public static" in the compact class definition.


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