Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 10, 2005 13:19:38

Jacob K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

GObject constructors


I'm having problems with figuring out how to initialize my data properly.
My scenario is that I have:

struct Parent {
GObject parent;
float *data;
}

struct ParentClass {
GObjectClass parent;
int data_size;
}

struct Child {
Parent parent;
}

struct ChildClass {
ParentClass parent;
}Each subclass of Parent will use different, but fixed data sizes, so Iwant Child to tell Parent how muchdata it needs, and then let Parent initialize the data accordingly.

The way I thought would work was this:

1. In child_class_init(*klass):
PARENT_CLASS(klass)->data_size = specific_size_for_this_subclass;
2. In parent_instance_init(*obj)
klass = PARENT_GET_CLASS(obj);
obj->data = g_new(float, klass->data_size);but this wont work since PARENT_GET_CLASS(obj) in parent_instance_init()returns the class for Parent,not the subclass ChildClass, so data_size won't have the value I intended.

Any help would be gladly appreciated.

Regards Jacob

_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#2 Nov. 10, 2005 19:08:40

Daniel E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

GObject constructors


2005/11/9, Jacob Kroon <>:
> I'm having problems with figuring out how to initialize my data properly.
> My scenario is that I have:
>
> struct Parent {
> GObject parent;
> float *data;
> }
>
> struct ParentClass {
> GObjectClass parent;
> int data_size;
> }
>
> struct Child {
> Parent parent;
> }
>
> struct ChildClass {
> ParentClass parent;
> }
>
> Each subclass of Parent will use different, but fixed data sizes, so I
> want Child to tell Parent how much
> data it needs, and then let Parent initialize the data accordingly.
>
> The way I thought would work was this:
>
> 1. In child_class_init(*klass):
> PARENT_CLASS(klass)->data_size = specific_size_for_this_subclass;
> 2. In parent_instance_init(*obj)
> klass = PARENT_GET_CLASS(obj);
> obj->data = g_new(float, klass->data_size);
>
> but this wont work since PARENT_GET_CLASS(obj) in parent_instance_init()
> returns the class for Parent,
> not the subclass ChildClass, so data_size won't have the value I intended.
As I know:

When you create a Class, you'll need to define macros for convertion
from a derived class to the class you want. Then in
parent_instance_init you'll need to Get the data defined in the parent
and define the value on it:

In parent.h definition of the class you'll need unleast the following
definition:

#define PARENT(obj) (G_TYPE_CHECK_INSTANCE_CAST (obj, PARENT, Parent))

Then now you can CAST from a derived class instance to the parent
instance and have access to the parent data (now part of the child
instance), then:

PARENT(obj)->data = g_new(float, klass->data_size);

will set the value of 'data', but MUST be in the
child_intances_init(); at this point when you create a new Parent
derived object (Child in your example), it will init him selft, and
can init the parent data.
_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#3 Nov. 10, 2005 19:22:42

David N.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

GObject constructors


On Wed, Nov 09, 2005 at 06:43:17PM +0100, Jacob Kroon wrote:
> I'm having problems with figuring out how to initialize my data properly.
> My scenario is that I have:
>
> struct Parent {
> GObject parent;
> float *data;
> }
>
> struct ParentClass {
> GObjectClass parent;
> int data_size;
> }
>
> struct Child {
> Parent parent;
> }
>
> struct ChildClass {
> ParentClass parent;
> }
>
> Each subclass of Parent will use different, but fixed data sizes, so I
> want Child to tell Parent how much
> data it needs, and then let Parent initialize the data accordingly.
>
> The way I thought would work was this:
>
> 1. In child_class_init(*klass):
> PARENT_CLASS(klass)->data_size = specific_size_for_this_subclass;
> 2. In parent_instance_init(*obj)
> klass = PARENT_GET_CLASS(obj);
> obj->data = g_new(float, klass->data_size);
>
> but this wont work since PARENT_GET_CLASS(obj) in parent_instance_init()
> returns the class for Parent,
> not the subclass ChildClass, so data_size won't have the value I intended.

