I want to use g_object_set with enum nick for enum property.
enum
{
PROP_0,
PROP_ENUM,
N_PROPERTIES
};
typedef enum
{
MY_ENUM_FOO,
MY_ENUM_BAR
} MyEnum;
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
static void
maman_bar_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
MamanBar *self = MAMAN_BAR (object);
switch (property_id)
{
case PROP_ENUM:
self->priv->enum = g_value_get_enum (value);
g_print ("enum: %u\n", self->priv->enum);
break;
default:
/* We don't have any other property... */
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
maman_bar_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
MamanBar *self = MAMAN_BAR (object);
switch (property_id)
{
case PROP_ENUM:
g_value_set_enum (value, self->priv->enum);
break;
default:
/* We don't have any other property... */
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
maman_bar_class_init (MamanBarClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->set_property = maman_bar_set_property;
gobject_class->get_property = maman_bar_get_property;
obj_properties[PROP_ENUM] =
g_param_spec_enum ("enum",
"Enum",
"Example enum",
my_enum_get_type();
MY_ENUM_FOO /* default value */,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
g_object_class_install_properties (gobject_class,
N_PROPERTIES,
obj_properties);
}
/************************************************/
/* Use */
/************************************************/
static const GEnumValue values[] = {
{ MY_ENUM_FOO, "MY_ENUM_FOO", "foo" },
{ MY_ENUM_BAR, "MY_ENUM_BAR", "bar" },
{ 0, NULL, NULL }
};
g_enum_register_static("MyEnum",values);
bar = g_object_new (MAMAN_TYPE_SUBBAR, NULL);
char* enum-nick = "bar"
g_object_set (G_OBJECT (bar), "enum", enum-nick, NULL);