Received: from eggs.gnu.org ([208.118.235.92]:56016) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tvjxr-0000uH-52 for qemu-devel@nongnu.org; Thu, 17 Jan 2013 02:32:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tvjxp-0007p7-Mh for qemu-devel@nongnu.org; Thu, 17 Jan 2013 02:32:43 -0500 Received: from cantor2.suse.de ([195.135.220.15]:32880 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tvjxp-0007ls-AL for qemu-devel@nongnu.org; Thu, 17 Jan 2013 02:32:41 -0500 From: =?UTF-8?q?Andreas=20F=C3=A4rber?= Date: Thu, 17 Jan 2013 08:31:50 +0100 Message-Id: <1358407910-6031-1-git-send-email-afaerber@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH for-1.4] qom: Extend documentation on QOM method concepts List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: ehabkost@redhat.com, anthony@codemonkey.ws, =?UTF-8?q?Andreas=20F=C3=A4rber?= Add a documentation section "Methods" and discuss among others how to handle overriding virtual methods. Clarify DeviceClass::realize documentation and refer to the above. Signed-off-by: Andreas F=C3=A4rber --- hw/qdev-core.h | 14 +++++-- include/qom/object.h | 104 ++++++++++++++++++++++++++++++++++++++++++++= ++++-- 2 Dateien ge=C3=A4ndert, 111 Zeilen hinzugef=C3=BCgt(+), 7 Zeilen entfer= nt(-) diff --git a/hw/qdev-core.h b/hw/qdev-core.h index 3d75ae2..731aadd 100644 --- a/hw/qdev-core.h +++ b/hw/qdev-core.h @@ -60,14 +60,20 @@ struct VMStateDescription; * The @init callback is considered private to a particular bus implemen= tation * (immediate abstract child types of TYPE_DEVICE). Derived leaf types s= et an * "init" callback on their parent class instead. + * * Any type may override the @realize and/or @unrealize callbacks but ne= eds - * to call (and thus save) the parent type's implementation if so desire= d. - * Usually this means storing the previous value of, e.g., @realized ins= ide - * the type's class structure and overwriting it with a function that fi= rst - * invokes the stored callback, then performs any additional steps. + * to call the parent type's implementation if keeping their functionali= ty + * is desired. Refer to QOM documentation for further discussion and exa= mples. + * + * + * * If a type derived directly from TYPE_DEVICE implements @realize, it d= oes * not need to implement @init and therefore does not need to store and = call * #DeviceClass' default @realize callback. + * For other types consult the documentation and implementation of the + * respective parent types. + * + * */ typedef struct DeviceClass { /*< private >*/ diff --git a/include/qom/object.h b/include/qom/object.h index 1ef2f0e..8e16ea8 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -147,9 +147,9 @@ typedef struct InterfaceInfo InterfaceInfo; * * * - * Introducing new virtual functions requires a class to define its own - * struct and to add a .class_size member to the TypeInfo. Each functio= n - * will also have a wrapper to call it easily: + * Introducing new virtual methods requires a class to define its own + * struct and to add a .class_size member to the #TypeInfo. Each method + * will also have a wrapper function to call it easily: * * * Defining an abstract class @@ -186,6 +186,104 @@ typedef struct InterfaceInfo InterfaceInfo; * similar to normal types except for the fact that are only defined by * their classes and never carry any state. You can dynamically cast an= object * to one of its #Interface types and vice versa. + * + * # Methods # + * + * A method is a function within the namespace scop= e of + * a class. It usually operates on the object instance by passing it as = a + * strongly-typed first argument. + * If it does not operate on an object instance, it is dubbed + * class method. + * + * Methods cannot be overloaded. That is, the #ObjectClass and method na= me + * uniquely identity the function to be called; the signature does not v= ary + * except for trailing varargs. + * + * Methods are always virtual. Overriding a method = in + * #TypeInfo.class_init of a subclass leads to any user of the class obt= ained + * via OBJECT_GET_CLASS() accessing the overridden function. + * The original function is not automatically invoked. It is the respons= ability + * of the overriding class to determine whether and when to invoke the m= ethod + * being overridden. + * + * To invoke the method being overridden, the preferred solution is to s= tore + * the original value in the overriding class before overriding the meth= od. + * This corresponds to |[ {super,base}.method(...) ]| in Java and C# + * respectively; this frees the overriding class from hardcoding its par= ent + * class, which someone might choose to change at some point. + * + * + * Overriding a virtual method + * + * typedef struct MyState MyState; + * + * typedef void (*MyDoSomething)(MyState *obj); + * + * typedef struct MyClass { + * ObjectClass parent_class; + * + * MyDoSomething do_something; + * } MyClass; + * + * static void my_do_something(MyState *obj) + * { + * // do something + * } + * + * static void my_class_init(ObjectClass *oc, void *data) + * { + * MyClass *mc =3D MY_CLASS(oc); + * + * mc->do_something =3D my_do_something; + * } + * + * static const TypeInfo my_type_info =3D { + * .name =3D TYPE_MY, + * .parent =3D TYPE_OBJECT, + * .instance_size =3D sizeof(MyState), + * .class_size =3D sizeof(MyClass), + * .class_init =3D my_class_init, + * }; + * + * typedef struct DerivedClass { + * MyClass parent_class; + * + * MyDoSomething parent_do_something; + * } MyClass; + * + * static void derived_do_something(MyState *obj) + * { + * DerivedClass *dc =3D DERIVED_GET_CLASS(obj); + * + * // do something here + * dc->parent_do_something(obj); + * // do something else here + * } + * + * static void derived_class_init(ObjectClass *oc, void *data) + * { + * MyClass *mc =3D MY_CLASS(oc); + * DerivedClass *dc =3D DERIVED_CLASS(oc); + * + * dc->parent_do_something =3D mc->do_something; + * mc->do_something =3D derived_do_something; + * } + * + * static const TypeInfo derived_type_info =3D { + * .name =3D TYPE_DERIVED, + * .parent =3D TYPE_MY, + * .class_size =3D sizeof(DerivedClass), + * .class_init =3D my_class_init, + * }; + * + * + * + * Alternatively, object_class_by_name() can be used to obtain the class= and + * its non-overridden methods for a specific type. This would correspond= to + * |[ MyClass::method(...) ]| in C++. + * + * The first example of such a QOM method was #CPUClass.reset, + * another example is #DeviceClass.realize. */ =20 =20 --=20 1.7.10.4