I had similar problem and came to conclusion it's impossible
to implement it this way, or at least it's against inheritance.

However, you can keep 1. as it is and define a parent_instance_setup()
method that contains the common code and call that from
child constructor:

2a. parent_instance_setup(obj) {
klass = PARENT_GET_CLASS(obj)
obj->data = g_new(float, klass->data_size);
}

2b. child_instance_init(obj) {
parent_instance_setup(obj);
}

Yeti


--
That's enough.
_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#4 Nov. 10, 2005 19:33:24

Dave B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

GObject constructors


On Thu, Nov 10, 2005 at 08:21:46PM +0100, David Necas (Yeti) wrote:
> On Wed, Nov 09, 2005 at 06:43:17PM +0100, Jacob Kroon wrote:
> > I'm having problems with figuring out how to initialize my data properly.
> > My scenario is that I have:
> >
> > struct Parent {
> > GObject parent;
> > float *data;
> > }
> >
> > struct ParentClass {
> > GObjectClass parent;
> > int data_size;
> > }
> >
> > struct Child {
> > Parent parent;
> > }
> >
> > struct ChildClass {
> > ParentClass parent;
> > }
> >
> > Each subclass of Parent will use different, but fixed data sizes, so I
> > want Child to tell Parent how much
> > data it needs, and then let Parent initialize the data accordingly.
> >
> > The way I thought would work was this:
> >
> > 1. In child_class_init(*klass):
> > PARENT_CLASS(klass)->data_size = specific_size_for_this_subclass;
> > 2. In parent_instance_init(*obj)
> > klass = PARENT_GET_CLASS(obj);
> > obj->data = g_new(float, klass->data_size);
> >
> > but this wont work since PARENT_GET_CLASS(obj) in parent_instance_init()
> > returns the class for Parent,
> > not the subclass ChildClass, so data_size won't have the value I intended.
>
> I had similar problem and came to conclusion it's impossible
> to implement it this way, or at least it's against inheritance.
>
> However, you can keep 1. as it is and define a parent_instance_setup()
> method that contains the common code and call that from
> child constructor:
>
> 2a. parent_instance_setup(obj) {
> klass = PARENT_GET_CLASS(obj)
> obj->data = g_new(float, klass->data_size);
> }
>
> 2b. child_instance_init(obj) {
> parent_instance_setup(obj);
> }


actually, you can take advantage of the fact that
real prototype of GInstanceInitFunc is

typedef void (*GInstanceInitFunc) (GTypeInstance *instance,
gpointer g_class);

so, instead of using G_DEFINE_TYPE do it manually and use the
fuller prototype:

static void
parent_init (Parent *parent, ParentClass *real_class)
{
/* Real class will point to the concrete class instance,
ie the child's class. */
parent->data = g_new (float, real_class->size);
}

GType parent_get_type()
{
static GType io_type = 0;
if (!io_type)
{
static const GTypeInfo io_info =
{
sizeof(ParentClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gsk_io_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (Parent),
0, /* n_preallocs */
(GInstanceInitFunc) gsk_io_init,
NULL /* value_table */
};
io_type = g_type_register_static (G_TYPE_OBJECT, "Parent",
&io_info, G_TYPE_FLAG_ABSTRACT);
}
return io_type;
}

etc
_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#5 Nov. 10, 2005 19:50:03

David N.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

GObject constructors


On Thu, Nov 10, 2005 at 11:33:04AM -0800, Dave Benson wrote:
>
> actually, you can take advantage of the fact that
> real prototype of GInstanceInitFunc is
>
> typedef void (*GInstanceInitFunc) (GTypeInstance *instance,
> gpointer g_class);

Nice! Still an inheritance violation, but now an official
one ;-)

Yeti


--
That's enough.
_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

Board footer

Moderator control

Enjoy the 20th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support