CObject
Class.h File Reference
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
+ Include dependency graph for Class.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

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

Macros

#define CBool   unsigned char
 
#define CTRUE   1
 
#define CFALSE   !CTRUE
 
#define C_DEBUG_DIAG_LEVEL   2
 
#define CDefaultFree   free
 
#define CFree   CDefaultFree
 
#define CMalloc   malloc
 
#define C_CLASS   _cc
 
#define C_ROOT   _rt
 
#define C_VTABLE   _vt
 
#define C_VTABLE_OFFSET   _vo
 
#define C_PRINT(...)   printf( __VA_ARGS__ )
 
#define C_FAILED_ASSERT_HANDLE()   for( ;; )
 
#define C_ASSERT_VIRTUAL_MESSAGE
 
#define C_ASSERT_OBJECT_MESSAGE   "NULL pointer used as input\n"
 
#define C_ASSERT_CAST_MESSAGE
 
#define CAssert(exp, msg, file, line)
 
#define C_ASSERT_VIRTUAL(method)   CAssert( ((method)==NULL), CAssertVirtualMessage_, __FILE__, __LINE__ )
 
#define C_ASSERT_OBJECT(object)   CAssert( ((object)==NULL), CAssertObjectMessage_, __FILE__, __LINE__ )
 
#define C_ASSERT_CAST(object, file, line)   CAssert( (object) == NULL, CAssertCastMessage_, file, line )
 
#define CObject(self)   CObject_Constructor(self)
 
#define CDestroy(mem)   CObject_Destroy((void*) (mem))
 
#define CDynamic(obj)   CObject_SetFree(((struct CClass*) (obj))->C_ROOT, CDefaultFree)
 
#define CFreeWith(obj, freep)   CObject_SetFree(((struct CClass*) (obj))->C_ROOT, (freep))
 
#define CAssertVirtual(func)   C_ASSERT_VIRTUAL(func)
 
#define CAssertObject(object)   C_ASSERT_OBJECT((object))
 
#define CCast(self_)   CObjectCast_( self_, __FILE__, __LINE__ )
 

Typedefs

typedef void(* CFreeType) (void *)
 

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_)
 

Variables

const char * CAssertVirtualMessage_
 
const char * CAssertObjectMessage_
 
const char * CAssertCastMessage_
 

Macro Definition Documentation

#define C_ASSERT_CAST_MESSAGE
Value:
"Failure to cast object. Possible causes:\n"\
"\t* [C0] Did not call interface constructor, CInterface( )\n"\
"\t* [C1] Did not call super's constructor.\n"
#define C_ASSERT_VIRTUAL_MESSAGE
Value:
"Virtual method not linked. Possible causes:\n"\
"\t* [A0] Class constructor did not link virtual table with CVTable\n"\
"\t* [A1] Constructor was never called on object\n"\
"\t* [A2] Constructor was called, but super's constructor was not called\n"\
"\t* [A3] Virtual table function assignment is incomplete\n"
C_CLASS   _cc

Used to provide consistent interface for finding virtual functions when dealing with object and interface references.

Used to back track from a pointer to an interface, to its implementing class' vtable, and the offset into that vtable where the implemented methods are located.

C_ROOT   _rt

This contains a pointer to the top of an objects memory. It is used to back track to the top of an objects memory regardless of what super class reference is used. Using the above example, with a reference to Interface0 (pointer to 5'th row in memory layout table above), the C_ROOT variable can resolve the top of the memory block.

C_VTABLE   _vt

Pointer to an objects vtable.

C_VTABLE_OFFSET   _vo

This is used to find the offset into the virtual table where a classes virtual methods are. Using the above example, with a reference to Interface0 (pointer to 5'th row in memory layout table above), the C_ROOT variable can resolve the top of the memory block and an offset into vtable (in this case, the offset is 2). With the top of the memory block known, a pointer to the virtual table can be obtained. Moving two rows down the virtual table (third row), as specified by the offset value, we find method0.

#define CAssert (   exp,
  msg,
  file,
  line 
)
Value:
do { \
(void) (exp); \
(void) (file); \
(void) (line); \
} while( 0 )