[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: instance_init() and the realize() functions
From: |
Liviu Ionescu |
Subject: |
Re: instance_init() and the realize() functions |
Date: |
Sat, 18 Jul 2020 11:26:44 +0300 |
> On 18 Jul 2020, at 10:09, Pratik Parvati <pratikp@vayavyalabs.com> wrote:
>
> The class_init() function is straight forward (it is similar to the
> constructor in C++ OOP
The C++ constructor initialises class **instances**, i.e. C++ objects, not C++
classes.
In QEMU, the OOP functionality is implemented with nested structures used to
store class and instance definitions, and callbacks as virtual methods.
The .class_init callbacks are called early, by a mechanism similar to C++
static constructors, and they initialise the structures used to store the class
definitions. They are recursively chained, i.e. first the parent callback is
called to initialise the parent structure members, then the current callback is
called, to fill in its own members.
The .instance_init callback are automatically called when new **instances** of
a class are created. Similarly, they are also recursively chained.
.instance_init may also create children objects, recursively.
.realize is a bit trickier. If .instance_init is the very first thing
automatically called when creating an object, .realize is the very last thing,
it is called manually when the whole hierarchy of objects is created and it
signals that everything is ready, a kind of 'rien ne va plus'.
.realize usually has to manually call the parent .realize.
---
For me it was difficult to draw a line of what initialisations should be done
in .instance_init and what in .realize, but given that .realise is called when
the whole hierarchy is ready, it might do links between objects, which are not
available when .instance_init is called.
A simple rule would be for .instance_init to ensure that all members have
default values and properties associated with them, and defer all functional
initialisations to .realize.
Hope this helps,
Liviu