Commit dd9d49d5a99516957618f155ce1ecfd51678c586
1 parent
dd9eae7e
made all class and interface basics to a small lib. This is the first one. All o…
…ther parts will become libs too. Right now these are only static libs but this way its easy to split them out if needed
Showing
56 changed files
with
173 additions
and
179 deletions
1 | -/** | |
2 | - * \file | |
3 | - * My own class implementation for C. It combines a data structure | |
4 | - * with a set of dynamically linked methods defined by an interface. A | |
5 | - * dynamically linked method will be called via a selector method which in | |
6 | - * turn gets the implementation stored in the class. | |
7 | - * | |
8 | - * \author Georg Hopp | |
9 | - * | |
10 | - * \copyright | |
11 | - * Copyright © 2012 Georg Hopp | |
12 | - * | |
13 | - * This program is free software: you can redistribute it and/or modify | |
14 | - * it under the terms of the GNU General Public License as published by | |
15 | - * the Free Software Foundation, either version 3 of the License, or | |
16 | - * (at your option) any later version. | |
17 | - * | |
18 | - * This program is distributed in the hope that it will be useful, | |
19 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | - * GNU General Public License for more details. | |
22 | - * | |
23 | - * You should have received a copy of the GNU General Public License | |
24 | - * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 | - */ | |
26 | - | |
27 | 1 | #ifndef __CLASS_H__ |
28 | 2 | #define __CLASS_H__ |
29 | 3 | |
30 | -#include <stdarg.h> | |
31 | -#include <sys/types.h> | |
32 | -#include <string.h> | |
33 | -#include <assert.h> | |
34 | - | |
35 | -#include "interface.h" | |
36 | - | |
37 | -#ifndef _ISOC99_SOURCE | |
38 | -#define _ISOC99_SOURCE | |
39 | -#endif | |
40 | - | |
41 | -#define CLASS_MAGIC 0xFEFE | |
42 | - | |
43 | -#define CLASS(name) \ | |
44 | - struct c_##name; \ | |
45 | - typedef struct c_##name * name; \ | |
46 | - extern struct class * const _##name; \ | |
47 | - struct c_##name | |
48 | - | |
49 | -#define EXTENDS(parent) \ | |
50 | - const char _[sizeof(struct c_##parent)] | |
51 | - | |
52 | -#define _NULL NULL | |
53 | -#define CREATE_CLASS(name,_parent,...) \ | |
54 | - static struct class c_##name; \ | |
55 | - static class_ptr _classInit_(void) { \ | |
56 | - c_##name.parent = _##_parent; \ | |
57 | - c_##name.init = NULL; \ | |
58 | - return &c_##name; \ | |
59 | - } \ | |
60 | - static struct class c_##name = { \ | |
61 | - CLASS_MAGIC, \ | |
62 | - NULL, \ | |
63 | - sizeof(struct c_##name), \ | |
64 | - _classInit_, \ | |
65 | - INIT_IFACE_IMPL(__VA_ARGS__) \ | |
66 | - }; struct class * const _##name = &c_##name | |
67 | - | |
68 | -#define INIT_CLASS(class) ((class)->init? (class)->init() : (class)) | |
69 | -#define GET_CLASS(object) (INIT_CLASS(*(class_ptr *)((object) - sizeof(void*)))) | |
70 | -#define IFACE_GET(class,iface) (interfaceGet(&((class)->impl),(iface))) | |
71 | -#define HAS_PARENT(class) (NULL != ((class)->parent) && INIT_CLASS((class)->parent)) | |
72 | - | |
73 | -/** | |
74 | - * \todo actually i use gcc feature ## for variadoc... think about | |
75 | - * a way to make this standard. | |
76 | - */ | |
77 | -#define _CALL(_class,_iface,method,...) \ | |
78 | - do { \ | |
79 | - class_ptr class = _class; \ | |
80 | - iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \ | |
81 | - while ((NULL == iface || NULL == iface->method) && HAS_PARENT(class)) { \ | |
82 | - class = class->parent; \ | |
83 | - iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \ | |
84 | - } \ | |
85 | - assert(NULL != iface->method); \ | |
86 | - } while(0) | |
87 | - | |
88 | -#define CALL(object,_iface,method,...) \ | |
89 | - do { \ | |
90 | - struct i_##_iface * iface; \ | |
91 | - _CALL(GET_CLASS(object), _iface, method, ##__VA_ARGS__); \ | |
92 | - iface->method(object, ##__VA_ARGS__); \ | |
93 | - } while(0) | |
94 | - | |
95 | -#define RETCALL(object,_iface,method,ret,...) \ | |
96 | - do { \ | |
97 | - struct i_##_iface * iface; \ | |
98 | - _CALL(GET_CLASS(object), _iface, method, ##__VA_ARGS__); \ | |
99 | - ret = iface->method(object, ##__VA_ARGS__); \ | |
100 | - } while(0) | |
101 | - | |
102 | -#define PARENTCALL(object,_iface,method,...) \ | |
103 | - do { \ | |
104 | - struct i_##_iface * iface; \ | |
105 | - class_ptr pc_class = GET_CLASS((object)); \ | |
106 | - assert(HAS_PARENT(pc_class)); \ | |
107 | - _CALL(pc_class->parent, _iface, method, ##__VA_ARGS__); \ | |
108 | - iface->method(object, ##__VA_ARGS__); \ | |
109 | - } while(0) | |
110 | - | |
111 | - | |
112 | -struct class; | |
113 | -typedef struct class * class_ptr; | |
114 | -typedef class_ptr (* fptr_classInit)(void); | |
115 | -struct class { | |
116 | - const int magic; | |
117 | - class_ptr parent; | |
118 | - size_t object_size; | |
119 | - fptr_classInit init; | |
120 | - struct iface_impl impl; | |
121 | -}; | |
4 | +#include "class/class.h" | |
5 | +#include "class/interface.h" | |
6 | +#include "class/interface/class.h" | |
122 | 7 | |
123 | 8 | #endif // __CLASS_H__ |
124 | 9 | ... | ... |
include/class/class.h
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * My own class implementation for C. It combines a data structure | |
4 | + * with a set of dynamically linked methods defined by an interface. A | |
5 | + * dynamically linked method will be called via a selector method which in | |
6 | + * turn gets the implementation stored in the class. | |
7 | + * | |
8 | + * \author Georg Hopp | |
9 | + * | |
10 | + * \copyright | |
11 | + * Copyright © 2012 Georg Hopp | |
12 | + * | |
13 | + * This program is free software: you can redistribute it and/or modify | |
14 | + * it under the terms of the GNU General Public License as published by | |
15 | + * the Free Software Foundation, either version 3 of the License, or | |
16 | + * (at your option) any later version. | |
17 | + * | |
18 | + * This program is distributed in the hope that it will be useful, | |
19 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | + * GNU General Public License for more details. | |
22 | + * | |
23 | + * You should have received a copy of the GNU General Public License | |
24 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 | + */ | |
26 | + | |
27 | +#ifndef __CLASS_CLASS_H__ | |
28 | +#define __CLASS_CLASS_H__ | |
29 | + | |
30 | +#include <stdarg.h> | |
31 | +#include <sys/types.h> | |
32 | +#include <string.h> | |
33 | +#include <assert.h> | |
34 | + | |
35 | +#include "class/interface.h" | |
36 | + | |
37 | +#ifndef _ISOC99_SOURCE | |
38 | +#define _ISOC99_SOURCE | |
39 | +#endif | |
40 | + | |
41 | +#define CLASS_MAGIC 0xFEFE | |
42 | + | |
43 | +#define CLASS(name) \ | |
44 | + struct c_##name; \ | |
45 | + typedef struct c_##name * name; \ | |
46 | + extern struct class * const _##name; \ | |
47 | + struct c_##name | |
48 | + | |
49 | +#define EXTENDS(parent) \ | |
50 | + const char _[sizeof(struct c_##parent)] | |
51 | + | |
52 | +#define _NULL NULL | |
53 | +#define CREATE_CLASS(name,_parent,...) \ | |
54 | + static struct class c_##name; \ | |
55 | + static class_ptr _classInit_(void) { \ | |
56 | + c_##name.parent = _##_parent; \ | |
57 | + c_##name.init = NULL; \ | |
58 | + return &c_##name; \ | |
59 | + } \ | |
60 | + static struct class c_##name = { \ | |
61 | + CLASS_MAGIC, \ | |
62 | + NULL, \ | |
63 | + sizeof(struct c_##name), \ | |
64 | + _classInit_, \ | |
65 | + INIT_IFACE_IMPL(__VA_ARGS__) \ | |
66 | + }; struct class * const _##name = &c_##name | |
67 | + | |
68 | +#define INIT_CLASS(class) ((class)->init? (class)->init() : (class)) | |
69 | +#define GET_CLASS(object) (INIT_CLASS(*(class_ptr *)((object) - sizeof(void*)))) | |
70 | +#define IFACE_GET(class,iface) (interfaceGet(&((class)->impl),(iface))) | |
71 | +#define HAS_PARENT(class) (NULL != ((class)->parent) && INIT_CLASS((class)->parent)) | |
72 | + | |
73 | +/** | |
74 | + * \todo actually i use gcc feature ## for variadoc... think about | |
75 | + * a way to make this standard. | |
76 | + */ | |
77 | +#define _CALL(_class,_iface,method,...) \ | |
78 | + do { \ | |
79 | + class_ptr class = _class; \ | |
80 | + iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \ | |
81 | + while ((NULL == iface || NULL == iface->method) && HAS_PARENT(class)) { \ | |
82 | + class = class->parent; \ | |
83 | + iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \ | |
84 | + } \ | |
85 | + assert(NULL != iface->method); \ | |
86 | + } while(0) | |
87 | + | |
88 | +#define CALL(object,_iface,method,...) \ | |
89 | + do { \ | |
90 | + struct i_##_iface * iface; \ | |
91 | + _CALL(GET_CLASS(object), _iface, method, ##__VA_ARGS__); \ | |
92 | + iface->method(object, ##__VA_ARGS__); \ | |
93 | + } while(0) | |
94 | + | |
95 | +#define RETCALL(object,_iface,method,ret,...) \ | |
96 | + do { \ | |
97 | + struct i_##_iface * iface; \ | |
98 | + _CALL(GET_CLASS(object), _iface, method, ##__VA_ARGS__); \ | |
99 | + ret = iface->method(object, ##__VA_ARGS__); \ | |
100 | + } while(0) | |
101 | + | |
102 | +#define PARENTCALL(object,_iface,method,...) \ | |
103 | + do { \ | |
104 | + struct i_##_iface * iface; \ | |
105 | + class_ptr pc_class = GET_CLASS((object)); \ | |
106 | + assert(HAS_PARENT(pc_class)); \ | |
107 | + _CALL(pc_class->parent, _iface, method, ##__VA_ARGS__); \ | |
108 | + iface->method(object, ##__VA_ARGS__); \ | |
109 | + } while(0) | |
110 | + | |
111 | + | |
112 | +struct class; | |
113 | +typedef struct class * class_ptr; | |
114 | +typedef class_ptr (* fptr_classInit)(void); | |
115 | +struct class { | |
116 | + const int magic; | |
117 | + class_ptr parent; | |
118 | + size_t object_size; | |
119 | + fptr_classInit init; | |
120 | + struct iface_impl impl; | |
121 | +}; | |
122 | + | |
123 | +#endif // __CLASS_CLASS_H__ | |
124 | + | |
125 | +// vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -24,8 +24,8 @@ |
24 | 24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
25 | 25 | */ |
26 | 26 | |
27 | -#ifndef __INTERFACE_H__ | |
28 | -#define __INTERFACE_H__ | |
27 | +#ifndef __CLASS_INTERFACE_H__ | |
28 | +#define __CLASS_INTERFACE_H__ | |
29 | 29 | |
30 | 30 | #include <sys/types.h> |
31 | 31 | |
... | ... | @@ -54,6 +54,6 @@ typedef struct iface_impl * iface_impl_ptr; |
54 | 54 | |
55 | 55 | extern iface_ptr interfaceGet(iface_impl_ptr, const iface_ptr); |
56 | 56 | |
57 | -#endif // __INTERFACE_H__ | |
57 | +#endif // __CLASS_INTERFACE_H__ | |
58 | 58 | |
59 | 59 | // vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -23,13 +23,13 @@ |
23 | 23 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
24 | 24 | */ |
25 | 25 | |
26 | -#ifndef __INTERFACE_CLASS_H__ | |
27 | -#define __INTERFACE_CLASS_H__ | |
26 | +#ifndef __CLASS_INTERFACE_CLASS_H__ | |
27 | +#define __CLASS_INTERFACE_CLASS_H__ | |
28 | 28 | |
29 | 29 | #include <stdarg.h> |
30 | 30 | |
31 | -#include "class.h" | |
32 | -#include "interface.h" | |
31 | +#include "class/class.h" | |
32 | +#include "class/interface.h" | |
33 | 33 | |
34 | 34 | typedef int (* fptr_ctor)(void *, va_list *); |
35 | 35 | typedef void (* fptr_dtor)(void *); |
... | ... | @@ -52,6 +52,6 @@ extern void * classClone(void *); |
52 | 52 | #define delete(object) classDelete((void **)&(object)) |
53 | 53 | #define clone(object) classClone((void *)(object)) |
54 | 54 | |
55 | -#endif // __INTERFACE_CLASS_H__ | |
55 | +#endif // __CLASS_INTERFACE_CLASS_H__ | |
56 | 56 | |
57 | 57 | // vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -24,7 +24,7 @@ |
24 | 24 | #ifndef __INTERFACE_HASHABLE_H__ |
25 | 25 | #define __INTERFACE_HASHABLE_H__ |
26 | 26 | |
27 | -#include "interface.h" | |
27 | +#include "class.h" | |
28 | 28 | |
29 | 29 | typedef unsigned long (* fptr_hashableGetHash)(void *); |
30 | 30 | typedef void (* fptr_hashableHandleDouble)(void *, void *); | ... | ... |
1 | 1 | ACLOCAL_AMFLAGS = -I m4 |
2 | 2 | |
3 | -IFACE = interface/class.c interface/stream_reader.c interface/logger.c \ | |
3 | +IFACE = interface/stream_reader.c interface/logger.c \ | |
4 | 4 | interface/stream_writer.c interface/http_intro.c \ |
5 | - interface/subject.c interface/observer.c interface.c | |
5 | + interface/subject.c interface/observer.c | |
6 | 6 | SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c |
7 | 7 | STREAM = stream.c stream/read.c stream/write.c |
8 | 8 | HASH = hash.c hash/add.c hash/get.c hash/delete.c \ |
... | ... | @@ -67,4 +67,6 @@ taskrambler_SOURCES = taskrambler.c \ |
67 | 67 | $(WRITER) $(RESP) $(HEADER) $(PARSER) $(WORKER) $(CB) \ |
68 | 68 | $(UTILS) $(MSGQ) $(SESSION) $(STREAM) $(HASH) $(AUTH) |
69 | 69 | taskrambler_CFLAGS = -Wall -I ../include/ |
70 | -taskrambler_LDFLAGS = -lrt -lssl -lldap | |
70 | +taskrambler_LDFLAGS = -L./class -lclass -lrt -lssl -lldap | |
71 | + | |
72 | +SUBDIRS = class | ... | ... |
src/class/Makefile.am
0 → 100644
src/class/libclass.a
0 → 100644
No preview for this file type
Please
register
or
login
to post a comment