libtrbase  1.0.2
Web server and task management solution.
class.h File Reference
#include <stdarg.h>
#include <sys/types.h>
#include <string.h>
#include <assert.h>
#include "tr/interface.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.

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, ...)
 

Detailed Description

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.

Author
Georg Hopp

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.

Macro Definition Documentation

#define _NULL   NULL

Some macros might translate a give NULL to _NULL, this should in turn be a real NULL again.

Definition at line 103 of file class.h.

#define TR_CALL (   object,
  _iface,
  method,
  ... 
)
Value:
do { \
struct i_##_iface * iface; \
_TR_CALL(TR_GET_CLASS(object), _iface, method); \
iface->method(object, ##__VA_ARGS__); \
} while(0)
#define TR_GET_CLASS(object)
Definition: class.h:188

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.

Todo:
actually i use gcc feature ## for variadoc... think about a way to make this standard.

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)
Value:
extern struct TR_class c_##name; \
struct c_##name; \
typedef struct c_##name * name; \
extern struct TR_class * const _##name; \
struct c_##name

Class declaration macro. This one declares all structures that are needed to create a new class.

Definition at line 63 of file class.h.

#define TR_CLASS_BY_NAME (   class_name)    (TR_INIT_CLASS(& c_##class_name))

Definition at line 191 of file class.h.

#define TR_CLASS_BY_NAME_STATIC (   class_name)    (& c_##class_name)

Definition at line 192 of file class.h.

#define TR_CLASS_MAGIC   0xFEFE

A magic number that identifies instances of a TR_CLASS class.

Definition at line 57 of file class.h.

#define TR_CLASSVARS (   name,
  class 
)    ((struct c_##name##_vars *)(class)->vars)

Definition at line 77 of file class.h.

#define TR_CLASSVARS_BY_NAME (   name)    ((struct c_##name##_vars *)(TR_CLASS_BY_NAME(name))->vars)

Definition at line 78 of file class.h.

#define TR_CLASSVARS_DECL (   name)    struct c_##name##_vars

Definition at line 76 of file class.h.

#define TR_CLASSVARS_STATIC (   name)    ((struct c_##name##_vars *)(TR_CLASS_BY_NAME_STATIC(name))->vars)

Definition at line 80 of file class.h.

#define TR_CREATE_CLASS (   name,
  _parent,
  cvInit,
  ... 
)
Value:
struct c_##name##_vars _##name##_vars; \
void (* TR_initClassVars##name)(TR_class_ptr) = cvInit; \
static TR_class_ptr _classInit##name##_(void) { \
c_##name.init = NULL; \
c_##name.parent = _##_parent; \
if (TR_initClassVars##name) \
TR_initClassVars##name(_##name); \
return &c_##name; \
}; \
struct TR_class c_##name = { \
NULL, \
sizeof(struct c_##name), \
_classInit##name##_, \
&_##name##_vars, \
TR_INIT_IFACE_IMPL(__VA_ARGS__) \
}; \
struct TR_class * const _##name = &c_##name; \
struct c_##name##_vars _##name##_vars
#define TR_CLASS_MAGIC
Definition: class.h:57

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.

Definition at line 119 of file class.h.

#define TR_CV_EXTENDS (   parent)    struct c_##parent##_vars _

Definition at line 97 of file class.h.

#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.

See also
TR_CREATE_CLASS

Definition at line 188 of file class.h.

#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.

See also
TR_EXTENDS

Definition at line 207 of file class.h.

#define TR_IFACE_GET (   class,
  iface 
)    (TR_interfaceGet(&((class)->impl),(iface)))

Returns this class's implementations of the interface identified by iface.

Definition at line 198 of file class.h.

#define TR_INHERIT_CLASSVARS (   dest,
  src 
)
Value:
memcpy(TR_CLASSVARS_BY_NAME(dest), \
sizeof(TR_CLASSVARS_DECL(src)))
#define TR_CLASSVARS_DECL(name)
Definition: class.h:76
#define TR_CLASSVARS_BY_NAME(name)
Definition: class.h:78

Definition at line 83 of file class.h.

#define TR_INSTANCE (   class,
  name,
  ... 
)
Value:
struct c_##class##_object _##name; \
class name = &(_##name.data); \
struct c_##class##_object _##name = { \
&c_##class, \
{ __VA_ARGS__ } \
}

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.

Definition at line 153 of file class.h.

#define TR_INSTANCE_CAST (   class,
  name 
)    ((class)&(_##name.data))

Definition at line 161 of file class.h.

#define TR_INSTANCE_INIT (   name)
Value:
struct c_##name##_object { \
void * TR_class; \
struct c_##name data; \
}

Definition at line 70 of file class.h.

#define TR_INSTANCE_OF (   class,
  obj 
)    ((TR_GET_CLASS((obj))) == _##class)

Check of obj points to an instance of class.

Definition at line 221 of file class.h.

#define TR_IS_OBJECT (   obj)    ((TR_GET_CLASS((obj)))->magic == TR_CLASS_MAGIC)

Check if obj really points to a class instance.

/see TR_CLASS_MAGIC

Definition at line 215 of file class.h.

#define TR_PARENTCALL (   class,
  object,
  _iface,
  method,
  ... 
)
Value:
do { \
struct i_##_iface * iface; \
TR_class_ptr pc_class = TR_CLASS_BY_NAME(class); \
assert(TR_HAS_PARENT(pc_class)); \
_TR_CALL(pc_class->parent, _iface, method); \
iface->method(object, ##__VA_ARGS__); \
} while(0)
#define TR_CLASS_BY_NAME(class_name)
Definition: class.h:191
#define TR_HAS_PARENT(class)
Definition: class.h:207

Like call but this calls the implementation of the direct parent class of this object.

See also
TR_CALL

Definition at line 283 of file class.h.

#define TR_PARENTRETCALL (   class,
  object,
  _iface,
  method,
  ret,
  ... 
)
Value:
do { \
struct i_##_iface * iface; \
TR_class_ptr pc_class = TR_CLASS_BY_NAME(class); \
assert(TR_HAS_PARENT(pc_class)); \
_TR_CALL(pc_class->parent, _iface, method); \
ret = iface->method(object, ##__VA_ARGS__); \
} while(0)
#define TR_CLASS_BY_NAME(class_name)
Definition: class.h:191
#define TR_HAS_PARENT(class)
Definition: class.h:207

Definition at line 298 of file class.h.

#define TR_RETCALL (   object,
  _iface,
  method,
  ret,
  ... 
)
Value:
do { \
struct i_##_iface * iface; \
_TR_CALL(TR_GET_CLASS(object), _iface, method); \
ret = iface->method(object, ##__VA_ARGS__); \
} while(0)
#define TR_GET_CLASS(object)
Definition: class.h:188

Call the class's implementation for the given method of the given interface _iface. The return value of the function is assigned to ret.

See also
TR_CALL

Definition at line 270 of file class.h.

Referenced by TR_getIndex().