CObject
Class

Data Structures

struct  CClass
 First variables in every object's memory allocation. More...
 
struct  CObject
 Base class. More...
 
struct  CInterface
 Base interface. More...
 

Functions

void * CObjectCast_ (void *super_reference, const char *file_name, int line_num)
 
void CVTable (void *self, const void *vtable)
 
const void * CGetVTable (void *self_)
 
void CInterface::CInterface (void *self, void *iface, const void *vtable)
 

Detailed Description

Methods used by all classes to implement object oriented back bone.

Function Documentation

const void* CGetVTable ( void *  self_)

This is used to get a pointer to the objects vtable. When calling a virtual method, this should always be used over _VTable_Key( ). For example, say we have a class inheritance chain like such: struct Square :: struct Point :: struct CObject Due to polymorphism, having a reference to an object of type struct Point does not guarentee that objects virtual table is given by Point_VTable_Key( ). We might have an instance of struct Square, and the correct virtual table is Square_VTable_Key( ). There is no way of knowing. Using this method guarentees we will get a pointer to the correct virtual table.

This method should only be used in a wrapper function for calling virtual methods. For example, say struct Point has a method struct Point::move.

1 void Point_Move( struct Point* self )
2 {
3  ((struct Point_VTable*) CGetVTAble(self))->move(self);
4 }

Then, application code simply calls Point_Move, which has the more cumbersome code for actually finding and calling the virtual method.

This method works on references to objects and interfaces.

Parameters
self_A pointer to the object whos virtual table is needed.
Returns
A pointer to the objects virtual table.
void CInterface ( void *  self,
void *  iface,
const void *  vtable 
)

This is the constructor for interfaces. All implementing classes must call this in their constructor AFTER mapping their virtual table with CVTable( ). This method will setup the interfaces data such that one can back track from the interface reference, param iface, to the object reference, param self, and find the offset into the class' virtual table where the implemented interface methods are located.

Parameters
selfThis is a pointer to the object whose class is implementing the interface.
ifaceThis is a pointer to the interface instance within the classes declaration. For example, if param self is of type struct A, and struct A implements interface struct I, then this parameter would be a pointer to A::I.
vtableThis is a pointer to the interfaces virtual table. For example, say, param self is of type struct A, which implements interface struct I, and we are calling this constructor to construct A::I. struct A's vtable, struct A_VT, will contain an instance of struct I's vtable, struct I_VT. This would be a pointer to A_VT::I_VT.
void* CObjectCast_ ( void *  super_reference,
const char *  file_name,
int  line_num 
)

This is used to cast an object. It must only be used in virtual method definitions. The usage would be this, say we are overriding the destructor, the destructors declaration looks like this void (*)( struct CObject* ). However, we are overriding the method in class struct ArrayList. We know we the input to the method is from an object of that type. This method will cast the input pointer to that type.

The method has a macro wrapper to simply its call signature, CCast( ), which only takes one input, the object refernce.

void* CCast( void* );

1 static void ArrayList_Destructor( struct CObject* self_ )
2 {
3  struct ArrayList* self = CCast(self_);
4  ...
5 }
Parameters
super_referenceReference to cast.
file_nameName of the file casting is done in - for debugging.
line_numLine number in the file casting is done - for debugging.
Returns
Pointer to casted object.
void CVTable ( void *  self,
const void *  vtable 
)

Used within a classes constructor to give the object instance the location of the class' virtual table. This must always be called after constructing the super class and before constructing any interfaces. For example,

1 void ArrayList( struct ArrayList* self )
2 {
3  CObject(self);
4  CVTable(self, ArrayList_VTable_Key( ));
5  ...
6 }
Parameters
selfThe object which needs a virtual table reference.
vtableThe pointer to the class' virtual table.