libtrbase
1.0.2
Web server and task management solution.
|
#include <stdarg.h>
#include <sys/types.h>
#include <string.h>
#include <assert.h>
#include "tr/interface.h"
Go to the source code of this file.
Macros | |
#define | TR_CLASS_MAGIC 0xFEFE |
#define | TR_CLASS(name) |
#define | TR_INSTANCE_INIT(name) |
#define | TR_CLASSVARS_DECL(name) struct c_##name##_vars |
#define | TR_CLASSVARS(name, class) ((struct c_##name##_vars *)(class)->vars) |
#define | TR_CLASSVARS_BY_NAME(name) ((struct c_##name##_vars *)(TR_CLASS_BY_NAME(name))->vars) |
#define | TR_CLASSVARS_STATIC(name) ((struct c_##name##_vars *)(TR_CLASS_BY_NAME_STATIC(name))->vars) |
#define | TR_INHERIT_CLASSVARS(dest, src) |
#define | TR_EXTENDS(parent) const char _[sizeof(struct c_##parent)] |
#define | TR_CV_EXTENDS(parent) struct c_##parent##_vars _ |
#define | _NULL NULL |
#define | TR_CREATE_CLASS(name, _parent, cvInit, ...) |
#define | TR_INSTANCE(class, name, ...) |
#define | TR_INSTANCE_CAST(class, name) ((class)&(_##name.data)) |
#define | TR_GET_CLASS(object) (TR_INIT_CLASS(*(TR_class_ptr *)((void*)(object) - sizeof(void*)))) |
#define | TR_CLASS_BY_NAME(class_name) (TR_INIT_CLASS(& c_##class_name)) |
#define | TR_CLASS_BY_NAME_STATIC(class_name) (& c_##class_name) |
#define | TR_IFACE_GET(class, iface) (TR_interfaceGet(&((class)->impl),(iface))) |
#define | TR_HAS_PARENT(class) (NULL != ((class)->parent) && TR_INIT_CLASS((class)->parent)) |
#define | TR_IS_OBJECT(obj) ((TR_GET_CLASS((obj)))->magic == TR_CLASS_MAGIC) |
#define | TR_INSTANCE_OF(class, obj) ((TR_GET_CLASS((obj))) == _##class) |
#define | TR_CALL(object, _iface, method, ...) |
#define | TR_RETCALL(object, _iface, method, ret, ...) |
#define | TR_PARENTCALL(class, object, _iface, method, ...) |
#define | TR_PARENTRETCALL(class, object, _iface, method, ret, ...) |
My own class implementation for C. It combines a data structure with a set of interfaces. Each interface is another structure containing one or more function pointers to concrete implementations of this interface for the defined class.
To each interface a set of caller functions exist, that take an instance of an object and then in turn call the implementation for the class of this object. If there is none within the class it looks into its parent class and so forth.
This is somewhat similar to late binding in real OOP languages, but by far not so elaborated. This is not a real object oriented language and will surely never ever provide all features these have.
That said it has proven very usefull for me to orgnize code and prevent code duplication.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
Definition in file class.h.
#define _NULL NULL |
#define TR_CALL | ( | object, | |
_iface, | |||
method, | |||
... | |||
) |
Call the class's implementation for the given method of the given interface _iface. For this the makro searches through all interfaces defined within a class for the correct one...from a performance aspect this might not be ideal but on the other hand...we don't expect more than a hand full of interfaces per class. Call the class's implementation for the given method of the given interface _iface. This one does not handle a return value of the called implementation and as such is only suitable if there is no return value or you are not interested in it.
Definition at line 256 of file class.h.
Referenced by TR_loggerLog(), TR_observerUpdate(), TR_serialize(), TR_subjectAttach(), TR_subjectDetach(), TR_subjectNotify(), and TR_unserialize().
#define TR_CLASS | ( | name | ) |
#define TR_CLASS_BY_NAME | ( | class_name | ) | (TR_INIT_CLASS(& c_##class_name)) |
#define TR_CLASS_BY_NAME_STATIC | ( | class_name | ) | (& c_##class_name) |
#define TR_CLASS_MAGIC 0xFEFE |
#define TR_CLASSVARS | ( | name, | |
class | |||
) | ((struct c_##name##_vars *)(class)->vars) |
#define TR_CLASSVARS_BY_NAME | ( | name | ) | ((struct c_##name##_vars *)(TR_CLASS_BY_NAME(name))->vars) |
#define TR_CLASSVARS_STATIC | ( | name | ) | ((struct c_##name##_vars *)(TR_CLASS_BY_NAME_STATIC(name))->vars) |
#define TR_CREATE_CLASS | ( | name, | |
_parent, | |||
cvInit, | |||
... | |||
) |
This will create a new class which previously has to be defined by TR_CLASS. This makro will create static variables and functions used to manage and controll the new class (and its instances.) Especially it creates and initializes tha class structure for this class. This structure contains all the meta informations of the new class. These are it's members as well as its interface implementations. Each class must at least provide an implementation for the ctor interface else no instances can be created.
#define TR_EXTENDS | ( | parent | ) | const char _[sizeof(struct c_##parent)] |
Make the new class a child of an existing class. This is used within the class declaration and can only be used once. If you do it twice the behaviour is undefined, but most likely the resulting code won't even compile.
Definition at line 96 of file class.h.
Referenced by TR_CLASS().
#define TR_GET_CLASS | ( | object | ) | (TR_INIT_CLASS(*(TR_class_ptr *)((void*)(object) - sizeof(void*)))) |
I initialize _ (the class's base or parent class) with the identifier of that class. This identifier is not static and thus can't be used for a static initialization. As a workaround, until I find a better solution I create an initialization function with each class, that will be called once as soon as an interface implementation will be called. In any case this is a call to the constructor of this class. Well, not in any...if I remember correct the static instance does not call anything at all... Returns the pointer to the class structure of the given object. The class structure is the structure that contains all the metainformations about our class.
#define TR_HAS_PARENT | ( | class | ) | (NULL != ((class)->parent) && TR_INIT_CLASS((class)->parent)) |
Check if the given class is a child of any base class. That is it has an TR_EXTENDS expression whithin its declaration.
#define TR_IFACE_GET | ( | class, | |
iface | |||
) | (TR_interfaceGet(&((class)->impl),(iface))) |
#define TR_INHERIT_CLASSVARS | ( | dest, | |
src | |||
) |
#define TR_INSTANCE | ( | class, | |
name, | |||
... | |||
) |
Create a static instance of a class. This is new and currently it is only possible to create instances for class that does not need a constructor.
This does not call any constructor with any value...this has to be fixed. // When this macro is used within a global (static) context this can't be fixed. No function can be called from there. Instead I add a TR_objectInit function which takes an object name (created on either the stack or the heap) and a variable argument list and calls its constructor with the arguments.
#define TR_INSTANCE_CAST | ( | class, | |
name | |||
) | ((class)&(_##name.data)) |
#define TR_INSTANCE_INIT | ( | name | ) |
#define TR_INSTANCE_OF | ( | class, | |
obj | |||
) | ((TR_GET_CLASS((obj))) == _##class) |
#define TR_IS_OBJECT | ( | obj | ) | ((TR_GET_CLASS((obj)))->magic == TR_CLASS_MAGIC) |
#define TR_PARENTCALL | ( | class, | |
object, | |||
_iface, | |||
method, | |||
... | |||
) |
Like call but this calls the implementation of the direct parent class of this object.
#define TR_PARENTRETCALL | ( | class, | |
object, | |||
_iface, | |||
method, | |||
ret, | |||
... | |||
) |
#define TR_RETCALL | ( | object, | |
_iface, | |||
method, | |||
ret, | |||
... | |||
) |
Call the class's implementation for the given method of the given interface _iface. The return value of the function is assigned to ret.
Definition at line 270 of file class.h.
Referenced by TR_getIndex().