How to store the value of a Gvalue of a property in a gchar*

After I was reading the documentation of Gvalue, GType, GparamSpec and Parameters and Values I noticed that I can not figure out how to store the value of a property in the Text format.

A this point with the following piece of code:

#include <gtk/gtk.h>

int main ( void )
{
    GtkWidget *button;

    GParamSpec *property;
    const GValue *default_prop_value;
    GType prop_type_value;

    /// ***
    gtk_init ( NULL, NULL );

    /// ***
    button  = gtk_button_new_with_label ( "Click" );

    /// *** Find the Property
    property = g_object_class_find_property ( G_OBJECT_GET_CLASS ( button ), "relief" );

    /// *** Get the name of the GparamSpec Instance
    const gchar *par_spec_type_name = G_PARAM_SPEC_TYPE_NAME ( property );
    g_print( "The GparamSpec Instance =\t%-10s\n", par_spec_type_name );

    /// *** Get the default value of the Label Property
    default_prop_value = g_param_spec_get_default_value ( property );

    /// *** Get the Type of the default value of the Property
    prop_type_value = G_PARAM_SPEC_VALUE_TYPE( property );

    /// *** Get the name of the Property
    const gchar *param_spec_name = g_param_spec_get_name ( property );
    g_print ( "The property name       =\t%-10s\n", param_spec_name );

    /// *** Get the Name of the type of the Property
    const gchar *prop_type_name = g_type_name( prop_type_value );
    g_print ( "The Value type          =\t%s\n", prop_type_name );

    /// *** Get the type of value
    GType type_default_value =  G_VALUE_TYPE ( default_prop_value );
    const gchar *value_type  = g_type_name ( type_default_value );
    g_print( "The Value type          =\t%s\n", value_type );

    gtk_main ();
}

I found out the Following:

The GparamSpec Instance =	GParamEnum
The property name       =	relief    
The Value type          =	GtkReliefStyle
The Value type          =	GtkReliefStyle

Now lets take for example the GtkButton which has the property “relief” and its default value during its creation is “GTK_RELIEF_NORMAL”.

Now with the help of the function:

g_param_spec_get_default_value()

I can retrive its default value, but I cannot fugure out how to print it to the screen in Text mode, something like:

The Default Value of the property is "GTK_RELIEF_NORMAL".

Same happens with the actual value, for example if I set its “relief” property to “GTK_RELIEF_NONE” I can not figure out how to print it like:

The actual Value of the property is “GTK_RELIEF_NONE”.

After I was reading the documentation of Gvalue, GType, GparamSpec and
Parameters and Values I noticed that I can not figure out how to store
the value of a property in the Text format.

Short answer is: you can’t.

Long answer is: The thing that made me say “you can’t” is that a GValue
can hold raw pointers and whatnot, and it doesn’t make sense to store
those. For the same reason, you cannot really serialize all GObject either.
This pretty much answers the question why there isn’t
g_value_to_string()/g_value_from_string() functions.

You can somewhat g_value_transform() [1], but it explicitly tells you
not to for anything else than debugging. Same goes for
g_strdup_value_contents().

However, you can partly do this for a selected set of GTypes. You’ll
have to handle some of those manually, but it might give you what you want.

[1]
https://developer.gnome.org/gobject/stable/gobject-Generic-values.html#g-value-transform

[…]

I can retrive its default value, but I cannot fugure out how to print it
to the screen in Text mode, something like:

The Default Value of the property is “GTK_RELIEF_NORMAL”. |

You can use g_enum_get_value() [2], see
https://developer.gnome.org/gobject/stable/gobject-Enumeration-and-Flag-Types.html#gobject-Enumeration-and-Flag-Types.description

[2]
https://developer.gnome.org/gobject/stable/gobject-Enumeration-and-Flag-Types.html#g-enum-get-value

1 Like

Well I was nut sure about that, but now reading your Answer I know.

Long answer is: The thing that made me say “you can’t” is that a GValue
can hold raw pointers and whatnot, and it doesn’t make sense to store
those. For the same reason, you cannot really serialize all GObjects either.

Well I tried something and works (partially), but like you said it is non sense for all Objects.
Thank you for your Time.

You actually can print it as text by using g_enum_to_string(), which should work for any property that uses GParamSpecEnum. (Or, if it uses GParamSpecFlags, use g_flags_to_string().)

If you ever want to use your own enum type in a property and need to generate the necessary GType goo to do so, use glib-mkenums.

You actually /can/ print it as text by using g_enum_to_string(), which
should work for any property that uses GParamSpecEnum. (Or, if it uses
GParamSpecFlags, use g_flags_to_string().)

The thing is the docs explicitly tell you it’s only intended for
debugging purposes and what this function outputs might change without
notice:

https://developer.gnome.org/gobject/stable/gobject-Enumeration-and-Flag-Types.html#g-enum-to-string

This is intended to be used for debugging purposes. The format of the
output may change in the future.

So I’d rather do it a little more manually and not to rely on something
that might break under my feet.

6 posts were split to a new topic: Accessing values of enumeration types

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.