Browse Source

cleanups in class and some code cleanups

release0.1.5
Georg Hopp 14 years ago
parent
commit
dd9eae7e5b
  1. 5
      configure.ac
  2. 99
      include/class.h
  3. 28
      include/http/cookie.h
  4. 28
      include/http/writer.h
  5. 4
      include/interface.h
  6. 119
      src/Makefile.am
  7. 8
      src/interface.c
  8. 2
      src/interface/class.c
  9. 1
      tests/Makefile.am
  10. 4
      tests/mock/class.h
  11. 4
      tests/runtest.h

5
configure.ac

@ -3,8 +3,9 @@
AC_PREREQ([2.68]) AC_PREREQ([2.68])
AC_INIT([taskrambler], [0.0.1], [Georg Hopp <georg@steffers.org>]) AC_INIT([taskrambler], [0.0.1], [Georg Hopp <georg@steffers.org>])
AM_INIT_AUTOMAKE
AC_COPYRIGHT([Copyright (C) 2012 Georg Hopp])
AM_INIT_AUTOMAKE([subdir-objects])
AM_SILENT_RULES([yes])
AC_COPYRIGHT([Copyright © 2012 Georg Hopp])
AC_REVISION([$Revision: 0.01 $]) AC_REVISION([$Revision: 0.01 $])
AC_CONFIG_SRCDIR([src/taskrambler.c]) AC_CONFIG_SRCDIR([src/taskrambler.c])
AC_CONFIG_HEADERS([config.h]) AC_CONFIG_HEADERS([config.h])

99
include/class.h

