taskrambler  0.1.9
Web server and task management solution.
class/class.h File Reference
#include <stdarg.h>
#include <sys/types.h>
#include <string.h>
#include <assert.h>
#include "class/interface.h"
+ Include dependency graph for class/class.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  class_ptr
 

Macros

#define _ISOC99_SOURCE
 
#define CLASS_MAGIC   0xFEFE
 
#define CLASS(name)
 
#define EXTENDS(parent)   const char _[sizeof(struct c_##parent)]
 
#define _NULL   NULL
 
#define CREATE_CLASS(name, _parent, ...)
 
#define INSTANCE(class, name)
 
#define INIT_CLASS(class)   ((class)->init? (class)->init() : (class))
 
#define GET_CLASS(object)   (INIT_CLASS(*(class_ptr *)((void*)(object) - sizeof(void*))))
 
#define IFACE_GET(class, iface)   (interfaceGet(&((class)->impl),(iface)))
 
#define HAS_PARENT(class)   (NULL != ((class)->parent) && INIT_CLASS((class)->parent))
 
#define IS_OBJECT(obj)   ((GET_CLASS((obj)))->magic == CLASS_MAGIC)
 
#define INSTANCE_OF(class, obj)   ((GET_CLASS((obj))) == _##class)
 
#define _CALL(_class, _iface, method, ...)
 
#define CALL(object, _iface, method, ...)
 
#define RETCALL(object, _iface, method, ret, ...)
 
#define PARENTCALL(object, _iface, method, ...)
 

Typedefs

typedef class_ptr(* fptr_classInit) (void)
 

Detailed Description

My own class implementation for C. It combines a data structure with a set of dynamically linked methods defined by an interface. A dynamically linked method will be called via a selector method which in turn gets the implementation stored in the class.

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/class.h.


Data Structure Documentation

struct class

Definition at line 133 of file class/class.h.

+ Collaboration diagram for class_ptr:
Data Fields
struct iface_impl impl
fptr_classInit init
const int magic
size_t object_size
class_ptr parent

Macro Definition Documentation

#define _CALL (   _class,
  _iface,
  method,
  ... 
)
Value:
do { \
class_ptr class = _class; \
iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \
while ((NULL == iface || NULL == iface->method) && HAS_PARENT(class)) { \
class = class->parent; \
iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \
} \
assert(NULL != iface->method); \
} while(0)
#define IFACE_GET(class, iface)
Definition: class/class.h:85
#define HAS_PARENT(class)
Definition: class/class.h:86
Todo:
actually i use gcc feature ## for variadoc... think about a way to make this standard.

Definition at line 95 of file class/class.h.

#define _ISOC99_SOURCE

Definition at line 38 of file class/class.h.

#define _NULL   NULL

Definition at line 52 of file class/class.h.

#define CALL (   object,
  _iface,
  method,
  ... 
)
Value:
do { \
struct i_##_iface * iface; \
_CALL(GET_CLASS(object), _iface, method, ##__VA_ARGS__); \
iface->method(object, ##__VA_ARGS__); \
} while(0)
#define GET_CLASS(object)
Definition: class/class.h:84
#define _CALL(_class, _iface, method,...)
Definition: class/class.h:95

Definition at line 106 of file class/class.h.

Referenced by classClone(), classDelete(), hashableHandleDouble(), loggerLog(), observerUpdate(), serialize(), subjectAttach(), subjectDetach(), subjectNotify(), and unserialize().

#define CLASS (   name)
Value:
struct c_##name; \
typedef struct c_##name * name; \
extern struct class * const _##name; \
struct c_##name

Definition at line 43 of file class/class.h.

#define CLASS_MAGIC   0xFEFE

Definition at line 41 of file class/class.h.

#define CREATE_CLASS (   name,
  _parent,
  ... 
)
Value:
static struct class c_##name; \
static class_ptr _classInit##name##_(void) { \
c_##name.parent = _##_parent; \
c_##name.init = NULL; \
return &c_##name; \
} \
static struct class c_##name = { \
NULL, \
sizeof(struct c_##name), \
_classInit##name##_, \
INIT_IFACE_IMPL(__VA_ARGS__) \
}; struct class * const _##name = &c_##name; \
struct c_##name##_object { void * class; struct c_##name data; }
class_ptr parent
Definition: class/class.h:135
#define CLASS_MAGIC
Definition: class/class.h:41
#define INIT_IFACE_IMPL(...)
Definition: interface.h:39

Definition at line 53 of file class/class.h.

#define EXTENDS (   parent)    const char _[sizeof(struct c_##parent)]

Definition at line 49 of file class/class.h.

Referenced by CLASS().

#define GET_CLASS (   object)    (INIT_CLASS(*(class_ptr *)((void*)(object) - sizeof(void*))))

Definition at line 84 of file class/class.h.

#define HAS_PARENT (   class)    (NULL != ((class)->parent) && INIT_CLASS((class)->parent))

Definition at line 86 of file class/class.h.

Referenced by hashableGetHash().

#define IFACE_GET (   class,
  iface 
)    (interfaceGet(&((class)->impl),(iface)))

Definition at line 85 of file class/class.h.

Referenced by hashableGetHash().

#define INIT_CLASS (   class)    ((class)->init? (class)->init() : (class))

Definition at line 83 of file class/class.h.

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

create a static instance of a class.

Todo:
this macro requires to close the initializer with an extra curly brancket. This is not nice...find a way to prevent this.

Definition at line 77 of file class/class.h.

#define INSTANCE_OF (   class,
  obj 
)    ((GET_CLASS((obj))) == _##class)

Definition at line 89 of file class/class.h.

Referenced by main().

#define IS_OBJECT (   obj)    ((GET_CLASS((obj)))->magic == CLASS_MAGIC)

Definition at line 88 of file class/class.h.

#define PARENTCALL (   object,
  _iface,
  method,
  ... 
)
Value:
do { \
struct i_##_iface * iface; \
class_ptr pc_class = GET_CLASS((object)); \
assert(HAS_PARENT(pc_class)); \
_CALL(pc_class->parent, _iface, method, ##__VA_ARGS__); \
iface->method(object, ##__VA_ARGS__); \
} while(0)
#define GET_CLASS(object)
Definition: class/class.h:84
#define HAS_PARENT(class)
Definition: class/class.h:86
#define _CALL(_class, _iface, method,...)
Definition: class/class.h:95

Definition at line 120 of file class/class.h.

Referenced by httpRequestCtor(), httpRequestDtor(), httpResponseCtor(), and httpResponseDtor().

#define RETCALL (   object,
  _iface,
  method,
  ret,
  ... 
)
Value:
do { \
struct i_##_iface * iface; \
_CALL(GET_CLASS(object), _iface, method, ##__VA_ARGS__); \
ret = iface->method(object, ##__VA_ARGS__); \
} while(0)
#define GET_CLASS(object)
Definition: class/class.h:84
#define _CALL(_class, _iface, method,...)
Definition: class/class.h:95

Definition at line 113 of file class/class.h.

Referenced by authenticate(), classNewParams(), httpIntroSizeGet(), httpIntroToString(), indexUuid(), streamReaderRead(), and streamWriterWrite().

Typedef Documentation

typedef class_ptr(* fptr_classInit) (void)

Definition at line 132 of file class/class.h.