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. 37
      include/class.h
  3. 28
      include/http/cookie.h
  4. 28
      include/http/writer.h
  5. 4
      include/interface.h
  6. 9
      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])

37
include/class.h

@ -52,73 +52,66 @@
#define _NULL NULL #define _NULL NULL
#define CREATE_CLASS(name,_parent,...) \ #define CREATE_CLASS(name,_parent,...) \
static struct class c_##name; \ static struct class c_##name; \
static void _classInit_(void) { \
static class_ptr _classInit_(void) { \
c_##name.parent = _##_parent; \ c_##name.parent = _##_parent; \
c_##name.init = NULL; \ c_##name.init = NULL; \
return &c_##name; \
} \ } \
static struct class c_##name = { \ static struct class c_##name = { \
CLASS_MAGIC, \ CLASS_MAGIC, \
NULL, \ NULL, \
sizeof(struct c_##name), \ sizeof(struct c_##name), \
_classInit_, \ _classInit_, \
INIT_IMPL(__VA_ARGS__) \
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,...) \
#define _CALL(_class,_iface,method,...) \
do { \ do { \
class_ptr class = GET_CLASS((object)); \
if (class->init) class->init(); \
class_ptr class = _class; \
iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \ iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \
while ((NULL == iface || NULL == iface->method) && HAS_PARENT(class)) { \ while ((NULL == iface || NULL == iface->method) && HAS_PARENT(class)) { \
class = class->parent; \ class = class->parent; \
if (class->init) class->init(); \
iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \ iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \
}; \
} \
assert(NULL != iface->method); \ assert(NULL != iface->method); \
} while(0) } while(0)
#define CALL(object,_iface,method,...) \ #define CALL(object,_iface,method,...) \
do { \ do { \
struct i_##_iface * iface; \ struct i_##_iface * iface; \
_CALL(object, _iface, method, ##__VA_ARGS__); \
_CALL(GET_CLASS(object), _iface, method, ##__VA_ARGS__); \
iface->method(object, ##__VA_ARGS__); \ iface->method(object, ##__VA_ARGS__); \
} while(0) } while(0)
#define RETCALL(object,_iface,method,ret,...) \ #define RETCALL(object,_iface,method,ret,...) \
do { \ do { \
struct i_##_iface * iface; \ struct i_##_iface * iface; \
_CALL(object, _iface, method, ##__VA_ARGS__); \
_CALL(GET_CLASS(object), _iface, method, ##__VA_ARGS__); \
ret = iface->method(object, ##__VA_ARGS__); \ ret = iface->method(object, ##__VA_ARGS__); \
} while(0) } while(0)
#define PARENTCALL(object,_iface,method,...) \ #define PARENTCALL(object,_iface,method,...) \
do { \ do { \
struct i_##_iface * iface; \ 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); \
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__); \ 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__

9
src/Makefile.am

@ -1,5 +1,4 @@
ACLOCAL_AMFLAGS = -I m4 ACLOCAL_AMFLAGS = -I m4
AUTOMAKE_OPTIONS = subdir-objects
IFACE = interface/class.c interface/stream_reader.c interface/logger.c \ IFACE = interface/class.c interface/stream_reader.c interface/logger.c \
interface/stream_writer.c interface/http_intro.c \ interface/stream_writer.c interface/http_intro.c \
@ -61,11 +60,11 @@ 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