Coder Social home page Coder Social logo

gyumeijie / qemu-object-model Goto Github PK

View Code? Open in Web Editor NEW
18.0 1.0 7.0 43 KB

A simplified realization of qemu object model, for more information about qemu, please see https://github.com/qemu/qemu

C 98.17% C++ 1.53% Makefile 0.30%
qemu object-model c

qemu-object-model's Introduction

What is the qom

The qom is the object model of qemu(a.k.a. quick emulator). With the qom, we can use c language to write code in OOP(object-oriented programming) way.

Usage

  • download the repository
git clone https://github.com/Gyumeijie/qemu-object-model.git
  • emit the make command
make
  • run the code
./main

Guide

In qom the root object is object, defined in object.h. all the newly-created object must inherit some object.And in qom a class is represented by two structs, one struct is called instance struct, the other is called class struct. For example, given a class Base, who has a data memeber named greeting, and a say member method,then the two structs are the following:

typedef struct Base {
  Object parent;

  char *greeting;
} Base;


typedef struct BaseClass {
   ObjectClass parent_class;

   void (*say)(void*); 
} BaseClass;

And we also need define the following three macro.

#define BASE_GET_CLASS(obj) \
        OBJECT_GET_CLASS(BaseClass, obj, TYPE_BASE)

#define BASE_CLASS(klass) \
        OBJECT_CLASS_CHECK(BaseClass, klass, TYPE_BASE)

#define BASE(obj) \
        OBJECT_CHECK(Base, obj, TYPE_BASE)

In OO language like c++, we can use new keyword to create a instance, similarly we use class-prefix new to instantiate a class, say we can use Base_new()to create an instance of tpye Base.

We can use the marocs above the acess members, supposed that we already have an instance obj of type Base, we can call the method by using the following code:

BASE_GET_CLASS(obj)->say(obj);

In qom, the creation of an instance can be finished by two steps:

  • allocate memery
  • initialize the data member qom supply us with an instance init hook function, which acts as constructor in OO language. The following shows the instance init hook in this example:
static void instance_init(Object *obj)
{
    Base *This = BASE(obj);
    This->greeting = "I am base";
}

Similarly, the class struct also need an init hook:

static void class_init(ObjectClass *oc, void *data)
{
    BaseClass *base = BASE_CLASS(oc);
    base->say = say;
}

The last thing to do is to fill the TypeInfo struct, which descripts some basic infomation of the type:

static const TypeInfo type_info = {
    .name = TYPE_BASE,
    .parent = TYPE_OBJECT,
    .instance_size = sizeof(Base),
    .abstract = false,
    .class_size = sizeof(BaseClass),
    .instance_init = instance_init,
    .class_init = class_init,
};

and then register it by calling type_register_static(&type_info).

Resources

  • There is a project named OBS-Framework athoured by me, heavily using qom model, you can visit it for more information.

  • In some way, the qom is an simiplified version of Gobject, which is also a type of object model and is the object system of Glib.

qemu-object-model's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

qemu-object-model's Issues

make error

~/test/qom/qemu-object-model$ make -B
gcc -c base.c -o base.o
gcc -c main.c -o main.o
gcc -c ./qom/gtestutil.c -o gtestutil.o
gcc -c ./qom/object.c -o object.o
gcc -c ./qom/ghash.c -o ghash.o
In file included from ./qom/ghash.h:27,
from ./qom/ghash.c:31:
./qom/ghash.c: In function ‘g_direct_hash’:
./qom/gtypes.h:49:30: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
49 | #define GPOINTER_TO_UINT(p) ((guint) (p))
| ^
./qom/ghash.c:1799:10: note: in expansion of macro ‘GPOINTER_TO_UINT’
1799 | return GPOINTER_TO_UINT (v);
| ^~~~~~~~~~~~~~~~
gcc -c ./qom/gslist.c -o gslist.o
gcc -c ./qom/error.c -o error.o
gcc -c ./qom/gstrfuncs.c -o gstrfuncs.o
In file included from ./qom/gstrfuncs.c:4:
./qom/gstrfuncs.c: In function ‘g_strdup_vprintf’:
./qom/gstrfuncs.h:47:42: error: assignment to expression with array type
47 | # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
| ^
./qom/gstrfuncs.c:65:3: note: in expansion of macro ‘G_VA_COPY’
65 | G_VA_COPY (args2, args1);
| ^~~~~~~~~
makefile:13: recipe for target 'gstrfuncs.o' failed
make: *** [gstrfuncs.o] Error 1

Why is 'class_init' needed

Thank you for the awesome writeup

I was however unable to understand why do we need a class_init function. From my earlier experience in OOPs, all I came across was 'initializing' only instances. However this is the first time I came across 'class initialization'

Could you please elaborate the model behind this thinking and why is it needed?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.