![]() |
ooc
1.3c
Object Oriented tollkit fo ANSI C
|
Object Oriented C - macros and definitions. More...
#include <stddef.h>
#include "port/anyc.h"
Data Structures | |
struct | Object |
ooc Object. More... | |
Macros | |
#define | OOC_MANAGE TRUE |
Container item management flag. More... | |
Class definitions. | |
These macros help define the class. | |
#define | DeclareClass(pClass, pParent) |
Class declaration macro. More... | |
#define | Virtuals(pClass, pParent) |
Class virtual functions declaration macro. More... | |
#define | EndOfVirtuals |
End of virtual functions. More... | |
#define | ClassMembers(pClass, pParent) |
Class members declaration macro. More... | |
#define | EndOfClassMembers |
End of class members definition. More... | |
#define | _ooc_decl_finalize(pClass) static void pClass ## _finalize ( Class ); |
#define | _ooc_decl_finalize_nopar(pClass) static void pClass ## _finalize (); |
#define | _ooc_func_finalize(pClass) (void (*)(ClassCommons)) pClass ## _finalize, |
#define | AllocateClass(pClass, pParent) |
Class allocation macro. More... | |
#define | AllocateClassWithInterface(pClass, pParent) |
Class allocation macro for classes with interfaces. More... | |
#define | chain_constructor(pClass, pSelf, pParam) |
Chain the parent constructor. More... | |
Managing Interfaces. | |
These macros help define and use the interfaces and mixins. | |
#define | DeclareInterface(pInterface) |
Declare an interface. More... | |
#define | DeclareMixinInterface(pMixin) |
Declare an interface for a mixin. More... | |
#define | EndOfInterface |
Terminates the interface declaration. More... | |
#define | Interface(pInterface) |
Place an interface into the class's virtual table. More... | |
#define | MixinMembers(pMixin) |
Mixin data members declaration macro. More... | |
#define | EndOfMixinMembers |
End of mixin members definition. More... | |
#define | MixinData(pMixin) |
Place the mixin data fields into the carrier class's members. More... | |
#define | AllocateInterface(pInterface) |
Allocates the interface descriptor table. More... | |
#define | AllocateMixin(pMixin) |
Allocates the mixin descriptor table. More... | |
#define | InterfaceRegister(pClass) |
Register for implemented interfaces of the class. More... | |
#define | AddInterface(pClass, pInterface) |
Adds an interface to the class's interface register. More... | |
#define | AddMixin(pClass, pMixin) |
Adds a mixin to the class's interface register. More... | |
#define | ooc_get_interface(pObject, pInterface) |
Retrieves an interface of the Object. More... | |
#define | ooc_get_interface_must_have(pObject, pInterface) |
Retrieves a mandatory interface of the Object. More... | |
#define | ooc_get_mixin_data(pObject, pMixin) |
Retrieves the mixin data within the carrier object. More... | |
Typedefs | |
typedef const struct ClassTable * | Class |
Class description table. | |
typedef const struct InterfaceID_struct * | InterfaceID |
Interface ID. More... | |
Functions | |
Memory handling. | |
ooc has it own memory handling that are based on their own stdc equivalents, except that they do not return a failure code, but throw an Exception instead. | |
void * | ooc_malloc (size_t size) |
Memory allocation. More... | |
void * | ooc_calloc (size_t num, size_t size) |
Memory allocation and clear. More... | |
void * | ooc_realloc (void *ptr, size_t size) |
Memory reallocation. More... | |
void * | ooc_memdup (const void *ptr, size_t size) |
Memory duplication. More... | |
char * | ooc_strdup (const char *s) |
C string duplication. More... | |
void | ooc_free (void *mem) |
Memory free. More... | |
void | ooc_free_and_null (void **mem) |
Memory free and nulling pointer. More... | |
void * | ooc_ptr_read_and_null (void **ptr_ptr) |
Pointer read and null. More... | |
Variables | |
const struct ClassTable | BaseClass |
Base Class. More... | |
Class initialization, finalization. | |
#define | ooc_init_class(pClass) _ooc_init_class( & pClass ## Class ) |
Initializes a class. More... | |
#define | ooc_finalize_class(pClass) _ooc_finalize_class( & pClass ## Class ) |
Finalizes a class. More... | |
#define | ooc_isInitialized(pClass) _ooc_isInitialized( & pClass ## Class ) |
Checks if Class is initialized. More... | |
void | _ooc_init_class (const Class class_ptr) |
Initializes a class by class table pointer. More... | |
void | _ooc_finalize_class (const Class class_ptr) |
Finalizes a class by the class table pointer. More... | |
void | ooc_finalize_all (void) |
Finalizes all classes. | |
int | _ooc_isInitialized (const Class class_ptr) |
Checks if Class is initialized via a class table pointer. More... | |
Creating and deleting Objects. | |
#define | ooc_new(pClass, pPar) ((pClass) ooc_new_classptr( & pClass ## Class, pPar )) |
Creates a new object of a class. More... | |
#define | ooc_use(mem, pClass, pPar) ooc_use_classptr( mem, & pClass ## Class, pPar ) |
Creates an Object of a Class using existing memory block. More... | |
Object | ooc_new_classptr (const Class class_ptr, const void *par_ptr) |
Creates a new object of a class using class table pointer. More... | |
Object | ooc_duplicate (const Object object) |
Duplicates an Object. More... | |
void | ooc_delete (Object object) |
Deletes an Object. More... | |
void | ooc_delete_and_null (Object *object_ptr) |
Deletes an Object via a pointer with nulling. More... | |
void | ooc_use_classptr (void *mem, const Class class_ptr, const void *param) |
Creates an Object from a class table pointer using existing memory block. More... | |
Object | ooc_copy (void *to, const Object from) |
Copies an Object to an another location. More... | |
void | ooc_release (Object object) |
Destroys an Object, but does not free the memory. More... | |
Dynamic type checking. | |
#define | ooc_isClassOf(pClass, pSuperClass) _ooc_isClassOf( & pClass ## Class, & pSuperClass ## Class ) |
Checks if a Class is inherited from an other Class. More... | |
#define | ooc_isInstanceOf(pObj, pClass) _ooc_isInstanceOf( (Object) pObj, & pClass ## Class ) |
Checks if an Object is of a given type. More... | |
#define | ooc_cast(pObj, pClass) ( ooc_check_cast( pObj, & pClass ## Class ), (pClass) pObj ) |
Run-time safe upcast of an Object. More... | |
#define | ooc_class_has_parent(this) (this->parent != &BaseClass) |
Checks if a Class has a parent class. More... | |
Class | ooc_get_type (const Object object) |
Gets the type of the Object in run-time. More... | |
int | _ooc_isClassOf (const Class this, const Class base) |
Checks if a Class is inherited from an other Class via class table pointers This is a convenient macro. More... | |
int | _ooc_isInstanceOf (const Object object, const Class base) |
Checks if an Object is of a given type via class table pointer. More... | |
void | ooc_check_cast (void *object, const Class target) |
Run-time safe check, if an Object can be upcast to an other type. More... | |
Object Oriented C - macros and definitions.
#define AddInterface | ( | pClass, | |
pInterface | |||
) |
Adds an interface to the class's interface register.
pClass | The name of the class that implements the interfaces. |
pInterface | The name of the interface to be implemented for the class. |
#define AddMixin | ( | pClass, | |
pMixin | |||
) |
Adds a mixin to the class's interface register.
pClass | The name of the class that implements the mixin (the carrier class). |
pMixin | The name of the mixin to be implemented for the class. |
#define AllocateClass | ( | pClass, | |
pParent | |||
) |
Class allocation macro.
This macro should be put int the implementation file of the class. Use:
pClass | The name of the class. |
pParent | The name of the parent class of the class. Must be Base if class does not have other parent. |
#define AllocateClassWithInterface | ( | pClass, | |
pParent | |||
) |
Class allocation macro for classes with interfaces.
Allocates the class using the InterfaceRegister for the given class. The InterfaceRegister must be put right before this class allocator. For proper use see InterfaceRegister.
pClass | The name of the class. |
pParent | The name of the parent class of the class. Must be Base if class does not have other parent. |
#define AllocateInterface | ( | pInterface | ) |
Allocates the interface descriptor table.
The interface descriptor table is used to uniquely identify an interface. This macro allocates this table and must be placed in a publicly available source file. In larger projects all interface descriptor table allocators can be put in a single file (e.g. in interfaces.c ).
pInterface | The name of the interface to be allocated. |
#define AllocateMixin | ( | pMixin | ) |
Allocates the mixin descriptor table.
The mixin descriptor table is used to identify and describe a mixin. This macro allocates this table and must be placed in the mixin's implementation file.
pMixin | The name of the mixin to be allocated. |
#define chain_constructor | ( | pClass, | |
pSelf, | |||
pParam | |||
) |
Chain the parent constructor.
This macro calls the constructor of the parent class. Must be used soley in the constructor of the class, at the beginning of the constructor. Use:
pClass | The name of the actual class (not the parent!) |
pSelf | Pointer to the Object being constructed. |
pParam | Pointer to the construction parameters for the parent class's constructor. This pointer is passed to the constructor without any check. |
ListNode
. #define ClassMembers | ( | pClass, | |
pParent | |||
) |
Class members declaration macro.
This macro should be put int the implementation header of the class. Use:
pClass | The name of the class. |
pParent | The name of the parent class of the class. Must be Base if class does not have other parent. |
#define DeclareClass | ( | pClass, | |
pParent | |||
) |
Class declaration macro.
This macro should be put int the publicly available header of the class. Use:
pClass | The name of the class. |
pParent | The name of the parent class of the class. Must be Base if class does not have other parent. |
#define DeclareInterface | ( | pInterface | ) |
Declare an interface.
This declaration should be placed in a publicly available header file. The declaration is followed by a semicolon separated list of function pointers. These function pointers represent the behavior of the interface. The first parameters of the function pointers should be an Object, since we do not know the exact type of the object right now, that implements the interface.
pInterface | The name of the interface. |
#define DeclareMixinInterface | ( | pMixin | ) |
Declare an interface for a mixin.
This is almost the same as the DeclareInterface() but is used to declare the interface methods of a mixin.
pMixin | The name of the mixin interface. |
#define EndOfClassMembers |
End of class members definition.
This macro terminates the ClassMembers
block.
#define EndOfInterface |
Terminates the interface declaration.
#define EndOfMixinMembers |
End of mixin members definition.
This macro terminates the MixinMembers
block.
#define EndOfVirtuals |
#define Interface | ( | pInterface | ) |
Place an interface into the class's virtual table.
This macro places the interface methods into the class's virtual table. This marks that he interface methods can be accessed via the virtual table, and are inherited as any normal virtual function. Use:
pInterface | The name of the interface to be implemented by the class. |
#define InterfaceRegister | ( | pClass | ) |
Register for implemented interfaces of the class.
In the class implementation code you must define, which interfaces are implemented by the class. These interfaces must be listed in the InterfaceRegister right before the AllocateClassWithInterface macro. Typical use (in myclass.c):
pClass | The name of the class that implements the listed interfaces. |
#define MixinData | ( | pMixin | ) |
Place the mixin data fields into the carrier class's members.
This macro places the mixin data fields (in case of Mixins) into the object struct. Use:
pMixin | The name of the mixin (must be idenctical with the name of the corresponding interface) to be implemented by the carrier class. |
#define MixinMembers | ( | pMixin | ) |
Mixin data members declaration macro.
This macro should be put int the implementation header of the mixin. Use:
pMixin | The name of the mixin (must be identical with the corresponding interface name!). |
#define ooc_cast | ( | pObj, | |
pClass | |||
) | ( ooc_check_cast( pObj, & pClass ## Class ), (pClass) pObj ) |
Run-time safe upcast of an Object.
Safely upcasts the Object to the specified type. Throws an Exception if not possible. This is a macro.
pObj | The Object to be cast. |
pClass | The desired type (Class name). |
#define ooc_class_has_parent | ( | this | ) | (this->parent != &BaseClass) |
Checks if a Class has a parent class.
This is a convenient macro.
this | The Class that should be checked. |
TRUE
or FALSE
. Returns TRUE
if the Class has a parent class. Does not throw an Exception. #define ooc_finalize_class | ( | pClass | ) | _ooc_finalize_class( & pClass ## Class ) |
Finalizes a class.
Finalizes a class using the class name. This is a convenient macro.
pClass | Tha name of the class. |
#define ooc_get_interface | ( | pObject, | |
pInterface | |||
) |
Retrieves an interface of the Object.
Returns the interface pointer for the given Object. Typical use:
pObject | The Object of which's interface we are interested in. |
pInterface | The name of the interface to be retrieved. |
#define ooc_get_interface_must_have | ( | pObject, | |
pInterface | |||
) |
Retrieves a mandatory interface of the Object.
Returns the interface pointer for the given Object. The Interface must have been implemented for the given Object, otherwise err_interface_not_implemented
Exception is thrown.
pObject | The Object of which's interface we are interested in. |
pInterface | The name of the interface to be retrieved. |
#define ooc_get_mixin_data | ( | pObject, | |
pMixin | |||
) |
Retrieves the mixin data within the carrier object.
Returns the pointer to the Mixin data for the given carrier Object. The Mixin must have been implemented for the given Object, otherwise err_interface_not_implemented
Exception is thrown.
pObject | The Object of which's interface we are interested in. (The carrier Object.) |
pMixin | The name of the mixin to be retrieved. |
#define ooc_init_class | ( | pClass | ) | _ooc_init_class( & pClass ## Class ) |
Initializes a class.
Initializes a class using the class name. This is a convenient macro.
pClass | Tha name of the class. |
#define ooc_isClassOf | ( | pClass, | |
pSuperClass | |||
) | _ooc_isClassOf( & pClass ## Class, & pSuperClass ## Class ) |
Checks if a Class is inherited from an other Class.
This is a convenient macro.
pClass | The Class that should be checked. |
pSuperClass | The required type |
TRUE
or FALSE
. Returns TRUE
if the Class is inherited from the given pSuperClass. Does not throw an Exception. #define ooc_isInitialized | ( | pClass | ) | _ooc_isInitialized( & pClass ## Class ) |
Checks if Class is initialized.
This is a convenient macro.
pClass | The Class that should be checked. |
TRUE
or FALSE
. Does not throw an Exception. #define ooc_isInstanceOf | ( | pObj, | |
pClass | |||
) | _ooc_isInstanceOf( (Object) pObj, & pClass ## Class ) |
Checks if an Object is of a given type.
This is a convenient macro. Returns TRUE
if the Object is of the given Class or any of its parent classes.
pObj | The Object to be checked. |
pClass | The Class that should be matched. |
TRUE
or FALSE
. Does not throw an Exception. #define OOC_MANAGE TRUE |
Container item management flag.
Indicates that the contents of a container must be managed by the container itself.
#define ooc_new | ( | pClass, | |
pPar | |||
) | ((pClass) ooc_new_classptr( & pClass ## Class, pPar )) |
Creates a new object of a class.
Creates a new object of a class with the given construction parameters.
pClass | The name of the class. |
pPar | Pointer to the construction parameters. This pointer is passed to the constructor without any check. |
#define ooc_use | ( | mem, | |
pClass, | |||
pPar | |||
) | ooc_use_classptr( mem, & pClass ## Class, pPar ) |
Creates an Object of a Class using existing memory block.
Creates a new object of a class with the given construction parameters using the memory block provided. This method does not call the memory allocator for the Object
mem | The memory block to be used for the Object |
pClass | The name of the class. |
pPar | Pointer to the construction parameters. This pointer is passed to the constructor without any check. |
#define Virtuals | ( | pClass, | |
pParent | |||
) |
Class virtual functions declaration macro.
This macro should be put int the publicly available header of the class. Use:
pClass | The name of the class. |
pParent | The name of the parent class of the class. Must be Base if class does not have other parent. |
typedef const struct InterfaceID_struct* InterfaceID |
Interface ID.
Each interface has a unique ID.
In this implementation the ID is a pointer to an interafce descriptor table in the memory, similarly to the Class definition.
void _ooc_finalize_class | ( | const Class | class_ptr | ) |
Finalizes a class by the class table pointer.
Finalizes a class using the class table pointer.
class_ptr | Pointer to the class description table. |
void _ooc_init_class | ( | const Class | class_ptr | ) |
Initializes a class by class table pointer.
Initializes the class pointed by the parameter.
class_ptr | Pointer to the class description table. |
Checks if a Class is inherited from an other Class via class table pointers This is a convenient macro.
this | The Class that should be checked. |
base | The required type |
TRUE
or FALSE
. Returns TRUE
if the Class is inherited from the given pSuperClass. Does not throw an Exception. int _ooc_isInitialized | ( | const Class | class_ptr | ) |
Checks if Class is initialized via a class table pointer.
This is a convenient macro.
class_ptr | The class table pointer to the Class that should be checked. |
TRUE
or FALSE
. int _ooc_isInstanceOf | ( | const Object | object, |
const Class | base | ||
) |
Checks if an Object is of a given type via class table pointer.
Returns TRUE
if the Object is of the given Class or any of its parent classes.
object | The Object to be checked. |
base | The Class that should be matched. |
TRUE
or FALSE
. Does not throw an Exception. void* ooc_calloc | ( | size_t | num, |
size_t | size | ||
) |
Memory allocation and clear.
Allocates memory like calloc(), but throws an Exception on error
void ooc_check_cast | ( | void * | object, |
const Class | target | ||
) |
Run-time safe check, if an Object can be upcast to an other type.
Safely chaecks if the Object could be upcast to the specified type. Throws an Exception if not possible.
object | The Object to be cast. |
target | The desired type (Class name). |
Object ooc_copy | ( | void * | to, |
const Object | from | ||
) |
Copies an Object to an another location.
Calling the copy constructor of the Object
, copies the content into an other location.
to | The destination location of the copied object. The caller must ensure, that the destination location is large enough to hold the newly created Object! |
from | The original Object to be copied. |
Object
needs to be in a consistent state while copying, you must lock it yourself. ooc_copy()
does not do any locking. void ooc_delete | ( | Object | object | ) |
Deletes an Object.
Deletes the Object using its destructor, then deallocating the memory.
object | The Object to be deleted. Can be NULL. |
void ooc_delete_and_null | ( | Object * | object_ptr | ) |
Deletes an Object via a pointer with nulling.
Deletes the Object using its destructor, then deallocating the memory. Thread safely and reentrat safely NULLs the pointer.
object_ptr | Pointer to the Object to be deleted. Can be NULL and the object pointer can be NULL as well. |
Object ooc_duplicate | ( | const Object | object | ) |
Duplicates an Object.
Calling the copy constructor of the Object
, creates a duplicate of it.
object | The original Object. |
NULL
if the parameter is NULL
. (Useful in copy constructors, you do not have to check if the member Object
to be copied exists or not.) Object
needs to be in a consistent state while copying, you must lock it yourself. ooc_duplicate()
does not do any locking. void ooc_free | ( | void * | mem | ) |
Memory free.
Frees memory allocated by ooc_malloc(), ooc_calloc() or ooc_realloc()
void ooc_free_and_null | ( | void ** | mem | ) |
Memory free and nulling pointer.
Frees memory via a pointer, and NULLs the pointer thread safely
Class ooc_get_type | ( | const Object | object | ) |
Gets the type of the Object in run-time.
object | The Object. |
object
is not a valid Object. void* ooc_malloc | ( | size_t | size | ) |
Memory allocation.
Allocates memory like malloc(), but throws an Exception on error
void* ooc_memdup | ( | const void * | ptr, |
size_t | size | ||
) |
Memory duplication.
Duplicates a memory block with a new alloc, throws an Exception on error
Object ooc_new_classptr | ( | const Class | class_ptr, |
const void * | par_ptr | ||
) |
Creates a new object of a class using class table pointer.
Creates a new object of a class with the given class table pointer and construction parameters.
class_ptr | The pointer to the class description table. |
par_ptr | Pointer to the construction parameters. This pointer is passed to the constructor without any check. |
void* ooc_ptr_read_and_null | ( | void ** | ptr_ptr | ) |
Pointer read and null.
Reads a pointer via a pointer, and nulls thread safely. Atomic operation in case of GNUC and MSVC on x86 platforms. For other compilers this is equivalent to the following code:
wich is not thread safe but re-entrant safe.
void* ooc_realloc | ( | void * | ptr, |
size_t | size | ||
) |
Memory reallocation.
Reallocates memory like realloc(), but throws an Exception on error
void ooc_release | ( | Object | object | ) |
Destroys an Object, but does not free the memory.
Calls the Object's destructor, but does not free the memory block used by the Object.
object | The Object to be released. |
char* ooc_strdup | ( | const char * | s | ) |
C string duplication.
Duplicates a C string with a new alloc, throws an Exception on error
void ooc_use_classptr | ( | void * | mem, |
const Class | class_ptr, | ||
const void * | param | ||
) |
Creates an Object from a class table pointer using existing memory block.
Creates a new object of a class with the given construction parameters using the memory block provided. This method does not call the memory allocator for the Object
mem | The memory block to be used for the Object |
class_ptr | The class table pointer. |
param | Pointer to the construction parameters. This pointer is passed to the constructor without any check. |
const struct ClassTable BaseClass |
Base Class.
Used for root Class in ooc. It must be a superclass for all classes.