Basic Class

Submitted by admin on Fri, 04/27/2018 - 14:16

We will introduce the concept of a basic class through an example:

#include <diablosupport_class.h>
#ifndef CLASS
#define CLASS ins
#define ins_field_select_prefix INS
#define ins_function_prefix Ins

We define a new class ins. This will result in a new type t_ins. The ins_field_select_prefix will be used in the getters and setters. The ins_function_prefix will be used in functions.


The class ins is an extension of the class relocatable. As such, it will inherit the getters, setters and non-private functions of the class relocatable. We will come back to this later.

MEMBER(t_cfg *, cfg, CFG)

A core members (as opposed to a dynamic member for a managed class) is defined using the MEMBER mechanism. This will create a member of the type t_cfg. The second and third argument indicate the name of the member in lowercase and uppercase. This mechanism will automatically generate a getter and setter, in this example: t_cfg * INS_CFG(t_ins * ins) and void INS_SET_CFG(t_ins * ins, t_cfg * cfg)

FUNCTION1(void, Kill, t_CLASS *){Free(ins);}

This mechanism will create the function with signature void InsKill(t_ins *){Free(ins);}.
This function can be inherited if other classes are extended from this class. To this end, t_CLASS is used instead of t_ins. Similarly, functions with multiple arguments can be defined by using FUNCTION2(), FUNCTION3(), ...

PFUNCTION1(void, Print, t_CLASS *){VERBOSE((0,"@I",ins));}

A PFUNCTION differs from a FUNCTION in that it can not be inherited (it is Private).

#define BASECLASS relocatable

To enable the inheritance mechanism, the definition of the base class needs to be included. The getters, setters and non-private functions of the base class will be inherited. If the base class contains the following definition of a member:

MEMBER(t_address, caddress, CADDRESS);

then this will have o.a. resulted in the getter t_address RELOCATABLE_CADDRESS(t_relocatable * rel). This will be inherited by the class ins and thus the getter t_address INS_CADDRESS(t_ins * ins) will be available. The same applies to setters and non-private functions.