After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 562545 - Add function taking / returning GValue
Add function taking / returning GValue
Status: RESOLVED FIXED
Product: gobject-introspection
Classification: Platform
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gobject-introspection Maintainer(s)
gobject-introspection Maintainer(s)
Depends on:
Blocks: 561664
 
 
Reported: 2008-11-28 10:29 UTC by Johan Bilien
Modified: 2015-02-07 16:58 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
GValue example (1.29 KB, patch)
2008-11-28 10:29 UTC, Johan Bilien
committed Details | Review

Description Johan Bilien 2008-11-28 10:29:10 UTC
gjs maps these to function taking / returning any JS type. This is useful for instance in a database binding when accessing / setting fields.
Comment 1 Johan Bilien 2008-11-28 10:29:41 UTC
Created attachment 123600 [details] [review]
GValue example
Comment 2 Johan (not receiving bugmail) Dahlin 2008-11-28 11:56:53 UTC
Comment on attachment 123600 [details] [review]
GValue example

>Index: tests/everything/everything.c

>+ * @v: (transfer none): a GValue expected to contain an int

>+int test_int_value_arg(const GValue *v) {
>+  int i;
>+
>+  i = g_value_get_int (v);
>+
>+  return i;

Transfer looks correct here.

>+static const GValue value;
>+/**
>+ * test_value_return:
>+ * @i: an int
>+ *
>+ * Return value: (transfer none): the int wrapped in a GValue.
>+ */
>+const GValue *test_value_return(int i) {
>+  g_value_init (&value, G_TYPE_INT);
>+  g_value_set_int (&value, i);
>+
>+  return &value;
>+}
>+

This function is leaking the GValue if you call it more than once, no?
I think it would be a better example to allocate the GValue on the heap and
mark the transfer as container or full.
Not sure if the GValue should be treated as a container or not, my gut feeling is that it should.
Comment 3 Johan Bilien 2008-11-28 12:09:52 UTC
(In reply to comment #2)
> (From update of attachment 123600 [details] [review] [edit])
> >Index: tests/everything/everything.c
> 
> >+ * @v: (transfer none): a GValue expected to contain an int
> 
> >+int test_int_value_arg(const GValue *v) {
> >+  int i;
> >+
> >+  i = g_value_get_int (v);
> >+
> >+  return i;
> 
> Transfer looks correct here.
> 
> >+static const GValue value;

hmm I should remove the const here

> >+/**
> >+ * test_value_return:
> >+ * @i: an int
> >+ *
> >+ * Return value: (transfer none): the int wrapped in a GValue.
> >+ */
> >+const GValue *test_value_return(int i) {
> >+  g_value_init (&value, G_TYPE_INT);
> >+  g_value_set_int (&value, i);
> >+
> >+  return &value;
> >+}
> >+
> 
> This function is leaking the GValue if you call it more than once, no?

Am I? I'm using a static variable, always the same

> I think it would be a better example to allocate the GValue on the heap and
> mark the transfer as container or full.

The problem with this is that the caller wouldn't know how to free it (there is no g_value_free

> Not sure if the GValue should be treated as a container or not, my gut feeling
> is that it should.
> 

Comment 4 Johan (not receiving bugmail) Dahlin 2008-12-02 11:07:22 UTC
No, but there is a g_value_unset.
Comment 5 Colin Walters 2008-12-02 13:42:06 UTC
I don't think anyone uses a const GValue return, do they?  There are only a few limited patterns for GValues in my experience.
Comment 6 Johan (not receiving bugmail) Dahlin 2008-12-02 14:03:37 UTC
Seems like quite a few do:

johan@cotovia:/usr/include$ find -name \*.h|xargs grep "const GValue"| wc -l
211
johan@cotovia:/usr/include$ find -name \*.h|xargs grep "GValue"| wc -l
568

So I guess it would make sense.
Comment 7 Johan Bilien 2008-12-02 15:04:56 UTC
(In reply to comment #6)
> Seems like quite a few do:
> 
> johan@cotovia:/usr/include$ find -name \*.h|xargs grep "const GValue"| wc -l
> 211
> johan@cotovia:/usr/include$ find -name \*.h|xargs grep "GValue"| wc -l
> 568
> 
> So I guess it would make sense.
> 

I guess these are mostly const GValue passed as argument, not returned.


Our usecase is quite specific, we use const GValue as the return value for a const GValue *ResultSet::getField method in our DB wrapper, where the GValue are owned by the object. If you think this is too specific I could move this test to gjs.

The test_value_return doesn't leak I think, since the value is set to an int unset doesn't release anything.
Comment 8 Dan Winship 2008-12-02 15:30:52 UTC
(In reply to comment #7)
> Our usecase is quite specific, we use const GValue as the return value for a
> const GValue *ResultSet::getField method in our DB wrapper, where the GValue
> are owned by the object. If you think this is too specific I could move this
> test to gjs.

This is essentially the same as glib's:

GValue*		g_value_array_get_nth	     (GValueArray	*value_array,
					      guint		 index_);

except that glib doesn't specify const (but it *is* (transfer none)). So it's not totally crazy.

Your example test function *is* totally crazy though :-).
Comment 9 Colin Walters 2008-12-02 15:40:45 UTC
Of course there are lot of functions like this in GLib in particular, the question is are they interesting to bind automatically?

GValue in general seems like something one would want to treat specially in a binding, thus transitively GValueArray.
Comment 10 Johan Bilien 2008-12-09 15:30:42 UTC
Committed (with a fix to reset to GValue to 0)
Comment 11 André Klapper 2015-02-07 16:58:17 UTC
[Mass-moving gobject-introspection tickets to its own Bugzilla product - see bug 708029. Mass-filter your bugmail for this message: introspection20150207 ]