@ -40,9 +40,9 @@
#define CLASS_MAGIC 0xFEFE #define CLASS_MAGIC 0xFEFE
#define CLASS(name) \
struct c_##name; \
typedef struct c_##name * name; \
#define CLASS(name) \
struct c_##name; \
typedef struct c_##name * name; \
extern struct class * const _##name; \ extern struct class * const _##name; \
struct c_##name struct c_##name
@ -50,75 +50,68 @@
const char _[sizeof(struct c_##parent)] const char _[sizeof(struct c_##parent)]
#define _NULL NULL #define _NULL NULL
#define CREATE_CLASS(name,_parent,...) \
static struct class c_##name; \
static void _classInit_(void) { \
c_##name.parent = _##_parent; \
c_##name.init = NULL; \
} \
static struct class c_##name = { \
CLASS_MAGIC, \
NULL, \
sizeof(struct c_##name), \
_classInit_, \
INIT_IMPL(__VA_ARGS__) \
#define CREATE_CLASS(name,_parent,...) \
static struct class c_##name; \
static class_ptr _classInit_(void) { \
c_##name.parent = _##_parent; \
c_##name.init = NULL; \
return &c_##name; \
} \
static struct class c_##name = { \
CLASS_MAGIC, \
NULL, \
sizeof(struct c_##name), \
_classInit_, \
INIT_IFACE_IMPL(__VA_ARGS__) \
}; struct class * const _##name = &c_##name }; struct class * const _##name = &c_##name
#define GET_CLASS(object) (*(class_ptr *)((object) - sizeof(void*)))
#define INIT_CLASS(class) ((class)->init? (class)->init() : (class))
#define GET_CLASS(object) (INIT_CLASS(*(class_ptr *)((object) - sizeof(void*))))
#define IFACE_GET(class,iface) (interfaceGet(&((class)->impl),(iface))) #define IFACE_GET(class,iface) (interfaceGet(&((class)->impl),(iface)))
#define IFACE_EXISTS(class,iface) (NULL != IFACE_GET((class),(iface)))
#define HAS_PARENT(class) (NULL != ((class)->parent) && INIT_CLASS((class)->parent))
/** /**
* \todo actually i use gcc feature ## for variadoc... think about * \todo actually i use gcc feature ## for variadoc... think about
* a way to make this standard. * a way to make this standard.
*/ */
#define _CALL(object,_iface,method,...) \
do { \
class_ptr class = GET_CLASS((object)); \
if (class->init) class->init(); \
iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \
while ((NULL == iface || NULL == iface->method) && HAS_PARENT(class)) { \
class = class->parent; \
if (class->init) class->init(); \
iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \
}; \
assert(NULL != iface->method); \
#define _CALL(_class,_iface,method,...) \
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) } while(0)
#define CALL(object,_iface,method,...) \
do { \
struct i_##_iface * iface; \
_CALL(object, _iface, method, ##__VA_ARGS__); \
iface->method(object, ##__VA_ARGS__); \
#define CALL(object,_iface,method,...) \
do { \
struct i_##_iface * iface; \
_CALL(GET_CLASS(object), _iface, method, ##__VA_ARGS__); \
iface->method(object, ##__VA_ARGS__); \
} while(0) } while(0)
#define RETCALL(object,_iface,method,ret,...) \
do { \
struct i_##_iface * iface; \
_CALL(object, _iface, method, ##__VA_ARGS__); \
ret = iface->method(object, ##__VA_ARGS__); \
#define RETCALL(object,_iface,method,ret,...) \
do { \
struct i_##_iface * iface; \
_CALL(GET_CLASS(object), _iface, method, ##__VA_ARGS__); \
ret = iface->method(object, ##__VA_ARGS__); \
} while(0) } while(0)
#define PARENTCALL(object,_iface,method,...) \
do { \
struct i_##_iface * iface; \
class_ptr class = GET_CLASS((object)); \
if (class->init) class->init(); \
assert(HAS_PARENT(class)); \
class = class->parent; \
if (class->init) class->init(); \
iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \
assert(NULL != iface->method); \
iface->method(object, ##__VA_ARGS__); \
#define PARENTCALL(object,_iface,method,...) \
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) } while(0)
#define HAS_PARENT(class) (NULL != ((class)->parent))
typedef void (* fptr_classInit)(void);
struct class; struct class;
typedef struct class * class_ptr; typedef struct class * class_ptr;
typedef class_ptr (* fptr_classInit)(void);
struct class { struct class {
const int magic; const int magic;
class_ptr parent; class_ptr parent;

28
include/http/cookie.h

@ -4,20 +4,20 @@
* \author Georg Hopp * \author Georg Hopp
* *
* \copyright * \copyright
* - Copyright © 2012 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/>.
* Copyright © 2012 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/>.
*/ */
#ifndef __HTTP_COOKIE_H__ #ifndef __HTTP_COOKIE_H__

28
include/http/writer.h

@ -5,20 +5,20 @@
* \author Georg Hopp * \author Georg Hopp
* *
* \copyright * \copyright
* - Copyright © 2012 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/>.
* Copyright © 2012 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/>.
*/ */
#ifndef __HTTP_WRITER_H__ #ifndef __HTTP_WRITER_H__

4
include/interface.h

@ -36,7 +36,7 @@
static const struct i_##name i_##name##_impl = {&i_##name,__VA_ARGS__} static const struct i_##name i_##name##_impl = {&i_##name,__VA_ARGS__}
#define NUMARGS(...) (sizeof((const void*[]){__VA_ARGS__})/sizeof(void*)) #define NUMARGS(...) (sizeof((const void*[]){__VA_ARGS__})/sizeof(void*))
#define INIT_IMPL(...) {NUMARGS(__VA_ARGS__), 0, {__VA_ARGS__}}
#define INIT_IFACE_IMPL(...) {NUMARGS(__VA_ARGS__), 0, {__VA_ARGS__}}
struct interface { struct interface {
@ -52,7 +52,7 @@ struct iface_impl {
}; };
typedef struct iface_impl * iface_impl_ptr; typedef struct iface_impl * iface_impl_ptr;
extern struct interface * interfaceGet(iface_impl_ptr, const iface_ptr);
extern iface_ptr interfaceGet(iface_impl_ptr, const iface_ptr);
#endif // __INTERFACE_H__ #endif // __INTERFACE_H__

119
src/Makefile.am

@ -1,71 +1,70 @@
ACLOCAL_AMFLAGS = -I m4 ACLOCAL_AMFLAGS = -I m4
AUTOMAKE_OPTIONS = subdir-objects
IFACE = interface/class.c interface/stream_reader.c interface/logger.c \
interface/stream_writer.c interface/http_intro.c \
interface/subject.c interface/observer.c interface.c
SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c
STREAM = stream.c stream/read.c stream/write.c
HASH = hash.c hash/add.c hash/get.c hash/delete.c \
hash/each.c interface/hashable.c hash_value.c
SERVER = server.c server/run.c server/close_conn.c server/poll.c \
server/handle_accept.c server/read.c server/write.c
LOGGER = logger.c logger/stderr.c logger/syslog.c
CB = cbuf.c cbuf/read.c cbuf/write.c \
cbuf/get_line.c cbuf/set_data.c cbuf/get_data.c \
cbuf/addr_index.c cbuf/get_free.c cbuf/get_read.c cbuf/get_write.c \
cbuf/inc_read.c cbuf/inc_write.c cbuf/is_empty.c cbuf/memchr.c \
cbuf/skip_non_alpha.c cbuf/is_locked.c cbuf/lock.c cbuf/release.c \
cbuf/empty.c
MSG = http/message.c \
http/message/has_keep_alive.c \
http/message/header_size_get.c \
http/message/header_to_string.c \
http/message/get_version.c \
http/message/has_valid_version.c
MSGQ = http/message/queue.c
REQ = http/request.c \
http/request/has_valid_method.c
RESP = http/response.c \
http/response/304.c \
http/response/404.c \
http/response/403.c \
http/response/login_form.c \
http/response/asset.c \
http/response/randval.c \
http/response/session.c
PARSER = http/parser.c \
http/parser/parse.c \
http/parser/new_message.c \
http/parser/header.c \
http/parser/body.c \
http/parser/request_vars.c \
http/parser/post_vars.c
WRITER = http/writer.c \
http/writer/write.c
WORKER = http/worker.c \
http/worker/process.c \
http/worker/write.c \
http/worker/get_asset.c \
http/worker/add_common_header.c
HEADER = http/header.c \
http/header/to_string.c
IFACE = interface/class.c interface/stream_reader.c interface/logger.c \
interface/stream_writer.c interface/http_intro.c \
interface/subject.c interface/observer.c interface.c
SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c
STREAM = stream.c stream/read.c stream/write.c
HASH = hash.c hash/add.c hash/get.c hash/delete.c \
hash/each.c interface/hashable.c hash_value.c
SERVER = server.c server/run.c server/close_conn.c server/poll.c \
server/handle_accept.c server/read.c server/write.c
LOGGER = logger.c logger/stderr.c logger/syslog.c
CB = cbuf.c cbuf/read.c cbuf/write.c \
cbuf/get_line.c cbuf/set_data.c cbuf/get_data.c \
cbuf/addr_index.c cbuf/get_free.c cbuf/get_read.c cbuf/get_write.c \
cbuf/inc_read.c cbuf/inc_write.c cbuf/is_empty.c cbuf/memchr.c \
cbuf/skip_non_alpha.c cbuf/is_locked.c cbuf/lock.c cbuf/release.c \
cbuf/empty.c
MSG = http/message.c \
http/message/has_keep_alive.c \
http/message/header_size_get.c \
http/message/header_to_string.c \
http/message/get_version.c \
http/message/has_valid_version.c
MSGQ = http/message/queue.c
REQ = http/request.c \
http/request/has_valid_method.c
RESP = http/response.c \
http/response/304.c \
http/response/404.c \
http/response/403.c \
http/response/login_form.c \
http/response/asset.c \
http/response/randval.c \
http/response/session.c
PARSER = http/parser.c \
http/parser/parse.c \
http/parser/new_message.c \
http/parser/header.c \
http/parser/body.c \
http/parser/request_vars.c \
http/parser/post_vars.c
WRITER = http/writer.c \
http/writer/write.c
WORKER = http/worker.c \
http/worker/process.c \
http/worker/write.c \
http/worker/get_asset.c \
http/worker/add_common_header.c
HEADER = http/header.c \
http/header/to_string.c
SESSION = session.c session/add.c session/get.c session/delete.c SESSION = session.c session/add.c session/get.c session/delete.c
UTILS = utils/hash.c \
utils/memory.c \
utils/http.c \
utils/daemonize.c \
utils/signalHandling.c
AUTH = interface/auth.c auth/ldap.c credential.c
UTILS = utils/hash.c \
utils/memory.c \
utils/http.c \
utils/daemonize.c \
utils/signalHandling.c
AUTH = interface/auth.c auth/ldap.c credential.c
AM_CFLAGS = -Wall -I ../include/ AM_CFLAGS = -Wall -I ../include/
bin_PROGRAMS = webgameserver
bin_PROGRAMS = taskrambler
webgameserver_SOURCES = webgameserver.c \
taskrambler_SOURCES = taskrambler.c \
$(IFACE) $(SOCKET) $(SERVER) $(LOGGER) $(MSG) $(REQ) \ $(IFACE) $(SOCKET) $(SERVER) $(LOGGER) $(MSG) $(REQ) \
$(WRITER) $(RESP) $(HEADER) $(PARSER) $(WORKER) $(CB) \ $(WRITER) $(RESP) $(HEADER) $(PARSER) $(WORKER) $(CB) \
$(UTILS) $(MSGQ) $(SESSION) $(STREAM) $(HASH) $(AUTH) $(UTILS) $(MSGQ) $(SESSION) $(STREAM) $(HASH) $(AUTH)
webgameserver_CFLAGS = -Wall -I ../include/
webgameserver_LDFLAGS = -lrt -lssl -lldap
taskrambler_CFLAGS = -Wall -I ../include/
taskrambler_LDFLAGS = -lrt -lssl -lldap

8
src/interface.c

@ -40,25 +40,25 @@ comp(const void * _a, const void * _b)
* this one is important in selector functions to get the correct interface * this one is important in selector functions to get the correct interface
* implementation of a class. * implementation of a class.
*/ */
struct interface *
iface_ptr
interfaceGet(iface_impl_ptr iface_impl, const iface_ptr _iface) interfaceGet(iface_impl_ptr iface_impl, const iface_ptr _iface)
{ {
const iface_ptr * iface = &_iface; const iface_ptr * iface = &_iface;
void * dummy;
iface_ptr * found;
if (! iface_impl->simpl) { if (! iface_impl->simpl) {
qsort((void**)(iface_impl->impl), iface_impl->nimpl, sizeof(iface_ptr), comp); qsort((void**)(iface_impl->impl), iface_impl->nimpl, sizeof(iface_ptr), comp);
iface_impl->simpl=TRUE; iface_impl->simpl=TRUE;
} }
dummy = bsearch(
found = bsearch(
&iface, &iface,
iface_impl->impl, iface_impl->impl,
iface_impl->nimpl, iface_impl->nimpl,
sizeof(iface_ptr), sizeof(iface_ptr),
comp); comp);
return dummy? *(struct interface **)dummy : dummy;
return found? *found : (iface_ptr)NULL;
} }
// vim: set ts=4 sw=4: // vim: set ts=4 sw=4:

2
src/interface/class.c

@ -40,8 +40,6 @@ classNew(class_ptr class, ...)
va_list params; va_list params;
int ret; int ret;
if (class->init) class->init();
* (class_ptr *)object = class; * (class_ptr *)object = class;
object += sizeof(void*); object += sizeof(void*);

1
tests/Makefile.am

@ -1,5 +1,4 @@
ACLOCAL_AMFLAGS = -I m4 ACLOCAL_AMFLAGS = -I m4
AUTOMAKE_OPTIONS = subdir-objects
TESTS_ENVIRONMENT = valgrind --error-exitcode=123 --leak-check=full --quiet TESTS_ENVIRONMENT = valgrind --error-exitcode=123 --leak-check=full --quiet
TESTS = cclassTest loggerTest socketTest serverTest TESTS = cclassTest loggerTest socketTest serverTest

4
tests/mock/class.h

@ -1,6 +1,10 @@
/** /**
* \file * \file
* mock/class.h: definitions for my mock to test my oop stuff * mock/class.h: definitions for my mock to test my oop stuff
*
* \author Georg Hopp <georg@steffers.org>
*
* \copyright
* Copyright (C) 2011 Georg Hopp * Copyright (C) 2011 Georg Hopp
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify

4
tests/runtest.h

@ -1,6 +1,10 @@
/** /**
* \file * \file
* runtest.h: assertions and other definitions for all my tests * runtest.h: assertions and other definitions for all my tests
*
* \author Georg Hopp <georg@steffers.org>
*
* \copyright
* Copyright (C) 2011 Georg Hopp * Copyright (C) 2011 Georg Hopp
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify

Loading…
Cancel
Save