From 129bdeeef39ba8dac2b6215ce95279dec8c39a8d Mon Sep 17 00:00:00 2001 From: Philipp Schafft Date: Thu, 27 Jun 2019 12:36:16 +0000 Subject: [PATCH] Feature: Make interface decleration much nicer --- include/igloo/interface.h | 12 ++++++++++++ src/interface.c | 30 ++++++++++++++++++++++++++++++ src/io.c | 23 +---------------------- src/private.h | 3 +++ src/stdio.c | 4 +++- 5 files changed, 49 insertions(+), 23 deletions(-) diff --git a/include/igloo/interface.h b/include/igloo/interface.h index 59292b1..45febc3 100644 --- a/include/igloo/interface.h +++ b/include/igloo/interface.h @@ -42,11 +42,23 @@ extern "C" { */ typedef int (*igloo_interface_free_t)(igloo_INTERFACE_BASIC_ARGS); +#define igloo_INTERFACE_DESCRIPTION_BASE__VERSION 1 typedef struct { + size_t base_length; + int base_version; + size_t description_length; igloo_interface_free_t free; } igloo_interface_base_ifdesc_t; +#define igloo_INTERFACE_DESCRIPTION_BASE(type, ...) \ + .__base = { \ + .base_length = sizeof(igloo_interface_base_ifdesc_t), \ + .base_version = igloo_INTERFACE_DESCRIPTION_BASE__VERSION, \ + .description_length = sizeof(type) \ + , ## __VA_ARGS__ \ + } + #ifdef __cplusplus } #endif diff --git a/src/interface.c b/src/interface.c index 5271d65..0a57aef 100644 --- a/src/interface.c +++ b/src/interface.c @@ -30,3 +30,33 @@ void igloo_interface_base_free(igloo_ro_t self) free(iface->backend_userdata); } +igloo_ro_t igloo_interface_base_new_real(const igloo_ro_type_t *type, size_t description_length, const igloo_interface_base_ifdesc_t *ifdesc, igloo_ro_t backend_object, void *backend_userdata, const char *name, igloo_ro_t associated) +{ + igloo_ro_t self; + igloo__interface_base_t *base; + + if (!ifdesc) + return igloo_RO_NULL; + + if (ifdesc->base_length != sizeof(igloo_interface_base_ifdesc_t) || ifdesc->base_version != igloo_INTERFACE_DESCRIPTION_BASE__VERSION || ifdesc->description_length != description_length) + return igloo_RO_NULL; + + self = igloo_ro_new__raw(type, name, associated); + base = igloo_INTERFACE_CAST(self); + + if (igloo_RO_IS_NULL(self)) + return igloo_RO_NULL; + + if (!igloo_RO_IS_NULL(backend_object)) { + if (igloo_ro_ref(backend_object) != 0) { + igloo_ro_unref(self); + return igloo_RO_NULL; + } + } + + base->ifdesc = ifdesc; + base->backend_object = backend_object; + base->backend_userdata = backend_userdata; + + return self; +} diff --git a/src/io.c b/src/io.c index 2b54b62..21c6a49 100644 --- a/src/io.c +++ b/src/io.c @@ -32,28 +32,7 @@ igloo_RO_PUBLIC_TYPE(igloo_io_t, igloo_io_t * igloo_io_new(const igloo_io_ifdesc_t *ifdesc, igloo_ro_t backend_object, void *backend_userdata, const char *name, igloo_ro_t associated) { - igloo_io_t *io; - - if (!ifdesc) - return NULL; - - io = igloo_ro_new_raw(igloo_io_t, name, associated); - - if (!io) - return NULL; - - if (!igloo_RO_IS_NULL(backend_object)) { - if (igloo_ro_ref(backend_object) != 0) { - igloo_ro_unref(io); - return NULL; - } - } - - io->ifdesc = ifdesc; - io->backend_object = backend_object; - io->backend_userdata = backend_userdata; - - return io; + return igloo_interface_base_new(igloo_io_t, ifdesc, backend_object, backend_userdata, name, associated); } diff --git a/src/private.h b/src/private.h index 113519f..35d0791 100644 --- a/src/private.h +++ b/src/private.h @@ -52,4 +52,7 @@ void igloo_interface_base_free(igloo_ro_t self); #define igloo_INTERFACE_CAST(obj) ((igloo__interface_base_t*)igloo_RO__GETBASE(obj)) #define igloo_INTERFACE_BASIC_CALL(obj) (obj), &(igloo_INTERFACE_CAST(obj)->backend_object), &(igloo_INTERFACE_CAST(obj)->backend_userdata) +igloo_ro_t igloo_interface_base_new_real(const igloo_ro_type_t *type, size_t description_length, const igloo_interface_base_ifdesc_t *ifdesc, igloo_ro_t backend_object, void *backend_userdata, const char *name, igloo_ro_t associated); +#define igloo_interface_base_new(type, ifdesc, backend_object, backend_userdata, name, associated) igloo_RO_TO_TYPE(igloo_interface_base_new_real(igloo_ro__type__ ## type, sizeof(*(ifdesc)), (const igloo_interface_base_ifdesc_t*)(ifdesc), (backend_object), (backend_userdata), (name), (associated)), type) + #endif diff --git a/src/stdio.c b/src/stdio.c index 1078f0a..27f9ced 100644 --- a/src/stdio.c +++ b/src/stdio.c @@ -49,7 +49,9 @@ static int __get_fd_for_systemcall(igloo_INTERFACE_BASIC_ARGS) } static const igloo_io_ifdesc_t igloo_stdio_ifdesc = { - .__base = {.free = __free}, + igloo_INTERFACE_DESCRIPTION_BASE(igloo_io_ifdesc_t, + .free = __free + ), .read = __read, .write = __write, .flush = __flush,