Re: my_object_new_from_file constructor



On Fri, 2010-01-22 at 02:20 +0300, Artur Galyamov wrote:
> 21.01.10, 23:40, "Martin Kalbfuß" <ma kalbfuss web de>:
> > Hi,
> > I've set up an object, based on GObject. It's working, but I'm confused
> > about creating constructors. My head hurts. Can anyone give me a small
> > code example as starting point. That would be very kind.
> > Thanks, 
> 
> MyObject *
> my_object_new(void)
> {
>     return MY_OBJECT(g_object_new(MY_TYPE_OBJECT, NULL));
> }

the case is not needed: g_object_new() returns a gpointer, and C will
implicit cast it to the return value:

  MyObject *
  my_object_new (void)
  {
    return g_object_new (MY_TYPE_OBJECT, NULL);
  }


> MyObject *
> my_object_new_from_file(const char *name)
> {
>     MyObject *object;
> 
>     object = MY_OBJECT(g_object_new(MY_TYPE_OBJECT, NULL));
>     my_object_load_from_file(MY_OBJECT(object), name);
>     return object;
> }

you *must* have a very strong argument for making your construction
function invoke another method internally, instead of just using a
constructor property. language bindings will almost never call your
constructor function, preferring a direct call to g_object_newv()
instead.

so the code above should really be:

  MyObject *
  my_object_new_from_file (const char *path)
  {
    return g_object_new (MY_TYPE_OBJECT, "file", path, NULL);
  }

> 2all: I have a perl-script that generates GObject templates; tell if
> you need it here (I have no idea how to attach it -- base64?)

there are lots and lots of code generators for GObject; you can use
Gob2, or Turbine[0]. you could even argue that Vala (even though is a
high level language in and of itself) is a code generator.

ciao,
 Emmanuele.

[0] http://blogs.gnome.org/thos/2009/05/28/introducing-turbine/

-- 
W: http://www.emmanuelebassi.name
B: http://blogs.gnome.org/ebassi



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