Murloc
August 29, 2022, 3:33pm
1
how can i get the arguments passed when creating an object?
I tried this but I get errors:
foo.h
#ifndef FOO_H
#define FOO_H
#include <glib-object.h>
G_BEGIN_DECLS
#define E_TYPE_FOO e_foo_get_type()
G_DECLARE_DERIVABLE_TYPE (EFoo, e_foo, E, FOO, GObject)
struct _EFooClass{
GObjectClass parent_class;
};
EFoo* e_foo_new(int a);
G_END_DECLS
#endif //FOO_H
foo.c
#include <stdio.h>
#include "foo.h"
G_DEFINE_TYPE (EFoo, e_foo, G_TYPE_OBJECT)
static void e_foo_class_init(EFooClass* self){
}
static void e_foo_init(EFoo* self)
{
int a;
g_object_get(self, "a", &a, NULL);
printf("%d\n",a);
}
EFoo* e_foo_new(int a){
return g_object_new(E_TYPE_FOO,"a",a, NULL);
}
errors:
(process:2010015): GLib-GObject-CRITICAL **: 22:29:31.151: g_object_new_is_valid_property: object class ‘EFoo’ has no property named ‘a’
(process:2010015): GLib-GObject-WARNING **: 22:29:31.153: g_object_get_is_valid_property: object class ‘EFoo’ has no property named ‘a’
how to get arguments correctly?
ebassi
(Emmanuele Bassi)
August 29, 2022, 3:35pm
2
You need to add properties to the object: GObject – 2.0: Type System Concepts
1 Like
Murloc
August 29, 2022, 4:15pm
3
i changed the code and it partly works but i get 0 in e_foo_init
and only then 10 is assigned. can i get the argument value in e_foo_init
function?
#include <stdio.h>
#include "foo.h"
struct _EFooPrivate
{
int a;
};
G_DEFINE_TYPE_WITH_PRIVATE (EFoo, e_foo, G_TYPE_OBJECT)
typedef enum
{
A = 1,
N_PROPERTIES
} Property;
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
static void e_foo_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
EFoo* self = E_FOO(object);
switch ((Property) property_id)
{
case A:
int a = g_value_get_int (value);
printf("%d\n", a);
EFooPrivate *priv;
priv = e_foo_get_instance_private (E_FOO(object));
priv->a = a;
break;
default:
/* We don't have any other property... */
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void e_foo_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
EFoo *self = E_FOO (object);
switch ((Property) property_id)
{
case A:
EFooPrivate *priv;
priv = e_foo_get_instance_private (E_FOO(object));
g_value_set_int (value, priv->a);
break;
default:
/* We don't have any other property... */
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void e_foo_class_init(EFooClass* self){
GObjectClass *object_class = G_OBJECT_CLASS (self);
object_class->set_property = e_foo_set_property;
object_class->get_property = e_foo_get_property;
obj_properties[A] =
g_param_spec_int ("a",
"a",
"the a",
-100,
100,
0 /* default value */,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
g_object_class_install_properties (object_class,
N_PROPERTIES,
obj_properties);
}
static void e_foo_init(EFoo* self)
{
int a;
g_object_get(self, "a", &a, NULL);
printf("new %d\n",a);
}
EFoo* e_foo_new(int a){
return g_object_new(E_TYPE_FOO,"a",a, NULL);
}
when an object is created, it prints:
new 0
10
main.c
#include "foo.h"
int main() {
EFoo* foo = e_foo_new(10);
return 0;
}
ebassi
(Emmanuele Bassi)
August 29, 2022, 4:36pm
4
No. The instance initialization is used precisely for that: instance initialization. Properties are applied after that.
If you have a constructor property, you want to override GObjectClass.constructed
; at at point, the value of constructor and constructor-only properties will be set.
I strongly encourage you to read the GObject documentation.
1 Like
system
(system)
Closed
September 28, 2022, 4:36pm
5
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.