Browse Source

get rid of some function calls when selection interfaces

master
Georg Hopp 14 years ago
parent
commit
95c0d00944
  1. 2
      configure.ac
  2. 20
      include/class.h
  3. 6
      src/Makefile.am
  4. 50
      src/class.c
  5. 2
      src/interface/class.c

2
configure.ac

@ -6,7 +6,7 @@ AC_INIT([cclass], [0.0.1], [Georg Hopp <georg@steffers.org>])
AM_INIT_AUTOMAKE AM_INIT_AUTOMAKE
AC_COPYRIGHT([Copyright (C) 2012 Georg Hopp]) AC_COPYRIGHT([Copyright (C) 2012 Georg Hopp])
AC_REVISION([$Revision: 0.01 $]) AC_REVISION([$Revision: 0.01 $])
AC_CONFIG_SRCDIR([src/class.c])
AC_CONFIG_SRCDIR([src/testserver.c])
AC_CONFIG_HEADERS([config.h]) AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_MACRO_DIR([m4])

20
include/class.h

@ -64,19 +64,23 @@
INIT_IMPL(__VA_ARGS__) \ INIT_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 IFACE_GET(class,iface) (interfaceGet(&((class)->impl),(iface)))
#define IFACE_EXISTS(class,iface) (NULL != IFACE_GET((class),(iface)))
/** /**
* \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(object,_iface,method,...) \
do { \ do { \
class_ptr class = class_getClass((object)); \
class_ptr class = GET_CLASS((object)); \
if (class->init) class->init(); \ if (class->init) class->init(); \
iface = (struct i_##_iface *)class_getInterface(&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(); \ if (class->init) class->init(); \
iface = (struct i_##_iface *)class_getInterface(&class, &i_##_iface); \
iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \
}; \ }; \
assert(NULL != iface->method); \ assert(NULL != iface->method); \
} while(0) } while(0)
@ -98,20 +102,17 @@
#define PARENTCALL(object,_iface,method,...) \ #define PARENTCALL(object,_iface,method,...) \
do { \ do { \
struct i_##_iface * iface; \ struct i_##_iface * iface; \
class_ptr class = class_getClass((object)); \
class_ptr class = GET_CLASS((object)); \
if (class->init) class->init(); \ if (class->init) class->init(); \
assert(HAS_PARENT(class)); \ assert(HAS_PARENT(class)); \
class = class->parent; \ class = class->parent; \
if (class->init) class->init(); \ if (class->init) class->init(); \
iface = (struct i_##_iface *)class_getInterface(&class, &i_##_iface); \
iface = (struct i_##_iface *)IFACE_GET(class, &i_##_iface); \
assert(NULL != iface->method); \ assert(NULL != iface->method); \
iface->method(object, ##__VA_ARGS__); \ iface->method(object, ##__VA_ARGS__); \
} while(0) } while(0)
#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)) #define HAS_PARENT(class) (NULL != ((class)->parent))
typedef void (* fptr_classInit)(void); typedef void (* fptr_classInit)(void);
@ -126,9 +127,6 @@ struct class {
struct iface_impl impl; struct iface_impl impl;
}; };
extern void * class_getInterface(class_ptr *, iface_ptr);
extern class_ptr class_getClass(void *);
#endif // __CLASS_H__ #endif // __CLASS_H__
// vim: set ts=4 sw=4: // vim: set ts=4 sw=4:

6
src/Makefile.am

@ -3,9 +3,7 @@ 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 \
interface/subject.c interface/observer.c
CLASS = class.c interface.c
RB = ringbuffer.c ringbuffer/rb_read.c
interface/subject.c interface/observer.c interface.c
SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c
SERVER = server.c server/run.c server/close_conn.c server/poll.c \ SERVER = server.c server/run.c server/close_conn.c server/poll.c \
server/handle_accept.c server/read.c server/handle_accept.c server/read.c
@ -41,7 +39,7 @@ AM_CFLAGS = -Wall -I ../include/
bin_PROGRAMS = testserver bin_PROGRAMS = testserver
testserver_SOURCES = testserver.c \ testserver_SOURCES = testserver.c \
$(IFACE) $(CLASS) $(SOCKET) $(SERVER) $(LOGGER) $(MSG) $(REQ) \
$(IFACE) $(SOCKET) $(SERVER) $(LOGGER) $(MSG) $(REQ) \
$(WRITER) $(RESP) $(HEADER) $(PARSER) $(WORKER) $(CB) \ $(WRITER) $(RESP) $(HEADER) $(PARSER) $(WORKER) $(CB) \
$(UTILS) $(UTILS)
testserver_CFLAGS = -Wall -I ../include/ testserver_CFLAGS = -Wall -I ../include/

50
src/class.c

@ -1,50 +0,0 @@
/**
* \file
* Helper functions for class.
*
* \todo rename function to fit in the otherwise used scheme.
* \author Georg Hopp
*
* \copyright
* Copyright (C) 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/>.
*/
#include <stdarg.h>
#include <stdlib.h>
#include "class.h"
#include "interface.h"
void *
class_getInterface(class_ptr * class, iface_ptr _iface)
{
void * iface = (void *)IFACE_GET(*class, _iface);
while(NULL == iface && HAS_PARENT(*class)) {
*class = (*class)->parent;
iface = (void *)IFACE_GET(*class, _iface);
}
return iface;
}
class_ptr
class_getClass(void * object)
{
return *(class_ptr *)(object - sizeof(void*));
}
// vim: set ts=4 sw=4:

2
src/interface/class.c

@ -70,7 +70,7 @@ classDelete(void ** object)
void * void *
classClone(void * _object) classClone(void * _object)
{ {
class_ptr class = class_getClass(_object);
class_ptr class = GET_CLASS(_object);
void * object = calloc(1, class->object_size + sizeof(void*)); void * object = calloc(1, class->object_size + sizeof(void*));
* (class_ptr *)object = class; * (class_ptr *)object = class;

Loading…
Cancel
Save