[Vala] Chaining up constructors.



Dear Jurg and others,

I have an idea about chaining up constructors to share with you.

Situation
========
We have 2 different constructors, the construction block(CB), and the
construct function(CF).

for GType based classes, CB doesn't work, chaining up CF works.
for GObject based classes, CB works, chaining up CF doesn't.

We also have construct properties(CP); CF can and should initialize CPs,
CB can not and should not.

Whenever there is a CF, two CCode functions are generated: _construct
and _new. For CB, _constructor is generated.

The memory allocation is done in _construct.

New Idea
=======

The basic idea is to move memory allocation to _new functions, and let
_construct functions initialize the properties. It is then much easier
to chain up the _construct functions without worrying about memory
issues. The other idea is to manifestly invoke '_constructor' for GType
based classes. 

With these two changes, we can arrive the maxium symmetry between
GObject classes and GType classes. It also permits one to freely switch
the base type of any classes between GLib.Object and nothing without the
need of modifying any code.

For GObject based classes:
-----------------------
1. for each CF, generate a _new function,
Object _new(param1, param2, param3...) {
int n_params;
GParameter * params = _construct(&n_params, param1, ....);
return g_object_newv(TYPE, n_params, params);
}

2. for each CF, generate a _construct function,
GParameter * _construct(&n_params, param1, param2, param3) {
        translate all the logic in CF, if there is an assignment to a property
with getter and setter, create the correspoding GParameter
        chain up is also done here by merging the return value of the
parent's _construct function.
}

3. for each CB, generate the _constructor function, and hook it to the
GObject class structure.


For GType based classes:
-----------------------------
1. for each CF, generate a _new function,
Object * _new(param1, param2, param3...) {
int n_params;
Object * self = g_type_instance_new(.....);
_construct(self, param1, 2, 3);
_constructor(self);
return self;
}

2. for each CF, generate a _construct function,
void _construct(Object * self, param1, param2, param3) {
translate all logics in the CF, if there is an assignment to a property
with getter and setter, call the property setters.
        chain up is also done here by calling parent's _construct
function with parameters.
}

3. for each CB, generate the _constructor function.
void _constructor();


Cheers,

Yu




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]